@@ -105,6 +105,59 @@ class TestSpm99AnalyzeImage(test_analyze.TestAnalyzeImage):
105
105
test_analyze .TestAnalyzeImage .test_data_hdr_cache
106
106
))
107
107
108
+ @scipy_skip
109
+ def test_mat_read (self ):
110
+ # Test mat file reading and writing for the SPM analyze types
111
+ img_klass = self .image_class
112
+ arr = np .arange (24 ).reshape ((2 ,3 ,4 ))
113
+ aff = np .diag ([2 ,3 ,4 ,1 ]) # no LR flip in affine
114
+ img = img_klass (arr , aff )
115
+ fm = img .file_map
116
+ for key , value in fm .items ():
117
+ value .fileobj = BytesIO ()
118
+ # Test round trip
119
+ img .to_file_map ()
120
+ r_img = img_klass .from_file_map (fm )
121
+ assert_array_equal (r_img .get_data (), arr )
122
+ assert_array_equal (r_img .get_affine (), aff )
123
+ # mat files are for matlab and have 111 voxel origins. We need to
124
+ # adjust for that, when loading and saving. Check for signs of that in
125
+ # the saved mat file
126
+ mat_fileobj = img .file_map ['mat' ].fileobj
127
+ from scipy .io import loadmat , savemat
128
+ mat_fileobj .seek (0 )
129
+ mats = loadmat (mat_fileobj )
130
+ assert_true ('M' in mats and 'mat' in mats )
131
+ from_111 = np .eye (4 )
132
+ from_111 [:3 ,3 ] = - 1
133
+ to_111 = np .eye (4 )
134
+ to_111 [:3 ,3 ] = 1
135
+ assert_array_equal (mats ['mat' ], np .dot (aff , from_111 ))
136
+ # The M matrix does not include flips, so if we only
137
+ # have the M matrix in the mat file, and we have default flipping, the
138
+ # mat resulting should have a flip. The 'mat' matrix does include flips
139
+ # and so should be unaffected by the flipping. If both are present we
140
+ # prefer the the 'mat' matrix.
141
+ assert_true (img .get_header ().default_x_flip ) # check the default
142
+ flipper = np .diag ([- 1 ,1 ,1 ,1 ])
143
+ assert_array_equal (mats ['M' ], np .dot (aff , np .dot (flipper , from_111 )))
144
+ mat_fileobj .seek (0 )
145
+ savemat (mat_fileobj , dict (M = np .diag ([3 ,4 ,5 ,1 ]), mat = np .diag ([6 ,7 ,8 ,1 ])))
146
+ # Check we are preferring the 'mat' matrix
147
+ r_img = img_klass .from_file_map (fm )
148
+ assert_array_equal (r_img .get_data (), arr )
149
+ assert_array_equal (r_img .get_affine (),
150
+ np .dot (np .diag ([6 ,7 ,8 ,1 ]), to_111 ))
151
+ # But will use M if present
152
+ mat_fileobj .seek (0 )
153
+ mat_fileobj .truncate (0 )
154
+ savemat (mat_fileobj , dict (M = np .diag ([3 ,4 ,5 ,1 ])))
155
+ r_img = img_klass .from_file_map (fm )
156
+ assert_array_equal (r_img .get_data (), arr )
157
+ assert_array_equal (r_img .get_affine (),
158
+ np .dot (np .diag ([3 ,4 ,5 ,1 ]), np .dot (flipper , to_111 )))
159
+
160
+
108
161
109
162
def test_origin_affine ():
110
163
hdr = Spm99AnalyzeHeader ()
0 commit comments