-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_data.py
60 lines (53 loc) · 1.97 KB
/
prepare_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import glob, os, shutil, nibabel, csv
import numpy as np
from sklearn.model_selection import KFold
root_dir = '/path_to_dataset/'
oasis_av45s = np.asarray(sorted(glob.glob(os.path.join(root_dir, "OASIS-CAPIIO/*_AV45SUVR_on_CAPIIO.nii"))))
oasis_pibs = np.asarray(sorted(glob.glob(os.path.join(root_dir, "OASIS-CAPIIO/*_PIBSUVR_on_CAPIIO.nii"))))
kf = KFold(n_splits=10)
counter = 0
for train_index, val_index in kf.split(oasis_av45s):
counter += 1
X_train, X_val = av45s[train_index], av45s[val_index]
y_train, y_val = pibs[train_index], pibs[val_index]
if os.path.exists(f"./fold{counter}"):
shutil.rmtree(f"./fold{counter}")
print(f"removed an existing ./fold{counter} directory")
for x in ["train", "val"]:
if not os.path.exists(f"./fold{counter}/{x}"):
os.makedirs(f"./fold{counter}/{x}")
for z in ["images", "targets"]:
if not os.path.exists(f"./fold{counter}/{x}/{z}"):
os.makedirs(f"./fold{counter}/{x}/{z}")
for x in X_train:
name = x.split("/")[-1]
img = nibabel.load(x)
try:
data = img.get_fdata()
os.symlink(x, f"./fold{counter}/train/images/{name}")
except:
print("Corrupted file")
for x in X_val:
name = x.split("/")[-1]
img = nibabel.load(x)
try:
data = img.get_fdata()
os.symlink(x, f"./fold{counter}/val/images/{name}")
except:
print("Corrupted file")
for x in y_train:
name = x.split("/")[-1]
img = nibabel.load(x)
try:
data = img.get_fdata()
os.symlink(x, f"./fold{counter}/train/targets/{name}")
except:
print("Corrupted file")
for x in y_val:
name = x.split("/")[-1]
img = nibabel.load(x)
try:
data = img.get_fdata()
os.symlink(x, f"./fold{counter}/val/targets/{name}")
except:
print("Corrupted file")