Repository navigation
Expand file tree
/
Copy path_visualization.py
More file actions
318 lines (269 loc) · 8.52 KB
/
Copy path_visualization.py
File metadata and controls
318 lines (269 loc) · 8.52 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
"""Adapted from https://github.com/cvnlab/nsdcode."""
from collections.abc import Collection
from pathlib import Path
import nibabel as nib
import numpy as np
import xarray as xr
# from bonner.datasets.allen2021_natural_scenes._data import load_brain_mask
# from bonner.datasets.allen2021_natural_scenes._utilities import BUCKET_NAME, CACHE_PATH
# from bonner.files import download_from_s3
from scipy.ndimage import map_coordinates
MNI_ORIGIN = np.asarray([183 - 91, 127, 73]) - 1
MNI_RESOLUTION = 1
# def load_transformation(
# subject: int,
# *,
# source_space: str,
# target_space: str,
# suffix: str,
# ) -> np.ndarray:
# filepath = (
# Path("nsddata")
# / "ppdata"
# / f"subj{subject + 1:02}"
# / "transforms"
# / f"{source_space}-to-{target_space}{suffix}"
# )
# download_from_s3(filepath, bucket=BUCKET_NAME, local_path=CACHE_PATH / filepath)
# return nib.load(CACHE_PATH / filepath).get_fdata()
# def load_native_surface(
# subject: int,
# *,
# hemisphere: str,
# surface_type: str = "w-g.pct.mgh",
# ) -> Path:
# filepath = (
# Path("nsddata")
# / "freesurfer"
# / f"subj{subject + 1:02}"
# / "surf"
# / f"{hemisphere}.{surface_type}"
# )
# download_from_s3(filepath, bucket=BUCKET_NAME, local_path=CACHE_PATH / filepath)
# return CACHE_PATH / filepath
def _interpolate(
volume: np.ndarray,
*,
coordinates: np.ndarray,
interpolation_type: str = "cubic",
) -> np.ndarray:
"""Wrap ba_interp3.
Normal calls to ba_interp3 assign values to interpolation points that lie outside the original data range. We ensure that coordinates outside the original field-of-view (i.e. if the value along a dimension is less than 1 or greater than the number of voxels in the original volume along that dimension) are returned as NaN and coordinates that have any NaNs are returned as NaN.
Args:
----
volume: 3D matrix (can be complex-valued)
coordinates: (3, N) matrix coordinates to interpolate at
interpolation_type: "nearest", "linear", or "cubic"
"""
# input
match interpolation_type:
case "cubic":
order = 3
case "linear":
order = 1
case "nearest":
order = 0
case _:
error = "interpolation method not implemented"
raise ValueError(error)
# bad locations must get set to NaN
bad = np.any(np.isinf(coordinates), axis=0)
coordinates[:, bad] = -1
# out of range must become NaN, too
bad = np.any(
np.c_[
bad,
coordinates[0, :] < 0,
coordinates[0, :] > volume.shape[0] - 1,
coordinates[1, :] < 0,
coordinates[1, :] > volume.shape[1] - 1,
coordinates[2, :] < 0,
coordinates[2, :] > volume.shape[2] - 1,
],
axis=1,
).astype(bool)
transformed_data = map_coordinates(
np.nan_to_num(volume).astype(np.float64),
coordinates,
order=order,
mode="nearest",
)
transformed_data[bad] = np.nan
return transformed_data
def _transform(
data: np.ndarray,
*,
transformation: np.ndarray,
interpolation_type: str,
target_type: str,
) -> np.ndarray:
"""_summary_
Args:
----
data: data to be transformed from one space to another
transformation: transformation matrix
interpolation_type: passed to _interpolate
target_type: "volume" or "surface"
Returns:
-------
Transformed data
"""
target_shape = transformation.shape[:3]
coordinates = np.c_[
transformation[..., 0].ravel(order="F"),
transformation[..., 1].ravel(order="F"),
transformation[..., 2].ravel(order="F"),
].T
coordinates -= 1 # Kendrick's 1-based indexing.
data_ = _interpolate(
data,
coordinates=coordinates,
interpolation_type=interpolation_type,
)
data_ = np.nan_to_num(data_)
if target_type == "volume":
data_ = data_.reshape(target_shape, order="F")
return data_
def convert_ndarray_to_nifti1image(
data: np.ndarray,
*,
resolution: float = MNI_RESOLUTION,
origin: np.ndarray = MNI_ORIGIN,
) -> nib.Nifti1Image:
header = nib.Nifti1Header()
header.set_data_dtype(data.dtype)
affine = np.diag([resolution] * 3 + [1])
if origin is None:
origin = (([1, 1, 1] + np.asarray(data.shape)) / 2) - 1
affine[0, -1] = -origin[0] * resolution
affine[1, -1] = -origin[1] * resolution
affine[2, -1] = -origin[2] * resolution
return nib.Nifti1Image(data, affine, header)
# def transform_volume_to_mni(
# data: np.ndarray,
# *,
# subject: int,
# source_space: str,
# interpolation_type: str,
# ) -> np.ndarray:
# transformation = load_transformation(
# subject=subject,
# source_space=source_space,
# target_space="MNI",
# suffix=".nii.gz",
# )
# return _transform(
# data=data,
# transformation=transformation,
# target_type="volume",
# interpolation_type=interpolation_type,
# )
# def transform_volume_to_native_surface(
# data: np.ndarray,
# *,
# subject: int,
# source_space: str,
# interpolation_type: str = "cubic",
# layers: Collection[str] = (
# "layerB1",
# "layerB2",
# "layerB3",
# ),
# average_across_layers: bool = True,
# ) -> dict[str, dict[str, np.ndarray]]:
# native_surface: dict[str, dict[str, np.ndarray]] = {}
# for hemisphere in ("lh", "rh"):
# native_surface[hemisphere] = {}
# for layer in layers:
# transformation = load_transformation(
# subject=subject,
# source_space=f"{hemisphere}.{source_space}",
# target_space=layer,
# suffix=".mgz",
# )
# native_surface[hemisphere][layer] = _transform(
# data,
# transformation=transformation,
# target_type="surface",
# interpolation_type=interpolation_type,
# )
# if average_across_layers:
# native_surface[hemisphere] = {
# "average": np.vstack(list(native_surface[hemisphere].values())).mean(
# axis=0,
# ),
# }
# return native_surface
# def reshape_dataarray_to_brain(
# data: xr.DataArray,
# *,
# subject: int,
# resolution: str,
# ) -> np.ndarray:
# brain_shape = load_brain_mask(subject=subject, resolution=resolution).shape
# if data.ndim == 2:
# output_shape = (data.shape[0], *brain_shape)
# else:
# output_shape = brain_shape
# output = np.full(output_shape, fill_value=np.nan, dtype=data.data.dtype)
# output[..., data["x"].data, data["y"].data, data["z"].data] = data.data
# return output
# def convert_dataarray_to_nifti1image(
# data: xr.DataArray,
# *,
# subject: int,
# resolution: str,
# interpolation_type: str = "cubic",
# ) -> nib.nifti1.Nifti1Image:
# return convert_ndarray_to_nifti1image(
# transform_volume_to_mni(
# data=reshape_dataarray_to_brain(
# data=data,
# subject=subject,
# resolution=resolution,
# ),
# subject=subject,
# source_space=f"func{resolution[:-2]}",
# interpolation_type=interpolation_type,
# ),
# )
# def load_surface_roi(
# *,
# subject: int,
# hemisphere: str,
# label: str,
# ) -> Path:
# """Load and format a surface ROI.
# Args:
# ----
# subject: subject ID
# """
# filepath = (
# Path("nsddata")
# / "freesurfer"
# / f"subj{subject + 1:02}"
# / "label"
# / f"{'l' if hemisphere == 'left' else 'r'}h.{label}.mgz"
# )
# download_from_s3(filepath, bucket=BUCKET_NAME, local_path=CACHE_PATH / filepath)
# return CACHE_PATH / filepath
# def load_surface_mesh(
# *,
# subject: int,
# hemisphere: str,
# label: str,
# ) -> Path:
# """Load and format a surface mesh.
# Args:
# ----
# subject: subject ID
# """
# filepath = (
# Path("nsddata")
# / "freesurfer"
# / f"subj{subject + 1:02}"
# / "surf"
# / f"{'l' if hemisphere == 'left' else 'r'}h.{label}"
# )
# download_from_s3(filepath, bucket=BUCKET_NAME, local_path=CACHE_PATH / filepath)
# return CACHE_PATH / filepath