Skip to content

Commit 3d3381e

Browse files
committed
RF: Use catch_warnings context managers to catch overflow
1 parent 26bb120 commit 3d3381e

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

nibabel/volumeutils.py

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from __future__ import division, print_function
1111

1212
import sys
13-
import re
1413
import warnings
1514
import gzip
1615
from collections import OrderedDict
@@ -42,13 +41,6 @@
4241
#: file-like classes known to hold compressed data
4342
COMPRESSED_FILE_LIKES = (gzip.GzipFile, BZ2File)
4443

45-
_OVERFLOW_FILTER = (
46-
'ignore',
47-
re.compile(r'.*overflow.*', re.IGNORECASE | re.UNICODE),
48-
RuntimeWarning,
49-
re.compile(r'', re.UNICODE),
50-
0)
51-
5244

5345
class Recoder(object):
5446
''' class to return canonical code(s) from code or aliases
@@ -1342,32 +1334,36 @@ def _ftype4scaled_finite(tst_arr, slope, inter, direction='read',
13421334
tst_arr = np.atleast_1d(tst_arr)
13431335
slope = np.atleast_1d(slope)
13441336
inter = np.atleast_1d(inter)
1345-
warnings.filters.insert(0, _OVERFLOW_FILTER)
1346-
getattr(warnings, '_filters_mutated', lambda: None)() # PY2
1347-
# warnings._filters_mutated() # PY3
1348-
try:
1349-
for ftype in OK_FLOATS[def_ind:]:
1350-
tst_trans = tst_arr.copy()
1351-
slope = slope.astype(ftype)
1352-
inter = inter.astype(ftype)
1337+
overflow_filter = ('error', '.*overflow.*', RuntimeWarning)
1338+
for ftype in OK_FLOATS[def_ind:]:
1339+
tst_trans = tst_arr.copy()
1340+
slope = slope.astype(ftype)
1341+
inter = inter.astype(ftype)
1342+
try:
13531343
if direction == 'read': # as in reading of image from disk
13541344
if slope != 1.0:
1355-
tst_trans = tst_trans * slope
1345+
# Keep warning contexts small to reduce the odds of a race
1346+
with warnings.catch_warnings():
1347+
# Error on overflows to short circuit the logic
1348+
warnings.filterwarnings(*overflow_filter)
1349+
tst_trans = tst_trans * slope
13561350
if inter != 0.0:
1357-
tst_trans = tst_trans + inter
1351+
with warnings.catch_warnings():
1352+
warnings.filterwarnings(*overflow_filter)
1353+
tst_trans = tst_trans + inter
13581354
elif direction == 'write':
13591355
if inter != 0.0:
1360-
tst_trans = tst_trans - inter
1356+
with warnings.catch_warnings():
1357+
warnings.filterwarnings(*overflow_filter)
1358+
tst_trans = tst_trans - inter
13611359
if slope != 1.0:
1362-
tst_trans = tst_trans / slope
1360+
with warnings.catch_warnings():
1361+
warnings.filterwarnings(*overflow_filter)
1362+
tst_trans = tst_trans / slope
1363+
# Double-check that result is finite
13631364
if np.all(np.isfinite(tst_trans)):
13641365
return ftype
1365-
finally:
1366-
try:
1367-
warnings.filters.remove(_OVERFLOW_FILTER)
1368-
getattr(warnings, '_filters_mutated', lambda: None)() # PY2
1369-
# warnings._filters_mutated() # PY3
1370-
except ValueError:
1366+
except RuntimeWarning:
13711367
pass
13721368
raise ValueError('Overflow using highest floating point type')
13731369

0 commit comments

Comments
 (0)