Skip to content

Commit dfdbbb7

Browse files
committed
ENH: Handle new axes in Minc
1 parent c686a5a commit dfdbbb7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

nibabel/minc1.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,42 @@ def _spatial_dims(self):
325325
return slice(1, 4)
326326
return slice(0, 3)
327327

328+
def _check_slicing(self, slicer, return_spatial=False):
329+
''' Canonicalize slicers and check for scalar indices in spatial dims
330+
331+
Parameters
332+
----------
333+
slicer : object
334+
something that can be used to slice an array as in
335+
``arr[sliceobj]``
336+
return_spatial : bool
337+
return only slices along spatial dimensions (x, y, z)
338+
339+
Returns
340+
-------
341+
slicer : object
342+
Validated slicer object that will slice image's `dataobj`
343+
without collapsing spatial dimensions
344+
'''
345+
try:
346+
all_slices = super(Minc1Image, self)._check_slicing(slicer, False)
347+
sp_dims = self._spatial_dims
348+
except IndexError:
349+
# Prepending a new axis for 3D images is valid in Minc
350+
if slicer[0] is None and self._spatial_dims == slice(0, 3):
351+
all_slices = (None,) + super(Minc1Image, self)._check_slicing(slicer[1:], False)
352+
sp_dims = slice(1, 4)
353+
else:
354+
raise
355+
# Added complications of first axis being time
356+
if self._spatial_dims == slice(1, 4) and all_slices[0] is None:
357+
raise IndexError("New temporal axis is not permitted in 4D Minc images")
358+
elif (self._spatial_dims == slice(0, 3) and len(all_slices) > 3 and
359+
all_slices[0] is not None):
360+
raise IndexError("New axes cannot be added to 3D Minc image "
361+
"without new temporal axis (first dimension)")
362+
return all_slices[sp_dims] if return_spatial else all_slices
363+
328364

329365
load = Minc1Image.load
330366

0 commit comments

Comments
 (0)