-
Notifications
You must be signed in to change notification settings - Fork 7
/
import_dataset.py
86 lines (70 loc) · 2.85 KB
/
import_dataset.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import random
import numpy as np
import nibabel as nib
import os
import glob
from funcs import preproc
def create_datasets(retrain=False, task=None, labels=False, ds_scale=0):
wd = "./Data/CamCAN_unbiased/CamCAN/T2w"
#os.chdir(wd)
subject_id = np.array([i for i in glob.glob(wd+"/*") if '.txt' not in i and 'normalized' in i])
subject_train_idx = random.sample(range(0, 652), 400)
subject_train = subject_id[subject_train_idx]
subject_test = [i for i in subject_id if i not in subject_train][:50]
if not retrain:
np.savetxt(str(task)+"subject_train.txt", subject_train, "%s", delimiter=',')
np.savetxt(str(task)+"subject_test.txt", subject_test, "%s", delimiter=',')
else:
subject_train = np.genfromtxt(str(task)+"subject_train.txt", dtype=str,delimiter=',')
subject_test = np.genfromtxt(str(task)+"subject_test.txt", dtype=str,delimiter=',')
print("retrain is {}".format(retrain))
print(str(task)+"training subject ids: {}".format(subject_train))
print(str(task)+"testing subject ids: {}".format(subject_test))
X_train_input = []
X_train_target = []
X_train_target_all = []
X_dev_input = []
X_dev_target = []
X_dev_target_all = []
for i in subject_train:
print(i)
pathx=i
img = nib.load(pathx).get_data()
img = np.transpose(img, [2, 0, 1])
idx = [s for s in range(img.shape[0]) if len(set(img[s].flatten())) > 1]
img = img[idx]
_, x, y = img.shape
max_xy = max(x,y)
a = (max_xy-x)/2
b = (max_xy-y)/2
if len(X_train_input)==0:
print(img.shape)
img = np.pad(img, ((0, 0),((a,a)), (b,b)), mode = 'edge')
img = preproc.resize(img, 256 /233.0)
if labels:
z = np.genfromtxt(os.path.join(wd, str(i) + '/' + str(i) + "_label.txt"))
assert len(z)==len(idx)
X_train_target.extend(z)
if ds_scale!=0:
img = preproc.downsample_image(img[np.newaxis,:,:,:],ds_scale)
X_train_input.extend(img)
for j in subject_test:
print(j)
pathx=j
img = nib.load(pathx).get_data()
img = np.transpose(img, [2, 0, 1])
idx = [s for s in range(img.shape[0]) if len(set(img[s].flatten())) > 1]
img = img[idx]
img = np.pad(img, ((0, 0), (a,a), (b,b)), mode='edge')
img = preproc.resize(img, 256 / 233.0)
if ds_scale!=0:
img = preproc.downsample_image(img[np.newaxis,:,:,:],ds_scale)
X_dev_input.extend(img)
if labels:
z = np.genfromtxt(os.path.join(wd, str(j) + '/' + str(j) + "_label.txt"))
print(len(z), img.shape)
X_dev_target.extend(z)
if not labels:
return np.asarray(X_train_input), np.asarray(X_dev_input)
else:
return X_train_input, X_train_target, X_dev_input, X_dev_target