Skip to content

Commit

Permalink
Merge pull request #389 from dmwelch/enh/mri_ms_LDA
Browse files Browse the repository at this point in the history
ENH: Added freesurfer wrapping of mri_ms_LDA
  • Loading branch information
satra committed May 25, 2012
2 parents 3f202ec + fa2dacf commit 11010d6
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 56 deletions.
2 changes: 1 addition & 1 deletion nipype/interfaces/freesurfer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
DICOMConvert, RobustRegister, FitMSParams,
SynthesizeFLASH)
from .model import (MRISPreproc, GLMFit, OneSampleTTest, Binarize, Concatenate,
SegStats, Label2Vol)
SegStats, Label2Vol, MS_LDA)
from .utils import (SampleToSurface, SurfaceSmooth, SurfaceTransform,
SurfaceSnapshots,ApplyMask, MRIsConvert, MRITessellate,
MRIMarchingCubes, SmoothTessellation, MakeAverageSubject)
77 changes: 76 additions & 1 deletion nipype/interfaces/freesurfer/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""The freesurfer module provides basic functions for interfacing with freesurfer tools.
"""The freesurfer module provides basic functions for interfacing with
freesurfer tools.
Change directory to provide relative paths for doctests
>>> import os
Expand Down Expand Up @@ -768,3 +769,77 @@ def _gen_filename(self, name):
if name == 'vol_label_file':
return self._list_outputs()[name]
return None


class MS_LDAInputSpec(FSTraitedSpec):
lda_labels = traits.List(traits.Int(), argstr='-lda %s', mandatory=True,
minlen=2, maxlen=2, sep=' ', position=1,
desc='pair of class labels to optimize')
weight_file = traits.File(argstr='-weight %s', mandatory=True,
position=2,
desc='filename for the LDA weights (input or output)')
output_synth = traits.File(exists=False, argstr='-synth %s',
mandatory=True, position=3,
desc='filename for the synthesized output volume')
label_file = traits.File(exists=True, argstr='-label %s', position=4,
desc='filename of the label volume')
mask_file = traits.File(exists=True, argstr='-mask %s', position=5,
desc='filename of the brain mask volume')
shift = traits.Int(argstr='-shift %d', position=6,
desc='shift all values equal to the given value to zero')
conform = traits.Bool(argstr='-conform', position=7,
desc='Conform the input volumes (brain mask typically already conformed)')
use_weights = traits.Bool(argstr='-W', position=8,
desc='Use the weights from a previously generated weight file')
images = InputMultiPath(File(exists=True), argstr='%s', mandatory=True,
copyfile=False, desc='list of input FLASH images')


class MS_LDAOutputSpec(TraitedSpec):
weight_file = File(exists=True, desc='')
vol_synth_file = File(exists=True, desc='')


class MS_LDA(FSCommand):
"""Perform LDA reduction on the intensity space of an arbitrary # of FLASH images
Examples
--------
>>> grey_label = 2
>>> white_label = 3
>>> zero_value = 1
>>> optimalWeights = MS_LDA(lda_labels=[grey_label, white_label], label_file='label.mgz', weight_file='weights.txt',
shift=zero_value, synth='synth_out.mgz', conform=True, use_weights=True, images=['FLASH1.mgz',
'FLASH2.mgz', 'FLASH3.mgz'])
>>> optimalWeights.cmdline
'mri_ms_LDA -lda 2 3 -label label.mgz -weight weights.txt -shift 0 -synth synth_out.mgz -conform -W FLASH1.mgz FLASH2.mgz
FLASH3.mgz'
"""

_cmd = 'mri_ms_LDA'
input_spec = MS_LDAInputSpec
output_spec = MS_LDAOutputSpec

def _list_outputs(self):
outputs = self._outputs().get()
outputs['vol_synth_file'] = os.path.abspath(self.inputs.output_synth)
if not isdefined(self.inputs.use_weights) or self.inputs.use_weights is False:
outputs['weight_file'] = os.path.abspath(self.inputs.weight_file)
return outputs

def _verify_weights_file_exists(self):
if not os.path.exists(os.path.abspath(self.inputs.weight_file)):
raise traits.TraitError("MS_LDA: use_weights must accompany an existing weights file")

def _format_arg(self, name, spec, value):
if name is 'use_weights':
if self.inputs.use_weights is True:
self._verify_weights_file_exists()
else:
return ''
# TODO: Fix bug when boolean values are set explicitly to false
return super(MS_LDA, self)._format_arg(name, spec, value)

def _gen_filename(self, name):
pass
Loading

0 comments on commit 11010d6

Please sign in to comment.