How to change the source for the output filename when using NiPype?

I want to transform a manually created mask to multiple BOLD images (of the same subject).

Using FSL, this is a two step process:

  1. Calculate the transform (FLIRT)
  • in_file - BOLD image that corresponds to the manually created mask
  • reference - the BOLD images
  • out_matrix_file
  1. Apply the transform (ApplyXFM)

out_matrix_file's name is based off of in_file. Is there a way to get it based off of reference?

I had two ideas on how to do this, but I haven’t been successful in figuring out how to get either to work. The ideas are:

  1. Set the name_source of out_matrix_file, although this seems hardcoded.
  2. Use substitution on the output directories. I can’t figure out how to replace the generic names (ie. “_maskthresh0”) with the names of the BOLD images. I read something on this in an old thread (!topic/nipy-user/n1dy_IIk-c8) , but as far as I can tell, this hasn’t been implemented in the core nipype.
  3. Swap the reference and image in FLIRT and then calculate the inverse of the transform. This resolves the issue for the first step, but then the same problem occurs with ApplyXFM (the in_file for ApplyXFM is the manual mask).

How can I get the output of the transformed images to be named after the BOLD images of the transformed masks instead of the manual mask or it’s corresponding BOLD image?

The solution I found was to create a subclass of ApplyXFM that defines its own InputSpec:

from nipype.interfaces.base import File

class ApplyXFMInputSpecRefName(fsl.preprocess.ApplyXFMInputSpec):
    out_file = File(argstr='-out %s', desc='registered output file',
                    name_source=['reference'], name_template='%s_flirt',
                    position=2, hash_files=False)

class ApplyXFMRefName(fsl.FLIRT):
    """Currently just a light wrapper around FLIRT,
    with no modifications
    ApplyXFMRefName uses the reference for naming the transformed images.
    input_spec = ApplyXFMInputSpecRefName

Hi Jonathan,

Looks like you found a workaround for this specific use-case.

FWIW, you may be able to do this in one step using ApplyXfm with the uses_qform flag, which would skip the out_matrix_file generation.

There’s some more info here