-
Notifications
You must be signed in to change notification settings - Fork 262
ENH: Add dtype argument to Cifti2Image #1111
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
82b0b05
FIX: Constrain permissible dtypes in CIFTI-2
effigies 696840f
ENH: Add dtype argument to Cifti2Image
effigies 4fa74c7
TEST: Add DtypeOverrideMixin to test __init__ and to_filename dtype args
effigies 7933fae
FIX: Clean up header values before raising exception in AnalyzeImage.…
effigies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,8 @@ | |
from .test_parrec import EXAMPLE_IMAGES as PARREC_EXAMPLE_IMAGES | ||
from .test_brikhead import EXAMPLE_IMAGES as AFNI_EXAMPLE_IMAGES | ||
|
||
from nibabel.arraywriters import WriterError | ||
|
||
|
||
def maybe_deprecated(meth_name): | ||
return pytest.deprecated_call() if meth_name == 'get_data' else nullcontext() | ||
|
@@ -181,7 +183,7 @@ def validate_get_data_deprecated(self, imaker, params): | |
assert_array_equal(np.asanyarray(img.dataobj), data) | ||
|
||
|
||
class GetSetDtypeMixin(object): | ||
class GetSetDtypeMixin: | ||
""" Adds dtype tests | ||
|
||
Add this one if your image has ``get_data_dtype`` and ``set_data_dtype``. | ||
|
@@ -666,6 +668,46 @@ def prox_imaker(): | |
yield make_prox_imaker(arr.copy(), aff, hdr), params | ||
|
||
|
||
class DtypeOverrideMixin(GetSetDtypeMixin): | ||
""" Test images that can accept ``dtype`` arguments to ``__init__`` and | ||
``to_file_map`` | ||
""" | ||
|
||
def validate_init_dtype_override(self, imaker, params): | ||
img = imaker() | ||
klass = img.__class__ | ||
for dtype in self.storable_dtypes: | ||
if hasattr(img, 'affine'): | ||
new_img = klass(img.dataobj, img.affine, header=img.header, dtype=dtype) | ||
else: # XXX This is for CIFTI-2, these validators might need refactoring | ||
new_img = klass(img.dataobj, header=img.header, dtype=dtype) | ||
assert new_img.get_data_dtype() == dtype | ||
|
||
if self.has_scaling and self.can_save: | ||
with np.errstate(invalid='ignore'): | ||
rt_img = bytesio_round_trip(new_img) | ||
assert rt_img.get_data_dtype() == dtype | ||
|
||
def validate_to_file_dtype_override(self, imaker, params): | ||
if not self.can_save: | ||
raise unittest.SkipTest | ||
img = imaker() | ||
orig_dtype = img.get_data_dtype() | ||
fname = 'image' + self.standard_extension | ||
with InTemporaryDirectory(): | ||
for dtype in self.storable_dtypes: | ||
try: | ||
img.to_filename(fname, dtype=dtype) | ||
except WriterError: | ||
# It's possible to try to save to a dtype that requires | ||
# scaling, and images without scale factors will fail. | ||
# We're not testing that here. | ||
continue | ||
rt_img = img.__class__.from_filename(fname) | ||
assert rt_img.get_data_dtype() == dtype | ||
assert img.get_data_dtype() == orig_dtype | ||
|
||
|
||
class ImageHeaderAPI(MakeImageAPI): | ||
""" When ``self.image_maker`` is an image class, make header from class | ||
""" | ||
|
@@ -674,7 +716,12 @@ def header_maker(self): | |
return self.image_maker.header_class() | ||
|
||
|
||
class TestAnalyzeAPI(ImageHeaderAPI): | ||
class TestSpatialImageAPI(ImageHeaderAPI): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This subclass/superclass swap occurs because not all spatial images have dtype overriding. |
||
klass = image_maker = SpatialImage | ||
can_save = False | ||
|
||
|
||
class TestAnalyzeAPI(TestSpatialImageAPI, DtypeOverrideMixin): | ||
""" General image validation API instantiated for Analyze images | ||
""" | ||
klass = image_maker = AnalyzeImage | ||
|
@@ -685,11 +732,6 @@ class TestAnalyzeAPI(ImageHeaderAPI): | |
storable_dtypes = (np.uint8, np.int16, np.int32, np.float32, np.float64) | ||
|
||
|
||
class TestSpatialImageAPI(TestAnalyzeAPI): | ||
klass = image_maker = SpatialImage | ||
can_save = False | ||
|
||
|
||
class TestSpm99AnalyzeAPI(TestAnalyzeAPI): | ||
# SPM-type analyze need scipy for mat file IO | ||
klass = image_maker = Spm99AnalyzeImage | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly there's a better name.
_Cifti2AsNiftiHeader
exists to do some validation during parsing, but isn't the type ofnifti_header
.nibabel/nibabel/cifti2/parse_cifti2.py
Lines 78 to 119 in 7933fae