|
6 | 6 | # copyright and license terms.
|
7 | 7 | #
|
8 | 8 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
| 9 | +from __future__ import with_statement |
| 10 | + |
9 | 11 | import os
|
| 12 | +import gzip |
| 13 | +import bz2 |
10 | 14 |
|
11 | 15 | import numpy as np
|
12 | 16 |
|
13 | 17 | from ..externals.netcdf import netcdf_file as netcdf
|
14 | 18 |
|
15 | 19 | from .. import load, Nifti1Image
|
16 | 20 | from ..minc import MincFile
|
17 |
| -from ..spatialimages import ImageFileError |
| 21 | + |
18 | 22 | from nose.tools import (assert_true, assert_equal, assert_false, assert_raises)
|
19 | 23 | from numpy.testing import assert_array_equal, assert_array_almost_equal
|
| 24 | +from ..tmpdirs import InTemporaryDirectory |
20 | 25 | from ..testing import data_path
|
21 | 26 |
|
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], |
25 | 31 | [0, 2.0, 0, -20],
|
26 | 32 | [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) |
28 | 38 |
|
29 | 39 |
|
30 | 40 | def test_mincfile():
|
31 |
| - mnc = MincFile(netcdf(mnc_fname, 'r')) |
| 41 | + mnc = MincFile(netcdf(MINC_EXAMPLE['fname'], 'r')) |
32 | 42 | 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']) |
34 | 44 | 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']) |
36 | 46 | data = mnc.get_scaled_data()
|
37 |
| - assert_equal(data.shape, mnc_shape) |
| 47 | + assert_equal(data.shape, MINC_EXAMPLE['shape']) |
38 | 48 |
|
39 | 49 |
|
40 | 50 | def test_load():
|
41 | 51 | # Check highest level load of minc works
|
42 |
| - img = load(mnc_fname) |
| 52 | + img = load(MINC_EXAMPLE['fname']) |
43 | 53 | data = img.get_data()
|
44 |
| - assert_equal(data.shape, mnc_shape) |
| 54 | + assert_equal(data.shape, MINC_EXAMPLE['shape']) |
45 | 55 | # min, max, mean values from read in SPM2
|
46 | 56 | assert_array_almost_equal(data.min(), 0.20784314)
|
47 | 57 | 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) |
49 | 59 | # check if mnc can be converted to nifti
|
50 | 60 | 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']) |
52 | 62 | assert_array_equal(ni_img.get_data(), data)
|
53 | 63 |
|
54 | 64 |
|
55 | 65 | 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