55
55
ImageDataError
56
56
from ..analyze import AnalyzeHeader , AnalyzeImage
57
57
from ..loadsave import read_img_data
58
+ from .. import imageglobals
58
59
59
60
from ..testing import parametric , data_path , ParametricTestCase
60
61
64
65
65
66
header_file = os .path .join (data_path , 'analyze.hdr' )
66
67
68
+ PIXDIM0_MSG = 'pixdim[1,2,3] should be non-zero; setting 0 dims to 1'
67
69
68
70
def _log_chk (hdr , level ):
69
71
# utility function to check header checking / logging
@@ -189,8 +191,7 @@ def test_log_checks(self):
189
191
hdr ['pixdim' ][1 ] = 0 # severity 30
190
192
fhdr , message , raiser = _log_chk (hdr , 30 )
191
193
yield assert_equal (fhdr ['pixdim' ][1 ], 1 )
192
- yield assert_equal (message , 'pixdim[1,2,3] should be '
193
- 'non-zero; setting 0 dims to 1' )
194
+ yield assert_equal (message , PIXDIM0_MSG )
194
195
yield assert_raises (* raiser )
195
196
# both
196
197
hdr = HC ()
@@ -205,6 +206,30 @@ def test_log_checks(self):
205
206
'and setting to abs of pixdim values' )
206
207
yield assert_raises (* raiser )
207
208
209
+ def test_logger_error (self ):
210
+ # Check that we can reset the logger and error level
211
+ HC = self .header_class
212
+ hdr = HC ()
213
+ # Make a new logger
214
+ str_io = StringIO ()
215
+ logger = logging .getLogger ('test.logger' )
216
+ logger .setLevel (30 ) # defaultish level
217
+ logger .addHandler (logging .StreamHandler (str_io ))
218
+ # Prepare an error
219
+ hdr ['pixdim' ][1 ] = 0 # severity 30
220
+ log_cache = imageglobals .logger , imageglobals .error_level
221
+ try :
222
+ # Check log message appears in new logger
223
+ imageglobals .logger = logger
224
+ hdr .copy ().check_fix ()
225
+ assert_equal (str_io .getvalue (), PIXDIM0_MSG + '\n ' )
226
+ # Check that error_level in fact causes error to be raised
227
+ imageglobals .error_level = 30
228
+ assert_raises (HeaderDataError , hdr .copy ().check_fix )
229
+ finally :
230
+ imageglobals .logger , imageglobals .error_level = log_cache
231
+
232
+
208
233
def test_datatype (self ):
209
234
ehdr = self .header_class ()
210
235
codes = self .header_class ._data_type_codes
0 commit comments