nipy / nipype

Workflows and interfaces for neuroimaging packages
https://nipype.readthedocs.org/en/latest/
Other
749 stars 530 forks source link

ants.BrainExtraction.run() [ index out of range due to version output in ants has changes] #3381

Open rperea14 opened 3 years ago

rperea14 commented 3 years ago

Summary

I am unable to execute bet.run() after the following lines:

from nipype.interfaces.ants.segmentation import BrainExtraction
import os
import sys

bet = BrainExtraction()
bet.inputs.dimension = 3
bet.inputs.anatomical_image = sstrip_in_fname
bet.inputs.brain_template = ('<>/T_template0.nii.gz')
bet.inputs.brain_probability_mask =('/<>/T_template0_BrainCerebellumProbabilityMask.nii.gz')
bet.inputs.extraction_registration_mask = ('<>'/T_template0_BrainCerebellumRegistrationMask.nii.gz')
bet.inputs.out_prefix = ('<>/reg_T1sstrip-ants2MNI/out/ants_subjtest_')
bet.inputs.keep_temporary_files = 1

bet.run() ## <--- I use os.sytem(bet.cmdline instead for now, but worth mentioning the issue)

The problem I get is:

Traceback (most recent call last):
  File "/pilothpc/dept/mdsa/MLAI/scripts/coreg_related/try_pilot_deps/reg_tests/reg_T1sstrip-ants2MNI/test_coreg2bet.py", line 27, in <module>
    bet.run()
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 406, in run
    version=self.version,
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/nipype/interfaces/ants/base.py", line 121, in version
    return Info.version()
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/nipype/interfaces/base/core.py", line 1145, in version
    klass._version = klass.parse_version(raw_info)
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/nipype/interfaces/ants/base.py", line 38, in parse_version
    v_string = line.split()[2]
IndexError: list index out of range

Because when executing a command to get an ants version, the output looks like this:


$antsRegistration --version
ANTs Version: 
Compiled: Oct 13 2020 14:02:11
effigies commented 3 years ago

Where are you installing ANTs from? That's unfortunate that there are binaries out there that aren't getting tagged with version information.

rperea14 commented 3 years ago

Hi @effigies, I believe these are binaries installed by the HPC-IT to be as a batch system :/


On a side note, I am attempting to output a set of commands to run them sequentially in a pipeline [e.g skull strip in step1 using ants, coreg in step2 skull stripped brains using elastix].

I realized that nipype has a file exists validation, which prevents me to move forwards from step1 if step1 outputs are not created yet I want to reference them in step2. Is there any way to omit this validation step?

Here is a snippet of the error I get [bc. the outputs in step1 are not yet submitted to run]:

Traceback (most recent call last):
  File "/pilothpc/dept/mdsa/MLAI/scripts/coreg_related/pipeline_210922/loop_preproc_TIV.py", line 270, in <module>
    cmds = apply_elastix(braimask_T1w_fname, braimask_FLAIR_fname,
  File "/pilothpc/dept/mdsa/MLAI/scripts/coreg_related/pipeline_210922/loop_preproc_TIV.py", line 41, in apply_elastix
    reg.inputs.fixed_image =fixed_img
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/nipype/interfaces/base/traits_extension.py", line 330, in validate
    value = super(File, self).validate(objekt, name, value, return_pathlike=True)
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/nipype/interfaces/base/traits_extension.py", line 135, in validate
    self.error(objekt, name, str(value))
  File "/home/rperea1/.conda/envs/nipype/lib/python3.9/site-packages/traits/base_trait_handler.py", line 74, in error
    raise TraitError(
traits.trait_errors.TraitError: The 'fixed_image' trait of a RegistrationInputSpec instance must be a pathlike object or string representing an existing file, but a value of '/pilothpc/dept/mdsa/MLAI/datasets/MS_888MS001/derivatives/prepc_s1_antsv20201013_skstripN4/output_T1w_braimask.nii.gz' <class 'str'> was specified
effigies commented 3 years ago

I believe these are binaries installed by the HPC-IT to be as a batch system :/

Rough. If you know the version, before creating any ANTs interfaces, you can add:

import nipype

nipype.interfaces.ants.base.Info._version = "X.Y.Z"

Not great for writing code that can be used elsewhere, though... You could make it more conditional like:

import nipype

try:
    nipype.interfaces.ants.base.Info.version()
except:
    nipype.interfaces.ants.base.Info._version = "X.Y.Z"

If you're trying to connect up the outputs of one interface to another, then you should be using a workflow:

from nipype.pipeline import engine as pe

step1 = pe.Node(Step1Interface(), name="step1")
step2 = pe.Node(Step2Interface(), name="step2")

wf = pe.Workflow(name="workflow")

wf.connect([
    (step1, step2, [("output-field1", "input-field1"),
                    ("output-field2", "input-field2")]),
])

Then you use wf.run(), rather than running each individually.

rperea14 commented 3 years ago

Is there any other way besides using workflows [looking to set some argument, validate=False ] ?

The output files from different processing tools are tricky and need additional manipulation after I start another step [e.g. ants skull stripp generate dependent n4corrected, brainmask, brains. I will grab the brain.nii.gz and use it as a mov_img in step2, elastix ]...

Or can you refer me to a good tutorial for using the workflows?

effigies commented 3 years ago

Not really. The whole point is to be able to tell somebody, before they start a long workflow, if it's going to fail because a required input is missing. You can leave these fields blank and only set them when you're ready, however...

We highly recommend @miykael's nipype tutorials: https://miykael.github.io/nipype_tutorial/

And here's the introduction to workflows: https://miykael.github.io/nipype_tutorial/notebooks/basic_workflow.html