-
Notifications
You must be signed in to change notification settings - Fork 23
/
transform.py
796 lines (670 loc) · 30.4 KB
/
transform.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2021 The NiPreps Developers <nipreps@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
r"""
The :math:`B_0` unwarping transform formalism.
This module implements a data structure to represent the displacements field associated
to the deformations caused by susceptibility-derived distortions.
This implementation attempts to provide a single representation of such distortions independently
of the estimation strategy (see :doc:`/methods`).
.. _bspline-interpolation:
That is achieved by implementing multi-level B-Spline cubic interpolators.
For one given level, a function :math:`f(\mathbf{s})` can be represented as a linear combination
of tensor-product cubic B-Spline basis (:math:`\Psi^3(\mathbf{k}, \mathbf{s})`;
see Eq. :math:`\eqref{eq:2}`).
.. math::
f(\mathbf{s}) =
\sum_{k_1} \sum_{k_2} \sum_{k_3} c(\mathbf{k}) \Psi^3(\mathbf{k}, \mathbf{s}).
\label{eq:1}\tag{1}
"""
from __future__ import annotations
import os
from functools import partial
import asyncio
from pathlib import Path
from typing import Callable, Sequence, Tuple
import attr
import numpy as np
from warnings import warn
from scipy import ndimage as ndi
from scipy.interpolate import BSpline
from scipy.sparse import hstack as sparse_hstack, kron, lil_array
import nibabel as nb
import nitransforms as nt
from bids.utils import listify
from niworkflows.interfaces.nibabel import reorient_image
from sdcflows.utils.tools import ensure_positive_cosines
def _sdc_unwarp(
data: np.ndarray,
coordinates: np.ndarray,
pe_info: Tuple[int, float],
hmc_xfm: np.ndarray | None,
fmap_hz: np.ndarray,
output_dtype: str | np.dtype | None = None,
order: int = 3,
mode: str = "constant",
cval: float = 0.0,
prefilter: bool = True,
) -> np.ndarray:
"""Resample one volume, moving through a head motion correction affine if provided."""
if hmc_xfm is not None:
# Move image with the head
coords_shape = coordinates.shape
coordinates = nb.affines.apply_affine(
hmc_xfm, coordinates.reshape(coords_shape[0], -1).T
).T.reshape(coords_shape)
# Map voxel coordinates applying the VSM
# The VSM is just the displacements field given in index coordinates
# voxcoords is the deformation field, i.e., the target position of each voxel
vsm = fmap_hz * pe_info[1]
coordinates[pe_info[0], ...] += vsm
# The Jacobian determinant image is the amount of stretching in the PE direction.
# Using central differences accounts for the shift in neighboring voxels.
# The full Jacobian at each voxel would be a 3x3 matrix, but because there is
# only warping in one direction, we end up with a diagonal matrix with two 1s.
# The following is the other entry at each voxel, and hence the determinant.
jacobian = 1 + np.gradient(vsm, axis=pe_info[0])
resampled = ndi.map_coordinates(
data,
coordinates,
output=output_dtype,
order=order,
mode=mode,
cval=cval,
prefilter=prefilter,
) * jacobian
return resampled
async def worker(
data: np.ndarray,
coordinates: np.ndarray,
pe_info: Tuple[int, float],
hmc_xfm: np.ndarray,
func: Callable,
semaphore: asyncio.Semaphore,
) -> np.ndarray:
"""Create one worker and attach it to the execution loop."""
async with semaphore:
loop = asyncio.get_running_loop()
result = await loop.run_in_executor(
None, func, data, coordinates, pe_info, hmc_xfm
)
return result
async def unwarp_parallel(
fulldataset: np.ndarray,
coordinates: np.ndarray,
fmap_hz: np.ndarray,
pe_info: Sequence[Tuple[int, float]],
xfms: Sequence[np.ndarray],
order: int = 3,
mode: str = "constant",
cval: float = 0.0,
prefilter: bool = True,
output_dtype: str | np.dtype | None = None,
max_concurrent: int = min(os.cpu_count(), 12),
) -> np.ndarray:
r"""
Unwarp an EPI dataset parallelizing across volumes.
Parameters
----------
fulldataset : :obj:`~numpy.ndarray`
An array of shape (I, J, K, T), where I, J, K are the dimensions of spatial axes
and T is the number of volumes.
The full data array of the EPI that are wanted after correction.
coordinates : :obj:`~numpy.ndarray`
An array of shape (3, I, J, K) array providing the voxel (index) coordinates of
the reference image (i.e., interpolated points) before SDC/HMC.
fmap_hz : :obj:`~numpy.ndarray`
An array of shape (I, J, K) containing the displacement of each voxel in voxel units.
pe_info : :obj:`tuple` of (:obj:`int`, :obj:`float`)
A tuple containing the index of the phase-encoding axis in the data array and
the readout time (including sign, if displacements must be reversed)
xfms : :obj:`list` of obj:`~numpy.ndarray`
A list of 4×4 matrices, each one formalizing
the estimated head motion alignment to the scan's reference.
Therefore, each of these matrices express the transform of every
voxel's RAS (physical) coordinates in the image used as reference
for realignment into the coordinates of each of the EPI series volume.
order : :obj:`int`, optional
The order of the spline interpolation, default is 3.
The order has to be in the range 0-5.
mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'}, optional
Determines how the input image is extended when the resamplings overflows
a border. Default is 'constant'.
cval : :obj:`float`, optional
Constant value for ``mode='constant'``. Default is 0.0.
prefilter : :obj:`bool`, optional
Determines if the image's data array is prefiltered with
a spline filter before interpolation. The default is ``True``,
which will create a temporary *float64* array of filtered values
if *order > 1*. If setting this to ``False``, the output will be
slightly blurred if *order > 1*, unless the input is prefiltered,
i.e. it is the result of calling the spline filter on the original
input.
output_dtype : :obj:`str` or :obj:`~numpy.dtype`
Override the output data type, instead of propagating it from the
moving image.
max_concurrent : :obj:`int`
The maximum number of parallel resamplings at any given time of execution.
Use this parameter to set an upper bound to memory utilization.
"""
semaphore = asyncio.Semaphore(max_concurrent)
if fulldataset.ndim == 3:
fulldataset = fulldataset[..., np.newaxis]
func = partial(
_sdc_unwarp,
fmap_hz=fmap_hz,
output_dtype=output_dtype,
order=order,
mode=mode,
cval=cval,
prefilter=prefilter,
)
# Create a worker task for each chunk
tasks = []
for volid, volume in enumerate(np.rollaxis(fulldataset, -1, 0)):
xfm = None if xfms is None else xfms[volid]
# IMPORTANT - the coordinates array must be copied every time anew per thread
task = asyncio.create_task(
worker(
volume,
coordinates.copy(),
pe_info[volid],
xfm,
func,
semaphore,
)
)
tasks.append(task)
# Wait for all tasks to complete
await asyncio.gather(*tasks)
# Collect the results and stack along last dimension
results = np.stack([task.result() for task in tasks], -1)
return results
@attr.s(slots=True)
class B0FieldTransform:
"""Represents and applies the transform to correct for susceptibility distortions."""
coeffs = attr.ib(default=None)
"""B-Spline coefficients (one value per control point)."""
mapped = attr.ib(default=None, init=False)
"""
A cache of the interpolated field in Hz (i.e., the fieldmap *mapped* on to the
target image we want to correct).
"""
def fit(
self,
target_reference: nb.spatialimages.SpatialImage,
xfm_data2fmap: np.ndarray | None = None,
approx: bool = True,
) -> bool:
r"""
Generate the interpolation matrix (and the VSM with it).
Implements Eq. :math:`\eqref{eq:1}`, interpolating :math:`f(\mathbf{s})`
for all voxels in the target-image's extent.
Parameters
----------
target_reference : :obj:`~nibabel.spatialimages.SpatialImage`
The image object containing a reference grid (same as that of the data
to be resampled). If a 4D dataset is provided, then the fourth dimension
will be dropped.
xfm_data2fmap : :obj:`numpy.ndarray`
Transform that maps coordinates on the `target_reference` onto the
fieldmap reference (that is, the linear transform through which the fieldmap can
be resampled in register with the `target_reference`).
In other words, `xfm_data2fmap` is the result of calling a registration tool
such as ANTs configured for a linear transform with at most 12 degrees of freedom
and called with the image carrying `target_affine` as reference and the fieldmap
reference as moving.
The result of such a registration framework is an affine (our `xfm_data2fmap` here)
that maps coordinates in reference (target) RAS onto the fieldmap RAS.
approx : :obj:`bool`
If ``True``, do not reconstruct the B-Spline field directly on the target
(which will likely not be aligned with the fieldmap's grid), but rather use
the fieldmap's grid and then use just regular interpolation.
Returns
-------
updated : :obj:`bool`
``True`` if the internal field representation was fit,
``False`` if cache was valid and will be reused.
"""
# Calculate the physical coordinates of target grid
if isinstance(target_reference, (str, bytes, Path)):
target_reference = nb.load(target_reference)
approx &= xfm_data2fmap is not None # Approximate iff xfm_data2fmap is defined
xfm_data2fmap = xfm_data2fmap if xfm_data2fmap is not None else np.eye(4)
# Project the reference's grid onto the fieldmap's
projected_reference = target_reference.__class__(
target_reference.dataobj,
xfm_data2fmap @ target_reference.affine,
target_reference.header,
)
# Make sure the data array has all cosines positive (i.e., no axes are flipped)
projected_reference, _ = ensure_positive_cosines(projected_reference)
# Approximate only if the coordinate systems are not aligned
coeffs = listify(self.coeffs)
approx &= not np.allclose(
np.linalg.norm(
np.cross(
coeffs[-1].affine[:-1, :-1].T,
target_reference.affine[:-1, :-1].T,
),
axis=1,
),
0,
atol=1e-3,
)
if approx:
from sdcflows.utils.tools import deoblique_and_zooms
# Generate a sampling reference on the fieldmap's space that fully covers
# the target_reference's grid.
projected_reference = deoblique_and_zooms(
coeffs[-1],
target_reference,
)
# Generate tensor-product B-Spline weights
colmat = sparse_hstack(
[grid_bspline_weights(projected_reference, level) for level in coeffs]
).tocsr()
coefficients = np.hstack(
[level.get_fdata(dtype="float32").reshape(-1) for level in coeffs]
)
# Reconstruct the fieldmap (in Hz) from coefficients
fmap = np.reshape(colmat @ coefficients, projected_reference.shape[:3])
# Generate a NIfTI object
hdr = target_reference.header.copy()
hdr.set_intent("estimate", name="fieldmap Hz")
hdr.set_data_dtype("float32")
hdr["cal_max"] = max((abs(fmap.min()), fmap.max()))
hdr["cal_min"] = -hdr["cal_max"]
# Cache
self.mapped = nb.Nifti1Image(fmap, projected_reference.affine, hdr)
if approx:
from nitransforms.linear import Affine
_tmp_reference = nb.Nifti1Image(
np.zeros(
target_reference.shape[:3], dtype=target_reference.get_data_dtype()
),
target_reference.affine,
target_reference.header,
)
# Interpolate fmap given on target_reference in the original target_reference
# voxel locations (overwrite fmap)
self.mapped = Affine(reference=_tmp_reference).apply(self.mapped)
return True
def apply(
self,
moving: nb.spatialimages.SpatialImage,
pe_dir: str | Sequence[str],
ro_time: float | Sequence[float],
xfms: Sequence[np.ndarray] | None = None,
xfm_data2fmap: np.ndarray | None = None,
approx: bool = True,
order: int = 3,
mode: str = "constant",
cval: float = 0.0,
prefilter: bool = True,
output_dtype: str | np.dtype | None = None,
num_threads: int = None,
allow_negative: bool = False,
):
r"""
Apply a transformation to an image, resampling on the reference spatial object.
Handles parallelization to resample 4D images.
Parameters
----------
moving : :obj:`~nibabel.spatialimages.SpatialImage`
The image object containing the data to be resampled in reference
space
pe_dir : :obj:`str` or :obj:`list` of :obj:`str`
A valid ``PhaseEncodingDirection`` metadata value.
ro_time : :obj:`float` or :obj:`list` of :obj:`float`
The total readout time in seconds.
xfms : :obj:`None` or :obj:`list`
A list of 4×4 matrices, each one formalizing
the estimated head motion alignment to the scan's reference.
Therefore, each of these matrices express the transform of every
voxel's RAS (physical) coordinates in the image used as reference
for realignment into the coordinates of each of the EPI series volume.
xfm_data2fmap : :obj:`numpy.ndarray`
Transform that maps coordinates on the ``target_reference`` onto the
fieldmap reference (that is, the linear transform through which the fieldmap can
be resampled in register with the ``target_reference``).
In other words, ``xfm_data2fmap`` is the result of calling a registration tool
such as ANTs configured for a linear transform with at most 12 degrees of freedom
and called with the image carrying ``target_affine`` as reference and the fieldmap
reference as moving.
The result of such a registration framework is an affine (our ``xfm_data2fmap`` here)
that maps coordinates in reference (target) RAS onto the fieldmap RAS.
approx : :obj:`bool`
If ``True``, do not reconstruct the B-Spline field directly on the target
(which will likely not be aligned with the fieldmap's grid), but rather use
the fieldmap's grid and then use just regular interpolation.
order : :obj:`int`, optional
The order of the spline interpolation, default is 3.
The order has to be in the range 0-5.
mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'}, optional
Determines how the input image is extended when the resamplings overflows
a border. Default is 'constant'.
cval : float, optional
Constant value for ``mode='constant'``. Default is 0.0.
prefilter : :obj:`bool`, optional
Determines if the image's data array is prefiltered with
a spline filter before interpolation. The default is ``True``,
which will create a temporary *float64* array of filtered values
if *order > 1*. If setting this to ``False``, the output will be
slightly blurred if *order > 1*, unless the input is prefiltered,
i.e. it is the result of calling the spline filter on the original
input.
output_dtype : :obj:`str` or :obj:`~numpy.dtype`
Override the output data type, instead of propagating it from the
moving image.
num_threads : :obj:`int`
The maximum number of parallel resamplings at any given time of execution.
Use this parameter to set an upper bound to memory utilization.
allow_negative : :obj:`bool`
Remove negative values introduced in interpolation (may happen for nonnegative data
when order :math:`\gt` 3). Set this value to `True` if your `moving` image does
have negative values.
Returns
-------
resampled : :obj:`~nibabel.spatialimages.SpatialImage`
The data imaged after resampling to reference space.
"""
# Ensure the fmap has been computed
if isinstance(moving, (str, bytes, Path)):
moving = nb.load(moving)
# Make sure the data array has all cosines positive (i.e., no axes are flipped)
moving, axcodes = ensure_positive_cosines(moving)
if self.mapped is not None:
warn(
"The fieldmap has been already fit, the user is responsible for "
"ensuring the parameters of the EPI target are consistent."
)
else:
# Generate warp field (before ensuring positive cosines)
self.fit(moving, xfm_data2fmap=xfm_data2fmap, approx=approx)
# Squeeze non-spatial dimensions
newshape = moving.shape[:3] + tuple(dim for dim in moving.shape[3:] if dim > 1)
data = nb.arrayproxy.reshape_dataobj(moving.dataobj, newshape)
ndim = min(data.ndim, 3)
n_volumes = data.shape[3] if data.ndim == 4 else 1
output_dtype = output_dtype or moving.header.get_data_dtype()
# Prepare input parameters
if isinstance(pe_dir, str):
pe_dir = [pe_dir]
if isinstance(ro_time, float):
ro_time = [ro_time]
if n_volumes > 1 and len(pe_dir) == 1:
pe_dir *= n_volumes
if n_volumes > 1 and len(ro_time) == 1:
ro_time *= n_volumes
pe_info = []
for volid in range(n_volumes):
pe_axis = "ijk".index(pe_dir[volid][0])
axis_flip = axcodes[pe_axis] in ("LPI")
pe_flip = pe_dir[volid].endswith("-")
pe_info.append((
pe_axis,
# Displacements are reversed if either is true (after ensuring positive cosines)
-ro_time[volid] if (axis_flip ^ pe_flip) else ro_time[volid],
))
# Reference image's voxel coordinates (in voxel units)
voxcoords = (
nt.linear.Affine(reference=moving)
.reference.ndindex.reshape((ndim, *data.shape[:ndim]))
.astype("float32")
)
# Convert head-motion transforms to voxel-to-voxel:
if xfms is not None:
# if len(xfms) != n_volumes:
# raise RuntimeError(
# f"Number of head-motion estimates ({len(xfms)}) does not match the "
# f"number of volumes ({n_volumes})"
# )
# vox2ras = moving.affine.copy()
# ras2vox = np.linalg.inv(vox2ras)
# xfms = [ras2vox @ xfm @ vox2ras for xfm in xfms]
xfms = None
warn(
"Head-motion compensating (realignment) transforms are ignored when applying "
"the unwarp with SDCFlows. This feature will be enabled as soon as unit tests "
"are implemented for its quality assurance."
)
# Resample
resampled = asyncio.run(
unwarp_parallel(
data,
voxcoords,
self.mapped.get_fdata(dtype="float32"), # fieldmap in Hz
pe_info,
xfms,
output_dtype='float32',
order=order,
mode=mode,
cval=cval,
prefilter=prefilter,
max_concurrent=num_threads or min(os.cpu_count(), 12),
)
)
if not allow_negative:
resampled[resampled < 0] = cval
moved = moving.__class__(resampled, moving.affine, moving.header)
moved.header.set_data_dtype(output_dtype)
return reorient_image(moved, axcodes)
def to_displacements(self, ro_time, pe_dir, itk_format=True):
"""
Generate a NIfTI file containing a displacements field transform compatible with ITK/ANTs.
The displacements field can be calculated following
`Eq. (2) in the fieldmap fitting section
<sdcflows.workflows.fit.fieldmap.html#mjx-eqn-eq%3Afieldmap-2>`__.
Parameters
----------
ro_time : :obj:`float`
The total readout time in seconds (only if ``vsm=False``).
pe_dir : :obj:`str`
The ``PhaseEncodingDirection`` metadata value (only if ``vsm=False``).
Returns
-------
spatialimage : :obj:`nibabel.nifti.Nifti1Image`
A NIfTI 1.0 object containing the distortion.
"""
return fmap_to_disp(self.mapped, ro_time, pe_dir, itk_format=itk_format)
def fmap_to_disp(fmap_nii, ro_time, pe_dir, itk_format=True):
"""
Convert a fieldmap in Hz into an ITK/ANTs-compatible displacements field.
The displacements field can be calculated following
`Eq. (2) in the fieldmap fitting section
<sdcflows.workflows.fit.fieldmap.html#mjx-eqn-eq%3Afieldmap-2>`__.
Parameters
----------
fmap_nii : :obj:`os.pathlike`
Path to a voxel-shift-map (VSM) in NIfTI format
ro_time : :obj:`float`
The total readout time in seconds
pe_dir : :obj:`str`
The ``PhaseEncodingDirection`` metadata value
Returns
-------
spatialimage : :obj:`nibabel.nifti.Nifti1Image`
A NIfTI 1.0 object containing the distortion.
"""
# Set polarity & scale VSM (voxel-shift-map) by readout time
vsm = fmap_nii.get_fdata().copy() * (-ro_time if pe_dir.endswith("-") else ro_time)
# Shape of displacements field
# Note that ITK NIfTI fields are 5D (have an empty 4th dimension)
fieldshape = vsm.shape[:3] + (1, 3)
# Convert VSM to voxel displacements
pe_axis = "ijk".index(pe_dir[0])
ijk_deltas = np.zeros((vsm.size, 3), dtype="float32")
ijk_deltas[:, pe_axis] = vsm.reshape(-1)
# To convert from VSM to RAS field we just apply the affine
aff = fmap_nii.affine.copy()
aff[:3, 3] = 0 # Translations MUST NOT be applied, though.
xyz_deltas = nb.affines.apply_affine(aff, ijk_deltas)
if itk_format:
# ITK displacement vectors are in LPS orientation
xyz_deltas[..., (0, 1)] *= -1.0
xyz_nii = nb.Nifti1Image(xyz_deltas.reshape(fieldshape), fmap_nii.affine)
xyz_nii.header.set_intent("vector", name="SDC")
xyz_nii.header.set_xyzt_units("mm")
return xyz_nii
def disp_to_fmap(xyz_nii, epi_nii, ro_time, pe_dir, itk_format=True):
"""
Convert a displacements field into a fieldmap in Hz.
This is the inverse operation to the previous function.
Parameters
----------
xyz_nii : :class:`nibabel.Nifti1Image`
Displacements field in NIfTI format.
epi_nii : :class:`nibabel.Nifti1Image`
Source EPI image.
ro_time : :obj:`float`
The total readout time of the EPI image in seconds.
pe_dir : :obj:`str`
The ``PhaseEncodingDirection`` metadata value of the EPI image.
Returns
-------
spatialimage : :obj:`nibabel.nifti.Nifti1Image`
A NIfTI 1.0 object containing the field in Hz.
"""
xyz_deltas = np.squeeze(xyz_nii.get_fdata(dtype="float32")).reshape((-1, 3))
if itk_format:
# ITK displacement vectors are in LPS orientation
xyz_deltas[:, (0, 1)] *= -1
# Use the EPI's affine to determine voxel spacing, axis ordering and flips
inv_aff = np.linalg.inv(epi_nii.affine)
inv_mat = inv_aff[:3, :3]
# Convert displacements from mm to voxel units
# Using the inverse affine accounts for reordering of axes, etc.
ijk_deltas = (xyz_deltas @ inv_mat.T).astype("float32")
pe_axis = "ijk".index(pe_dir[0])
vsm = ijk_deltas[:, pe_axis].reshape(xyz_nii.shape[:3])
scale_factor = -ro_time if pe_dir.endswith("-") else ro_time
fmap_nii = nb.Nifti1Image(vsm / scale_factor, xyz_nii.affine)
fmap_nii.header.set_intent("estimate", name="Delta_B0 [Hz]")
fmap_nii.header.set_xyzt_units("mm")
fmap_nii.header["cal_max"] = max(
(
abs(np.asanyarray(fmap_nii.dataobj).min()),
np.asanyarray(fmap_nii.dataobj).max(),
)
)
fmap_nii.header["cal_min"] = -fmap_nii.header["cal_max"]
return fmap_nii
def grid_bspline_weights(target_nii, ctrl_nii, dtype="float32"):
r"""
Evaluate tensor-product B-Spline weights on a grid.
.. _bspline-tensor:
For each of the *N* input samples :math:`(s_1, s_2, s_3)` and *K* control
points or *knots* :math:`\mathbf{k} =(k_1, k_2, k_3)`, the tensor-product
cubic B-Spline kernel weights are calculated:
.. math::
\Psi^3(\mathbf{k}, \mathbf{s}) =
\beta^3(s_1 - k_1) \cdot \beta^3(s_2 - k_2) \cdot \beta^3(s_3 - k_3),
\label{eq:2}\tag{2}
where each :math:`\beta^3` represents the cubic B-Spline for one dimension.
The 1D B-Spline kernel implementation uses :obj:`numpy.piecewise`, and is based on the
closed-form given by Eq. (6) of [Unser1999]_.
By iterating over dimensions, the data samples that fall outside of the compact
support of the tensor-product kernel associated to each control point can be filtered
out and dismissed to lighten computation.
Finally, the resulting weights matrix :math:`\Psi^3(\mathbf{k}, \mathbf{s})` can easily be
identified in `Eq. (1) <sdcflows.interfaces.bspline.html#bspline-interpolation>`_,
and used as the design matrix for approximation of data.
Parameters
----------
target_nii : :obj:`nibabel.spatialimages`
An spatial image object (typically, a :obj:`~nibabel.nifti1.Nifti1Image`)
embedding the target EPI image to be corrected.
Provides the location of the *N* samples (total number of voxels) in the space.
ctrl_nii : :obj:`nibabel.spatialimages`
An spatial image object (typically, a :obj:`~nibabel.nifti1.Nifti1Image`)
embedding the location of the control points of the B-Spline grid.
The data array should contain a total of :math:`K` knots (control points).
Returns
-------
weights : :obj:`numpy.ndarray` (:math:`N \times K`)
A sparse matrix of interpolating weights :math:`\Psi^3(\mathbf{k}, \mathbf{s})`
for the *N* voxels of the target EPI, for each of the total *K* knots.
This sparse matrix can be directly used as design matrix for the fitting
step of approximation/extrapolation.
"""
sample_shape = target_nii.shape[:3]
knots_shape = ctrl_nii.shape[:3]
# Ensure the cross-product of affines is near zero (i.e., both coordinate systems are aligned)
if not np.allclose(
np.linalg.norm(
np.cross(ctrl_nii.affine[:-1, :-1].T, target_nii.affine[:-1, :-1].T),
axis=1,
),
0,
atol=1e-3,
):
warn("Image's and B-Spline's grids are not aligned.")
target_to_grid = np.linalg.inv(ctrl_nii.affine) @ target_nii.affine
wd = []
for axis in range(3):
# 3D ijk coordinates of current axis
coords = np.zeros((3, sample_shape[axis]), dtype=dtype)
coords[axis] = np.arange(sample_shape[axis], dtype=dtype)
# Calculate the index component of samples w.r.t. B-Spline knots along current axis
# Size of locations is L
locs = nb.affines.apply_affine(target_to_grid, coords.T)[:, axis]
# Size of knots is K + 6 so that all locations are fully covered by basis
knots = np.arange(-3, knots_shape[axis] + 3, dtype=dtype)
bspl = BSpline(knots, np.eye(len(knots) - 3 - 1), 3)
# Construct a sparse design matrix (L, K)
distance = np.abs(locs[..., np.newaxis] - knots[np.newaxis, 3:-3])
within_support = distance < 2.0
colloc_ax = lil_array(distance.shape, dtype=dtype)
colloc_ax[within_support] = bspl(locs)[:, 1:-1][within_support]
# Convert to CSR for efficient multiplication
wd.append(colloc_ax.tocsr())
# Calculate the tensor product of the three design matrices
return kron(kron(wd[0], wd[1]), wd[2])
def _move_coeff(in_coeff, fmap_ref, transform, fmap_target=None):
"""Read in a rigid transform from ANTs, and update the coefficients field affine."""
xfm = nt.linear.Affine(
nt.io.itk.ITKLinearTransform.from_filename(transform).to_ras(),
reference=fmap_ref,
)
coeff = nb.load(in_coeff)
hdr = coeff.header.copy()
if fmap_target is not None: # Debug mode: generate fieldmap reference
nii_target = nb.load(fmap_target)
debug_ref = (~xfm).apply(fmap_ref, reference=nii_target)
debug_ref.header.set_qform(nii_target.affine, code=1)
debug_ref.header.set_sform(nii_target.affine, code=1)
debug_ref.to_filename(Path() / "debug_fmapref.nii.gz")
# Generate a new transform
newaff = np.linalg.inv(np.linalg.inv(coeff.affine) @ (~xfm).matrix)
# Prepare output file
hdr.set_qform(newaff, code=1)
hdr.set_sform(newaff, code=1)
# Make it easy on viz software to render proper range
hdr["cal_max"] = max(
(
abs(np.asanyarray(coeff.dataobj).min()),
np.asanyarray(coeff.dataobj).max(),
)
)
hdr["cal_min"] = -hdr["cal_max"]
return coeff.__class__(coeff.dataobj, newaff, hdr)