nipype icon indicating copy to clipboard operation
nipype copied to clipboard

Request for freesurfer mri_cvs_register

Open rami-hamati opened this issue 5 years ago • 4 comments

I am trying to wrap https://surfer.nmr.mgh.harvard.edu/fswiki/mri_cvs_register The command line is simple, "mri_cvs_register --mov subjID" The problem is I do not completely know all the inputs used in this script (i.e. the script automatically finds the inputs based on subjects_dir), and so not sure how to wrap in nipype.

I have attempted below.

I try avoiding explicitly stating inputs by using exists=False, but anyways, when trying to connect to a workflow, it does not recognise it as an interface. Here is what I have so far running on Ubuntu linux Python 3.7.

"def MIL_305to152(): import os from nipype.interfaces.base import ( TraitedSpec, CommandLineInputSpec, CommandLine, File )

os.putenv('SUBJECTS_DIR','/home/lauri/Documents/FearCond/SUBJECTS_DIR')

class MNI152InputSpec(CommandLineInputSpec):
    input_file = File(desc="subject ID", exists=False, resolve=True, position=0, argstr="--mov %s")

class MNI152OutputSpec(TraitedSpec):
    output_file = File(desc = ".nii", exists=False, resolve=True)

class MNI152(CommandLine):
    input_spec = MNI152InputSpec
    output_spec = MNI152OutputSpec
    _cmd = 'mri_cvs_register'
    
    def _list_outputs(self):

        # Get the attribute saved during _run_interface
        return {'output_file': self.inputs.out_file}"

Thanks for your help.

Rami

rami-hamati avatar Sep 14 '20 14:09 rami-hamati

We have some base classes to get going with FreeSurfer tools:

https://github.com/nipy/nipype/blob/97ce446a9f55d72993b851436fdff7af57383be4/nipype/interfaces/freesurfer/base.py#L114-L118

effigies avatar Sep 14 '20 15:09 effigies

Hi there,

I've attempted to wrap mri_cvs_register using FSCommand:

import os
from nipype.interfaces.freesurfer.base import ( TraitedSpec, FSCommand, FSTraitedSpec, FSScriptOutputSpec, File )
class mni152InputSpec(FSTraitedSpec):
    in_file = File(
        argstr="--mov %s",
        position=-2,
        mandatory=True,
        exists=True,
        desc="Input file for mri_cvs_register",)

class mni152OutputSpec(TraitedSpec):
    out_cvs= File(exists=False, desc="cvs register output file")

class mni152(FSCommand):

    _cmd = "mri_cvs_register"
    input_spec = mni152InputSpec
    output_spec = mni152OutputSpec

    def _format_arg(self, name, spec, value):
        if self.inputs.copy_input:
            if name == "in_file":
                basename = os.path.basename(value)
                return spec.argstr % basename
        return super(mni152, self)._format_arg(name, spec, value)

    def _list_outputs(self):
        outputs = self._outputs().get()
        if self.inputs.copy_input:
            in_file = os.path.basename(self.inputs.in_file)
        else:
            in_file = self.inputs.in_file
        outputs["out_cvs"] = os.path.abspath(in_file) + ".nii"
        return outputs

Then I tried testing it:

mni = mni152()
mni.inputs.in_file = 'SUB-003'
mni.cmdline

Gives me the error:

TraitError: The 'in_file' trait of a mni152InputSpec instance must be a pathlike object or string representing an existing file, but a value of 'AVL-003' <class 'str'> was specified.

Really do not know what I am doing wrong and would appreciate some guidance.

Thanks

rami-hamati avatar Sep 18 '20 15:09 rami-hamati

Apologies for the slow response.

First, I think your _format_arg() can be replaced by setting the copyfile=False metadata in in_file. (copyfile=False will symlink into the current directory if possible, copyfile=True will always copy.)

Second, your in_file needs to be a filename, not a string.

Please do not hesitate to ask more questions if that's unclear. And I hope you've seen the tutorial on creating interfaces?

effigies avatar Nov 21 '20 14:11 effigies

Hi, I was wondering whether there was any progress on that one?

RDoerfel avatar May 21 '24 08:05 RDoerfel