nipy / nibabel

Python package to access a cacophony of neuro-imaging file formats
http://nipy.org/nibabel/
Other
652 stars 259 forks source link

Nibabel 5.0 missing attributes #1185

Closed marcelzwiers closed 1 year ago

marcelzwiers commented 1 year ago

This works in nibabel v4 but no longer in v5 (or what do I wrong)?

>>> import nibabel
>>> nibabel.info.VERSION
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'nibabel.info' has no attribute 'VERSION'

and

>>> nibabel.ext_map.keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'nibabel' has no attribute 'ext_map'
effigies commented 1 year ago

ext_map was deprecated in 2.1 and finally removed in 5.0.

As to nibabel.info.VERSION, that used to be the source for nibabel.__version__. Then for a while it was a development version that was always <= nibabel.__version__. Now it isn't needed to provide fallback versions, so it was removed. I'm a bit surprised anybody was using it, though it admittedly wasn't an announced removal.

I can make it equal for backwards compatibility in the next release, but would it make more sense for you to move nibabel.__version__?

marcelzwiers commented 1 year ago

nibabel.__version__ is just fine as well, but what is the replacement for getting the supported file extensions (i.e. for ext_map.keys())?

effigies commented 1 year ago

Hmm. Apparently we didn't mark all uses of the object as deprecated, just trying to use it as a lookup table:

>>> import nibabel as nb
>>> nb.__version__
'4.0.2'
>>> nb.ext_map.keys()
odict_keys(['nifti_single', '.nii', 'nifti_pair', '.img', '.hdr', 'minc', '.mnc', 'mgh', '.mgh', 'mgz', '.mgz', 'par', '.par', 'brik', '.brik'])
>>> nb.ext_map['.nii']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/chris/miniconda3/envs/fmriprep-lts/lib/python3.7/site-packages/nibabel/deprecator.py", line 181, in deprecated_func
    raise error_class(message)
nibabel.deprecator.ExpiredDeprecationError: ext_map is deprecated.

* deprecated from version: 2.1
* Raises <class 'nibabel.deprecator.ExpiredDeprecationError'> as of version: 4.0

This is a bit unfortunate, since we also didn't update this with all image types. The way to do this would be to look up the valid extensions for each image class. Using set() you can get unique values:

>>> import nibabel as nb
>>> set(sum((klass.valid_exts for klass in nb.imageclasses.all_image_classes), ()))
{'.brik',
 '.gii',
 '.hdr',
 '.head',
 '.img',
 '.mgh',
 '.mgz',
 '.mnc',
 '.nii',
 '.par',
 '.rec'}
marcelzwiers commented 1 year ago

Great, that works fine for me, thanks a lot!