Skip to content

Commit c52c362

Browse files
MAINT: Implement lazy-loading.
'import ufunclab' was taking up to 5 seconds, so try lazy-loading.
1 parent c978126 commit c52c362

File tree

1 file changed

+92
-44
lines changed

1 file changed

+92
-44
lines changed

ufunclab/__init__.py

+92-44
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,96 @@
11
"""
22
NumPy ufuncs and utilities.
33
"""
4+
import importlib as _imp
5+
# To allow moving 'erfcx' from the 'normal' submodule to the
6+
# top-level namespace here in __init__.py, 'normal' is not
7+
# lazy-loaded.
8+
from . import normal
9+
from .normal import erfcx
10+
del normal.erfcx
411

5-
from ._logfact import logfactorial
6-
from ._loggamma1p import loggamma1p
7-
from ._issnan import issnan
8-
from ._abs_squared import abs_squared
9-
from ._cabssq import cabssq
10-
from ._log1p import log1p_theorem4, log1p_doubledouble
11-
from ._debye1 import debye1
12-
from ._expint1 import expint1, logexpint1
13-
from ._pow1pm1 import pow1pm1
14-
from ._logistic import logistic, logistic_deriv, log_logistic, swish
15-
from ._ramp import hyperbolic_ramp, exponential_ramp
16-
from ._yeo_johnson import yeo_johnson, inv_yeo_johnson
17-
from ._cross import cross3, cross2
12+
# To allow `op` to be created here, ._first is not lazy-loaded.
13+
# It is imported here to have access to the constants _LT, _LE, etc.
1814
from ._first import first, argfirst, _LT, _LE, _EQ, _NE, _GT, _GE
19-
from ._searchsorted import searchsortedl, searchsortedr
20-
from ._peaktopeak import peaktopeak
21-
from ._minmax import argmin, argmax, minmax, argminmax, min_argmin, max_argmax
22-
from ._multivariate_logbeta import multivariate_logbeta
23-
from ._means import gmean, hmean
24-
from ._meanvar import meanvar
25-
from ._corr import pearson_corr
26-
from ._wjaccard import wjaccard
27-
from ._mad import mad, rmad, gini
28-
from ._vnorm import vnorm, rms, vdot
29-
from ._tri_area import tri_area, tri_area_indexed
30-
from ._backlash import backlash
31-
from ._fillnan1d import fillnan1d
32-
from ._linear_interp1d import linear_interp1d
33-
from ._deadzone import deadzone
34-
from ._trapezoid_pulse import trapezoid_pulse
35-
from ._hysteresis_relay import hysteresis_relay
36-
from ._all_same import all_same
37-
from ._sosfilter import sosfilter, sosfilter_ic, sosfilter_ic_contig
15+
import numpy as _np
3816

39-
from ._step import step, linearstep, smoothstep3, invsmoothstep3, smoothstep5
40-
from ._next import next_greater, next_less
4117

42-
from ._gendot_wrap import gendot
43-
from ._ufunc_inspector import ufunc_inspector
44-
from .normal import erfcx
45-
from . import normal
46-
# For the public API, we want erfcx in the top-level module
47-
# instead of in the 'normal' submodule.
48-
del normal.erfcx
49-
from . import semivar
50-
from ._version import __version__
18+
# The keys of this dict are in modules that are lazy-loaded.
19+
_name_to_module = {
20+
'logfactorial': '._logfact',
21+
'loggamma1p': '._loggamma1p',
22+
'issnan': '._issnan',
23+
'abs_squared': '._abs_squared',
24+
'cabssq': '._cabssq',
25+
'log1p_theorem4': '._log1p',
26+
'log1p_doubledouble': '._log1p',
27+
'debye1': '._debye1',
28+
'expint1': '._expint1',
29+
'logexpint1': '._expint1',
30+
'pow1pm1': '._pow1pm1',
31+
'logistic': '._logistic',
32+
'logistic_deriv': '._logistic',
33+
'log_logistic': '._logistic',
34+
'swish': '._logistic',
35+
'hyperbolic_ramp': '._ramp',
36+
'exponential_ramp': '._ramp',
37+
'yeo_johnson': '._yeo_johnson',
38+
'inv_yeo_johnson': '._yeo_johnson',
39+
'cross3': '._cross',
40+
'cross2': '._cross',
41+
'searchsortedl': '._searchsorted',
42+
'searchsortedr': '._searchsorted',
43+
'peaktopeak': '._peaktopeak',
44+
'argmin': '._minmax',
45+
'argmax': '._minmax',
46+
'minmax': '._minmax',
47+
'argminmax': '._minmax',
48+
'min_argmin': '._minmax',
49+
'max_argmax': '._minmax',
50+
'multivariate_logbeta': '._multivariate_logbeta',
51+
'gmean': '._means',
52+
'hmean': '._means',
53+
'meanvar': '._meanvar',
54+
'pearson_corr': '._corr',
55+
'wjaccard': '._wjaccard',
56+
'mad': '._mad',
57+
'rmad': '._mad',
58+
'gini': '._mad',
59+
'vnorm': '._vnorm',
60+
'rms': '._vnorm',
61+
'vdot': '._vnorm',
62+
'tri_area': '._tri_area',
63+
'tri_area_indexed': '._tri_area',
64+
'backlash': '._backlash',
65+
'fillnan1d': '._fillnan1d',
66+
'linear_interp1d': '._linear_interp1d',
67+
'deadzone': '._deadzone',
68+
'trapezoid_pulse': '._trapezoid_pulse',
69+
'hysteresis_relay': '._hysteresis_relay',
70+
'all_same': '._all_same',
71+
'sosfilter': '._sosfilter',
72+
'sosfilter_ic': '._sosfilter',
73+
'sosfilter_ic_contig': '._sosfilter',
74+
'step': '._step',
75+
'linearstep': '._step',
76+
'smoothstep3': '._step',
77+
'invsmoothstep3': '._step',
78+
'smoothstep5': '._step',
79+
'next_greater': '._next',
80+
'next_less': '._next',
81+
'gendot': '._gendot_wrap',
82+
'ufunc_inspector': '._ufunc_inspector',
83+
'__version__': '._version',
84+
}
5185

52-
import numpy as _np
86+
87+
def __getattr__(name):
88+
try:
89+
module_name = _name_to_module[name]
90+
except Exception:
91+
raise AttributeError
92+
module = _imp.import_module(module_name, __name__)
93+
return getattr(module, name)
5394

5495

5596
class op:
@@ -65,3 +106,10 @@ class op:
65106

66107

67108
del _np, _LT, _LE, _EQ, _NE, _GT, _GE
109+
110+
__all__ = sorted(list(_name_to_module.keys()) +
111+
['first', 'argfirst', 'op'])
112+
113+
114+
def __dir__():
115+
return __all__

0 commit comments

Comments
 (0)