|
8 | 8 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
9 | 9 | '''Tests for mghformat reading writing'''
|
10 | 10 | from __future__ import with_statement
|
| 11 | + |
11 | 12 | import os
|
| 13 | +from io import StringIO, BytesIO |
| 14 | + |
12 | 15 | import numpy as np
|
13 | 16 | from .. import load, save, MGHImage
|
14 | 17 | from ..mghformat import MGHError
|
15 | 18 | from ...tmpdirs import InTemporaryDirectory
|
| 19 | +from ...py3k import unicode |
| 20 | +from ...fileholders import FileHolder |
| 21 | + |
16 | 22 | from ...testing import data_path
|
17 | 23 | from numpy.testing import assert_equal, assert_array_equal, \
|
18 | 24 | assert_array_almost_equal, assert_almost_equal, assert_raises
|
@@ -47,7 +53,7 @@ def test_read_mgh():
|
47 | 53 | assert_equal(h['goodRASFlag'], 1)
|
48 | 54 | assert_array_equal(h['dims'], [3, 4, 5, 2])
|
49 | 55 | assert_array_almost_equal(h['mrparms'], [2.0, 0.0, 0.0, 0.0])
|
50 |
| - assert_array_almost_equal(h.get_zooms(), zooms) |
| 56 | + assert_array_almost_equal(h.get_zooms(), 1) |
51 | 57 | assert_array_almost_equal(h.get_vox2ras(), v2r)
|
52 | 58 | assert_array_almost_equal(h.get_vox2ras_tkr(), v2rtkr)
|
53 | 59 |
|
@@ -150,3 +156,45 @@ def test_filename_exts():
|
150 | 156 | img_back = load(fname)
|
151 | 157 | assert_array_equal(img_back.get_data(), v)
|
152 | 158 | del img_back
|
| 159 | + |
| 160 | + |
| 161 | +def _mgh_rt(img, fobj): |
| 162 | + file_map = {'image': FileHolder(fileobj=fobj)} |
| 163 | + img.to_file_map(file_map) |
| 164 | + return MGHImage.from_file_map(file_map) |
| 165 | + |
| 166 | + |
| 167 | +def test_header_updating(): |
| 168 | + # Don't update the header information if the affine doesn't change. |
| 169 | + # Luckily the test.mgz dataset had a bad set of cosine vectors, so these |
| 170 | + # will be changed if the affine gets updated |
| 171 | + mgz_path = os.path.join(data_path, 'test.mgz') |
| 172 | + mgz = load(mgz_path) |
| 173 | + hdr = mgz.get_header() |
| 174 | + # Test against mri_info output |
| 175 | + exp_aff = np.loadtxt(StringIO(unicode(""" |
| 176 | + 1.0000 2.0000 3.0000 -13.0000 |
| 177 | + 2.0000 3.0000 1.0000 -11.5000 |
| 178 | + 3.0000 1.0000 2.0000 -11.5000 |
| 179 | + 0.0000 0.0000 0.0000 1.0000"""))) |
| 180 | + assert_almost_equal(mgz.get_affine(), exp_aff, 6) |
| 181 | + assert_almost_equal(hdr.get_affine(), exp_aff, 6) |
| 182 | + # Test that initial wonky header elements have not changed |
| 183 | + assert_equal(hdr['delta'], 1) |
| 184 | + assert_almost_equal(hdr['Mdc'], exp_aff[:3, :3].T) |
| 185 | + # Save, reload, same thing |
| 186 | + img_fobj = BytesIO() |
| 187 | + mgz2 = _mgh_rt(mgz, img_fobj) |
| 188 | + hdr2 = mgz2.get_header() |
| 189 | + assert_almost_equal(hdr2.get_affine(), exp_aff, 6) |
| 190 | + assert_equal(hdr2['delta'], 1) |
| 191 | + # Change affine, change underlying header info |
| 192 | + exp_aff_d = exp_aff.copy() |
| 193 | + exp_aff_d[0, -1] = -14 |
| 194 | + # This will (probably) become part of the official API |
| 195 | + mgz2._affine[:] = exp_aff_d |
| 196 | + mgz2.update_header() |
| 197 | + assert_almost_equal(hdr2.get_affine(), exp_aff_d, 6) |
| 198 | + RZS = exp_aff_d[:3, :3] |
| 199 | + assert_almost_equal(hdr2['delta'], np.sqrt(np.sum(RZS ** 2, axis=0))) |
| 200 | + assert_almost_equal(hdr2['Mdc'], (RZS / hdr2['delta']).T) |
0 commit comments