Skip to content

ENH: Warn on invalid MINC2 spacing declarations, treat as missing #1237

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

Merged
merged 5 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .zenodo.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@
},
{
"name": "freec84"
},
{
"name": "Suter, Peter"
}
],
"keywords": [
Expand Down
8 changes: 7 additions & 1 deletion nibabel/minc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

mincstats my_funny.mnc
"""
import warnings
import numpy as np

from .minc1 import Minc1File, Minc1Image, MincError, MincHeader
Expand Down Expand Up @@ -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']
Expand Down
Binary file added nibabel/tests/data/minc2_baddim.mnc
Binary file not shown.
10 changes: 10 additions & 0 deletions nibabel/tests/test_minc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)