-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
171 lines (154 loc) · 5.91 KB
/
preprocess.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import monai as mn
import glob
import numpy as np
import os
from torch.utils.data import DataLoader
import torch
from random import shuffle, seed
import lesion
import custom
import custom_cc
fslab = [0,2,3,4,5,7,8,10,11,12,13,14,15,16,17,18,24,26,28,77,80,192,786]
targetlab = list(range(len(fslab)))
labmap = {}
for i, l in enumerate(fslab):
labmap[l] = i
fsnames = ["Background"
,"Left cerebral white matter"
,"Left cerebral cortex"
,"Left lateral ventricle"
,"Left inferior lateral ventricle"
,"Left cerebellum white matter"
,"Left cerebellum cortex"
,"Left thalamus"
,"Left caudate"
,"Left putamen"
,"Left pallidum"
,"3rd ventricle"
,"4th ventricle"
,"Brain-stem"
,"Left hippocampus"
,"Left amygdala"
,"CSF"
,"Left accumbens area"
,"Left ventral DC"
# ,"Right cerebral white matter"
# ,"Right cerebral cortex"
# ,"Right lateral ventricle"
# ,"Right inferior lateral ventricle"
# ,"Right cerebellum white matter"
# ,"Right cerebellum cortex"
# ,"Right thalamus"
# ,"Right caudate"
# ,"Right putamen"
# ,"Right pallidum"
# ,"Right hippocampus"
# ,"Right amygdala"
# ,"Right accumbens area"
# ,"Right ventral DC"
,"WM Anomaly"
,"Non-WM Anomaly"
,"Corpus Callopsum"
,"Stroke lesion"]
seed(0)
# Add your ATLAS data paths here (or list of whatever stroke label niftis you have)
atlas_train_txt = "/your/path/atlas_train.txt"
atlas_val_txt = "/your/path/atlas_val.txt"
oasis_path = "/your/path/here/"
def printshape(x):
print(x.shape)
print(x.min(), x.max())
return x
def get_loaders(
batch_size=1,
fs_healthy=False,
mb_healthy=False,
device='cpu',
fade=False,
lowres=False,
ptch=128,
):
train_files = glob.glob(os.path.join(oasis_path, "OAS*/OAS*_Freesurfer*/DATA/OAS*/mri/mni_1mm_healthy_symmetric.nii.gz"))
train_dict = [
{"healthy": f, "label": f.replace("healthy_symmetric", "mb_labels")}
for f in train_files
]
shuffle(train_dict)
train_dict, val_dict = (
train_dict[: -100],
train_dict[-100 :],
)
print(f"\nHealthy labels: Train {len(train_dict)} Val {len(val_dict)}\n")
train_label_list = list(np.loadtxt(atlas_train_txt, dtype=str))
val_label_list = list(np.loadtxt(atlas_val_txt, dtype=str))
print(f"\nLesion labels: Train {len(train_label_list)} Val {len(val_label_list)}\n")
if lowres:
train_label_list = [lst.replace('1mm','2mm') for lst in train_label_list]
val_label_list = [lst.replace('1mm','2mm') for lst in val_label_list]
train_transform = mn.transforms.Compose(
transforms=[
mn.transforms.LoadImageD(keys=["label","healthy"], image_only=True),
mn.transforms.EnsureChannelFirstD(keys=["label","healthy"]),
mn.transforms.SpacingD(keys=["label","healthy"], pixdim=1 if not lowres else 2),
mn.transforms.ResizeWithPadOrCropD(
keys=["label","healthy"], spatial_size=(256, 256, 256) if not lowres else (128, 128, 128)
),
mn.transforms.ToTensorD(dtype=float, keys=["label","healthy"], device=device),
mn.transforms.LambdaD(keys=["healthy"],
func=mn.transforms.MapLabelValue(orig_labels=fslab,
target_labels=targetlab)) if fs_healthy else mn.transforms.AsDiscreteD(keys="healthy", threshold=0.5),
mn.transforms.AsDiscreteD(keys="healthy", to_onehot=len(fslab)-1) if fs_healthy else mn.transforms.IdentityD(keys="label"),
lesion.LesionPasteD(
keys="label", new_keys=["seg"], label_list=train_label_list, fs_healthy=fs_healthy, mb_healthy=mb_healthy, lesion_fading=fade
),
mn.transforms.AsDiscreteD(keys="seg", to_onehot=2) if not fs_healthy and not mb_healthy else mn.transforms.IdentityD(keys="label"),
custom_cc.CCSynthSeg(label_key='label', image_key='image', coreg_keys=['seg','healthy']),
custom.RemapSegToLabel(in_key="seg", out_key="label"),
mn.transforms.RandSpatialCropD(
keys=["image", "label", "seg", "healthy"], roi_size=(ptch, ptch, ptch), random_size=False
) if not lowres else mn.transforms.IdentityD(keys="label"),
mn.transforms.OneOf(
transforms=[
custom.RandomSkullStrip(
label_key="healthy",
image_key=["image","label"],
out_key="mask",
channels_to_use=targetlab[1:] if fs_healthy else [0],
dilate_prob=0.3,
erode_prob=0.3,
),
mn.transforms.IdentityD(keys=["image"]),
],
weights=[0.3, 0.7],
),
mn.transforms.RandAxisFlipd(keys=["image", "label"], prob=0.8),
mn.transforms.RandAxisFlipd(keys=["image", "label"], prob=0.8),
mn.transforms.RandAxisFlipd(keys=["image", "label"], prob=0.8),
mn.transforms.NormalizeIntensityD(
keys="image", nonzero=False, channel_wise=True
),
mn.transforms.ResizeD(keys=["image", "label"], spatial_size=(ptch, ptch, ptch)) if not lowres else mn.transforms.IdentityD(keys="label"),
mn.transforms.ToTensorD(dtype=torch.float32, keys="image"),
mn.transforms.ToTensorD(dtype=torch.float32, keys="label"),
mn.transforms.DeleteItemsD(keys=["healthy","seg","healthy_meta_dict","seg_meta_dict"]),
]
)
train_data = mn.data.Dataset(train_dict, transform=train_transform)
val_data = mn.data.Dataset(val_dict, transform=train_transform)
train_loader = DataLoader(
train_data,
batch_size=batch_size,
shuffle=True,
sampler=None,
batch_sampler=None,
num_workers=0,
)
val_loader = DataLoader(
val_data,
batch_size=1,
shuffle=False,
sampler=None,
batch_sampler=None,
num_workers=0,
)
return train_loader, val_loader