Skip to content

Commit

Permalink
[FIX] Force maskers to be array images instead of proxy images (#588)
Browse files Browse the repository at this point in the history
* Coerce proxy images to array images.

* Check that maskers are not proxies.
  • Loading branch information
tsalo authored Oct 26, 2021
1 parent 48fbae0 commit fd46f9c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions nimare/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def test_dataset_smoke():
dset = dataset.Dataset(db_file)
dset.update_path(get_test_data_path())
assert isinstance(dset, nimare.dataset.Dataset)
# Test that Dataset.masker is portable
assert not nib.is_proxy(dset.masker.mask_img_.dataobj)

methods = [dset.get_images, dset.get_labels, dset.get_metadata, dset.get_texts]
for method in methods:
assert isinstance(method(), list)
Expand Down
2 changes: 2 additions & 0 deletions nimare/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_get_template():
"""Test nimare.utils.get_template."""
img = utils.get_template(space="mni152_1mm", mask=None)
assert isinstance(img, nib.Nifti1Image)
assert not nib.is_proxy(img.dataobj)
img = utils.get_template(space="mni152_1mm", mask="brain")
assert isinstance(img, nib.Nifti1Image)
img = utils.get_template(space="mni152_1mm", mask="gm")
Expand All @@ -67,6 +68,7 @@ def test_get_template():
assert isinstance(img, nib.Nifti1Image)
img = utils.get_template(space="mni152_2mm", mask="gm")
assert isinstance(img, nib.Nifti1Image)
assert not nib.is_proxy(img.dataobj)


def test_get_resource_path():
Expand Down
6 changes: 6 additions & 0 deletions nimare/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ def get_template(space="mni152_2mm", mask=None):
img = nib.load(op.join(get_resource_path(), "templates/MNI152_2x2x2_brainmask.nii"))
else:
raise ValueError(f"Space {space} not supported")

# Coerce to array-image
img = nib.Nifti1Image(img.get_fdata(), affine=img.affine, header=img.header)
return img


Expand All @@ -309,6 +312,9 @@ def get_masker(mask):
mask = nib.load(mask)

if isinstance(mask, nib.nifti1.Nifti1Image):
# Coerce to array-image
mask = nib.Nifti1Image(mask.get_fdata(), affine=mask.affine, header=mask.header)

mask = NiftiMasker(mask)

if not (hasattr(mask, "transform") and hasattr(mask, "inverse_transform")):
Expand Down

0 comments on commit fd46f9c

Please sign in to comment.