Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

traits.trait_errors.TraitError: Each element of the 'in_files' trait of a SmoothInputSpec instance must be a pathlike object or string representing an existing file, but a value of 'xxx’ <class 'str'> was specified. #3693

Open
abcvav opened this issue Oct 17, 2024 · 8 comments

Comments

@abcvav
Copy link

abcvav commented Oct 17, 2024

Summary

When running a script using smooth.inputs.in_files, an error occurs indicating that the in_files trait must be a pathlike object or a string representing an existing file, despite the file path being valid and existing.

Actual behavior

The script fails with the following error traceback:

Traceback (most recent call last):
  File "/Users/ab/Documents/Code/nipype_tutorial/tests/t.py", line 4, in <module>
    smooth.inputs.in_files = '/Users/ab/Documents/Code/nipype_tutorial/tests/test.nii.gz'
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/nipype/interfaces/base/traits_extension.py", line 424, in validate
    value = super(MultiObject, self).validate(objekt, name, newvalue)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/traits/trait_types.py", line 2699, in validate
    return TraitListObject(self, object, name, value)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/traits/trait_list_object.py", line 582, in __init__
    super().__init__(
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/traits/trait_list_object.py", line 213, in __init__
    super().__init__(self.item_validator(item) for item in iterable)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/traits/trait_list_object.py", line 213, in <genexpr>
    super().__init__(self.item_validator(item) for item in iterable)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/traits/trait_list_object.py", line 865, in _item_validator
    return trait_validator(object, self.name, value)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/nipype/interfaces/base/traits_extension.py", line 334, in validate
    self.error(objekt, name, str(value))
  File "/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/traits/base_trait_handler.py", line 74, in error
    raise TraitError(
traits.trait_errors.TraitError: Each element of the 'in_files' trait of a SmoothInputSpec instance must be a pathlike object or string representing an existing file, but a value of '/Users/ab/Documents/Code/nipype_tutorial/tests/test.nii.gz' <class 'str'> was specified.

Expected behavior

The file path '/Users/ab/Documents/Code/nipype_tutorial/tests/test.nii.gz' should be accepted as a valid path, and the smooth.inputs.in_files trait should accept it without raising an error, as the file does exist. But using an absolute path results in the same outcome.

How to replicate the behavior

run the following scripts:

from nipype.interfaces.spm import Smooth
smooth = Smooth()
smooth.inputs.in_files = '/Users/ab/Documents/Code/nipype_tutorial/tests/test.nii.gz'

Script/Workflow details

>>> from pathlib import Path
>>> Path('/Users/ab/Documents/Code/nipype_tutorial/tests/test.nii.gz').exists()
True

Platform details:

{'commit_hash': '%h',
 'commit_source': 'archive substitution',
 'networkx_version': '3.4.1',
 'nibabel_version': '5.3.1',
 'nipype_version': '1.8.6',
 'numpy_version': '2.1.2',
 'pkg_path': '/opt/homebrew/Caskroom/miniconda/base/envs/preproc/lib/python3.10/site-packages/nipype',
 'scipy_version': '1.14.1',
 'sys_executable': '/opt/homebrew/Caskroom/miniconda/base/envs/preproc/bin/python',
 'sys_platform': 'darwin',
 'sys_version': '3.10.13 | packaged by conda-forge | (main, Dec 23 2023, '
                '15:35:25) [Clang 16.0.6 ]',
 'traits_version': '6.3.2'}

Execution environment

  • My python environment outside container

I use miniconda, the conda version is conda 24.7.1. Nipype was installed using conda install --channel conda-forge nipype.
Python version 3.10.13

@tamruta
Copy link

tamruta commented Feb 12, 2025

SmoothInputSpec and similar SPM functions only accept uncompressed nifti files. Uncompress the file and try again. It should work.

class RealignInputSpec(SPMCommandInputSpec):
in_files = InputMultiPath(
traits.Either(
ImageFileSPM(exists=True), traits.List(ImageFileSPM(exists=True))
),
field="data",
mandatory=True,
copyfile=True,
desc="list of filenames to realign",
)

class ImageFileSPM(ImageFile):
"""Defines a trait whose value must be a NIfTI file."""

Maybe changing the value of allow_compressed to true could solve this problem.

allow_compressed=False,

@effigies
Copy link
Member

Has SPM started accepting compressed files? If so, in what version?

@tamruta
Copy link

tamruta commented Feb 14, 2025

Not that I'm aware unfortunately. Related to issue #2420
Changing the error message would be helpful. Could there be an intermediate function to unzip the files?

@effigies
Copy link
Member

Changing the error message would be helpful.

It looks like we would need to do that in this class:

By overriding

@property
def info_text(self):
"""Create the trait's general description."""
info_text = "a pathlike object or string"
if any((self.exists, self._is_file, self._is_dir)):
info_text += " representing a"
if self.exists:
info_text += "n existing"
if self._is_file:
info_text += " file"
elif self._is_dir:
info_text += " directory"
else:
info_text += " file or directory"
return info_text

to indicate acceptable extensions.

Could there be an intermediate function to unzip the files?

Automatically? I can't immediately think of a way. But there is nipype.algorithms.misc.Gunzip.

@tamruta
Copy link

tamruta commented Feb 19, 2025

I'm assuming that would not work well with other libraries that accept compressed nifti.

@effigies
Copy link
Member

Sorry, I don't understand that comment.

@tamruta
Copy link

tamruta commented Feb 19, 2025

Ah I'm sorry. Nipype incorporates a lot of libraries. Do they all use the File class to confirm filetype? If so, it would be hard to raise an error only for SPM. Or even add a restriction for .nii filetypes

@effigies
Copy link
Member

Got it. No, we should be able to do this without changing what succeeds or fails. What I'm proposing is just to improve the text emitted by the .info_text property when we know that there are required extensions, e.g.,

class File(BasePath):
    ...
    @property
    def info_text(self):
        info_text = super().info_text
        if self._exts:
            if len(self._exts) == 1:
                info_text += f" with extension {self._exts[0]}"
            else:
                info_text += f" with one of the extensions: {self.exts}"
        return info_text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants