MNE-BIDS-PIPELINE custom config.mri_t1_path_generator_func :: AttributeError: 'NoneType' object has no attribute 'suffix'

  • mne 1.8.0
  • mne_bids_pipeline 1.9.0
  • Ubuntu 24.04.1 LTS

I am running the pipilene step source/_04_make_forward.py with custom function to get paths to T1w files. They are recorded in a separate day and have a bit different session labeling compared to the EEG data, also not all subjects have T1w files.

The function i use:

def mri_t1_path_generator_func(bids_path):
    sessions = ['a, 'b', 'c', 'd']
    mri_dir = '../data_BIDS'

    for session in sessions: # Iterate to find the subjects label for session
        # Make path to the subject's anat folder
        anat_dir = os.path.join(mri_dir, f'sub-{bids_path.subject}', f'ses-{session}', 'anat')

        if os.path.isdir(anat_dir):  # Ensure the directory exists
            t1w_path = BIDSPath(subject=bids_path.subject, 
                                        session=session,
                                        suffix='T1w', datatype='anat', extension='.nii.gz',
                                        root=mri_dir)
            full_path = t1w_path.fpath(strict=False)  
            if os.path.exists(full_path): # if T1w file exists
                return t1w_path
            print(f"No T1w MRI files found in: {anat_dir}")
        else:
            print(f"Anat directory not found: {anat_dir}")
        
    warnings.warn(f"No MRI file found for the subject {bids_path.subject} across specified sessions.")
    return None  # Return None instead of raising an error

the error i get:

Traceback (most recent call last):
  File "/home/gorel321/miniconda3/envs/mne/bin/mne_bids_pipeline", line 10, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/mne_bids_pipeline/_main.py", line 234, in main
    step_module.main(config=config_imported)
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/mne_bids_pipeline/steps/source/_04_make_forward.py", line 276, in main
    logs = parallel(
           ^^^^^^^^^
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 2007, in __call__
    return output if self.return_generator else list(output)
                                                ^^^^^^^^^^^^
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 1650, in _get_outputs
    yield from self._retrieve()
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 1754, in _retrieve
    self._raise_error_fast()
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 1789, in _raise_error_fast
    error_job.get_result(self.timeout)
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 745, in get_result
    return self._return_or_raise()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 763, in _return_or_raise
    raise self._result
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/joblib/parallel.py", line 1469, in dispatch_one_batch
    islice = list(itertools.islice(iterator, big_batch_size))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/mne_bids_pipeline/steps/source/_04_make_forward.py", line 278, in <genexpr>
    cfg=get_config(config=config, subject=subject, session=session),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gorel321/miniconda3/envs/mne/lib/python3.12/site-packages/mne_bids_pipeline/steps/source/_04_make_forward.py", line 238, in get_config
    if t1_bids_path.suffix is None:
       ^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'suffix'

the _04_make_forward.py code part that throws the error:

def get_config(
    *,
    config: SimpleNamespace,
    subject: str,
    session: str | None,
) -> SimpleNamespace:
    if config.mri_t1_path_generator is None:
        t1_bids_path = None
    else:
        t1_bids_path = BIDSPath(subject=subject, session=session, root=config.bids_root)
        t1_bids_path = config.mri_t1_path_generator(t1_bids_path.copy())
        if t1_bids_path.suffix is None:
            t1_bids_path.update(suffix="T1w")
        if t1_bids_path.datatype is None:
            t1_bids_path.update(datatype="anat")
<..>

What is wrong with my function?

Hello,

You’re missing a quotation mark here after the a.

Best wishes,
Richard

this typo i did here while changing the session names to occupy less space. so the question is still relevant)

@neuroLena

I believe your path generator function is returning None in your case. So I assume you’re for some reason always hitting the code path that leads to:

You should check if and why that’s the case.

Best wishes,
Richard