|
| 1 | +# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 4 | +# |
| 5 | +# See COPYING file distributed along with the NiBabel package for the |
| 6 | +# copyright and license terms. |
| 7 | +# |
| 8 | +### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## |
| 9 | +""" Test we can correctly import example MINC2_PATH files |
| 10 | +""" |
| 11 | +from __future__ import print_function, absolute_import |
| 12 | + |
| 13 | +import os |
| 14 | +from os.path import join as pjoin |
| 15 | + |
| 16 | +import numpy as np |
| 17 | + |
| 18 | +from nibabel.optpkg import optional_package |
| 19 | + |
| 20 | +h5py, have_h5py, setup_module = optional_package('h5py') |
| 21 | + |
| 22 | +from .nibabel_data import get_nibabel_data, needs_nibabel_data |
| 23 | +from .. import load as top_load, Nifti1Image |
| 24 | + |
| 25 | +from nose.tools import assert_equal |
| 26 | +from numpy.testing import (assert_array_equal, assert_almost_equal) |
| 27 | + |
| 28 | +NIBABEL_DATA = get_nibabel_data() |
| 29 | +if NIBABEL_DATA is not None: |
| 30 | + MINC2_PATH = pjoin(NIBABEL_DATA, 'nitest-minc2') |
| 31 | + |
| 32 | + |
| 33 | +def _make_affine(coses, zooms, starts): |
| 34 | + R = np.column_stack(coses) |
| 35 | + Z = np.diag(zooms) |
| 36 | + affine = np.eye(4) |
| 37 | + affine[:3, :3] = np.dot(R, Z) |
| 38 | + affine[:3, 3] = np.dot(R, starts) |
| 39 | + return affine |
| 40 | + |
| 41 | + |
| 42 | +class TestEPIFrame(object): |
| 43 | + opener = staticmethod(top_load) |
| 44 | + x_cos = [1, 0, 0] |
| 45 | + y_cos = [0., 1, 0] |
| 46 | + z_cos = [0, 0, 1] |
| 47 | + zooms = [-0.8984375, -0.8984375, 3.] |
| 48 | + starts = [117.25609125, 138.89861125, -54.442028] |
| 49 | + example_params = dict( |
| 50 | + fname = os.path.join(MINC2_PATH, 'mincex_EPI-frame.mnc'), |
| 51 | + shape = (40, 256, 256), |
| 52 | + type = np.int16, |
| 53 | + affine = _make_affine((z_cos, y_cos, x_cos), |
| 54 | + zooms[::-1], |
| 55 | + starts[::-1]), |
| 56 | + zooms = [abs(v) for v in zooms[::-1]], |
| 57 | + # These values from mincstats |
| 58 | + min = 0., |
| 59 | + max = 1273, |
| 60 | + mean = 93.52085367) |
| 61 | + |
| 62 | + @needs_nibabel_data('nitest-minc2') |
| 63 | + def test_load(self): |
| 64 | + # Check highest level load of minc works |
| 65 | + img = self.opener(self.example_params['fname']) |
| 66 | + assert_equal(img.shape, self.example_params['shape']) |
| 67 | + assert_almost_equal(img.header.get_zooms(), |
| 68 | + self.example_params['zooms'], 5) |
| 69 | + assert_almost_equal(img.affine, self.example_params['affine'], 4) |
| 70 | + assert_equal(img.get_data_dtype().type, self.example_params['type']) |
| 71 | + # Check correspondence of data and recorded shape |
| 72 | + data = img.get_data() |
| 73 | + assert_equal(data.shape, self.example_params['shape']) |
| 74 | + # min, max, mean values from read in SPM2 |
| 75 | + assert_almost_equal(data.min(), self.example_params['min'], 4) |
| 76 | + assert_almost_equal(data.max(), self.example_params['max'], 4) |
| 77 | + assert_almost_equal(data.mean(), self.example_params['mean'], 4) |
| 78 | + # check if mnc can be converted to nifti |
| 79 | + ni_img = Nifti1Image.from_image(img) |
| 80 | + assert_almost_equal(ni_img.get_affine(), |
| 81 | + self.example_params['affine'], 2) |
| 82 | + assert_array_equal(ni_img.get_data(), data) |
| 83 | + |
| 84 | + |
| 85 | +class TestB0(TestEPIFrame): |
| 86 | + x_cos = [0.9970527523765, 0., 0.0767190261828617] |
| 87 | + y_cos = [0., 1., -6.9388939e-18] |
| 88 | + z_cos = [-0.0767190261828617, 6.9184432614435e-18, 0.9970527523765] |
| 89 | + zooms = [-0.8984375, -0.8984375, 6.49999990444107] |
| 90 | + starts = [105.473101260826, 151.74885125, -61.8714747993248] |
| 91 | + example_params = dict( |
| 92 | + fname = os.path.join(MINC2_PATH, 'mincex_diff-B0.mnc'), |
| 93 | + shape = (19, 256, 256), |
| 94 | + type = np.int16, |
| 95 | + affine = _make_affine((z_cos, y_cos, x_cos), |
| 96 | + zooms[::-1], |
| 97 | + starts[::-1]), |
| 98 | + zooms = [abs(v) for v in zooms[::-1]], |
| 99 | + # These values from mincstats |
| 100 | + min = 4.566971917, |
| 101 | + max = 3260.121093, |
| 102 | + mean = 163.8305553) |
| 103 | + |
| 104 | + |
| 105 | +class TestFA(TestEPIFrame): |
| 106 | + example_params = TestB0.example_params.copy() |
| 107 | + new_params = dict( |
| 108 | + fname = os.path.join(MINC2_PATH, 'mincex_diff-FA.mnc'), |
| 109 | + # These values from mincstats |
| 110 | + min = 0.008068881038, |
| 111 | + max = 1.224754546, |
| 112 | + mean = 0.7520087469) |
| 113 | + example_params.update(new_params) |
| 114 | + |
| 115 | + |
| 116 | +class TestGado(TestEPIFrame): |
| 117 | + x_cos = [0.999695413509548, -0.0174524064372835, 0.0174497483512505] |
| 118 | + y_cos = [0.0174497483512505, 0.999847695156391, 0.000304586490452135] |
| 119 | + z_cos = [-0.0174524064372835, 0., 0.999847695156391] |
| 120 | + zooms = [1, -1, -1] |
| 121 | + starts = [-75.76775, 115.80462, 81.38605] |
| 122 | + example_params = dict( |
| 123 | + fname = os.path.join(MINC2_PATH, 'mincex_gado-contrast.mnc'), |
| 124 | + shape = (100, 170, 146), |
| 125 | + type = np.int16, |
| 126 | + affine = _make_affine((z_cos, y_cos, x_cos), |
| 127 | + zooms[::-1], |
| 128 | + starts[::-1]), |
| 129 | + zooms = [abs(v) for v in zooms[::-1]], |
| 130 | + # These values from mincstats |
| 131 | + min = 0, |
| 132 | + max = 938668.8698, |
| 133 | + mean = 128169.3488) |
| 134 | + |
| 135 | + |
| 136 | +class TestT1(TestEPIFrame): |
| 137 | + x_cos = [1, 0, 0] |
| 138 | + y_cos = [0, 1, 0] |
| 139 | + z_cos = [0, 0, 1] |
| 140 | + zooms = [1, 1, 1] |
| 141 | + starts = [-90, -126, -12] |
| 142 | + example_params = dict( |
| 143 | + fname = os.path.join(MINC2_PATH, 'mincex_t1.mnc'), |
| 144 | + shape = (110, 217, 181), |
| 145 | + type = np.int16, |
| 146 | + affine = _make_affine((z_cos, y_cos, x_cos), |
| 147 | + zooms[::-1], |
| 148 | + starts[::-1]), |
| 149 | + zooms = [abs(v) for v in zooms[::-1]], |
| 150 | + # These values from mincstats |
| 151 | + min = 0, |
| 152 | + max = 100, |
| 153 | + mean = 23.1659928) |
| 154 | + |
| 155 | + |
| 156 | +class TestPD(TestEPIFrame): |
| 157 | + example_params = TestT1.example_params.copy() |
| 158 | + new_params = dict( |
| 159 | + fname = os.path.join(MINC2_PATH, 'mincex_pd.mnc'), |
| 160 | + # These values from mincstats |
| 161 | + min = 0, |
| 162 | + max = 102.5024482, |
| 163 | + mean = 23.82625718) |
| 164 | + example_params.update(new_params) |
| 165 | + |
| 166 | + |
| 167 | +class TestMask(TestEPIFrame): |
| 168 | + example_params = TestT1.example_params.copy() |
| 169 | + new_params = dict( |
| 170 | + fname = os.path.join(MINC2_PATH, 'mincex_mask.mnc'), |
| 171 | + type = np.uint8, |
| 172 | + # These values from mincstats |
| 173 | + min = 0, |
| 174 | + max = 1, |
| 175 | + mean = 0.3817466618) |
| 176 | + example_params.update(new_params) |
0 commit comments