CPAC Error: Template Referenced Before Assignment

Hi all,

I am running the default cpac pipeline and getting a variable referenced before assignment error.

Summary of what happened:

Using CPAC default pipeline for fMRI pre-processing in an autism fMRI dataset.

Command used (and if a helper script was used, a link to the helper script or the command generated):

Utilizing default pipeline, as mentioned above. Here’s the command used:

cpac run GU_28830 ABIDE_2_OUTPUT participant

Version:

cpac 1.8.6

Environment (Docker, Singularity / Apptainer, custom installation):

Singularity

Data formatted according to a validatable standard? Please provide the output of the validator:

Number of participants to run in parallel: 1
Input directory: /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/GU_28830
Output directory: /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/ABIDE_2_OUTPUT/output
Working directory: /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/ABIDE_2_OUTPUT/working
Log directory: /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/ABIDE_2_OUTPUT/log
Remove working directory: True
Available memory: 51200.0 (GB)
Available threads: 56
Number of threads for ANTs: 10
Parsing /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/GU_28830..
Starting participant level processing
Run called with config file /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/ABIDE_2_OUTPUT/log/pipeline_cpac-default-pipeline/sub-GU28830_ses-1/cpac_pipeline_config_9c8e9d7f_2024-01-22T09-37-19Z.yml

Relevant log outputs (up to 20 lines):

Traceback:
	Traceback (most recent call last):
	  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/interfaces/base/core.py", line 397, in run
	    runtime = self._run_interface(runtime)
	  File "/code/CPAC/utils/interfaces/function/function.py", line 216, in _run_interface
	    out = function_handle(**args)
	  File "<string>", line 267, in get_scan_params
	UnboundLocalError: local variable 'template' referenced before assignment

Screenshots / relevant information:

Utilizing Biowulf (NIH supercomputer) to run this analysis.


Any thoughts on why this might be occurring?

Hi @asd_tms,

It would help others answer your question if you fill in all the information that was requested by the Software Support post template.

Best,
Steven

Hi Steven,

Thanks for the suggestion. I modified my post.

Hello @asd_tms,

Not sure but at quick glance this might be an error catch that went wrong and didn’t provide a better message.

Can you post a brief example of your input data directory format?

Thanks,
Steve G.

Hi @asd_tms,

I would confirm the version you’re using. On the GitHub repo, the function get_scan_params is a part of a different function then what your error indicates: https://github.com/FCP-INDI/C-PAC/blob/6448fed9d4b78029fd86a82fb9942f105cd2b563/CPAC/utils/utils.py#L697. Perhaps explicitly link your cpac run command to your image that you are sure is version 1.8.6 (or do 1.8.5, since 1.8.6 is currently a beta release).

Best,
Steven

I think both Steven and Steve have good instincts here.

I think you are indeed running C-PAC 1.8.6 since out = function_handle(**args) is line 216 of CPAC/utils/interfaces/function/function.py in 1.8.6 but line 152 in C-PAC 1.8.5.

The only way I see only place I see where template could become undefined in get_scan_params in 1.8.6 is if this block fails with something other than a TypeError:

            try: 
                template = str(
                    try_fetch_parameter(
                        params_dct,
                        subject_id,
                        scan,
                        ['Template', 'template']
                    )
                )
            except TypeError:
                template = None

which I think indicates a problem with the input data configuration, as Steve suggested

Hi all,

In case it helps to share the input data configuration, here’s my file structure:

GU_28830/
sub-GU28830
├── anat
│ └── sub-GU28830_T1w.nii.gz
└── func
└── sub-GU28830_task-rest_bold.nii.gz

Thanks @asd_tms! Do you have JSON sidecars with metadata for the input data?

If you’re able to, can you also share the YAML file from the C-PAC log directory that starts with cpac_data_config_?

And is there more traceback than shared at the top of this thread?

Unfortunately, I didn’t get any JSON sidecars with my ABIDE data download.

Here’s the data config file as requested:

- anat:
    T1w: /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/GU_28830/sub-GU28830/anat/sub-GU28830_T1w.nii.gz
  creds_path: null
  func:
    rest:
      scan: /vf/users/NNU_NDData_Projs/ASD_Targeting/MRI_Raw/ABIDE_2/ABIDE_2_BIDS/GU_28830/sub-GU28830/func/sub-GU28830_task-rest_bold.nii.gz
  site_id: site-none
  subject_id: sub-GU28830
  unique_id: ses-1

I believe this is the only traceback I’ve been able to produce.

I actually re-ran the cpac command with a different output directory and replicated the error with additional traceback this time:

Traceback (most recent call last):
  File "/code/CPAC/pipeline/cpac_pipeline.py", line 575, in run_workflow
    workflow_result = workflow.run(plugin=plugin,
  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/pipeline/engine/workflows.py", line 638, in run
    runner.run(execgraph, updatehash=updatehash, config=self.config)
  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/pipeline/plugins/base.py", line 224, in run
    raise error from cause
RuntimeError: Traceback (most recent call last):
  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/pipeline/plugins/multiproc.py", line 67, in run_node
    result["result"] = node.run(updatehash=updatehash)
  File "/code/CPAC/pipeline/nipype_pipeline_engine/engine.py", line 428, in run
    return super().run(updatehash)
  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/pipeline/engine/nodes.py", line 527, in run
    result = self._run_interface(execute=True)
  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/pipeline/engine/nodes.py", line 645, in _run_interface
    return self._run_command(execute)
  File "/usr/share/fsl/6.0/lib/python3.10/site-packages/nipype/pipeline/engine/nodes.py", line 771, in _run_command
    raise NodeExecutionError(msg)
nipype.pipeline.engine.nodes.NodeExecutionError: Exception raised while executing Node bold_scan_params_sub-GU28830_ses-1.

Not sure if that helps.

Thanks @asd_tms, both of those posts are helpful, although I still haven’t quite cracked this one yet.

Are there any files in your ABIDE_2_OUTPUT/log directory besides the pipeline_cpac-default-pipeline subdirectory?

Does ABIDE_2_OUTPUT/working/pipeline_cpac-default-pipeline/cpac_sub-GU28830_ses-1/_scan_rest/bold_scan_params_sub-GU28830_ses-1 exist?

And can you please also share ABIDE_2_OUTPUT/log/pipeline_cpac-default-pipeline/sub-GU28830_ses-1/pypeline.log if it exists?

The sidecars follow BIDS inheritance and are available on S3 at the same level as the sub-.* directories (e.g., in s3://fcp-indi/data/Projects/ABIDE2/RawData/ABIDEII-GU_1/ you can find the files dataset_description.json, participants.tsv, task-rest_bold.json, and T1w.json which apply to all subdirectories of ABIDEII-GU_1, including sub-28830).

(That might not be relevant to this issue ― I was able to run this node locally with and without the metadata sidecar, but in case you need or want them later, they are available)