warpkit 1.3.0__tar.gz → 1.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {warpkit-1.3.0 → warpkit-1.4.1}/PKG-INFO +1 -1
- {warpkit-1.3.0 → warpkit-1.4.1}/codecov +0 -0
- warpkit-1.4.1/codecov.SHA256SUM +1 -0
- warpkit-1.4.1/codecov.SHA256SUM.sig +16 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/romeo.h +16 -4
- {warpkit-1.3.0 → warpkit-1.4.1}/include/warps.h +5 -10
- warpkit-1.4.1/tests/test_orientation.py +255 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_utilities.py +29 -14
- {warpkit-1.3.0 → warpkit-1.4.1}/uv.lock +79 -81
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/distortion.py +3 -33
- warpkit-1.4.1/warpkit/scripts/_cli.py +51 -0
- warpkit-1.4.1/warpkit/scripts/_outputs.py +54 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/apply_warp.py +2 -4
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/compute_fieldmap.py +12 -65
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/compute_jacobian.py +2 -4
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/convert_fieldmap.py +3 -7
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/convert_warp.py +2 -4
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/medic.py +6 -48
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/unwrap_phase.py +3 -7
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/unwrap.py +4 -2
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/utilities.py +186 -60
- warpkit-1.3.0/codecov.SHA256SUM +0 -1
- warpkit-1.3.0/codecov.SHA256SUM.sig +0 -16
- {warpkit-1.3.0 → warpkit-1.4.1}/.github/workflows/build.yml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/.gitignore +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/.pre-commit-config.yaml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/.python-version +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/.vscode/c_cpp_properties.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/CLAUDE.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/CMakeLists.txt +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/Dockerfile +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/LICENSE +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/README.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/codecov.yml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/itk/itkModifiedInvertDisplacementFieldImageFilter.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/itk/itkModifiedInvertDisplacementFieldImageFilter.hxx +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/LICENSE +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/README.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/algorithm.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/priority_queue.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/seed.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/unwrap.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/utility.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/volume_view.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/voxel_quality.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/romeo/weights.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/include/utilities.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/notes/ROMEO_port_plan.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/notes/fmap.png +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/notes/phase.png +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/build_bundle.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/bundle_README.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/hooks/hook-warpkit.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-apply-warp.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-compute-fieldmap.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-compute-jacobian.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-convert-fieldmap.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-convert-warp.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-medic.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/launchers/wk-unwrap-phase.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/packaging/pyinstaller/warpkit.spec +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/pyproject.toml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/scripts/check-gitmoji.sh +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/scripts/regen-stub.sh +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/src/warpkit.cpp +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/conftest.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/romeo/Mag.nii +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/romeo/Phase.nii +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-1_part-mag_bold.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-1_part-mag_bold.nii.gz +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-1_part-phase_bold.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-1_part-phase_bold.nii.gz +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-2_part-mag_bold.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-2_part-mag_bold.nii.gz +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-2_part-phase_bold.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-2_part-phase_bold.nii.gz +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-3_part-mag_bold.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-3_part-mag_bold.nii.gz +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-3_part-phase_bold.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/data/test_data/sub-a01_task-rest_acq-tr1800_echo-3_part-phase_bold.nii.gz +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_concurrency.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_distortion.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_model.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_romeo.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_scripts.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_unwrap.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/tests/test_version.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/__init__.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/api.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/concurrency.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/model.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/py.typed +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/__init__.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/_metadata.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/scripts/_warp_io.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.1}/warpkit/warpkit_cpp.pyi +0 -0
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ca1d64196d2d34771084afe76ea657d581bf628e31d993ff8e52ea09cc88a56d codecov
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
-----BEGIN PGP SIGNATURE-----
|
|
2
|
+
|
|
3
|
+
iQIzBAABCgAdFiEEJwNOf9uFDgu8LGL/gGuyiu13mGkFAmpO+3UACgkQgGuyiu13
|
|
4
|
+
mGm0UxAAne5/lA7YQHQlWt455twIGwZgcSX13LTQw45+goIfZC7IGDQ7J6/kLyJG
|
|
5
|
+
sGvkfSmYEODgsgo6474CWcm16VPMZa3QWa4z6EdfyNHhDo5nNe358ZPA1dOemMKZ
|
|
6
|
+
eV2/9WpDcIEIAhsCj8HKjNjsLBcGrAQRbOw2tABg2dbE8Yg94SMdEqjMCswOQn3i
|
|
7
|
+
OPsybWDjbAdqUVIWylUXbned5/hVnzNAMeo6WvC5+LNw1iS+8rzSIUu5iSxCz9jV
|
|
8
|
+
XBe0ZNZ/z1jLZ0NHQXWoOWVT6aHI3Aml/V+ttduuSPwVqWUtqT+XWnnxXJlkt3fQ
|
|
9
|
+
uB8Fy9NBkj5qkls81pUyt0Z9QuMvnI/OxWiUZ6jOEItiZMkK87ryPiheBoaU4kuf
|
|
10
|
+
fcpoRz/EVNsAgcrHBFQCiam7I32h5R5O9orDiU4pKzahSdgelnvOPIZQzSKT5m/M
|
|
11
|
+
50tuts7npVQib2op7XCv+6d3t8ZnUHzXnqiej6n/vWxE3nWJiw39a5ww7RDO1RQB
|
|
12
|
+
8oNPX3qvPGpNmspXSf57oLyJqiSs4t7B9yIacCMPnlVDZt3MseXP/UHH7cXMmRuX
|
|
13
|
+
bU2mkdieBgkGtkNkB76sp/ob5FWQmynW1+Q3ZcDGrGeysnRgDGWD2Yy92lcABZDc
|
|
14
|
+
DXolC9kpvP+UrUXG5XSTIKksMbH4YacgNIKHzwyhOgViMLWBA4g=
|
|
15
|
+
=fH2Y
|
|
16
|
+
-----END PGP SIGNATURE-----
|
|
@@ -153,8 +153,13 @@ py::array_t<T, py::array::f_style> romeo_unwrap3D(py::array_t<T, py::array::f_st
|
|
|
153
153
|
opts.correct_global = correctglobal;
|
|
154
154
|
opts.maxseeds = maxseeds;
|
|
155
155
|
opts.wrap_addition = T(0);
|
|
156
|
-
// Standalone 3D unwrap: no phase2/TEs.
|
|
157
|
-
|
|
156
|
+
// Standalone 3D unwrap: no phase2/TEs. The kernel touches only raw buffers
|
|
157
|
+
// (out_ptr and the pointers in opts), so release the GIL around it to let
|
|
158
|
+
// thread-pool callers unwrap frames concurrently.
|
|
159
|
+
{
|
|
160
|
+
py::gil_scoped_release release;
|
|
161
|
+
unwrap_3d<T>(out_ptr, nx, ny, nz, opts);
|
|
162
|
+
}
|
|
158
163
|
return out;
|
|
159
164
|
}
|
|
160
165
|
|
|
@@ -209,8 +214,15 @@ py::array_t<T, py::array::f_style> romeo_unwrap4D(py::array_t<T, py::array::f_st
|
|
|
209
214
|
for (std::size_t i = 0; i < n_total; ++i) out_ptr[i] = phase_ptr[i];
|
|
210
215
|
|
|
211
216
|
// Julia's default template echo is 1 (first echo). In 0-based: 0.
|
|
212
|
-
|
|
213
|
-
|
|
217
|
+
// Hoist the TEs pointer before releasing the GIL (.data() must run with the
|
|
218
|
+
// GIL held); the kernel then works purely on raw buffers, so release the
|
|
219
|
+
// GIL around it for concurrent thread-pool unwrapping.
|
|
220
|
+
const T* tes_ptr = TEs.data();
|
|
221
|
+
{
|
|
222
|
+
py::gil_scoped_release release;
|
|
223
|
+
unwrap_4d<T>(out_ptr, nx, ny, nz, ne, tes_ptr, mag_ptr, mask_ptr, correctglobal, maxseeds,
|
|
224
|
+
/*template_echo=*/0);
|
|
225
|
+
}
|
|
214
226
|
return out;
|
|
215
227
|
}
|
|
216
228
|
|
|
@@ -19,17 +19,12 @@
|
|
|
19
19
|
|
|
20
20
|
namespace py = pybind11;
|
|
21
21
|
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
|
|
22
|
+
// Signal checking is intentionally a no-op on all builds. PyErr_CheckSignals()
|
|
23
|
+
// requires holding the GIL, which would block releasing it around the compute
|
|
24
|
+
// kernels (see py::gil_scoped_release in the bindings). We trade in-call Ctrl+C
|
|
25
|
+
// interruptibility for GIL-free execution; SIGINT is still handled between the
|
|
26
|
+
// per-frame C++ calls dispatched from Python.
|
|
26
27
|
#define WARPKIT_CHECK_SIGNALS() ((void)0)
|
|
27
|
-
#else
|
|
28
|
-
#define WARPKIT_CHECK_SIGNALS() \
|
|
29
|
-
do { \
|
|
30
|
-
if (PyErr_CheckSignals() != 0) throw py::error_already_set(); \
|
|
31
|
-
} while (0)
|
|
32
|
-
#endif
|
|
33
28
|
|
|
34
29
|
/**
|
|
35
30
|
* @brief Invert a displacement map
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""Orientation x phase-encoding-direction correctness.
|
|
2
|
+
|
|
3
|
+
MEDIC (and the correction it produces) is a physical operation: the same field
|
|
4
|
+
map, phase-encode direction, and image must yield the *same physical* correction
|
|
5
|
+
no matter which of the 48 valid voxel orientations the data happens to be stored
|
|
6
|
+
in on disk — provided the phase-encoding-direction (PED) string is relabelled to
|
|
7
|
+
match each storage orientation, exactly as BIDS requires.
|
|
8
|
+
|
|
9
|
+
The suite therefore takes RAS as the golden reference (the orientation warpkit
|
|
10
|
+
was validated against) and asserts that every other orientation reduces to the
|
|
11
|
+
*same* physical result. Because RAS is correct, this equivariance is equivalent
|
|
12
|
+
to correctness for every orientation.
|
|
13
|
+
|
|
14
|
+
Two absolute anchors in RAS pin the sign/magnitude so the sweep can't pass by
|
|
15
|
+
being uniformly wrong, and an oracle-guard test independently checks that the
|
|
16
|
+
PED-remap helper the sweep relies on really does track the intended physical
|
|
17
|
+
axis.
|
|
18
|
+
|
|
19
|
+
See ``tests/test_utilities.py`` for the per-function (Hz<->mm, map<->field)
|
|
20
|
+
convention tests; this file is the end-to-end orientation behaviour.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
import itertools
|
|
24
|
+
from collections.abc import Sequence
|
|
25
|
+
from typing import cast
|
|
26
|
+
|
|
27
|
+
import nibabel as nib
|
|
28
|
+
import numpy as np
|
|
29
|
+
import pytest
|
|
30
|
+
from nibabel.orientations import axcodes2ornt, io_orientation, ornt_transform
|
|
31
|
+
from numpy.testing import assert_allclose
|
|
32
|
+
from warpkit.utilities import (
|
|
33
|
+
AXIS_MAP,
|
|
34
|
+
displacement_map_to_field,
|
|
35
|
+
field_maps_to_displacement_maps,
|
|
36
|
+
reconstruct_displacement_and_field_maps,
|
|
37
|
+
resample_image,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# ---------------------------------------------------------------------------
|
|
41
|
+
# helpers
|
|
42
|
+
# ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
# The three anatomical axis pairs (positive, negative endpoint per world axis).
|
|
45
|
+
_AXIS_PAIRS = (("R", "L"), ("A", "P"), ("S", "I"))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _all_axcodes() -> list[tuple[str, str, str]]:
|
|
49
|
+
"""Every valid voxel orientation: the 6 axis permutations x 8 sign
|
|
50
|
+
combinations = 48 axcode triples (RAS, LAS, PSR, ...)."""
|
|
51
|
+
codes = []
|
|
52
|
+
for perm in itertools.permutations(range(3)):
|
|
53
|
+
for signs in itertools.product((0, 1), repeat=3):
|
|
54
|
+
codes.append(tuple(_AXIS_PAIRS[perm[d]][signs[d]] for d in range(3)))
|
|
55
|
+
return codes
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
ALL_AXCODES = _all_axcodes()
|
|
59
|
+
|
|
60
|
+
# base phase-encode directions in RAS, keyed by the world axis they point along
|
|
61
|
+
# (+R = world 0, +A = world 1, +S = world 2).
|
|
62
|
+
_BASE_PEDS = {0: "i", 1: "j", 2: "k"}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _reorient(img: nib.Nifti1Image, axcodes: tuple[str, str, str]) -> nib.Nifti1Image:
|
|
66
|
+
"""Relabel ``img`` into the target voxel orientation (lossless: physical
|
|
67
|
+
content unchanged, only the array layout + affine)."""
|
|
68
|
+
transform = ornt_transform(io_orientation(img.affine), axcodes2ornt(axcodes))
|
|
69
|
+
# ornt_transform returns an ndarray; as_reoriented is typed for the nested
|
|
70
|
+
# Sequence form nibabel also accepts.
|
|
71
|
+
return img.as_reoriented(cast(Sequence[Sequence[int]], transform))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _ped_for_axcodes(axcodes: tuple[str, str, str], base_world_axis: int) -> str:
|
|
75
|
+
"""The PED string that, in ``axcodes`` storage, points along the same
|
|
76
|
+
physical direction as the positive ``base_world_axis`` does in RAS.
|
|
77
|
+
|
|
78
|
+
``axcodes2ornt(axcodes)[q] = (w, s)`` means voxel axis q maps to world axis
|
|
79
|
+
w with sign s; we want the voxel axis carrying ``base_world_axis``, and a
|
|
80
|
+
trailing ``-`` when increasing that index runs *against* the world axis.
|
|
81
|
+
"""
|
|
82
|
+
ornt = axcodes2ornt(axcodes)
|
|
83
|
+
for voxel_axis in range(3):
|
|
84
|
+
world_axis, sign = ornt[voxel_axis]
|
|
85
|
+
if int(world_axis) == base_world_axis:
|
|
86
|
+
letter = "ijk"[voxel_axis]
|
|
87
|
+
return letter if sign > 0 else letter + "-"
|
|
88
|
+
raise AssertionError(f"no voxel axis maps to world axis {base_world_axis}")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# synthetic acquisition (RAS): anisotropic voxels + a non-trivial origin so the
|
|
92
|
+
# sweep is sensitive to axis-size and translation bookkeeping, an asymmetric
|
|
93
|
+
# smooth field map (Hz), and an off-centre blob to correct.
|
|
94
|
+
_SHAPE = (11, 13, 15)
|
|
95
|
+
_AFFINE = np.diag([2.0, 3.0, 4.0, 1.0])
|
|
96
|
+
_AFFINE[:3, 3] = [5.0, -7.0, 11.0]
|
|
97
|
+
_TRT = 0.04
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _ras_inputs() -> tuple[nib.Nifti1Image, nib.Nifti1Image]:
|
|
101
|
+
ii, jj, kk = np.indices(_SHAPE)
|
|
102
|
+
fmap = (
|
|
103
|
+
20.0 * np.sin(2 * np.pi * (ii / _SHAPE[0] + jj / _SHAPE[1] + kk / _SHAPE[2]))
|
|
104
|
+
).astype(np.float32)
|
|
105
|
+
centre = np.array([4.0, 8.0, 6.0])
|
|
106
|
+
blob = np.exp(
|
|
107
|
+
-(
|
|
108
|
+
((ii - centre[0]) / 3) ** 2
|
|
109
|
+
+ ((jj - centre[1]) / 2.5) ** 2
|
|
110
|
+
+ ((kk - centre[2]) / 3.5) ** 2
|
|
111
|
+
)
|
|
112
|
+
).astype(np.float32)
|
|
113
|
+
fmap_img = nib.Nifti1Image(fmap[..., None], _AFFINE) # 4D (single frame)
|
|
114
|
+
moving_img = nib.Nifti1Image(blob, _AFFINE)
|
|
115
|
+
return fmap_img, moving_img
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _corrected(
|
|
119
|
+
fmap: nib.Nifti1Image, moving: nib.Nifti1Image, ped: str
|
|
120
|
+
) -> nib.Nifti1Image:
|
|
121
|
+
"""Full correction pipeline: reconstruct the displacement map (with the
|
|
122
|
+
C++ inversion) then apply it to ``moving`` — exactly what medic + apply-warp
|
|
123
|
+
do, minus the ROMEO unwrap (which is per-voxel and orientation-agnostic)."""
|
|
124
|
+
dmaps, _ = reconstruct_displacement_and_field_maps(fmap, _TRT, ped)
|
|
125
|
+
field = displacement_map_to_field(dmaps, axis=ped, format="itk", frame=0)
|
|
126
|
+
return resample_image(moving, moving, field)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@pytest.fixture(scope="module")
|
|
130
|
+
def ras_references() -> dict[int, np.ndarray]:
|
|
131
|
+
"""The corrected image in RAS for each base PE axis — the golden result
|
|
132
|
+
every orientation must reproduce."""
|
|
133
|
+
fmap, moving = _ras_inputs()
|
|
134
|
+
return {
|
|
135
|
+
world_axis: _corrected(fmap, moving, ped).get_fdata()
|
|
136
|
+
for world_axis, ped in _BASE_PEDS.items()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# ---------------------------------------------------------------------------
|
|
141
|
+
# oracle guard: the PED-remap helper really tracks the physical axis
|
|
142
|
+
# ---------------------------------------------------------------------------
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@pytest.mark.parametrize("axcodes", ALL_AXCODES, ids=lambda a: "".join(a))
|
|
146
|
+
def test_ped_remap_tracks_physical_axis(axcodes):
|
|
147
|
+
"""For every orientation and every physical PE axis, the remapped PED must
|
|
148
|
+
name the voxel axis whose (signed) direction equals the physical axis — the
|
|
149
|
+
invariant the equivariance sweep depends on."""
|
|
150
|
+
affine = _reorient(
|
|
151
|
+
nib.Nifti1Image(np.zeros(_SHAPE, np.float32), _AFFINE), axcodes
|
|
152
|
+
).affine
|
|
153
|
+
ornt = io_orientation(affine) # voxel axis -> (world axis, sign)
|
|
154
|
+
for base_world_axis in (0, 1, 2):
|
|
155
|
+
ped = _ped_for_axcodes(axcodes, base_world_axis)
|
|
156
|
+
voxel_axis = AXIS_MAP[ped]
|
|
157
|
+
world_axis, sign = ornt[voxel_axis]
|
|
158
|
+
assert int(world_axis) == base_world_axis
|
|
159
|
+
# a trailing "-" is present iff increasing the voxel index runs against
|
|
160
|
+
# the positive world direction.
|
|
161
|
+
expected_negative = sign < 0
|
|
162
|
+
assert ped.endswith("-") == expected_negative
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# ---------------------------------------------------------------------------
|
|
166
|
+
# core: orientation equivariance of the full correction over all 48 orientations
|
|
167
|
+
# ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@pytest.mark.parametrize("axcodes", ALL_AXCODES, ids=lambda a: "".join(a))
|
|
171
|
+
def test_correction_is_orientation_equivariant(axcodes, ras_references):
|
|
172
|
+
"""Reorient the acquisition into ``axcodes``, remap the PED, run the full
|
|
173
|
+
correction, then reorient the corrected image back to RAS: it must match the
|
|
174
|
+
RAS reference for every base PE axis, i.e. the physical correction does not
|
|
175
|
+
depend on how the volume is stored."""
|
|
176
|
+
fmap_ras, moving_ras = _ras_inputs()
|
|
177
|
+
fmap = _reorient(fmap_ras, axcodes)
|
|
178
|
+
moving = _reorient(moving_ras, axcodes)
|
|
179
|
+
|
|
180
|
+
for base_world_axis, reference in ras_references.items():
|
|
181
|
+
ped = _ped_for_axcodes(axcodes, base_world_axis)
|
|
182
|
+
corrected = _corrected(fmap, moving, ped)
|
|
183
|
+
# back to RAS for comparison (scalar image -> lossless reorient)
|
|
184
|
+
back = _reorient(corrected, ("R", "A", "S")).get_fdata()
|
|
185
|
+
assert_allclose(
|
|
186
|
+
back,
|
|
187
|
+
reference,
|
|
188
|
+
atol=1e-4,
|
|
189
|
+
err_msg=(
|
|
190
|
+
f"orientation {''.join(axcodes)} PED {ped!r} (physical axis "
|
|
191
|
+
f"{base_world_axis}) diverged from the RAS correction"
|
|
192
|
+
),
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
# ---------------------------------------------------------------------------
|
|
197
|
+
# absolute anchors in RAS (so the sweep cannot pass by being uniformly wrong)
|
|
198
|
+
# ---------------------------------------------------------------------------
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@pytest.mark.parametrize(
|
|
202
|
+
"pe_dir, channel, lps_sign",
|
|
203
|
+
[
|
|
204
|
+
("i", 0, -1.0), # world x: RAS->LPS flip
|
|
205
|
+
("j", 1, -1.0), # world y: RAS->LPS flip
|
|
206
|
+
("k", 2, +1.0), # world z: no flip
|
|
207
|
+
],
|
|
208
|
+
)
|
|
209
|
+
def test_displacement_field_known_value_ras(pe_dir, channel, lps_sign):
|
|
210
|
+
"""A constant field map yields a constant displacement field: magnitude
|
|
211
|
+
``fmap * trt * voxel`` in the PE world channel (carrying the RAS->LPS flip),
|
|
212
|
+
and exactly zero in the other two channels."""
|
|
213
|
+
voxel = (2.0, 3.0, 4.0)
|
|
214
|
+
affine = np.diag([*voxel, 1.0])
|
|
215
|
+
fmap_hz, trt = 10.0, 0.05
|
|
216
|
+
fmap = nib.Nifti1Image(np.full((5, 5, 5), fmap_hz, np.float32), affine)
|
|
217
|
+
dmap = field_maps_to_displacement_maps(fmap, trt, pe_dir)
|
|
218
|
+
field = displacement_map_to_field(dmap, axis=pe_dir, format="itk", frame=0)
|
|
219
|
+
data = field.get_fdata()
|
|
220
|
+
expected = fmap_hz * trt * voxel[channel] * lps_sign
|
|
221
|
+
assert_allclose(data[..., channel], expected, atol=1e-5)
|
|
222
|
+
for other in {0, 1, 2} - {channel}:
|
|
223
|
+
assert_allclose(data[..., other], 0.0, atol=1e-6)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@pytest.mark.parametrize("pe_dir, expected_shift", [("j", -2.0), ("j-", +2.0)])
|
|
227
|
+
def test_resample_integer_voxel_shift_ras(pe_dir, expected_shift):
|
|
228
|
+
"""With a constant field map chosen to displace by exactly two voxels, a
|
|
229
|
+
linear ramp resamples to that ramp shifted by exactly +-2 voxels along the
|
|
230
|
+
PE axis (linear interpolation is exact on a ramp), and only along that axis.
|
|
231
|
+
Pins the absolute correction direction/magnitude, and that ``j``/``j-`` are
|
|
232
|
+
mirror images."""
|
|
233
|
+
voxel_y = 3.0
|
|
234
|
+
affine = np.diag([2.0, voxel_y, 4.0, 1.0])
|
|
235
|
+
shape = (13, 15, 17)
|
|
236
|
+
# fmap * trt * voxel_y = 20 * 0.1 * 3 = 6 mm = 2 voxels along y
|
|
237
|
+
fmap = nib.Nifti1Image(np.full(shape, 20.0, np.float32), affine)
|
|
238
|
+
dmap = field_maps_to_displacement_maps(fmap, 0.1, pe_dir)
|
|
239
|
+
field = displacement_map_to_field(dmap, axis=pe_dir, format="itk", frame=0)
|
|
240
|
+
ramp = np.broadcast_to(
|
|
241
|
+
np.arange(shape[1], dtype=np.float32)[None, :, None], shape
|
|
242
|
+
).copy()
|
|
243
|
+
moving = nib.Nifti1Image(ramp, affine)
|
|
244
|
+
out = resample_image(moving, moving, field).get_fdata()
|
|
245
|
+
|
|
246
|
+
# interior column along y (avoid the clamped edges), fixed x, z
|
|
247
|
+
interior = slice(4, shape[1] - 4)
|
|
248
|
+
col_in = ramp[6, interior, 8]
|
|
249
|
+
col_out = out[6, interior, 8]
|
|
250
|
+
assert_allclose(col_out - col_in, expected_shift, atol=1e-4)
|
|
251
|
+
# there really is no x/z displacement — assert it on the field directly,
|
|
252
|
+
# since the ramp is flat in x/z and can't reveal off-axis motion via resample.
|
|
253
|
+
field_data = field.get_fdata()
|
|
254
|
+
assert_allclose(field_data[..., AXIS_MAP["x"]], 0.0, atol=1e-6)
|
|
255
|
+
assert_allclose(field_data[..., AXIS_MAP["z"]], 0.0, atol=1e-6)
|
|
@@ -23,6 +23,7 @@ from warpkit.utilities import (
|
|
|
23
23
|
invert_displacement_field,
|
|
24
24
|
invert_displacement_maps,
|
|
25
25
|
normalize,
|
|
26
|
+
reconstruct_displacement_and_field_maps,
|
|
26
27
|
rescale_phase,
|
|
27
28
|
setup_logging,
|
|
28
29
|
)
|
|
@@ -209,18 +210,18 @@ def test_displacement_maps_to_field_maps_flip_sign():
|
|
|
209
210
|
|
|
210
211
|
|
|
211
212
|
def test_displacement_map_to_field_inserts_along_axis():
|
|
212
|
-
"""A scalar displacement map at axis="y" must end up entirely in the y
|
|
213
|
-
of the resulting 3-vector field
|
|
214
|
-
the
|
|
213
|
+
"""A scalar displacement map at axis="y" must end up entirely in the y
|
|
214
|
+
component of the resulting 3-vector field, negated by the NIfTI-RAS ->
|
|
215
|
+
ITK-LPS y flip that displacement_map_to_field applies at the field boundary."""
|
|
215
216
|
rng = np.random.default_rng(0)
|
|
216
217
|
affine = np.eye(4)
|
|
217
218
|
data = rng.standard_normal((4, 4, 4, 2)).astype(np.float32)
|
|
218
219
|
dmap = nib.Nifti1Image(data, affine)
|
|
219
220
|
field = displacement_map_to_field(dmap, axis="y", format="itk", frame=0)
|
|
220
221
|
field_data = field.get_fdata().squeeze()
|
|
221
|
-
#
|
|
222
|
-
#
|
|
223
|
-
assert_allclose(field_data[..., AXIS_MAP["y"]], data[..., 0])
|
|
222
|
+
# y (RAS axis 1) picks up the RAS->LPS flip → negated source frame; x and z
|
|
223
|
+
# are zero.
|
|
224
|
+
assert_allclose(field_data[..., AXIS_MAP["y"]], -data[..., 0])
|
|
224
225
|
assert_allclose(field_data[..., AXIS_MAP["x"]], 0.0)
|
|
225
226
|
assert_allclose(field_data[..., AXIS_MAP["z"]], 0.0)
|
|
226
227
|
|
|
@@ -404,12 +405,16 @@ def test_displacement_field_to_map_clears_vector_intent():
|
|
|
404
405
|
@pytest.mark.parametrize(
|
|
405
406
|
"pe_dir, expected_sign",
|
|
406
407
|
[
|
|
407
|
-
|
|
408
|
-
("
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
("
|
|
412
|
-
("
|
|
408
|
+
# Data-space convention: displacement = fmap * trt * |voxel|, signed only
|
|
409
|
+
# by the PE polarity ("-"). The NIfTI-RAS -> ITK-LPS x/y flip is applied
|
|
410
|
+
# later, at the ITK boundary (invert_displacement_maps /
|
|
411
|
+
# displacement_map_to_field), so this conversion is orientation-free.
|
|
412
|
+
("i", +1.0), # positive polarity
|
|
413
|
+
("i-", -1.0), # "-" polarity
|
|
414
|
+
("j", +1.0),
|
|
415
|
+
("j-", -1.0),
|
|
416
|
+
("k", +1.0),
|
|
417
|
+
("k-", -1.0),
|
|
413
418
|
],
|
|
414
419
|
)
|
|
415
420
|
def test_field_maps_to_displacement_maps_known_value(pe_dir, expected_sign):
|
|
@@ -426,6 +431,15 @@ def test_field_maps_to_displacement_maps_known_value(pe_dir, expected_sign):
|
|
|
426
431
|
assert_allclose(dmap.get_fdata(), expected, atol=1e-5)
|
|
427
432
|
|
|
428
433
|
|
|
434
|
+
def test_reconstruct_displacement_and_field_maps_rejects_3d():
|
|
435
|
+
"""The reconstruction tail iterates the trailing axis as frames, so a 3D
|
|
436
|
+
field map must be rejected up front rather than silently treated as a
|
|
437
|
+
stack of 2D slices."""
|
|
438
|
+
fmap_3d = nib.Nifti1Image(np.zeros((4, 4, 4), dtype=np.float32), np.eye(4))
|
|
439
|
+
with pytest.raises(ValueError, match="must be 4D"):
|
|
440
|
+
reconstruct_displacement_and_field_maps(fmap_3d, 0.05, "j")
|
|
441
|
+
|
|
442
|
+
|
|
429
443
|
@pytest.mark.parametrize("axis", ["i", "j", "k"])
|
|
430
444
|
def test_field_maps_to_displacement_maps_anisotropic_voxels(axis):
|
|
431
445
|
"""The voxel size used for scaling must come from the PE-axis dimension,
|
|
@@ -482,9 +496,10 @@ def test_displacement_field_to_map_drops_off_axis_channels():
|
|
|
482
496
|
rng = np.random.default_rng(0)
|
|
483
497
|
field_data = rng.standard_normal((4, 4, 4, 3)).astype(np.float32)
|
|
484
498
|
field = nib.Nifti1Image(field_data, affine)
|
|
485
|
-
#
|
|
499
|
+
# displacement_field_to_map undoes the RAS->LPS y flip, so the extracted map
|
|
500
|
+
# is the negated y channel (x and z channels are dropped).
|
|
486
501
|
extracted_y = displacement_field_to_map(field, axis="y", format="itk").get_fdata()
|
|
487
|
-
assert_allclose(extracted_y, field_data[..., 1], atol=1e-6)
|
|
502
|
+
assert_allclose(extracted_y, -field_data[..., 1], atol=1e-6)
|
|
488
503
|
|
|
489
504
|
|
|
490
505
|
# ---------------------------------------------------------------------------
|