Skip to content

Commit 773cbcf

Browse files
committed
NF - cache clipping values
Trying to avoid import-time use of np.finfo because of warnings in the code that this will increase import time.
1 parent 77b4600 commit 773cbcf

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

nibabel/casting.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ def int_clippers(flt_type, int_type):
3434

3535
# Cache clip values
3636
FLT_INT_CLIPS = {}
37-
for _ft in np.sctypes['float']:
38-
for _it in np.sctypes['int'] + np.sctypes['uint']:
39-
FLT_INT_CLIPS[_ft, _it] = int_clippers(_ft, _it)
4037

38+
def _cached_int_clippers(flt_type, int_type):
39+
if not (flt_type, int_type) in FLT_INT_CLIPS:
40+
FLT_INT_CLIPS[flt_type, int_type] = int_clippers(flt_type, int_type)
41+
return FLT_INT_CLIPS[(flt_type, int_type)]
4142

4243

4344
class RoundingError(Exception):
@@ -75,7 +76,8 @@ def nice_round(arr, int_type, nan2zero=True):
7576
"""
7677
arr = np.asarray(arr)
7778
flt_type = arr.dtype.type
78-
mn, mx = FLT_INT_CLIPS[flt_type, int_type]
79+
int_type = np.dtype(int_type).type
80+
mn, mx = _cached_int_clippers(flt_type, int_type)
7981
nans = np.isnan(arr)
8082
have_nans = np.any(nans)
8183
if not nan2zero and have_nans:

0 commit comments

Comments
 (0)