|
31 | 31 | pipeline
|
32 | 32 | Module which allowing to create pipeline with scikit-learn estimators.
|
33 | 33 | """
|
| 34 | +import importlib |
| 35 | +import types |
| 36 | + |
34 | 37 | from . import combine
|
35 | 38 | from . import ensemble
|
36 | 39 | from . import exceptions
|
37 |
| -from . import keras |
38 | 40 | from . import metrics
|
39 | 41 | from . import over_sampling
|
40 | 42 | from . import tensorflow
|
|
46 | 48 | from ._version import __version__
|
47 | 49 | from .utils._show_versions import show_versions
|
48 | 50 |
|
| 51 | + |
| 52 | +# # FIXME: When we get Python 3.7 as minimal version, we will need to switch to |
| 53 | +# # the following solution: |
| 54 | +# # https://snarky.ca/lazy-importing-in-python-3-7/ |
| 55 | +class LazyLoader(types.ModuleType): |
| 56 | + """Lazily import a module, mainly to avoid pulling in large dependencies. |
| 57 | +
|
| 58 | + Adapted from TensorFlow: |
| 59 | + https://github.com/tensorflow/tensorflow/blob/master/tensorflow/ |
| 60 | + python/util/lazy_loader.py |
| 61 | + """ |
| 62 | + def __init__(self, local_name, parent_module_globals, name, warning=None): |
| 63 | + self._local_name = local_name |
| 64 | + self._parent_module_globals = parent_module_globals |
| 65 | + self._warning = warning |
| 66 | + |
| 67 | + super(LazyLoader, self).__init__(name) |
| 68 | + |
| 69 | + def _load(self): |
| 70 | + """Load the module and insert it into the parent's globals.""" |
| 71 | + # Import the target module and insert it into the parent's namespace |
| 72 | + module = importlib.import_module(self.__name__) |
| 73 | + self._parent_module_globals[self._local_name] = module |
| 74 | + |
| 75 | + # Update this object's dict so that if someone keeps a reference to the |
| 76 | + # LazyLoader, lookups are efficient (__getattr__ is only called on |
| 77 | + # lookups that fail). |
| 78 | + self.__dict__.update(module.__dict__) |
| 79 | + |
| 80 | + return module |
| 81 | + |
| 82 | + def __getattr__(self, item): |
| 83 | + module = self._load() |
| 84 | + return getattr(module, item) |
| 85 | + |
| 86 | + def __dir__(self): |
| 87 | + module = self._load() |
| 88 | + return dir(module) |
| 89 | + |
| 90 | + |
| 91 | +# delay the import of keras since we are going to import either tensorflow |
| 92 | +# or keras |
| 93 | +keras = LazyLoader("keras", globals(), "imblearn.keras") |
| 94 | + |
49 | 95 | __all__ = [
|
50 | 96 | "combine",
|
51 | 97 | "ensemble",
|
|
0 commit comments