Skip to content

Commit 045f9ff

Browse files
mwaskomsatra
authored andcommitted
Added interface for mri_mask
1 parent 46f782c commit 045f9ff

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

nipype/interfaces/freesurfer/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
from nipype.interfaces.freesurfer.model import (MRISPreproc, GLMFit,
1515
OneSampleTTest, Binarize,
1616
Concatenate, SegStats, Label2Vol)
17-
from nipype.interfaces.freesurfer.utils import (SampleToSurface, SurfaceSmooth, SurfaceTransform, SurfaceSnapshots)
17+
from nipype.interfaces.freesurfer.utils import (SampleToSurface, SurfaceSmooth, SurfaceTransform, SurfaceSnapshots,
18+
ApplyMask)

nipype/interfaces/freesurfer/tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,36 @@ def test_surfxfm():
161161
# Clean up
162162
clean_directory(cwd, oldwd)
163163

164+
@skipif(no_freesurfer)
165+
def test_applymask():
166+
masker = fs.ApplyMask()
167+
168+
filelist, testdir, origdir = create_files_in_directory()
169+
170+
# Test underlying command
171+
yield assert_equal, masker.cmd, "mri_mask"
172+
173+
# Test exception with mandatory args absent
174+
yield assert_raises, ValueError, masker.run
175+
for input in ["in_file", "mask_file"]:
176+
indict = {input:filelist[0]}
177+
willbreak = fs.ApplyMask(**indict)
178+
yield assert_raises, ValueError, willbreak.run
179+
180+
# Now test a basic command line
181+
masker.inputs.in_file = filelist[0]
182+
masker.inputs.mask_file = filelist[1]
183+
outfile = os.path.join(testdir, "a_masked.nii")
184+
yield assert_equal, masker.cmdline, "mri_mask a.nii b.nii %s"%outfile
185+
# Now test that optional inputs get formatted properly
186+
masker.inputs.mask_thresh = 2
187+
yield assert_equal, masker.cmdline, "mri_mask -T 2.0000 a.nii b.nii %s"%outfile
188+
masker.inputs.use_abs = True
189+
yield assert_equal, masker.cmdline, "mri_mask -T 2.0000 -abs a.nii b.nii %s"%outfile
190+
191+
# Now clean up
192+
clean_directory(testdir, origdir)
193+
164194
@skipif(no_freesurfer)
165195
def test_surfshots():
166196

nipype/interfaces/freesurfer/utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,53 @@ def _gen_filename(self, name):
357357
return None
358358

359359

360+
class ApplyMaskInputSpec(FSTraitedSpec):
361+
362+
in_file = File(exists=True,mandatory=True,position=-3,argstr="%s",
363+
desc="input image (will be masked)")
364+
mask_file = File(exists=True,mandatory=True,position=-2,argstr="%s",
365+
desc="image defining mask space")
366+
out_file = File(genfile=True,position=-1,argstr="%s",
367+
desc="final image to write")
368+
xfm_file = File(exists=True,argstr="-xform %s",
369+
desc="LTA-format transformation matrix to align mask with input")
370+
invert_xfm = traits.Bool(argstr="-invert",desc="invert transformation")
371+
xfm_source = File(exists=True,argstr="-lta_src %s",desc="image defining transform source space")
372+
xfm_target = File(exists=True,argstr="-lta_dst %s",desc="image defining transform target space")
373+
use_abs = traits.Bool(argstr="-abs",desc="take absolute value of mask before applying")
374+
mask_thresh = traits.Float(argstr="-T %.4f",desc="threshold mask before applying")
375+
376+
class ApplyMaskOutputSpec(TraitedSpec):
377+
378+
out_file = File(exists=True,desc="masked image")
379+
380+
class ApplyMask(FSCommand):
381+
"""Use Freesurfer's mri_mask to apply a mask to an image.
382+
383+
The mask file need not be binarized; it can be thresholded above a given
384+
value before application. It can also optionally be transformed into input
385+
space with an LTA matrix.
386+
387+
"""
388+
_cmd = "mri_mask"
389+
input_spec = ApplyMaskInputSpec
390+
output_spec = ApplyMaskOutputSpec
391+
392+
def _list_outputs(self):
393+
outputs = self._outputs().get()
394+
outputs["out_file"] = self.inputs.out_file
395+
if not isdefined(outputs["out_file"]):
396+
outputs["out_file"] = fname_presuffix(self.inputs.in_file,
397+
suffix="_masked",
398+
newpath=os.getcwd(),
399+
use_ext=True)
400+
return outputs
401+
402+
def _gen_filename(self, name):
403+
if name == "out_file":
404+
return self._list_outputs()[name]
405+
return None
406+
360407
class SurfaceSnapshotsInputSpec(FSTraitedSpec):
361408

362409
subject_id = traits.String(position=1,argstr="%s",mandatory=True,

0 commit comments

Comments
 (0)