warpkit 1.3.0__tar.gz → 1.4.0__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.0}/PKG-INFO +1 -1
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/romeo.h +16 -4
- {warpkit-1.3.0 → warpkit-1.4.0}/include/warps.h +5 -10
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_utilities.py +10 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/distortion.py +3 -33
- warpkit-1.4.0/warpkit/scripts/_cli.py +51 -0
- warpkit-1.4.0/warpkit/scripts/_outputs.py +54 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/apply_warp.py +2 -4
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/compute_fieldmap.py +12 -65
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/compute_jacobian.py +2 -4
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/convert_fieldmap.py +3 -7
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/convert_warp.py +2 -4
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/medic.py +6 -48
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/unwrap_phase.py +3 -7
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/unwrap.py +4 -2
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/utilities.py +98 -28
- {warpkit-1.3.0 → warpkit-1.4.0}/.github/workflows/build.yml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/.gitignore +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/.pre-commit-config.yaml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/.python-version +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/.vscode/c_cpp_properties.json +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/CLAUDE.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/CMakeLists.txt +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/Dockerfile +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/LICENSE +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/README.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/codecov +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/codecov.SHA256SUM +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/codecov.SHA256SUM.sig +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/codecov.yml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/itk/itkModifiedInvertDisplacementFieldImageFilter.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/itk/itkModifiedInvertDisplacementFieldImageFilter.hxx +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/LICENSE +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/README.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/algorithm.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/priority_queue.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/seed.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/unwrap.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/utility.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/volume_view.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/voxel_quality.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/romeo/weights.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/include/utilities.h +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/notes/ROMEO_port_plan.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/notes/fmap.png +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/notes/phase.png +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/build_bundle.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/bundle_README.md +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/hooks/hook-warpkit.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-apply-warp.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-compute-fieldmap.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-compute-jacobian.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-convert-fieldmap.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-convert-warp.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-medic.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/launchers/wk-unwrap-phase.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/packaging/pyinstaller/warpkit.spec +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/pyproject.toml +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/scripts/check-gitmoji.sh +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/scripts/regen-stub.sh +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/src/warpkit.cpp +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/conftest.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/data/romeo/Mag.nii +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/data/romeo/Phase.nii +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/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.0}/tests/test_concurrency.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_distortion.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_model.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_romeo.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_scripts.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_unwrap.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/tests/test_version.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/uv.lock +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/__init__.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/api.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/concurrency.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/model.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/py.typed +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/__init__.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/_metadata.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/scripts/_warp_io.py +0 -0
- {warpkit-1.3.0 → warpkit-1.4.0}/warpkit/warpkit_cpp.pyi +0 -0
|
@@ -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
|
|
@@ -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
|
)
|
|
@@ -426,6 +427,15 @@ def test_field_maps_to_displacement_maps_known_value(pe_dir, expected_sign):
|
|
|
426
427
|
assert_allclose(dmap.get_fdata(), expected, atol=1e-5)
|
|
427
428
|
|
|
428
429
|
|
|
430
|
+
def test_reconstruct_displacement_and_field_maps_rejects_3d():
|
|
431
|
+
"""The reconstruction tail iterates the trailing axis as frames, so a 3D
|
|
432
|
+
field map must be rejected up front rather than silently treated as a
|
|
433
|
+
stack of 2D slices."""
|
|
434
|
+
fmap_3d = nib.Nifti1Image(np.zeros((4, 4, 4), dtype=np.float32), np.eye(4))
|
|
435
|
+
with pytest.raises(ValueError, match="must be 4D"):
|
|
436
|
+
reconstruct_displacement_and_field_maps(fmap_3d, 0.05, "j")
|
|
437
|
+
|
|
438
|
+
|
|
429
439
|
@pytest.mark.parametrize("axis", ["i", "j", "k"])
|
|
430
440
|
def test_field_maps_to_displacement_maps_anisotropic_voxels(axis):
|
|
431
441
|
"""The voxel size used for scaling must come from the PE-axis dimension,
|
|
@@ -4,11 +4,7 @@ import nibabel as nib
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
|
|
6
6
|
from warpkit.unwrap import unwrap_and_compute_field_maps
|
|
7
|
-
from warpkit.utilities import
|
|
8
|
-
displacement_maps_to_field_maps,
|
|
9
|
-
field_maps_to_displacement_maps,
|
|
10
|
-
invert_displacement_maps,
|
|
11
|
-
)
|
|
7
|
+
from warpkit.utilities import reconstruct_displacement_and_field_maps
|
|
12
8
|
|
|
13
9
|
|
|
14
10
|
def medic(
|
|
@@ -119,36 +115,10 @@ def medic(
|
|
|
119
115
|
":("
|
|
120
116
|
) from e
|
|
121
117
|
|
|
122
|
-
#
|
|
123
|
-
|
|
118
|
+
# reconstruct displacement maps and undistorted-space field maps
|
|
119
|
+
displacement_maps, field_maps = reconstruct_displacement_and_field_maps(
|
|
124
120
|
field_maps_native, total_readout_time, phase_encoding_direction
|
|
125
121
|
)
|
|
126
122
|
|
|
127
|
-
# invert displacement maps (these are in undistorted space)
|
|
128
|
-
displacement_maps = invert_displacement_maps(
|
|
129
|
-
inv_displacement_maps, phase_encoding_direction, ignore_rotation=True
|
|
130
|
-
)
|
|
131
|
-
|
|
132
|
-
# convert correction maps back to undistorted space field map. No
|
|
133
|
-
# flip_sign here: the correlation check below sets the sign relative to
|
|
134
|
-
# the native field map, which makes an up-front flip redundant.
|
|
135
|
-
field_maps = displacement_maps_to_field_maps(
|
|
136
|
-
displacement_maps, total_readout_time, phase_encoding_direction
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
# check if we need to flip sign of field maps (this is done by comparing sign of correlation between
|
|
140
|
-
# native and undistorted field maps)
|
|
141
|
-
if (
|
|
142
|
-
np.corrcoef(
|
|
143
|
-
field_maps.dataobj[..., 0].ravel(),
|
|
144
|
-
field_maps_native.dataobj[..., 0].ravel(),
|
|
145
|
-
)[0, 1]
|
|
146
|
-
< 0
|
|
147
|
-
):
|
|
148
|
-
field_map_data = field_maps.get_fdata() * -1
|
|
149
|
-
field_maps = nib.Nifti1Image(
|
|
150
|
-
field_map_data, field_maps.affine, field_maps.header
|
|
151
|
-
)
|
|
152
|
-
|
|
153
123
|
# return correction maps
|
|
154
124
|
return field_maps_native, displacement_maps, field_maps
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Shared argparse argument builders for the warpkit CLI scripts.
|
|
2
|
+
|
|
3
|
+
These cover the argument blocks that are byte-identical across multiple
|
|
4
|
+
scripts (``--version``, ``-n/--n-cpus``, and the EPI ``--total-readout-time`` /
|
|
5
|
+
``--phase-encoding-direction`` pair). Args whose help text is worded per
|
|
6
|
+
script (``--magnitude``, ``--metadata``, ``--debug``, ...) stay inline in each
|
|
7
|
+
script so their wording can diverge freely.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
|
|
14
|
+
from warpkit import __version__
|
|
15
|
+
from warpkit.utilities import PE_DIRECTIONS
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def add_version_arg(parser: argparse.ArgumentParser) -> None:
|
|
19
|
+
"""Add the standard ``--version`` action."""
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def add_n_cpus_arg(parser: argparse.ArgumentParser) -> None:
|
|
26
|
+
"""Add the standard ``-n/--n-cpus`` option (default 4)."""
|
|
27
|
+
parser.add_argument(
|
|
28
|
+
"-n", "--n-cpus", type=int, default=4, help="Number of CPUs to use."
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def add_trt_pe_args(parser: argparse.ArgumentParser) -> None:
|
|
33
|
+
"""Add ``--total-readout-time`` and ``--phase-encoding-direction``.
|
|
34
|
+
|
|
35
|
+
Used by the pipeline scripts where both are optional (resolved from
|
|
36
|
+
``--metadata`` when omitted).
|
|
37
|
+
"""
|
|
38
|
+
parser.add_argument(
|
|
39
|
+
"--total-readout-time",
|
|
40
|
+
type=float,
|
|
41
|
+
help="Total readout time in seconds. Required unless --metadata is given.",
|
|
42
|
+
)
|
|
43
|
+
parser.add_argument(
|
|
44
|
+
"--phase-encoding-direction",
|
|
45
|
+
choices=PE_DIRECTIONS,
|
|
46
|
+
metavar="DIR",
|
|
47
|
+
help=(
|
|
48
|
+
f"Phase encoding direction (one of: {', '.join(PE_DIRECTIONS)}). "
|
|
49
|
+
"Required unless --metadata is given."
|
|
50
|
+
),
|
|
51
|
+
)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Shared output handling for the MEDIC field-map scripts.
|
|
2
|
+
|
|
3
|
+
``wk-medic`` and ``wk-compute-fieldmap`` write the same three NIfTIs from a
|
|
4
|
+
prefix — native-space field map, displacement maps, undistorted-space field
|
|
5
|
+
map — and return the same triple of resolved paths. This module hosts the
|
|
6
|
+
result type and the writer they share.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from os import PathLike
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
import nibabel as nib
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class MedicResult:
|
|
20
|
+
"""Absolute paths of the three NIfTIs written by the MEDIC field-map
|
|
21
|
+
scripts: native-space field map, displacement maps, and undistorted-space
|
|
22
|
+
field map."""
|
|
23
|
+
|
|
24
|
+
fieldmap_native: Path
|
|
25
|
+
displacement_map: Path
|
|
26
|
+
fieldmap: Path
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def write_medic_outputs(
|
|
30
|
+
out_prefix: PathLike[str] | str,
|
|
31
|
+
fieldmap_native: nib.Nifti1Image,
|
|
32
|
+
displacement_map: nib.Nifti1Image,
|
|
33
|
+
fieldmap: nib.Nifti1Image,
|
|
34
|
+
) -> MedicResult:
|
|
35
|
+
"""Write the three MEDIC NIfTIs under ``out_prefix`` and return their
|
|
36
|
+
resolved paths.
|
|
37
|
+
|
|
38
|
+
Writes ``<prefix>_fieldmaps_native.nii``, ``<prefix>_displacementmaps.nii``
|
|
39
|
+
and ``<prefix>_fieldmaps.nii``.
|
|
40
|
+
"""
|
|
41
|
+
out_prefix_str = str(out_prefix)
|
|
42
|
+
print("Saving field maps and displacement maps to file...")
|
|
43
|
+
fmap_native_path = Path(f"{out_prefix_str}_fieldmaps_native.nii").resolve()
|
|
44
|
+
dmap_path = Path(f"{out_prefix_str}_displacementmaps.nii").resolve()
|
|
45
|
+
fmap_path = Path(f"{out_prefix_str}_fieldmaps.nii").resolve()
|
|
46
|
+
fieldmap_native.to_filename(str(fmap_native_path))
|
|
47
|
+
displacement_map.to_filename(str(dmap_path))
|
|
48
|
+
fieldmap.to_filename(str(fmap_path))
|
|
49
|
+
print("Done.")
|
|
50
|
+
return MedicResult(
|
|
51
|
+
fieldmap_native=fmap_native_path,
|
|
52
|
+
displacement_map=dmap_path,
|
|
53
|
+
fieldmap=fmap_path,
|
|
54
|
+
)
|
|
@@ -19,7 +19,6 @@ from typing import cast
|
|
|
19
19
|
import nibabel as nib
|
|
20
20
|
import numpy as np
|
|
21
21
|
|
|
22
|
-
from warpkit import __version__
|
|
23
22
|
from warpkit.utilities import (
|
|
24
23
|
AXIS_MAP,
|
|
25
24
|
WARP_ITK_FLIPS,
|
|
@@ -30,6 +29,7 @@ from warpkit.utilities import (
|
|
|
30
29
|
)
|
|
31
30
|
|
|
32
31
|
from . import epilog
|
|
32
|
+
from ._cli import add_version_arg
|
|
33
33
|
from ._metadata import ensure_image, ensure_images
|
|
34
34
|
|
|
35
35
|
|
|
@@ -243,9 +243,7 @@ def main():
|
|
|
243
243
|
),
|
|
244
244
|
epilog=f"{epilog} 04/24/2026",
|
|
245
245
|
)
|
|
246
|
-
parser
|
|
247
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
248
|
-
)
|
|
246
|
+
add_version_arg(parser)
|
|
249
247
|
parser.add_argument(
|
|
250
248
|
"--input",
|
|
251
249
|
required=True,
|
|
@@ -12,35 +12,23 @@ from __future__ import annotations
|
|
|
12
12
|
|
|
13
13
|
import argparse
|
|
14
14
|
from collections.abc import Sequence
|
|
15
|
-
from dataclasses import dataclass
|
|
16
15
|
from os import PathLike
|
|
17
|
-
from pathlib import Path
|
|
18
16
|
|
|
19
17
|
import nibabel as nib
|
|
20
|
-
import numpy as np
|
|
21
18
|
|
|
22
|
-
from warpkit import __version__
|
|
23
19
|
from warpkit.unwrap import compute_field_maps
|
|
24
20
|
from warpkit.utilities import (
|
|
25
|
-
|
|
26
|
-
field_maps_to_displacement_maps,
|
|
27
|
-
invert_displacement_maps,
|
|
21
|
+
reconstruct_displacement_and_field_maps,
|
|
28
22
|
setup_logging,
|
|
29
23
|
)
|
|
30
24
|
|
|
31
25
|
from . import epilog
|
|
26
|
+
from ._cli import add_n_cpus_arg, add_trt_pe_args, add_version_arg
|
|
32
27
|
from ._metadata import ensure_image, ensure_images, resolve_acquisition
|
|
28
|
+
from ._outputs import MedicResult, write_medic_outputs
|
|
33
29
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
@dataclass(frozen=True, slots=True)
|
|
38
|
-
class ComputeFieldmapResult:
|
|
39
|
-
"""Same three-tuple as :class:`MedicResult`."""
|
|
40
|
-
|
|
41
|
-
fieldmap_native: Path
|
|
42
|
-
displacement_map: Path
|
|
43
|
-
fieldmap: Path
|
|
30
|
+
# wk-compute-fieldmap writes and returns the same three NIfTIs as wk-medic.
|
|
31
|
+
ComputeFieldmapResult = MedicResult
|
|
44
32
|
|
|
45
33
|
|
|
46
34
|
def compute_fieldmap(
|
|
@@ -102,34 +90,10 @@ def compute_fieldmap(
|
|
|
102
90
|
|
|
103
91
|
# Convert native-space field maps to displacement maps in distorted space,
|
|
104
92
|
# invert to get distorted -> undistorted, then re-derive an undistorted-
|
|
105
|
-
# space field map.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
np.corrcoef(
|
|
112
|
-
fmaps.dataobj[..., 0].ravel(),
|
|
113
|
-
fmaps_native.dataobj[..., 0].ravel(),
|
|
114
|
-
)[0, 1]
|
|
115
|
-
< 0
|
|
116
|
-
):
|
|
117
|
-
fmaps = nib.Nifti1Image(fmaps.get_fdata() * -1, fmaps.affine, fmaps.header)
|
|
118
|
-
|
|
119
|
-
out_prefix_str = str(out_prefix)
|
|
120
|
-
print("Saving field maps and displacement maps to file...")
|
|
121
|
-
fmap_native_path = Path(f"{out_prefix_str}_fieldmaps_native.nii").resolve()
|
|
122
|
-
dmap_path = Path(f"{out_prefix_str}_displacementmaps.nii").resolve()
|
|
123
|
-
fmap_path = Path(f"{out_prefix_str}_fieldmaps.nii").resolve()
|
|
124
|
-
fmaps_native.to_filename(str(fmap_native_path))
|
|
125
|
-
dmaps.to_filename(str(dmap_path))
|
|
126
|
-
fmaps.to_filename(str(fmap_path))
|
|
127
|
-
print("Done.")
|
|
128
|
-
return ComputeFieldmapResult(
|
|
129
|
-
fieldmap_native=fmap_native_path,
|
|
130
|
-
displacement_map=dmap_path,
|
|
131
|
-
fieldmap=fmap_path,
|
|
132
|
-
)
|
|
93
|
+
# space field map. Shared with warpkit.distortion.medic.
|
|
94
|
+
dmaps, fmaps = reconstruct_displacement_and_field_maps(fmaps_native, trt, ped)
|
|
95
|
+
|
|
96
|
+
return write_medic_outputs(out_prefix, fmaps_native, dmaps, fmaps)
|
|
133
97
|
|
|
134
98
|
|
|
135
99
|
def main():
|
|
@@ -144,9 +108,7 @@ def main():
|
|
|
144
108
|
),
|
|
145
109
|
epilog=f"{epilog} 04/24/2026",
|
|
146
110
|
)
|
|
147
|
-
parser
|
|
148
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
149
|
-
)
|
|
111
|
+
add_version_arg(parser)
|
|
150
112
|
parser.add_argument(
|
|
151
113
|
"--magnitude",
|
|
152
114
|
nargs="+",
|
|
@@ -184,20 +146,7 @@ def main():
|
|
|
184
146
|
"--unwrapped order). Required unless --metadata is given."
|
|
185
147
|
),
|
|
186
148
|
)
|
|
187
|
-
parser
|
|
188
|
-
"--total-readout-time",
|
|
189
|
-
type=float,
|
|
190
|
-
help="Total readout time in seconds. Required unless --metadata is given.",
|
|
191
|
-
)
|
|
192
|
-
parser.add_argument(
|
|
193
|
-
"--phase-encoding-direction",
|
|
194
|
-
choices=PE_DIRECTIONS,
|
|
195
|
-
metavar="DIR",
|
|
196
|
-
help=(
|
|
197
|
-
f"Phase encoding direction (one of: {', '.join(PE_DIRECTIONS)}). "
|
|
198
|
-
"Required unless --metadata is given."
|
|
199
|
-
),
|
|
200
|
-
)
|
|
149
|
+
add_trt_pe_args(parser)
|
|
201
150
|
parser.add_argument(
|
|
202
151
|
"--out-prefix",
|
|
203
152
|
required=True,
|
|
@@ -217,9 +166,7 @@ def main():
|
|
|
217
166
|
default=10,
|
|
218
167
|
help="SVD components for global denoising (default: 10).",
|
|
219
168
|
)
|
|
220
|
-
parser
|
|
221
|
-
"-n", "--n-cpus", type=int, default=4, help="Number of CPUs to use."
|
|
222
|
-
)
|
|
169
|
+
add_n_cpus_arg(parser)
|
|
223
170
|
|
|
224
171
|
args = parser.parse_args()
|
|
225
172
|
setup_logging()
|
|
@@ -18,7 +18,6 @@ from typing import cast
|
|
|
18
18
|
|
|
19
19
|
import nibabel as nib
|
|
20
20
|
|
|
21
|
-
from warpkit import __version__
|
|
22
21
|
from warpkit.utilities import (
|
|
23
22
|
AXIS_MAP,
|
|
24
23
|
WARP_ITK_FLIPS,
|
|
@@ -29,6 +28,7 @@ from warpkit.utilities import (
|
|
|
29
28
|
)
|
|
30
29
|
|
|
31
30
|
from . import epilog
|
|
31
|
+
from ._cli import add_version_arg
|
|
32
32
|
from ._warp_io import read_input_frames, write_output
|
|
33
33
|
|
|
34
34
|
|
|
@@ -119,9 +119,7 @@ def main():
|
|
|
119
119
|
),
|
|
120
120
|
epilog=f"{epilog} 04/25/2026",
|
|
121
121
|
)
|
|
122
|
-
parser
|
|
123
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
124
|
-
)
|
|
122
|
+
add_version_arg(parser)
|
|
125
123
|
parser.add_argument(
|
|
126
124
|
"--input",
|
|
127
125
|
nargs="+",
|
|
@@ -17,9 +17,8 @@ from pathlib import Path
|
|
|
17
17
|
|
|
18
18
|
import nibabel as nib
|
|
19
19
|
|
|
20
|
-
from warpkit import __version__
|
|
21
20
|
from warpkit.utilities import (
|
|
22
|
-
|
|
21
|
+
PE_DIRECTIONS,
|
|
23
22
|
WARP_ITK_FLIPS,
|
|
24
23
|
displacement_field_to_map,
|
|
25
24
|
displacement_map_to_field,
|
|
@@ -29,10 +28,9 @@ from warpkit.utilities import (
|
|
|
29
28
|
)
|
|
30
29
|
|
|
31
30
|
from . import epilog
|
|
31
|
+
from ._cli import add_version_arg
|
|
32
32
|
from ._warp_io import read_input_frames, write_output
|
|
33
33
|
|
|
34
|
-
PE_DIRECTIONS = tuple(AXIS_MAP)
|
|
35
|
-
|
|
36
34
|
|
|
37
35
|
@dataclass(frozen=True, slots=True)
|
|
38
36
|
class ConvertFieldmapResult:
|
|
@@ -169,9 +167,7 @@ def main():
|
|
|
169
167
|
),
|
|
170
168
|
epilog=f"{epilog} 04/25/2026",
|
|
171
169
|
)
|
|
172
|
-
parser
|
|
173
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
174
|
-
)
|
|
170
|
+
add_version_arg(parser)
|
|
175
171
|
parser.add_argument(
|
|
176
172
|
"--input",
|
|
177
173
|
nargs="+",
|
|
@@ -20,7 +20,6 @@ from pathlib import Path
|
|
|
20
20
|
import nibabel as nib
|
|
21
21
|
import numpy as np
|
|
22
22
|
|
|
23
|
-
from warpkit import __version__
|
|
24
23
|
from warpkit.utilities import (
|
|
25
24
|
AXIS_MAP,
|
|
26
25
|
WARP_ITK_FLIPS,
|
|
@@ -35,6 +34,7 @@ from warpkit.utilities import (
|
|
|
35
34
|
)
|
|
36
35
|
|
|
37
36
|
from . import epilog
|
|
37
|
+
from ._cli import add_version_arg
|
|
38
38
|
from ._warp_io import read_input_frames, write_output
|
|
39
39
|
|
|
40
40
|
|
|
@@ -227,9 +227,7 @@ def main():
|
|
|
227
227
|
),
|
|
228
228
|
epilog=f"{epilog} 04/25/2026",
|
|
229
229
|
)
|
|
230
|
-
parser
|
|
231
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
232
|
-
)
|
|
230
|
+
add_version_arg(parser)
|
|
233
231
|
parser.add_argument(
|
|
234
232
|
"--input",
|
|
235
233
|
nargs="+",
|
|
@@ -18,29 +18,17 @@ from __future__ import annotations
|
|
|
18
18
|
|
|
19
19
|
import argparse
|
|
20
20
|
from collections.abc import Sequence
|
|
21
|
-
from dataclasses import dataclass
|
|
22
21
|
from os import PathLike
|
|
23
|
-
from pathlib import Path
|
|
24
22
|
|
|
25
23
|
import nibabel as nib
|
|
26
24
|
|
|
27
|
-
from warpkit import __version__
|
|
28
25
|
from warpkit.distortion import medic as _medic_distortion
|
|
29
26
|
from warpkit.utilities import setup_logging
|
|
30
27
|
|
|
31
28
|
from . import epilog
|
|
29
|
+
from ._cli import add_n_cpus_arg, add_trt_pe_args, add_version_arg
|
|
32
30
|
from ._metadata import ensure_images, resolve_acquisition, trim_noise_frames
|
|
33
|
-
|
|
34
|
-
PE_DIRECTIONS = ("i", "j", "k", "i-", "j-", "k-", "x", "y", "z", "x-", "y-", "z-")
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
@dataclass(frozen=True, slots=True)
|
|
38
|
-
class MedicResult:
|
|
39
|
-
"""Absolute paths of the three NIfTIs written by :func:`medic`."""
|
|
40
|
-
|
|
41
|
-
fieldmap_native: Path
|
|
42
|
-
displacement_map: Path
|
|
43
|
-
fieldmap: Path
|
|
31
|
+
from ._outputs import MedicResult, write_medic_outputs
|
|
44
32
|
|
|
45
33
|
|
|
46
34
|
def medic(
|
|
@@ -126,29 +114,14 @@ def medic(
|
|
|
126
114
|
wrap_limit=wrap_limit,
|
|
127
115
|
)
|
|
128
116
|
|
|
129
|
-
|
|
130
|
-
print("Saving field maps and displacement maps to file...")
|
|
131
|
-
fmap_native_path = Path(f"{out_prefix_str}_fieldmaps_native.nii").resolve()
|
|
132
|
-
dmap_path = Path(f"{out_prefix_str}_displacementmaps.nii").resolve()
|
|
133
|
-
fmap_path = Path(f"{out_prefix_str}_fieldmaps.nii").resolve()
|
|
134
|
-
fmaps_native.to_filename(str(fmap_native_path))
|
|
135
|
-
dmaps.to_filename(str(dmap_path))
|
|
136
|
-
fmaps.to_filename(str(fmap_path))
|
|
137
|
-
print("Done.")
|
|
138
|
-
return MedicResult(
|
|
139
|
-
fieldmap_native=fmap_native_path,
|
|
140
|
-
displacement_map=dmap_path,
|
|
141
|
-
fieldmap=fmap_path,
|
|
142
|
-
)
|
|
117
|
+
return write_medic_outputs(out_prefix, fmaps_native, dmaps, fmaps)
|
|
143
118
|
|
|
144
119
|
|
|
145
120
|
def main():
|
|
146
121
|
parser = argparse.ArgumentParser(
|
|
147
122
|
description="Multi-Echo DIstortion Correction", epilog=f"{epilog}"
|
|
148
123
|
)
|
|
149
|
-
parser
|
|
150
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
151
|
-
)
|
|
124
|
+
add_version_arg(parser)
|
|
152
125
|
parser.add_argument("--magnitude", nargs="+", required=True, help="Magnitude data")
|
|
153
126
|
parser.add_argument("--phase", nargs="+", required=True, help="Phase data")
|
|
154
127
|
parser.add_argument(
|
|
@@ -171,20 +144,7 @@ def main():
|
|
|
171
144
|
"Required unless --metadata is given."
|
|
172
145
|
),
|
|
173
146
|
)
|
|
174
|
-
parser
|
|
175
|
-
"--total-readout-time",
|
|
176
|
-
type=float,
|
|
177
|
-
help="Total readout time in seconds. Required unless --metadata is given.",
|
|
178
|
-
)
|
|
179
|
-
parser.add_argument(
|
|
180
|
-
"--phase-encoding-direction",
|
|
181
|
-
choices=PE_DIRECTIONS,
|
|
182
|
-
metavar="DIR",
|
|
183
|
-
help=(
|
|
184
|
-
f"Phase encoding direction (one of: {', '.join(PE_DIRECTIONS)}). "
|
|
185
|
-
"Required unless --metadata is given."
|
|
186
|
-
),
|
|
187
|
-
)
|
|
147
|
+
add_trt_pe_args(parser)
|
|
188
148
|
parser.add_argument(
|
|
189
149
|
"--out-prefix",
|
|
190
150
|
required=True,
|
|
@@ -193,9 +153,7 @@ def main():
|
|
|
193
153
|
parser.add_argument(
|
|
194
154
|
"-f", "--noiseframes", type=int, default=0, help="Number of noise frames"
|
|
195
155
|
)
|
|
196
|
-
parser
|
|
197
|
-
"-n", "--n-cpus", type=int, default=4, help="Number of CPUs to use."
|
|
198
|
-
)
|
|
156
|
+
add_n_cpus_arg(parser)
|
|
199
157
|
parser.add_argument("--debug", action="store_true", help="Debug mode")
|
|
200
158
|
parser.add_argument(
|
|
201
159
|
"--wrap-limit",
|
|
@@ -19,11 +19,11 @@ from pathlib import Path
|
|
|
19
19
|
|
|
20
20
|
import nibabel as nib
|
|
21
21
|
|
|
22
|
-
from warpkit import __version__
|
|
23
22
|
from warpkit.unwrap import unwrap_phases
|
|
24
23
|
from warpkit.utilities import setup_logging
|
|
25
24
|
|
|
26
25
|
from . import epilog
|
|
26
|
+
from ._cli import add_n_cpus_arg, add_version_arg
|
|
27
27
|
from ._metadata import ensure_images, resolve_acquisition, trim_noise_frames
|
|
28
28
|
|
|
29
29
|
|
|
@@ -113,9 +113,7 @@ def main():
|
|
|
113
113
|
),
|
|
114
114
|
epilog=f"{epilog} 04/24/2026",
|
|
115
115
|
)
|
|
116
|
-
parser
|
|
117
|
-
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
118
|
-
)
|
|
116
|
+
add_version_arg(parser)
|
|
119
117
|
parser.add_argument("--magnitude", nargs="+", required=True, help="Magnitude data")
|
|
120
118
|
parser.add_argument("--phase", nargs="+", required=True, help="Phase data")
|
|
121
119
|
parser.add_argument(
|
|
@@ -144,9 +142,7 @@ def main():
|
|
|
144
142
|
parser.add_argument(
|
|
145
143
|
"-f", "--noiseframes", type=int, default=0, help="Number of noise frames"
|
|
146
144
|
)
|
|
147
|
-
parser
|
|
148
|
-
"-n", "--n-cpus", type=int, default=4, help="Number of CPUs to use."
|
|
149
|
-
)
|
|
145
|
+
add_n_cpus_arg(parser)
|
|
150
146
|
parser.add_argument(
|
|
151
147
|
"--debug",
|
|
152
148
|
action="store_true",
|
|
@@ -820,10 +820,12 @@ def unwrap_phases(
|
|
|
820
820
|
# store in the captured unwrapped and new_mask_data arrays
|
|
821
821
|
unwrapped[..., idx], new_masks[..., idx] = result
|
|
822
822
|
|
|
823
|
-
# unwrap the phase of each frame
|
|
823
|
+
# unwrap the phase of each frame. romeo_unwrap* releases the GIL around the
|
|
824
|
+
# C++ kernel, so threads parallelize it as well as processes while avoiding
|
|
825
|
+
# per-frame array pickling and per-worker memory copies.
|
|
824
826
|
run_executor(
|
|
825
827
|
ncpus=n_cpus,
|
|
826
|
-
type="
|
|
828
|
+
type="thread",
|
|
827
829
|
fn=unwrap_phase,
|
|
828
830
|
iterator=phase_iterator(
|
|
829
831
|
phase, mag, tes, mask, frames, automask, automask_dilation
|
|
@@ -44,6 +44,10 @@ AXIS_MAP = {
|
|
|
44
44
|
"k-": 2,
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
# accepted phase-encoding direction strings (the keys of AXIS_MAP), shared by
|
|
48
|
+
# the field-map conversions and the CLI ``choices=`` lists.
|
|
49
|
+
PE_DIRECTIONS = tuple(AXIS_MAP)
|
|
50
|
+
|
|
47
51
|
|
|
48
52
|
# warp_itk_flips
|
|
49
53
|
WARP_ITK_FLIPS = {
|
|
@@ -242,6 +246,23 @@ def create_brain_mask(
|
|
|
242
246
|
return mask_data.astype(np.bool_)
|
|
243
247
|
|
|
244
248
|
|
|
249
|
+
def _signed_pe_voxel_size(img: nib.Nifti1Image, phase_encoding_direction: str) -> float:
|
|
250
|
+
"""Voxel size (mm) along the phase-encoding axis, signed for the EPI
|
|
251
|
+
displacement <-> field-map conversions.
|
|
252
|
+
|
|
253
|
+
The sign carries two conventions: a trailing ``-`` in the phase-encoding
|
|
254
|
+
direction flips the displacement direction, and the x/y axes are flipped
|
|
255
|
+
for the ITK (LPS) frame relative to NIfTI RAS.
|
|
256
|
+
"""
|
|
257
|
+
axis_code = AXIS_MAP[phase_encoding_direction]
|
|
258
|
+
voxel_size = img.header.get_zooms()[axis_code] # type: ignore
|
|
259
|
+
if "-" in phase_encoding_direction:
|
|
260
|
+
voxel_size *= -1
|
|
261
|
+
if axis_code == 0 or axis_code == 1:
|
|
262
|
+
voxel_size *= -1
|
|
263
|
+
return voxel_size
|
|
264
|
+
|
|
265
|
+
|
|
245
266
|
def field_maps_to_displacement_maps(
|
|
246
267
|
field_maps: nib.Nifti1Image,
|
|
247
268
|
total_readout_time: float,
|
|
@@ -263,20 +284,7 @@ def field_maps_to_displacement_maps(
|
|
|
263
284
|
nib.Nifti1Image
|
|
264
285
|
Displacment maps in mm
|
|
265
286
|
"""
|
|
266
|
-
|
|
267
|
-
axis_code = AXIS_MAP[phase_encoding_direction]
|
|
268
|
-
|
|
269
|
-
# get voxel size
|
|
270
|
-
voxel_size = field_maps.header.get_zooms()[axis_code] # type: ignore
|
|
271
|
-
|
|
272
|
-
# if there is a negative sign in the phase encoding direction
|
|
273
|
-
# we need to flip the direction of the displacment map
|
|
274
|
-
if "-" in phase_encoding_direction:
|
|
275
|
-
voxel_size *= -1
|
|
276
|
-
|
|
277
|
-
# for itk form, we need to flip x/y axis
|
|
278
|
-
if axis_code == 0 or axis_code == 1:
|
|
279
|
-
voxel_size *= -1
|
|
287
|
+
voxel_size = _signed_pe_voxel_size(field_maps, phase_encoding_direction)
|
|
280
288
|
|
|
281
289
|
# convert field maps to displacement maps
|
|
282
290
|
data = field_maps.get_fdata()
|
|
@@ -311,20 +319,7 @@ def displacement_maps_to_field_maps(
|
|
|
311
319
|
nib.Nifti1Image
|
|
312
320
|
Field maps in Hz
|
|
313
321
|
"""
|
|
314
|
-
|
|
315
|
-
axis_code = AXIS_MAP[phase_encoding_direction]
|
|
316
|
-
|
|
317
|
-
# get voxel size
|
|
318
|
-
voxel_size = displacement_maps.header.get_zooms()[axis_code] # type: ignore
|
|
319
|
-
|
|
320
|
-
# if there is a negative sign in the phase encoding direction
|
|
321
|
-
# we need to flip the direction of the displacment map
|
|
322
|
-
if "-" in phase_encoding_direction:
|
|
323
|
-
voxel_size *= -1
|
|
324
|
-
|
|
325
|
-
# for itk form, we need to flip x/y axis
|
|
326
|
-
if axis_code == 0 or axis_code == 1:
|
|
327
|
-
voxel_size *= -1
|
|
322
|
+
voxel_size = _signed_pe_voxel_size(displacement_maps, phase_encoding_direction)
|
|
328
323
|
|
|
329
324
|
# convert displacement maps to field maps
|
|
330
325
|
data = displacement_maps.get_fdata()
|
|
@@ -335,6 +330,81 @@ def displacement_maps_to_field_maps(
|
|
|
335
330
|
return nib.Nifti1Image(new_data, displacement_maps.affine, displacement_maps.header)
|
|
336
331
|
|
|
337
332
|
|
|
333
|
+
def reconstruct_displacement_and_field_maps(
|
|
334
|
+
field_maps_native: nib.Nifti1Image,
|
|
335
|
+
total_readout_time: float,
|
|
336
|
+
phase_encoding_direction: str,
|
|
337
|
+
) -> tuple[nib.Nifti1Image, nib.Nifti1Image]:
|
|
338
|
+
"""Reconstruct correction maps from a native-space field map.
|
|
339
|
+
|
|
340
|
+
Converts native-space field maps (Hz) to displacement maps in distorted
|
|
341
|
+
space, inverts them to get distorted -> undistorted correction maps, and
|
|
342
|
+
re-derives an undistorted-space field map. This is the shared reconstruction
|
|
343
|
+
tail used by both :func:`warpkit.distortion.medic` and
|
|
344
|
+
:func:`warpkit.scripts.compute_fieldmap.compute_fieldmap`.
|
|
345
|
+
|
|
346
|
+
Parameters
|
|
347
|
+
----------
|
|
348
|
+
field_maps_native : nib.Nifti1Image
|
|
349
|
+
Field maps in Hz (distorted/native space).
|
|
350
|
+
total_readout_time : float
|
|
351
|
+
Total readout time (in seconds).
|
|
352
|
+
phase_encoding_direction : str
|
|
353
|
+
Phase encoding direction.
|
|
354
|
+
|
|
355
|
+
Returns
|
|
356
|
+
-------
|
|
357
|
+
nib.Nifti1Image
|
|
358
|
+
Displacement maps (distorted -> undistorted) in mm.
|
|
359
|
+
nib.Nifti1Image
|
|
360
|
+
Field maps in Hz (undistorted space).
|
|
361
|
+
"""
|
|
362
|
+
# Require a 4D (x, y, z, frame) series: invert_displacement_maps() iterates
|
|
363
|
+
# over the trailing axis as frames, so a 3D input would be silently treated
|
|
364
|
+
# as a stack of 2D slices and fed to the C++ inverter as such.
|
|
365
|
+
if field_maps_native.ndim != 4:
|
|
366
|
+
raise ValueError(
|
|
367
|
+
"field_maps_native must be 4D (x, y, z, frames); got "
|
|
368
|
+
f"{field_maps_native.ndim}D with shape {field_maps_native.shape}."
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
# convert to displacement maps (these are in distorted space)
|
|
372
|
+
inv_displacement_maps = field_maps_to_displacement_maps(
|
|
373
|
+
field_maps_native, total_readout_time, phase_encoding_direction
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
# invert displacement maps (these are in undistorted space). The maps are
|
|
377
|
+
# composed along the phase-encode voxel axis, so invert in the voxel grid
|
|
378
|
+
# frame (ignore_rotation=True) rather than the affine's physical frame —
|
|
379
|
+
# otherwise oblique acquisitions pick up an obliquity-dependent error.
|
|
380
|
+
displacement_maps = invert_displacement_maps(
|
|
381
|
+
inv_displacement_maps, phase_encoding_direction, ignore_rotation=True
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
# convert correction maps back to undistorted-space field map. No flip_sign
|
|
385
|
+
# here: the correlation check below sets the sign relative to the native
|
|
386
|
+
# field map, which makes an up-front flip redundant.
|
|
387
|
+
field_maps = displacement_maps_to_field_maps(
|
|
388
|
+
displacement_maps, total_readout_time, phase_encoding_direction
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
# flip sign of the field maps if needed, determined by the sign of the
|
|
392
|
+
# correlation between the native and undistorted field maps.
|
|
393
|
+
if (
|
|
394
|
+
np.corrcoef(
|
|
395
|
+
field_maps.dataobj[..., 0].ravel(),
|
|
396
|
+
field_maps_native.dataobj[..., 0].ravel(),
|
|
397
|
+
)[0, 1]
|
|
398
|
+
< 0
|
|
399
|
+
):
|
|
400
|
+
field_map_data = field_maps.get_fdata() * -1
|
|
401
|
+
field_maps = nib.Nifti1Image(
|
|
402
|
+
field_map_data, field_maps.affine, field_maps.header
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
return displacement_maps, field_maps
|
|
406
|
+
|
|
407
|
+
|
|
338
408
|
def displacement_map_to_field(
|
|
339
409
|
displacement_map: nib.Nifti1Image,
|
|
340
410
|
axis: str = "y",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{warpkit-1.3.0 → warpkit-1.4.0}/include/itk/itkModifiedInvertDisplacementFieldImageFilter.hxx
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|