8000 ENH: Add get/set_zooms(units='norm') to manipulate zooms in mm/s units by effigies · Pull Request #567 · nipy/nibabel · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add get/set_zooms(units='norm') to manipulate zooms in mm/s units #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3f3b450
ENH: Add get_norm_zooms for zooms in mm/s units
effigies Oct 24, 2017
d26acfb
ENH: Add get_norm_zooms for MGHHeader
effigies Dec 18, 2017
de9323c
ENH: Add set_norm_zooms
effigies Dec 21, 2017
5c30366
FIX: Various unit issues
effigies Dec 21, 2017
4b86d3e
TEST: Test get/set_norm_zooms
effigies Dec 21, 2017
eb1d8ff
TEST: Fix warnings
effigies Dec 21, 2017
6ba8b66
RF: get_norm_zooms -> get_zooms(units="canonical")
effigies Jan 12, 2018
1357a3b
TEST: Update get_zooms tests
effigies Jan 13, 2018
096da88
FIX: Set default t_code even if no t_zoom
effigies Jan 13, 2018
798fc4e
TEST: Add units specification to tests
effigies Feb 19, 2018
f31aba5
TEST: Add setup/teardown to try to catch warnings properly [WIP]
effigies Feb 19, 2018
27234c3
TEST: Try using clear_and_catch_warnings
effigies Feb 19, 2018
8899bc7
RF: Add units parameter to set_zooms
effigies Mar 12, 2018
da54665
TEST: More complete zoom testing, revert unnecessary change
effigies Mar 12, 2018
1620f31
TEST: Improve warning filters, check set_zooms behavior more thoroughly
effigies Mar 12, 2018
5d96f5c
TEST: Explicitly test non-temporal t_units during set_zooms(units="no…
effigies Mar 12, 2018
233f7f0
TEST: Filter warnings for unmodified superclass tests only
effigies Mar 12, 2018
4babf08
ENH: Add units/raise_unknown to MINC/ECAT get_zooms
effigies Mar 12, 2018
6cd83f7
ENH: Validate units parameter in all get/set_zooms
effigies Mar 13, 2018
94cbd0d
FIX: Correct and simplify NIFTI logic
effigies Mar 14, 2018
cfc5abe
TEST: Clear nifti1 module warnings
effigies Mar 20, 2018
3562921
TEST: Check edge cases
effigies Mar 20, 2018
d9199ca
TEST: Add unit to bad set_zoom call
effigies Mar 20, 2018
98e43a0
TEST: Check get_zooms units arg in MINC/ECAT
effigies Mar 20, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TEST: Update get_zooms tests
  • Loading branch information
effigies committed Dec 26, 2022
commit 1357a3b8bd231e81f0e9a27ba466e85f53e742e4
39 changes: 24 additions & 15 deletions nibabel/freesurfer/tests/test_mghformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,49 +351,58 @@ def check_dtypes(self, expected, actual):
# MGH requires the actual to be a big endian version of expected
assert expected.newbyteorder('>') == actual

def test_norm_zooms_edge_cases(self):
def test_zooms_edge_cases(self):
img_klass = self.image_class
aff = np.eye(4)
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
img = img_klass(arr, aff)

assert_array_almost_equal(img.header.get_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(1, 1, 1, 0))
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 0))

img.header.set_zooms((1, 1, 1, 2000))
assert_array_almost_equal(img.header.get_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(1, 1, 1, 2000))
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 2))
assert_array_almost_equal(img.header.get_zooms(), (1, 1, 1, 2))

img.header.set_norm_zooms((2, 2, 2, 3))
assert_array_almost_equal(img.header.get_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(2, 2, 2, 3000))
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(2, 2, 2, 3))
assert_array_almost_equal(img.header.get_zooms(), (2, 2, 2, 3))

# It's legal to set zooms for spatial dimensions only
img.header.set_norm_zooms((3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(3, 3, 3, 3000))
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(3, 3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(), (3, 3, 3, 3))

arr = np.arange(24, dtype=np.int16).reshape((2, 3, 4))
img = img_klass(arr, aff)

assert_array_almost_equal(img.header.get_zooms(), (1, 1, 1))
assert_array_almost_equal(img.header.get_norm_zooms(), (1, 1, 1))
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(1, 1, 1))
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1))

img.header.set_zooms((2, 2, 2))
assert_array_almost_equal(img.header.get_zooms(), (2, 2, 2))
assert_array_almost_equal(img.header.get_norm_zooms(), (2, 2, 2))
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(2, 2, 2))
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(2, 2, 2))

