@@ -169,6 +169,44 @@ def test_nifti_log_checks(self):
169
169
assert_equal (message , 'sform_code -1 not valid; '
170
170
'setting to 0' )
171
171
172
+ def test_freesurfer_hack (self ):
173
+ # For large vector images, Freesurfer appears to set dim[1] to -1 and
174
+ # then use glmin for the vector length (an i4)
175
+ HC = self .header_class
176
+ # The standard case
177
+ hdr = HC ()
178
+ hdr .set_data_shape ((2 , 3 , 4 ))
179
+ assert_equal (hdr .get_data_shape (), (2 , 3 , 4 ))
180
+ assert_equal (hdr ['glmin' ], 0 )
181
+ # Just left of the freesurfer case
182
+ dim_type = hdr .template_dtype ['dim' ].base
183
+ glmin = hdr .template_dtype ['glmin' ].base
184
+ too_big = int (np .iinfo (dim_type ).max ) + 1
185
+ hdr .set_data_shape ((too_big - 1 , 1 , 1 ))
186
+ assert_equal (hdr .get_data_shape (), (too_big - 1 , 1 , 1 ))
187
+ # The freesurfer case
188
+ hdr .set_data_shape ((too_big , 1 , 1 ))
189
+ assert_equal (hdr .get_data_shape (), (too_big , 1 , 1 ))
190
+ assert_array_equal (hdr ['dim' ][:4 ], [3 , - 1 , 1 , 1 ])
191
+ assert_equal (hdr ['glmin' ], too_big )
192
+ # This only works for the case of a 3D with -1, 1, 1
193
+ assert_raises (HeaderDataError , hdr .set_data_shape , (too_big ,))
194
+ assert_raises (HeaderDataError , hdr .set_data_shape , (too_big ,1 ))
195
+ assert_raises (HeaderDataError , hdr .set_data_shape , (too_big ,1 ,2 ))
196
+ assert_raises (HeaderDataError , hdr .set_data_shape , (too_big ,2 ,1 ))
197
+ assert_raises (HeaderDataError , hdr .set_data_shape , (1 , too_big ))
198
+ assert_raises (HeaderDataError , hdr .set_data_shape , (1 , too_big , 1 ))
199
+ assert_raises (HeaderDataError , hdr .set_data_shape , (1 , 1 , too_big ))
200
+ # Outside range of glmin raises error
201
+ far_too_big = int (np .iinfo (glmin ).max ) + 1
202
+ hdr .set_data_shape ((far_too_big - 1 , 1 , 1 ))
203
+ assert_equal (hdr .get_data_shape (), (far_too_big - 1 , 1 , 1 ))
204
+ assert_raises (HeaderDataError , hdr .set_data_shape , (far_too_big ,1 ,1 ))
205
+ # glmin of zero raises error (implausible vector length)
206
+ hdr .set_data_shape ((- 1 ,1 ,1 ))
207
+ hdr ['glmin' ] = 0
208
+ assert_raises (HeaderDataError , hdr .get_data_shape )
209
+
172
210
173
211
class TestNifti1SingleHeader (TestNifti1PairHeader ):
174
212
0 commit comments