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
19
25
20
- # sample voxel to ras matrix
26
+ # sample voxel to ras matrix (mri_info --vox2ras)
21
27
v2r = np .array ([[1 , 2 , 3 , - 13 ], [2 , 3 , 1 , - 11.5 ],
22
28
[3 , 1 , 2 , - 11.5 ], [0 , 0 , 0 , 1 ]], dtype = np .float32 )
29
+ # sample voxel to ras - tkr matrix (mri_info --vox2ras-tkr)
30
+ v2rtkr = np .array ([[- 1.0 , 0.0 , 0.0 , 1.5 ],
31
+ [0.0 , 0.0 , 1.0 , - 2.5 ],
32
+ [0.0 , - 1.0 , 0.0 , 2.0 ],
33
+ [0.0 , 0.0 , 0.0 , 1.0 ]], dtype = np .float32 )
23
34
24
35
25
36
def test_read_mgh ():
@@ -38,7 +49,9 @@ def test_read_mgh():
38
49
assert_equal (h ['goodRASFlag' ], 1 )
39
50
assert_array_equal (h ['dims' ], [3 , 4 , 5 , 2 ])
40
51
assert_array_almost_equal (h ['mrparms' ], [2.0 , 0.0 , 0.0 , 0.0 ])
52
+ assert_array_almost_equal (h .get_zooms (), 1 )
41
53
assert_array_almost_equal (h .get_vox2ras (), v2r )
54
+ assert_array_almost_equal (h .get_vox2ras_tkr (), v2rtkr )
42
55
43
56
# data. will be different for your own mri_volsynth invocation
44
57
v = mgz .get_data ()
@@ -139,3 +152,61 @@ def test_filename_exts():
139
152
img_back = load (fname )
140
153
assert_array_equal (img_back .get_data (), v )
141
154
del img_back
155
+
156
+
157
+ def _mgh_rt (img , fobj ):
158
+ file_map = {'image' : FileHolder (fileobj = fobj )}
159
+ img .to_file_map (file_map )
160
+ return MGHImage .from_file_map (file_map )
161
+
162
+
163
+ def test_header_updating ():
164
+ # Don't update the header information if the affine doesn't change.
165
+ # Luckily the test.mgz dataset had a bad set of cosine vectors, so these
166
+ # will be changed if the affine gets updated
167
+ mgz_path = os .path .join (data_path , 'test.mgz' )
168
+ mgz = load (mgz_path )
169
+ hdr = mgz .get_header ()
170
+ # Test against mri_info output
171
+ exp_aff = np .loadtxt (StringIO (unicode ("""
172
+ 1.0000 2.0000 3.0000 -13.0000
173
+ 2.0000 3.0000 1.0000 -11.5000
174
+ 3.0000 1.0000 2.0000 -11.5000
175
+ 0.0000 0.0000 0.0000 1.0000""" )))
176
+ assert_almost_equal (mgz .get_affine (), exp_aff , 6 )
177
+ assert_almost_equal (hdr .get_affine (), exp_aff , 6 )
178
+ # Test that initial wonky header elements have not changed
179
+ assert_equal (hdr ['delta' ], 1 )
180
+ assert_almost_equal (hdr ['Mdc' ], exp_aff [:3 , :3 ].T )
181
+ # Save, reload, same thing
182
+ img_fobj = BytesIO ()
183
+ mgz2 = _mgh_rt (mgz , img_fobj )
184
+ hdr2 = mgz2 .get_header ()
185
+ assert_almost_equal (hdr2 .get_affine (), exp_aff , 6 )
186
+ assert_equal (hdr2 ['delta' ], 1 )
187
+ # Change affine, change underlying header info
188
+ exp_aff_d = exp_aff .copy ()
189
+ exp_aff_d [0 , - 1 ] = - 14
190
+ # This will (probably) become part of the official API
191
+ mgz2 ._affine [:] = exp_aff_d
192
+ mgz2 .update_header ()
193
+ assert_almost_equal (hdr2 .get_affine (), exp_aff_d , 6 )
194
+ RZS = exp_aff_d [:3 , :3 ]
195
+ assert_almost_equal (hdr2 ['delta' ], np .sqrt (np .sum (RZS ** 2 , axis = 0 )))
196
+ assert_almost_equal (hdr2 ['Mdc' ], (RZS / hdr2 ['delta' ]).T )
197
+
198
+
199
+ def test_cosine_order ():
200
+ # Test we are interpreting the cosine order right
201
+ data = np .arange (60 ).reshape ((3 , 4 , 5 )).astype (np .int32 )
202
+ aff = np .diag ([2. , 3 , 4 , 1 ])
203
+ aff [0 ] = [2 , 1 , 0 , 10 ]
204
+ img = MGHImage (data , aff )
205
+ assert_almost_equal (img .get_affine (), aff , 6 )
206
+ img_fobj = BytesIO ()
207
+ img2 = _mgh_rt (img , img_fobj )
208
+ hdr2 = img2 .get_header ()
209
+ RZS = aff [:3 , :3 ]
210
+ zooms = np .sqrt (np .sum (RZS ** 2 , axis = 0 ))
211
+ assert_almost_equal (hdr2 ['Mdc' ], (RZS / zooms ).T )
212
+ assert_almost_equal (hdr2 ['delta' ], zooms )
0 commit comments