Skip to content

Commit

Permalink
Transpose nifti on read
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Dec 4, 2023
1 parent d8e8dcf commit 9a14835
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions elf/io/nifti_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Mapping
from ..util import normalize_index, squeeze_singletons

import numpy as np
try:
Expand Down Expand Up @@ -60,10 +61,13 @@ def chunks(self):

@property
def shape(self):
return self._data.shape
return self._data.shape[::-1]

def __getitem__(self, key):
return self._data.dataobj[key]
key, to_squeeze = normalize_index(key, self.shape)
transposed_key = key[::-1]
data = self._data.dataobj[transposed_key].T
return squeeze_singletons(data, to_squeeze)

@property
def size(self):
Expand Down
6 changes: 3 additions & 3 deletions test/io_tests/test_nifti_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _check_data(self, expected_data, f):
shape = dset.shape

# bounding boxes for testing sub-sampling
bbs = [np.s_[:]]
bbs = [0, np.s_[:]]
for i in range(dset.ndim):
bbs.extend([
tuple(slice(0, shape[i] // 2) if d == i else slice(None) for d in range(dset.ndim)),
Expand All @@ -39,7 +39,7 @@ def test_read_nifti(self):

paths = glob(os.path.join(data_path, "*.nii"))
for path in paths:
expected_data = np.asarray(nibabel.load(path).dataobj)
expected_data = np.asarray(nibabel.load(path).dataobj).T
# the resampled image causes errors
if os.path.basename(path).startswith("resampled"):
continue
Expand All @@ -52,7 +52,7 @@ def test_read_nifti_compressed(self):

paths = glob(os.path.join(data_path, "*.nii.gz"))
for path in paths:
expected_data = np.asarray(nibabel.load(path).dataobj)
expected_data = np.asarray(nibabel.load(path).dataobj).T
with open_file(path, "r") as f:
self._check_data(expected_data, f)

Expand Down

0 comments on commit 9a14835

Please sign in to comment.