Skip to content

Commit 134d469

Browse files
petsutereffigies
authored andcommitted
ENH: Warn on invalid MINC2 spacing declarations, treat as missing
Backport gh-1237
1 parent 233ae95 commit 134d469

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

.zenodo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@
377377
},
378378
{
379379
"name": "freec84"
380+
},
381+
{
382+
"name": "Suter, Peter"
380383
}
381384
],
382385
"keywords": [

nibabel/minc2.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
2626
mincstats my_funny.mnc
2727
"""
28+
import warnings
2829
import numpy as np
2930

3031
from .minc1 import Minc1File, Minc1Image, MincError, MincHeader
@@ -58,8 +59,13 @@ def __init__(self, mincfile):
5859
# We don't currently support irregular spacing
5960
# https://en.wikibooks.org/wiki/MINC/Reference/MINC2.0_File_Format_Reference#Dimension_variable_attributes
6061
for dim in self._dims:
61-
if dim.spacing != b'regular__':
62+
# "If this attribute is absent, a value of regular__ should be assumed."
63+
spacing = getattr(dim, 'spacing', b'regular__')
64+
if spacing == b'irregular':
6265
raise ValueError('Irregular spacing not supported')
66+
elif spacing != b'regular__':
67+
warnings.warn(f'Invalid spacing declaration: {spacing}; assuming regular')
68+
6369
self._spatial_dims = [name for name in self._dim_names if name.endswith('space')]
6470
self._image_max = image['image-max']
6571
self._image_min = image['image-min']

nibabel/tests/data/minc2_baddim.mnc

19.4 KB
Binary file not shown.

nibabel/tests/test_minc2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from os.path import join as pjoin
1111

1212
import numpy as np
13+
import pytest
1314

1415
from .. import minc2
1516
from ..minc2 import Minc2File, Minc2Image
@@ -121,3 +122,12 @@ class TestMinc2Image(tm2.TestMinc1Image):
121122
image_class = Minc2Image
122123
eg_images = (pjoin(data_path, 'small.mnc'),)
123124
module = minc2
125+
126+
127+
def test_bad_diminfo():
128+
fname = pjoin(data_path, 'minc2_baddim.mnc')
129+
# File has a bad spacing field 'xspace' when it should be
130+
# `irregular`, `regular__` or absent (default to regular__).
131+
# We interpret an invalid spacing as absent, but warn.
132+
with pytest.warns(UserWarning) as w:
133+
Minc2Image.from_filename(fname)

0 commit comments

Comments
 (0)