-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementing nifti file wrapper (#79)
Implement nifti file wrapper, update MRC error handling
- Loading branch information
1 parent
c0f5f61
commit a47d46d
Showing
6 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from collections.abc import Mapping | ||
from ..util import normalize_index, squeeze_singletons | ||
|
||
import numpy as np | ||
try: | ||
import nibabel | ||
except ImportError: | ||
nibabel = None | ||
|
||
|
||
class NiftiFile(Mapping): | ||
def __init__(self, path, mode="r"): | ||
if nibabel is None: | ||
raise AttributeError("nibabel is required for nifti images, but is not installed.") | ||
self.path = path | ||
self.mode = mode | ||
self.nifti = nibabel.load(self.path) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pass | ||
|
||
# dummy attrs to be compatible with h5py/z5py/zarr API | ||
# alternatively we could also map the header to attributes | ||
@property | ||
def attrs(self): | ||
return {} | ||
|
||
def __getitem__(self, key): | ||
if key != "data": | ||
raise KeyError(f"Could not find key {key}") | ||
return NiftiDataset(self.nifti) | ||
|
||
def __iter__(self): | ||
yield "data" | ||
|
||
def __len__(self): | ||
return 1 | ||
|
||
def __contains__(self, name): | ||
return name == "data" | ||
|
||
|
||
class NiftiDataset: | ||
def __init__(self, data): | ||
self._data = data | ||
|
||
@property | ||
def dtype(self): | ||
return self.data.get_data_dtype() | ||
|
||
@property | ||
def ndim(self): | ||
return self._data.ndim | ||
|
||
@property | ||
def chunks(self): | ||
return None | ||
|
||
@property | ||
def shape(self): | ||
return self._data.shape[::-1] | ||
|
||
def __getitem__(self, 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): | ||
return np.prod(self._data.shape) | ||
|
||
# dummy attrs to be compatible with h5py/z5py/zarr API | ||
# alternatively we could also map the header to attributes | ||
@property | ||
def attrs(self): | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import unittest | ||
from glob import glob | ||
|
||
import numpy as np | ||
|
||
try: | ||
import nibabel | ||
except ImportError: | ||
nibabel = None | ||
|
||
|
||
@unittest.skipIf(nibabel is None, "Needs nibabel") | ||
class TestNiftiWrapper(unittest.TestCase): | ||
|
||
def _check_data(self, expected_data, f): | ||
dset = f["data"] | ||
|
||
self.assertEqual(expected_data.shape, dset.shape) | ||
shape = dset.shape | ||
|
||
# bounding boxes for testing sub-sampling | ||
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)), | ||
tuple(slice(shape[i] // 2, None) if d == i else slice(None) for d in range(dset.ndim)) | ||
]) | ||
bbs.append( | ||
tuple(slice(shape[i] // 4, 3 * shape[i] // 4) for i in range(dset.ndim)) | ||
) | ||
|
||
for bb in bbs: | ||
self.assertTrue(np.allclose(dset[bb], expected_data[bb])) | ||
|
||
def test_read_nifti(self): | ||
from elf.io import open_file | ||
from nibabel.testing import data_path | ||
|
||
paths = glob(os.path.join(data_path, "*.nii")) | ||
for path in paths: | ||
expected_data = np.asarray(nibabel.load(path).dataobj).T | ||
# the resampled image causes errors | ||
if os.path.basename(path).startswith("resampled"): | ||
continue | ||
with open_file(path, "r") as f: | ||
self._check_data(expected_data, f) | ||
|
||
def test_read_nifti_compressed(self): | ||
from elf.io import open_file | ||
from nibabel.testing import data_path | ||
|
||
paths = glob(os.path.join(data_path, "*.nii.gz")) | ||
for path in paths: | ||
expected_data = np.asarray(nibabel.load(path).dataobj).T | ||
with open_file(path, "r") as f: | ||
self._check_data(expected_data, f) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |