nipype icon indicating copy to clipboard operation
nipype copied to clipboard

Setting up PATH for nipype.interfaces.ants

Open lukevano opened this issue 9 months ago • 2 comments
trafficstars

Summary I am attempting to execute antsRegistration via nipype.interfaces.ants import Registration but get an error stating that the command "antsRegistration" found on my host. I have ANTs loaded on the terminal and can run the command directly here. I can see that there was a similar issue (https://github.com/nipy/nipype/issues/3202) but that this could be solved by updating the PATH.

I cannot find documentation on how to update my PATH to allow my command to work. I believe I am working in ~/.cshrc

Actual behavior

def ants_linear_T1w_NM_registration(T1_corrected_path, NM_corrected_path, output_dir, subject_id):
    """
    Perform ANTs rigid registration to align the NM image to the T1-weighted image using the Nipype ANTs wrapper.
    """
    # Define output file paths
    output_prefix = os.path.join(output_dir, f"{subject_id}_NM_space-T1w")
    warped_image_path = f"{output_prefix}_Warped.nii.gz"
    inverse_warped_image_path = f"{output_prefix}_InverseWarped.nii.gz"
    affine_transform_path = f"{output_prefix}_Affine.mat"

    # Create registration object
    reg = Registration()

    # Set input images
    reg.inputs.fixed_image = T1_corrected_path
    reg.inputs.moving_image = NM_corrected_path

    # Set output transform prefix
    reg.inputs.output_transform_prefix = output_prefix

    # Define transformations
    reg.inputs.transforms = ["Rigid"]
    reg.inputs.transform_parameters = [(0.1,)]  # Equivalent to --transform Rigid[0.1]

    # Convergence parameters
    reg.inputs.number_of_iterations = [[100, 70, 50, 20]]  # Corresponds to --convergence [100x70x50x20, 1e-6, 10]
    reg.inputs.convergence_threshold = [1e-6]
    reg.inputs.convergence_window_size = [10]

    # Shrink factors and smoothing sigmas
    reg.inputs.shrink_factors = [[8, 4, 2, 1]]  # Corresponds to --shrink-factors 8x4x2x1
    reg.inputs.smoothing_sigmas = [[3, 2, 1, 0]]  # Corresponds to --smoothing-sigmas 3x2x1x0vox
    reg.inputs.sigma_units = ["vox"]

    # Similarity metric
    reg.inputs.metric = ["CC"]  # Corresponds to --metric CC[T1_corrected_path, NM_corrected_path,1,4]
    reg.inputs.metric_weight = [1]
    reg.inputs.radius_or_number_of_bins = [4]

    # Histogram matching & intensity settings
    reg.inputs.use_histogram_matching = [False]  # Corresponds to --use-histogram-matching 0
    reg.inputs.winsorize_lower_quantile = 0.005  # Corresponds to --winsorize-image-intensities [0.005, 0.995]
    reg.inputs.winsorize_upper_quantile = 0.995

    # Interpolation method
    reg.inputs.interpolation = "Linear"  # Corresponds to --interpolation Linear

    # Output file settings
    reg.inputs.output_warped_image = warped_image_path
    reg.inputs.output_inverse_warped_image = inverse_warped_image_path

    # Run the registration
    reg.run()

    print(f"NM-to-T1 registration saved to {warped_image_path}")
Traceback (most recent call last):
  File "/data/project/CARDS/kcl_nm/scripts/main_script.py", line 119, in <module>
    main()
  File "/data/project/CARDS/kcl_nm/scripts/main_script.py", line 109, in main
    process_all_subjects(
  File "/data/project/CARDS/kcl_nm/scripts/main_script.py", line 94, in process_all_subjects
    process_subject(nifti_dir, output_dir, subject_id, templates_dir, results_dir, apply_robustfov_flag)
  File "/data/project/CARDS/kcl_nm/scripts/main_script.py", line 51, in process_subject
    ants_linear_T1w_NM_registration(T1_corrected_path, NM_corrected_path, subject_output_path, subject_id)
  File "/data/project/CARDS/kcl_nm/scripts/image_processing_functions_wrapper.py", line 93, in ants_linear_T1w_NM_registration
    reg.run()
  File "/home/k1754339/.conda/envs/kcl_nm/lib/python3.11/site-packages/nipype/interfaces/base/core.py", line 397, in run
    runtime = self._run_interface(runtime)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/k1754339/.conda/envs/kcl_nm/lib/python3.11/site-packages/nipype/interfaces/ants/registration.py", line 1020, in _run_interface
    runtime = super(Registration, self)._run_interface(runtime)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/k1754339/.conda/envs/kcl_nm/lib/python3.11/site-packages/nipype/interfaces/base/core.py", line 752, in _run_interface
    raise IOError(
OSError: No command "antsRegistration" found on host grid03.nan.kcl.ac.uk. Please check that the corresponding package is installed.

Versions: ants/2.4.0 nipype/1.8.6

lukevano avatar Jan 29 '25 22:01 lukevano

So in the same terminal, you can run:

antsRegistration --version

and get a result, but

python <your script>

fails? That shouldn't happen, since both will have the same access to $PATH.

I notice you're using conda. You might simplify matters by installing ANTs through conda:

conda create -n envname -c conda-forge nipype ants [...]

effigies avatar Jan 29 '25 23:01 effigies

Many thanks for this prompt response. This fixes the problem. I do have two more quick queries:

  • Is it possible to specify that I want to install ants (v2.4.0)?

With my current pipeline I ask users to do the following before running my code: module load fsl/6.0.5.2 module load freesurfer/7.3.2 source /software/system/freesurfer/7.3.2/SetUpFreeSurfer.csh

  • Is it possible to conda install these packages to skip this step?

lukevano avatar Jan 30 '25 09:01 lukevano