diff --git a/.zenodo.json b/.zenodo.json index a436bfd31b..d79c0cf934 100644 --- a/.zenodo.json +++ b/.zenodo.json @@ -377,6 +377,9 @@ }, { "name": "freec84" + }, + { + "name": "Suter, Peter" } ], "keywords": [ diff --git a/nibabel/minc2.py b/nibabel/minc2.py index e00608eb2f..d02eb6cefc 100644 --- a/nibabel/minc2.py +++ b/nibabel/minc2.py @@ -25,6 +25,7 @@ mincstats my_funny.mnc """ +import warnings import numpy as np from .minc1 import Minc1File, Minc1Image, MincError, MincHeader @@ -58,8 +59,13 @@ def __init__(self, mincfile): # We don't currently support irregular spacing # https://en.wikibooks.org/wiki/MINC/Reference/MINC2.0_File_Format_Reference#Dimension_variable_attributes for dim in self._dims: - if dim.spacing != b'regular__': + # "If this attribute is absent, a value of regular__ should be assumed." + spacing = getattr(dim, 'spacing', b'regular__') + if spacing == b'irregular': raise ValueError('Irregular spacing not supported') + elif spacing != b'regular__': + warnings.warn(f'Invalid spacing declaration: {spacing}; assuming regular') + self._spatial_dims = [name for name in self._dim_names if name.endswith('space')] self._image_max = image['image-max'] self._image_min = image['image-min'] diff --git a/nibabel/tests/data/minc2_baddim.mnc b/nibabel/tests/data/minc2_baddim.mnc new file mode 100644 index 0000000000..c7de97bd5e Binary files /dev/null and b/nibabel/tests/data/minc2_baddim.mnc differ diff --git a/nibabel/tests/test_minc2.py b/nibabel/tests/test_minc2.py index 251393818a..e76cb05ce7 100644 --- a/nibabel/tests/test_minc2.py +++ b/nibabel/tests/test_minc2.py @@ -10,6 +10,7 @@ from os.path import join as pjoin import numpy as np +import pytest from .. import minc2 from ..minc2 import Minc2File, Minc2Image @@ -121,3 +122,12 @@ class TestMinc2Image(tm2.TestMinc1Image): image_class = Minc2Image eg_images = (pjoin(data_path, 'small.mnc'),) module = minc2 + + +def test_bad_diminfo(): + fname = pjoin(data_path, 'minc2_baddim.mnc') + # File has a bad spacing field 'xspace' when it should be + # `irregular`, `regular__` or absent (default to regular__). + # We interpret an invalid spacing as absent, but warn. + with pytest.warns(UserWarning) as w: + Minc2Image.from_filename(fname)