img.header.set_norm_zooms((3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(), (3, 3, 3))
ass 8000 ert_array_almost_equal(img.header.get_norm_zooms(), (3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(3, 3, 3))
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(3, 3, 3))

# Cannot set TR as zoom for 3D image
assert_raises(HeaderDataError, img.header.set_zooms, (4, 4, 4, 5))
Expand Down
32 changes: 19 additions & 13 deletions nibabel/tests/test_nifti1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ def test_static_dtype_aliases(self):
img_rt = bytesio_round_trip(img)
assert img_rt.get_data_dtype() == effective_dt

def test_norm_zooms_edge_cases(self):
def test_zooms_edge_cases(self):
img_klass = self.image_class
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
aff = np.eye(4)
Expand All @@ -1186,47 +1186,53 @@ def test_norm_zooms_edge_cases(self):
# Unknown units = 2 warnings
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 1))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me why (in Python 2 only) this is failing to raise warnings.

assert_equal(len(warns), 2)
assert_raises(ValueError, img.header.get_norm_zooms, True)
assert_raises(ValueError, img.header.get_zooms,
units='canonical', raise_unknown=True)

img.header.set_xyzt_units(xyz='meter')
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1000, 1000, 1000, 1))
assert_equal(len(warns), 1)
assert_raises(ValueError, img.header.get_norm_zooms, True)
assert_raises(ValueError, img.header.get_zooms,
units='canonical', raise_unknown=True)

img.header.set_xyzt_units(xyz='mm', t='sec')
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 1))
img.header.set_xyzt_units(xyz='micron', t='sec')
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(0.001, 0.001, 0.001, 1))

img.header.set_xyzt_units(t='sec')
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter('always')
assert_array_equal(img.header.get_norm_zooms(), (1, 1, 1, 1))
assert_array_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 1))
assert_equal(len(warns), 1)
assert_raises(ValueError, img.header.get_norm_zooms, True)
assert_raises(ValueError, img.header.get_zooms,
units='canonical', raise_unknown=True)

img.header.set_xyzt_units(xyz='mm', t='msec')
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 0.001))

img.header.set_xyzt_units(xyz='mm', t='usec')
assert_array_almost_equal(img.header.get_norm_zooms(),
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(1, 1, 1, 0.000001))

# Verify `set_norm_zooms` resets units
img.header.set_xyzt_units(xyz='meter', t='usec')
assert_equal(img.header.get_xyzt_units(), ('meter', 'usec'))
img.header.set_norm_zooms((2, 2, 2, 2.5))
assert_array_almost_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))
assert_array_almost_equal(img.header.get_zooms(), (2, 2, 2, 2.5))
assert_array_almost_equal(img.header.get_zooms(units='canonical'),
(2, 2, 2, 2.5))
assert_array_almost_equal(img.header.get_zooms(units='raw'),
(2, 2, 2, 2.5))
assert_equal(img.header.get_xyzt_units(), ('mm', 'sec'))


Expand Down
14 changes: 7 additions & 7 deletions nibabel/tests/test_spatialimages.py
628C
Original file line number Diff line number Diff line change
Expand Up @@ -528,27 +528,27 @@ def test_slicer(self):
assert (sliced_data == img.get_data()[sliceobj]).all()
assert (sliced_data == img.get_fdata()[sliceobj]).all()

def test_norm_zooms(self):
def test_zooms(self):
''' Should be true for all images '''
img_klass = self.image_class
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
aff = np.eye(4)
img = img_klass(arr, aff)
img.header.set_norm_zooms((2, 2, 2, 2.5))
assert_array_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(units='canonical'), (2, 2, 2, 2.5))

def test_norm_zooms_edge_cases(self):
def test_zooms_edge_cases(self):
''' Override for classes where *_norm_zooms != *_zooms '''
img_klass = self.image_class
arr = np.arange(120, dtype=np.int16).reshape((2, 3, 4, 5))
aff = np.eye(4)
img = img_klass(arr, aff)
img.header.set_zooms((2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(units='raw'), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(units='canonical'), (2, 2, 2, 2.5))
img.header.set_norm_zooms((2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_norm_zooms(), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(units='raw'), (2, 2, 2, 2.5))
assert_array_equal(img.header.get_zooms(units='canonical'), (2, 2, 2, 2.5))


class MmapImageMixin:
Expand Down
0