forked from hzaoheng123/SurfNet
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataload.py
More file actions
350 lines (276 loc) · 12.6 KB
/
dataload.py
File metadata and controls
350 lines (276 loc) · 12.6 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import os
import numpy as np
import torch
from tqdm import tqdm
import nibabel as nib
from torch.utils.data import Dataset
from typing import Union, Tuple
import trimesh
from trimesh import Trimesh
from trimesh.scene.scene import Scene
from pytorch3d.structures import Meshes
from pytorch3d.ops import sample_points_from_meshes
"""
volume: brain MRI volume
v_in: vertices of input white matter surface
f_in: faces of ground truth pial surface
v_gt: vertices of input white matter surface
f_gt: faces of ground truth pial surface
"""
from nibabel.affines import apply_affine
import numpy.linalg as npl
def surf_v2vox(v, Torig):
# Torig is to be obtained from a mgz image img
# img = nibabel.load('img.mgz')
# Torig = img.header.get_vox2ras_tkr()
vox = apply_affine(npl.inv(Torig), v)
return vox
def vox2surf_v(vox, Torig):
# Torig is to be obtained from a mgz image img
# img = nibabel.load('img.mgz')
# Torig = img.header.get_vox2ras_tkr()
v = apply_affine(Torig, vox)
return v
def load_mri(path):
brain = nib.load(path)
brain_arr = brain.get_fdata()
brain_arr = brain_arr / 255.
# ====== change to your own transformation ======
# transpose and clip the data to [192,224,192]
brain_arr = brain_arr.transpose(1,2,0)
#brain_arr = brain_arr[::-1,:,:]
#brain_arr = brain_arr[:,:,::-1]
#brain_arr = brain_arr[32:-32, 16:-16, 32:-32]
#================================================
return brain_arr.copy()
def load_distmap(path):
brain = nib.load(path)
brain_arr = brain.get_fdata()
brain_arr = 1.0 - np.exp(-np.abs(brain_arr))
# ====== change to your own transformation ======
# transpose and clip the data to [192,224,192]
brain_arr = brain_arr.transpose(1,2,0)
brain_arr = brain_arr[::-1,:,:]
brain_arr = brain_arr[:,:,::-1]
brain_arr = brain_arr[32:-32, 16:-16, 32:-32]
#================================================
return brain_arr.copy()
def load_surf(path):
v, f = nib.freesurfer.io.read_geometry(path)
# ====== change to your own transformation ======
# transpose and clip the data to [192,224,192]
v = v[:,[0,2,1]]
v[:,0] = v[:,0] - 32
v[:,1] = - v[:,1] - 15
v[:,2] = v[:,2] - 32
# normalize to [-1, 1]
v = v + 128
v = (v - [96, 112, 96]) / 112
f = f.astype(np.int32)
#================================================
return v, f
def save_surf(file, surf, fc, Torig):
# surf is vertices from data loading
# fc is face information
# Torig is to be obtained from a mgz image img
# img = nibabel.load('img.mgz')
# Torig = img.header.get_vox2ras_tkr()
# map vertices back to int values in voxel space
surf[:,0] = (surf[:,0] + 1) / 2 * 191 + 32
surf[:,1] = (surf[:,1] + 1) / 2 * 223 + 16
surf[:,2] = (surf[:,2] + 1) / 2 * 191 + 32
# map them back to surface space
surf = vox2surf_v(surf, Torig)
nib.freesurfer.io.write_geometry(file, surf, fc)
return surf
def transform_mesh_affine(vertices: Union[np.ndarray, torch.Tensor],
faces: Union[np.ndarray, torch.Tensor],
transformation_matrix: Union[np.ndarray, torch.Tensor]):
""" Transform vertices of shape (V, D) or (S, V, D) using a given
transformation matrix such that v_new = (mat @ v.T).T. """
ndims = vertices.shape[-1]
if (tuple(transformation_matrix.shape) != (ndims + 1, ndims + 1)):
raise ValueError("Wrong shape of transformation matrix.")
# Convert to torch if necessary
if isinstance(vertices, np.ndarray):
vertices_ = torch.from_numpy(vertices).float()
else:
vertices_ = vertices
if isinstance(faces, np.ndarray):
faces_ = torch.from_numpy(faces).long()
else:
faces_ = faces
if isinstance(transformation_matrix, np.ndarray):
transformation_matrix = torch.from_numpy(
transformation_matrix
).float().to(vertices_.device)
vertices_ = vertices_.view(-1, ndims)
faces_ = faces_.view(-1, ndims)
coords = torch.cat(
(vertices_.T, torch.ones(1, vertices_.shape[0]).to(vertices_.device)),
dim=0
)
# Transform
new_coords = (transformation_matrix @ coords)
# Adapt faces s.t. normal convention is still fulfilled
if torch.sum(torch.sign(torch.diag(transformation_matrix)) == -1) % 2 == 1:
new_faces = faces_.flip(dims=[1])
else: # No flip required
new_faces = faces_
# Correct shape
new_coords = new_coords.T[:,:-1].view(vertices.shape)
new_faces = new_faces.view(faces.shape)
# Correct data type
if isinstance(vertices, np.ndarray):
new_coords = new_coords.numpy()
if isinstance(faces, np.ndarray):
new_faces = new_faces.numpy()
return new_coords, new_faces
def process_volume(x, data_name='adni'):
if data_name == 'adni':
# pad from [182, 218, 182] to [192,224,192] by 10/2,6/2,10/2
return torch.nn.functional.pad(x,(5,5,3,3,5,5))
else:
raise ValueError("data_name should be in ['hcp','adni','dhcp']")
def process_distmap(x, data_name='adni'):
if data_name == 'adni':
# pad from [1,182,218,182] to [1,192,224,192] by 10/2,6/2,10/2
return torch.nn.functional.pad(x,(5,5,3,3,5,5),mode='replicate')
else:
raise ValueError("data_name should be in ['hcp','adni','dhcp']")
def process_surface(v, data_name='adni'):
if data_name == 'adni':
# clip/pad the surface according to the volume & normalize to [-1, 1]
v[:,0] = (v[:,0] + 5)/192 * 2 - 1
v[:,1] = (v[:,1] + 3)/224 * 2 - 1
v[:,2] = (v[:,2] + 5)/192 * 2 - 1
else:
raise ValueError("data_name should be in ['hcp','adni','dhcp']")
return v
class ADNIDataset2(Dataset):
"""ADNI dataset."""
def __init__(self, root_dir, hemisphere):
self.root_dir = root_dir
self.subject_lists = sorted(os.listdir(self.root_dir))
self.hemisphere = hemisphere
def __len__(self):
return len(self.subject_lists)
def __getitem__(self, idx):
subid = self.subject_lists[idx]
if self.hemisphere in ['lh', 'rh']:
surf_hemi = self.hemisphere
else:
l_r_choice = np.random.rand()
if l_r_choice>0.5:
surf_hemi = 'lh'
else:
surf_hemi = 'rh'
# load brain MRI
mri = nib.load(os.path.join(self.root_dir, subid ,'mri.nii.gz'))
volume = mri.get_fdata() / 255.
# get affine matrix from voxels to surf vertices
# Torig = mri.header.get_vox2ras_tkr()
vox2world_affine = mri.affine
world2vox_affine = np.linalg.inv(vox2world_affine)
# load pial surface & convert World --> voxel coordinates
gm_mesh = trimesh.load_mesh(os.path.join(self.root_dir, subid, surf_hemi + "_pial.ply"))
gm_voxel_verts, gm_voxel_faces = transform_mesh_affine(gm_mesh.vertices, gm_mesh.faces, world2vox_affine)
# load mid-layer surface
mid_mesh = trimesh.load_mesh(os.path.join(self.root_dir, subid, surf_hemi + "_mid2.ply"))
mid_voxel_verts, mid_voxel_faces = transform_mesh_affine(mid_mesh.vertices, mid_mesh.faces, world2vox_affine)
# load white matter surface
wm_mesh = trimesh.load_mesh(os.path.join(self.root_dir, subid, surf_hemi + "_white.ply"))
wm_voxel_verts, wm_voxel_faces = transform_mesh_affine(wm_mesh.vertices, wm_mesh.faces, world2vox_affine)
# load distance map for pial surface
gm_vol_dist = nib.load(os.path.join(self.root_dir, subid , surf_hemi + '_gm_dist.nii.gz'))
gm_vol_dist = gm_vol_dist.get_fdata() / 1.
temp_min = gm_vol_dist.min()
temp_max = gm_vol_dist.max()
gm_vol_dist = (gm_vol_dist-temp_min)/(temp_max-temp_min)
# load distance map for pial surface
wm_vol_dist = nib.load(os.path.join(self.root_dir, subid , surf_hemi + '_wm_dist.nii.gz'))
wm_vol_dist = wm_vol_dist.get_fdata() / 1.
temp_min = wm_vol_dist.min()
temp_max = wm_vol_dist.max()
wm_vol_dist = (wm_vol_dist-temp_min)/(temp_max-temp_min)
# load segmentation map and process
seg_vol = nib.load(os.path.join(self.root_dir, subid , 'ribbon.nii.gz'))
seg_vol = seg_vol.get_fdata() / 1.
seg_vol_map = np.zeros_like(seg_vol)
if surf_hemi == 'lh':
seg_vol_map[seg_vol==2] = 0.5
seg_vol_map[seg_vol==3] = 1
else:
seg_vol_map[seg_vol==41] = 0.5
seg_vol_map[seg_vol==42] = 1
gm_vol_dist = process_distmap(torch.Tensor(gm_vol_dist).unsqueeze(0))
wm_vol_dist = process_distmap(torch.Tensor(wm_vol_dist).unsqueeze(0))
#print(gm_vol_dist.shape, wm_vol_dist.shape)
seg_vol_map = process_distmap(torch.Tensor(seg_vol_map).unsqueeze(0))
volume = process_volume(torch.Tensor(volume)).unsqueeze(0)
gm_voxel_verts = process_surface(gm_voxel_verts)
wm_voxel_verts = process_surface(wm_voxel_verts)
mid_voxel_verts = process_surface(mid_voxel_verts)
return volume, seg_vol_map, gm_vol_dist, wm_vol_dist, \
gm_voxel_verts, gm_voxel_faces, wm_voxel_verts, wm_voxel_faces, mid_voxel_verts,mid_voxel_faces, \
subid, surf_hemi
class ADNIDataset2_test(Dataset):
"""ADNI dataset."""
def __init__(self, root_dir, hemisphere):
self.root_dir = root_dir
self.subject_lists = sorted(os.listdir(self.root_dir))
self.hemisphere = hemisphere
def __len__(self):
return len(self.subject_lists)
def __getitem__(self, idx):
subid = self.subject_lists[idx]
# load brain MRI
mri = nib.load(os.path.join(self.root_dir, subid ,'mri.nii.gz'))
volume = mri.get_fdata() / 255.
# get affine matrix from voxels to surf vertices
# Torig = mri.header.get_vox2ras_tkr()
vox2world_affine = mri.affine
world2vox_affine = np.linalg.inv(vox2world_affine)
# load pial surface & convert World --> voxel coordinates
gm_mesh = trimesh.load_mesh(os.path.join(self.root_dir, subid, self.hemisphere + "_pial.ply"))
gm_voxel_verts, gm_voxel_faces = transform_mesh_affine(gm_mesh.vertices, gm_mesh.faces, world2vox_affine)
# load mid-layer surface
mid_mesh = trimesh.load_mesh(os.path.join(self.root_dir, subid, self.hemisphere + "_mid2.ply"))
mid_voxel_verts, mid_voxel_faces = transform_mesh_affine(mid_mesh.vertices, mid_mesh.faces, world2vox_affine)
# load white matter surface
wm_mesh = trimesh.load_mesh(os.path.join(self.root_dir, subid, self.hemisphere + "_white.ply"))
wm_voxel_verts, wm_voxel_faces = transform_mesh_affine(wm_mesh.vertices, wm_mesh.faces, world2vox_affine)
# load distance map for pial surface
gm_vol_dist = nib.load(os.path.join(self.root_dir, subid , self.hemisphere + '_gm_dist.nii.gz'))
gm_vol_dist = gm_vol_dist.get_fdata() / 1.
temp_min = gm_vol_dist.min()
temp_max = gm_vol_dist.max()
gm_vol_dist = (gm_vol_dist-temp_min)/(temp_max-temp_min)
# load distance map for pial surface
wm_vol_dist = nib.load(os.path.join(self.root_dir, subid , self.hemisphere + '_wm_dist.nii.gz'))
wm_vol_dist = wm_vol_dist.get_fdata() / 1.
temp_min = wm_vol_dist.min()
temp_max = wm_vol_dist.max()
wm_vol_dist = (wm_vol_dist-temp_min)/(temp_max-temp_min)
# load segmentation map and process
seg_vol = nib.load(os.path.join(self.root_dir, subid , 'ribbon.nii.gz'))
seg_vol = seg_vol.get_fdata() / 1.
seg_vol_map = np.zeros_like(seg_vol)
if self.hemisphere == 'lh':
seg_vol_map[seg_vol==2] = 0.5
seg_vol_map[seg_vol==3] = 1
else:
seg_vol_map[seg_vol==41] = 0.5
seg_vol_map[seg_vol==42] = 1
gm_vol_dist = process_distmap(torch.Tensor(gm_vol_dist).unsqueeze(0))
wm_vol_dist = process_distmap(torch.Tensor(wm_vol_dist).unsqueeze(0))
#print(gm_vol_dist.shape, wm_vol_dist.shape)
seg_vol_map = process_distmap(torch.Tensor(seg_vol_map).unsqueeze(0))
volume = process_volume(torch.Tensor(volume))
volume = volume.unsqueeze(0)
gm_voxel_verts = process_surface(gm_voxel_verts)
wm_voxel_verts = process_surface(wm_voxel_verts)
mid_voxel_verts = process_surface(mid_voxel_verts)
return volume, seg_vol_map, gm_vol_dist, wm_vol_dist, \
gm_voxel_verts, gm_voxel_faces, wm_voxel_verts, wm_voxel_faces, mid_voxel_verts,mid_voxel_faces, \
subid, self.hemisphere, world2vox_affine