Skip to content

Commit b29130b

Browse files
committed
Merge branch 'compressed-minc' into main-master
* compressed-minc: RF - slightly simplify close method NF - testing compressed reading of minc files
2 parents 4c9422f + 7c75fde commit b29130b

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

nibabel/externals/netcdf.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,15 @@ def __setattr__(self, attr, value):
285285

286286
def close(self):
287287
"""Closes the NetCDF file."""
288-
if not self.fp.closed:
289-
try:
290-
self.flush()
291-
finally:
292-
self.fp.close()
288+
try:
289+
if self.fp.closed:
290+
return
291+
except AttributeError: # gzip files don't have closed attr
292+
pass
293+
try:
294+
self.flush()
295+
finally:
296+
self.fp.close()
293297
__del__ = close
294298

295299
def createDimension(self, name, length):

nibabel/minc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class MincImage(SpatialImage):
191191
the MINC file on load.
192192
'''
193193
files_types = (('image', '.mnc'),)
194+
_compressed_exts = ('.gz', '.bz2')
194195

195196
class ImageArrayProxy(object):
196197
''' Minc implemention of array proxy protocol
@@ -209,7 +210,6 @@ def __array__(self):
209210
self._data = self.minc_file.get_scaled_data()
210211
return self._data
211212

212-
213213
@classmethod
214214
def from_file_map(klass, file_map):
215215
fobj = file_map['image'].get_prepare_fileobj()

nibabel/tests/test_minc.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,73 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9+
from __future__ import with_statement
10+
911
import os
12+
import gzip
13+
import bz2
1014

1115
import numpy as np
1216

1317
from ..externals.netcdf import netcdf_file as netcdf
1418

1519
from .. import load, Nifti1Image
1620
from ..minc import MincFile
17-
from ..spatialimages import ImageFileError
21+
1822
from nose.tools import (assert_true, assert_equal, assert_false, assert_raises)
1923
from numpy.testing import assert_array_equal, assert_array_almost_equal
24+
from ..tmpdirs import InTemporaryDirectory
2025
from ..testing import data_path
2126

22-
mnc_fname = os.path.join(data_path, 'tiny.mnc')
23-
mnc_shape = (10,20,20)
24-
mnc_affine = np.array([[0, 0, 2.0, -20],
27+
MINC_EXAMPLE = dict(
28+
fname = os.path.join(data_path, 'tiny.mnc'),
29+
shape = (10,20,20),
30+
affine = np.array([[0, 0, 2.0, -20],
2531
[0, 2.0, 0, -20],
2632
[2.0, 0, 0, -10],
27-
[0, 0, 0, 1]])
33+
[0, 0, 0, 1]]),
34+
# These values from SPM2
35+
min = 0.20784314,
36+
max = 0.74901961,
37+
mean = 0.60602819)
2838

2939

3040
def test_mincfile():
31-
mnc = MincFile(netcdf(mnc_fname, 'r'))
41+
mnc = MincFile(netcdf(MINC_EXAMPLE['fname'], 'r'))
3242
assert_equal(mnc.get_data_dtype().type, np.uint8)
33-
assert_equal(mnc.get_data_shape(), mnc_shape)
43+
assert_equal(mnc.get_data_shape(), MINC_EXAMPLE['shape'])
3444
assert_equal(mnc.get_zooms(), (2.0, 2.0, 2.0))
35-
assert_array_equal(mnc.get_affine(), mnc_affine)
45+
assert_array_equal(mnc.get_affine(), MINC_EXAMPLE['affine'])
3646
data = mnc.get_scaled_data()
37-
assert_equal(data.shape, mnc_shape)
47+
assert_equal(data.shape, MINC_EXAMPLE['shape'])
3848

3949

4050
def test_load():
4151
# Check highest level load of minc works
42-
img = load(mnc_fname)
52+
img = load(MINC_EXAMPLE['fname'])
4353
data = img.get_data()
44-
assert_equal(data.shape, mnc_shape)
54+
assert_equal(data.shape, MINC_EXAMPLE['shape'])
4555
# min, max, mean values from read in SPM2
4656
assert_array_almost_equal(data.min(), 0.20784314)
4757
assert_array_almost_equal(data.max(), 0.74901961)
48-
np.testing.assert_array_almost_equal(data.mean(), 0.60602819)
58+
assert_array_almost_equal(data.mean(), 0.60602819)
4959
# check if mnc can be converted to nifti
5060
ni_img = Nifti1Image.from_image(img)
51-
assert_array_equal(ni_img.get_affine(), mnc_affine)
61+
assert_array_equal(ni_img.get_affine(), MINC_EXAMPLE['affine'])
5262
assert_array_equal(ni_img.get_data(), data)
5363

5464

5565
def test_compressed():
56-
# we can't read minc compreesed, raise error
57-
assert_raises(ImageFileError, load, 'test.mnc.gz')
58-
assert_raises(ImageFileError, load, 'test.mnc.bz2')
66+
# we can read minc compressed
67+
content = open(MINC_EXAMPLE['fname'], 'rb').read()
68+
openers_exts = ((gzip.open, '.gz'), (bz2.BZ2File, '.bz2'))
69+
with InTemporaryDirectory():
70+
for opener, ext in openers_exts:
71+
fname = 'test.mnc' + ext
72+
fobj = opener(fname, 'wb')
73+
fobj.write(content)
74+
fobj.close()
75+
img = load(fname)
76+
data = img.get_data()
77+
assert_array_almost_equal(data.mean(), 0.60602819)
78+
del img

0 commit comments

Comments
 (0)