8000 Refactor lazy loading · scikit-image/scikit-image@057cba4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 057cba4

Browse files
committed
Refactor lazy loading
- Use lazy_loader package - Use stubs to specify lazy imports
1 parent 6423c5f commit 057cba4

File tree

11 files changed

+208
-226
lines changed

11 files changed

+208
-226
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ recursive-include requirements *.txt
88
include requirements/README.md
99
include Makefile
1010
include skimage/restoration/unwrap_*_ljmu.c
11-
recursive-include skimage *.pyx *.pxd *.pxi *.py *.h *.ini *.npy *.txt *.in *.md
11+
recursive-include skimage *.pyx *.pxd *.pxi *.py *.pyi *.h *.ini *.npy *.txt *.in *.md
1212
recursive-include skimage/data *
1313
recursive-include skimage/*/tests/data *
1414

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ requires = [
55
"packaging",
66
"Cython>=0.29.24,<3.0",
77
"pythran",
8+
"lazy_loader>=0.1",
89

910
# We follow scipy for much of these pinnings
1011
# https://github.com/scipy/scipy/blob/master/pyproject.toml

requirements/default.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ imageio>=2.4.1
77
tifffile>=2019.7.26
88
PyWavelets>=1.1.1
99
packaging>=20.0
10+
lazy_loader>=0.1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def configuration(parent_package='', top_path=None):
249249
exclude=['doc', 'doc.*', 'benchmarks']),
250250
package_data={
251251
# distribute Cython source files in the wheel
252-
"": ["*.pyx", "*.pxd", "*.pxi", ""],
252+
"": ["*.pyx", "*.pxd", "*.pxi", "*.pyi", ""],
253253
# tests dirs have an __init__.py so are automatically included
254254
},
255255
include_package_data=False,

skimage/__init__.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,11 @@
6767

6868
__version__ = '0.20.0.dev0'
6969

70-
submodules = [
71-
'color',
72-
'data',
73-
'draw',
74-
'exposure',
75-
'feature',
76-
'filters',
77-
'future',
78-
'graph',
79-
'io',
80-
'measure',
81-
'metrics',
82-
'morphology',
83-
'registration',
84-
'restoration',
85-
'segmentation',
86-
'transform',
87-
'util',
88-
]
89-
9070
from ._shared.version_requirements import ensure_python_version
9171
ensure_python_version((3, 7))
9272

93-
from ._shared import lazy
94-
__getattr__, __lazy_dir__, _ = lazy.attach(
95-
__name__,
96-
submodules,
97-
submod_attrs={'data': ['data_dir']}
98-
)
99-
73+
import lazy_loader as lazy
74+
__getattr__, __lazy_dir__, _ = lazy.attach_stub(__name__, __file__)
10075

10176
def __dir__():
10277
return __lazy_dir__() + ['__version__']
@@ -149,17 +124,23 @@ def _raise_build_error(e):
149124
except ImportError as e:
150125
_raise_build_error(e)
151126

152-
# All skimage root imports go here
153-
from .util.dtype import (img_as_float32,
154-
img_as_float64,
155-
img_as_float,
156-
img_as_int,
157-
img_as_uint,
158-
img_as_ubyte,
159-
img_as_bool,
160-
dtype_limits)
127+
# Legacy imports into the root namespace; not advertised in __all__
128+
from .util.dtype import (
129+
dtype_limits,
130+
img_as_float32,
131+
img_as_float64,
132+
img_as_float,
133+
img_as_int,
134+
img_as_uint,
135+
img_as_ubyte,
136+
img_as_bool
137+
)
138+
161139
from .util.lookfor import lookfor
162140

141+
from .data import data_dir
142+
143+
163144
if 'dev' in __version__:
164145
# Append last commit date and hash to dev version information, if available
165146

skimage/__init__.pyi

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
submodules = [
2+
'color',
3+
'data',
4+
'draw',
5+
'exposure',
6+
'feature',
7+
'filters',
8+
'future',
9+
'graph',
10+
'io',
11+
'measure',
12+
'metrics',
13+
'morphology',
14+
'registration',
15+
'restoration',
16+
'segmentation',
17+
'transform',
18+
'util'
19+
]
20+
21+
__all__ = submodules + [
22+
'__version__'
23+
]
24+
25+
from . import (color, data, draw, exposure, feature, filters, future,
26+
graph, io, measure, metrics, morphology, registration,
27+
restoration, segmentation, transform, util)

skimage/_shared/lazy.py

Lines changed: 0 additions & 139 deletions
This file was deleted.

skimage/data/__init__.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
from .._shared import lazy
1+
import lazy_loader as lazy
22

3-
__getattr__, __dir__, __all__ = lazy.attach(
4-
__name__,
5-
submodules={},
6-
submod_attrs={
7-
'_binary_blobs': ['binary_blobs'],
8-
'_fetchers': ['create_image_fetcher', 'data_dir', 'download_all',
9-
'file_hash', 'image_fetcher', 'astronaut',
10-
'binary_blobs', 'brain', 'brick', 'camera', 'cat',
11-
'cell', 'cells3d', 'checkerboard', 'chelsea', 'clock',
12-
'coffee', 'coins', 'colorwheel', 'eagle', 'grass',
13-
'gravel', 'horse', 'hubble_deep_field', 'human_mitosis',
14-
'immunohistochemistry', 'kidney',
15-
'lbp_frontal_face_cascade_filename', 'lfw_subset',
16-
'lily', 'logo', 'microaneurysms', 'moon',
17-
'nickel_solidification', 'page', 'protein_transport',
18-
'retina', 'rocket', 'shepp_logan_phantom',
19-
'skin', 'stereo_motorcycle', 'text', 'vortex']
20-
}
21-
)
3+
__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)

skimage/data/__init__.pyi

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
__all__ = [
2+
'astronaut',
3+
'binary_blobs',
4+
'brain',
5+
'brick',
6+
'camera',
7+
'cat',
8+
'cell',
9+
'cells3d',
10+
'checkerboard',
11+
'chelsea',
12+
'clock',
13+
'coffee',
14+
'coins',
15+
'colorwheel',
16+
'create_image_fetcher',
17+
'data_dir',
18+
'download_all',
19+
'eagle',
20+
'file_hash',
21+
'grass',
22+
'gravel',
23+
'horse',
24+
'hubble_deep_field',
25+
'human_mitosis',
26+
'image_fetcher',
27+
'immunohistochemistry',
28+
'kidney',
29+
'lbp_frontal_face_cascade_filename',
30+
'lfw_subset',
31+
'lily',
32+
'logo',
33+
'microaneurysms',
34+
'moon',
35+
'nickel_solidification',
36+
'page',
37+
'protein_transport',
38+
'retina',
39+
'rocket',
40+
'shepp_logan_phantom',
41+
'skin',
42+
'stereo_motorcycle',
43+
'text',
44+
'vortex'
45+
]
46+
47+
from ._binary_blobs import binary_blobs
48+
from ._fetchers import (create_image_fetcher, data_dir, download_all,
49+
file_hash, image_fetcher, astronaut,
50+
binary_blobs, brain, brick, camera, cat,
51+
cell, cells3d, checkerboard, chelsea, clock,
52+
coffee, coins, colorwheel, eagle, grass,
53+
gravel, horse, hubble_deep_field, human_mitosis,
54+
immunohistochemistry, kidney,
55+
lbp_frontal_face_cascade_filename, lfw_subset,
56+
lily, logo, microaneurysms, moon,
57+
nickel_solidification, page, protein_transport,
58+
retina, rocket, shepp_logan_phantom,
59+
skin, stereo_motorcycle, text, vortex)

skimage/filters/__init__.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
from .._shared import lazy
1+
import lazy_loader as lazy
22

3-
__getattr__, __dir__, __all__ = lazy.attach(
4-
__name__,
5-
submodules={'rank'},
6-
submod_attrs={
7-
'lpi_filter': ['filter_inverse', 'wiener', 'LPIFilter2D'],
8-
'_gaussian': ['gaussian', 'difference_of_gaussians'],
9-
'edges': ['sobel', 'sobel_h', 'sobel_v',
10-
'scharr', 'scharr_h', 'scharr_v',
11-
'prewitt', 'prewitt_h', 'prewitt_v',
12-
'roberts', 'roberts_pos_diag', 'roberts_neg_diag',
13-
'laplace',
14-
'farid', 'farid_h', 'farid_v'],
15-
'_rank_order': ['rank_order'],
16-
'_gabor': ['gabor_kernel', 'gabor'],
17-
'thresholding': ['threshold_local', 'threshold_otsu', 'threshold_yen',
18-
'threshold_isodata', 'threshold_li', 'threshold_minimum',
19-
'threshold_mean', 'threshold_triangle',
20-
'threshold_niblack', 'threshold_sauvola',
21-
'threshold_multiotsu', 'try_all_threshold',
22-
'apply_hysteresis_threshold'],
23-
'ridges': ['meijering', 'sato', 'frangi', 'hessian'],
24-
'_median': ['median'],
25-
'_sparse': ['correlate_sparse'],
26-
'_unsharp_mask': ['unsharp_mask'],
27-
'_window': ['window'],
28-
'_fft_based': ['butterworth']
29-
}
30-
)
3+
__getattr__, __dir__, __all__ = lazy.attach_stub(__name__, __file__)

0 commit comments

Comments
 (0)
0