Skip to content

Commit 01e60bc

Browse files
committed
Merge remote branch 'nipy/master'
2 parents e857411 + b37565c commit 01e60bc

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

nibabel/analyze.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,12 @@ def items(self):
680680
''' Return items from header data'''
681681
return zip(self.keys(), self.values())
682682

683-
def check_fix(self,
684-
logger=imageglobals.logger,
685-
error_level=imageglobals.error_level):
683+
def check_fix(self, logger=None, error_level=None):
686684
''' Check header data with checks '''
685+
if logger is None:
686+
logger = imageglobals.logger
687+
if error_level is None:
688+
error_level = imageglobals.error_level
687689
battrun = BatteryRunner(self.__class__._get_checks())
688690
self, reports = battrun.check_fix(self)
689691
for report in reports:

nibabel/imageglobals.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
# copyright and license terms.
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9-
''' Defaults for images and headers '''
9+
""" Defaults for images and headers
10+
11+
error_level is the problem level (see BatteryRunners) at which an error will be
12+
raised, by the batteryrunners ``log_raise`` method. Thus a level of 0 will
13+
result in an error for any problem at all, and a level of 50 will mean no errors
14+
will be raised (unless someone's put some strange problem_level > 50 code in).
15+
16+
``logger`` is the default logger (python log instance)
17+
18+
To set the log level (log message appears for problem of level >= log level),
19+
use e.g. ``logger.level = 40``
20+
"""
1021

1122
import logging
1223

1324
error_level = 40
14-
log_level = 30
15-
logger = logging.getLogger('nifti.global')
25+
logger = logging.getLogger('nibabel.global')
1626
logger.addHandler(logging.StreamHandler())

nibabel/tests/test_analyze.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
ImageDataError
5656
from ..analyze import AnalyzeHeader, AnalyzeImage
5757
from ..loadsave import read_img_data
58+
from .. import imageglobals
5859

5960
from ..testing import parametric, data_path, ParametricTestCase
6061

@@ -64,6 +65,7 @@
6465

6566
header_file = os.path.join(data_path, 'analyze.hdr')
6667

68+
PIXDIM0_MSG = 'pixdim[1,2,3] should be non-zero; setting 0 dims to 1'
6769

6870
def _log_chk(hdr, level):
6971
# utility function to check header checking / logging
@@ -189,8 +191,7 @@ def test_log_checks(self):
189191
hdr['pixdim'][1] = 0 # severity 30
190192
fhdr, message, raiser = _log_chk(hdr, 30)
191193
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)
194195
yield assert_raises(*raiser)
195196
# both
196197
hdr = HC()
@@ -205,6 +206,30 @@ def test_log_checks(self):
205206
'and setting to abs of pixdim values')
206207
yield assert_raises(*raiser)
207208

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+
208233
def test_datatype(self):
209234
ehdr = self.header_class()
210235
codes = self.header_class._data_type_codes

0 commit comments

Comments
 (0)