prune_image
ðŸ§
Pruning MRIs has the goal to remove background around brains / heads, i.e., reduce the size of a 3D image.
One key objective is to find the smallest box in the whole dataset, which can surround each brain / head in it. That is, the space of the 'biggest' brain.
Author: Simon M. Hofmann
Years: 2023-2024
find_brain_edges
ðŸ§
Find the on- & the offset of brain (or head) voxels for each plane.
This will find the tangential edges of the brain in the given 3D volume.
/ 3D-volume /
+-------------------+
| +_____edge____+ |
| | ***** | |
| | ** ** | |
| | * ** ** * | |
| |* *** *| |
| Y |* *** *| |
| |* *** *| | Z
| |* ** ** *| | /
| | * ** ** * | |/ /
| | ** ** | | /
| | ***** |/| /
| +––––– X –––––+ |/
+-------------------+
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x3d |
ndarray
|
3D data. |
required |
sl |
bool
|
Whether to return |
False
|
Returns:
Type | Description |
---|---|
tuple[slice, slice, slice] | tuple[int, ...]
|
Tuple with six values of slices or coordinates, two values (lower, upper) per dimension / axis. |
Source code in src/xai4mri/dataloader/prune_image.py
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 |
|
get_brain_axes_length
ðŸ§
Get the length of each brain axis (x,y,z) in voxels.
This will find the tangential edges of the brain in the given 3D volume and measure their lengths.
/ 3D-volume /
+-------------------+
| +_____edge____+ |
| | ***** | |
| | ** ** | |
| | * ** ** * | |
| |* *** *| |
| Y |* *** *| |
| |* *** *| | Z
| |* ** ** *| | /
| | * ** ** * | |/ /
| | ** ** | | /
| | ***** |/| /
| +––––– X –––––+ |/
+-------------------+
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x3d |
ndarray
|
3D volume holding a brain / mask. |
required |
Returns:
Type | Description |
---|---|
Sequence[int]
|
The brain axes lengths. |
Source code in src/xai4mri/dataloader/prune_image.py
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 |
|
get_global_max_axes
ðŸ§
Get the global max axis-length(s) for the given brain.
The global lengths are the maximum axis-length for all brain axes.
It is globally defined for all brains in the dataset.
The value can be set in the PruneConfig
class (PruneConfig.largest_brain_max_axes
).
These values are used for pruning brain images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nifti_img |
Nifti1Image
|
NIfTI image. |
required |
per_axis |
bool
|
True: return max axis-length for each axis (for |
required |
Returns:
Type | Description |
---|---|
int | Sequence[int]
|
Global max axis-length(s). |
Source code in src/xai4mri/dataloader/prune_image.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
permute_array
ðŸ§
permute_array(xd: ndarray) -> ndarray
Swap all entries (e.g., voxels) in the given x-dimensional array (e.g., 3D-MRI).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xd |
ndarray
|
x-dimensional array |
required |
Returns:
Type | Description |
---|---|
ndarray
|
permuted array |
Source code in src/xai4mri/dataloader/prune_image.py
357 358 359 360 361 362 363 364 365 366 |
|
permute_nifti
ðŸ§
permute_nifti(nifti_img: Nifti1Image) -> Nifti1Image
Swap all entries (e.g., voxels) in the given NIfTI image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nifti_img |
Nifti1Image
|
NIfTI image |
required |
Returns:
Type | Description |
---|---|
Nifti1Image
|
permuted NIfTI image (i.e., a noise image) |
Source code in src/xai4mri/dataloader/prune_image.py
369 370 371 372 373 374 375 376 377 378 379 380 |
|
prune_mri
ðŸ§
prune_mri(
x3d: ndarray,
make_cube: bool = False,
max_axis: (
int | Sequence[int] | ndarray[int] | None
) = None,
padding: int = 0,
) -> ndarray | None
Prune given 3D MRI to (smaller) volume with side-length(s) == max_axis
[int
OR 3D tuple].
If max_axis
is None
, find the smallest volume, which covers the brain (i.e., remove zero-padding).
Works very fast.
[Implementation with np.pad
is possible, too].
Compare to: nilearn.image.crop_img()
for NIfTI's:
* This crops exactly along the brain only
* which is the same as: mri[find_brain_edges(mri, sl=True)]
* but it is slower
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x3d |
ndarray
|
3D MRI |
required |
max_axis |
int | Sequence[int] | ndarray[int] | None
|
Either side-length [int] of a pruned cube; Or pruned side-length for each axis [3D-sequence: [int, int, int]]. |
None
|
make_cube |
bool
|
True: pruned MRI will be a cube; False: each axis will be pruned to |
False
|
padding |
int
|
Number of zero-padding layers that should remain around the brain [int >= 0] |
0
|
Returns:
Type | Description |
---|---|
ndarray | None
|
pruned brain image or |
Source code in src/xai4mri/dataloader/prune_image.py
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 |
|
reverse_pruning
ðŸ§
reverse_pruning(
original_mri: ndarray | Nifti1Image,
pruned_mri: ndarray,
pruned_stats_map: ndarray | None = None,
) -> ndarray | Nifti1Image
Reverse the pruning of an MRI or its corresponding statistical map.
If a statistical map is given, both the original MRI and the pruned MRI are necessary to find the edges of
the cut-off during pruning.
If no statistical map is given, only the original MRI and the pruned MRI are required.
Note, in this case reverse_pruning()
is applied to a processed and pruned version of the original MRI.
Make sure that the original MRI and the pruned MRI have the same orientation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original_mri |
ndarray | Nifti1Image
|
Original (i.e., non-pruned) MRI. |
required |
pruned_mri |
ndarray
|
Pruned MRI. |
required |
pruned_stats_map |
ndarray | None
|
[Optional] pruned statistical map. |
None
|
Returns:
Type | Description |
---|---|
ndarray | Nifti1Image
|
MRI with original size (if original_mri is given as Nifti1Image, returns Nifti1Image). |
Source code in src/xai4mri/dataloader/prune_image.py
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 |
|