Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #693.
Problem
ants.to_nibabelandants.from_nibabelwere removed in 0.5.3 (PR #637 renamedants/utils/convert_nibabel.pyand dropped the short names). The longerants.to_nibabel_nifti/ants.from_nibabel_niftinames added later (PRs #877/#878/#892) do the conversion, but the names requested in #693 — and used by downstream code — are still missing:What this PR does
ants/utils/convert_nibabel.pywithto_nibabel/from_nibabelas thin wrappers overto_nibabel_nifti/from_nibabel_nifti, exported fromants.utilsso they are available asants.to_nibabel/ants.from_nibabel. The*_niftinames are unchanged and remain the canonical implementations.get_fdata()(the oldnifti_to_antsused the long-deprecatedget_data(), which is what crashed downstream in to_nibabel and from_nibabel missing from version 0.5.3 api #693).to_nibabel_nifti.from_nibabeldoes not import nibabel at all — it works on any object exposing the nibabel image interface (.affine,.header,.get_fdata()).to_nibabelwithout nibabel installed now raises anImportErrortelling the user topip install nibabelinstead of failing with a bare import traceback.Affine conventions
The part that historically went wrong (#52, #64), documented in the new module docstring and pinned down by tests:
diag(-1, -1, 1, 1)) in both directions.from_nibabelreadsnib_image.affine, which nibabel builds as the sform whensform_code > 0, otherwise the qform — so when both are present the sform wins, matching nibabel's own rule.max_angle_deviationdegrees aValueErroris raised. A rigid sform converts exactly.to_nibabelwrites the geometry as the NIfTI sform (sform_code2, 'aligned') and nibabel derives a matching qform from it, so sform and qform agree on the output. Units are set to mm / sec.One gotcha found while writing the tests: setting a transform on a nibabel image's header alone does not update the image's cached
.affine— the image-levelset_sform/set_qformmethods must be used. The sform/qform tests use the image-level setters.Tests
New tests in
tests/test_utils.py(TestModule_convert_nibabel,TestModule_convert_nibabel_no_nibabel), all run against installed antspyx 0.6.3 with the patched pure-Python files overlaid (no C++ build):test_short_names_exist—ants.to_nibabel/ants.from_nibabelexist and live inants.utils.convert_nibabeltest_roundtrip_3d— 3D oblique image round-trips (data + affine, geometry stable across round-trips)test_affine_ras_lps_convention— origin x/y negated, z unchanged; direction matches the LPS-converted affinetest_sform_preferred_over_qform— sform and qform disagree, sform winstest_qform_used_when_sform_absent—sform_code == 0, qform is usedtest_to_nibabel_writes_sform— output hassform_code == 2('aligned'), sform == affine, qform == affinetest_orientation_roundtrip— permuted/flipped (det +1) orientation round-trips exactlytest_timeseries_roundtrip— 4D image,toffsetpreservedtest_from_nibabel_needs_no_nibabel_import— conversion works with nibabel hidden fromsys.modules, using a stub image objecttest_to_nibabel_without_nibabel_raises_helpful_error—ImportErrormentioningpip install nibabelTest results
10 new tests pass; the 4 pre-existing
TestModule_nibabel_to_antstests also pass (no regression from the lazy-import change). CPU-only; no GPU tests involved. Ruff: the new module is clean (ruff check+ruff format --checkpass);tests/test_utils.pykeeps its pre-existing 57 ruff findings, unchanged by this PR, and the touchednibabel_nifti_to_ants.pygoes from 4 pre-existing findings to 3 (the dropped unusedimport nibabel as nib).Signed-off-by: Ilyass Emssaad emssaadelias@gmail.com