Skip to content
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

FIX: Ensure data type of masked image matches T1.mgz #430

Merged
merged 2 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion niworkflows/interfaces/freesurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def inject_skullstripped(subjects_dir, subject_id, skullstripped):
mask = nb.load(skullstripped)
bmask = new_img_like(mask, np.asanyarray(mask.dataobj) > 0)
resampled_mask = resample_to_img(bmask, img, 'nearest')
masked_image = new_img_like(img, img.get_fdata() * resampled_mask.dataobj)
masked_image = new_img_like(img, np.asanyarray(img.dataobj) * resampled_mask.dataobj)
masked_image.to_filename(bm_auto)

if not op.exists(bm):
Expand Down
30 changes: 30 additions & 0 deletions niworkflows/interfaces/tests/test_freesurfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pathlib import Path
import numpy as np
import nibabel as nb
from ..freesurfer import FSInjectBrainExtracted


def test_inject_skullstrip(tmp_path):
t1_mgz = tmp_path / "sub-01" / "mri" / "T1.mgz"
t1_mgz.parent.mkdir(parents=True)
# T1.mgz images are uint8
nb.MGHImage(np.ones((5, 5, 5), dtype=np.uint8), np.eye(4)).to_filename(str(t1_mgz))

mask_nii = tmp_path / "mask.nii.gz"
# Masks may be in a different space (and need resampling), but should be boolean,
# or uint8 in NIfTI
nb.Nifti1Image(np.ones((6, 6, 6), dtype=np.uint8), np.eye(4)).to_filename(
str(mask_nii)
)

FSInjectBrainExtracted(
subjects_dir=str(tmp_path), subject_id="sub-01", in_brain=str(mask_nii)
).run()

assert Path.exists(tmp_path / "sub-01" / "mri" / "brainmask.auto.mgz")
assert Path.exists(tmp_path / "sub-01" / "mri" / "brainmask.mgz")

# Run a second time to hit "already exists" condition
FSInjectBrainExtracted(
subjects_dir=str(tmp_path), subject_id="sub-01", in_brain=str(mask_nii)
).run()