Skip to content

Instantly share code, notes, and snippets.

@Erotemic
Created December 24, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Erotemic/2b2b38bc87386d97ef91ede3b349f5d6 to your computer and use it in GitHub Desktop.
Save Erotemic/2b2b38bc87386d97ef91ede3b349f5d6 to your computer and use it in GitHub Desktop.
Demo auto-generated version of skimage.filters
def lazy_install(module_name, submodules, submod_attrs):
"""
Defines gettr for lazy import via PEP 562
https://www.python.org/dev/peps/pep-0562/
"""
import sys
import importlib
import importlib.util
all_funcs = []
for mod, funcs in submod_attrs.items():
all_funcs.extend(funcs)
name_to_submod = {
func: mod for mod, funcs in submod_attrs.items()
for func in funcs
}
def require(fullname):
if fullname in sys.modules:
return sys.modules[fullname]
spec = importlib.util.find_spec(fullname)
try:
module = importlib.util.module_from_spec(spec)
except:
raise ImportError(f'Could not lazy import module {fullname}') from None
loader = importlib.util.LazyLoader(spec.loader)
sys.modules[fullname] = module
# Make module with proper locking and get it inserted into sys.modules.
loader.exec_module(module)
return module
def __getattr__(name):
if name in submodules:
fullname = f'{module_name}.{name}'
attr = require(fullname)
elif name in name_to_submod:
modname = name_to_submod[name]
module = importlib.import_module(
f'{module_name}.{modname}'
)
attr = getattr(module, name)
else:
raise AttributeError(f'No {module_name} attribute {name}')
# Set module-level attribute so getattr is not called again
globals()[name] = attr
return attr
return __getattr__
__getattr__ = lazy_install(
__name__,
submodules={
'edges',
'lpi_filter',
'rank',
'ridges',
'setup',
'tests',
'thresholding',
},
submod_attrs={
'edges': [
'HFARID_WEIGHTS',
'HPREWITT_WEIGHTS',
'HSCHARR_WEIGHTS',
'HSOBEL_WEIGHTS',
'PREWITT_EDGE',
'PREWITT_SMOOTH',
'ROBERTS_ND_WEIGHTS',
'ROBERTS_PD_WEIGHTS',
'SCHARR_EDGE',
'SCHARR_SMOOTH',
'SOBEL_EDGE',
'SOBEL_SMOOTH',
'VFARID_WEIGHTS',
'VPREWITT_WEIGHTS',
'VSCHARR_WEIGHTS',
'VSOBEL_WEIGHTS',
'd1',
'farid',
'farid_h',
'farid_v',
'laplace',
'p',
'prewitt',
'prewitt_h',
'prewitt_v',
'roberts',
'roberts_neg_diag',
'roberts_pos_diag',
'scharr',
'scharr_h',
'scharr_v',
'sobel',
'sobel_h',
'sobel_v',
],
'lpi_filter': [
'LPIFilter2D',
'constrained_least_squares',
'eps',
'forward',
'inverse',
'wiener',
],
'rank': [
'autolevel',
'autolevel_percentile',
'bottomhat',
'enhance_contrast',
'enhance_contrast_percentile',
'entropy',
'equalize',
'geometric_mean',
'gradient',
'gradient_percentile',
'majority',
'maximum',
'mean',
'mean_bilateral',
'mean_percentile',
'median',
'minimum',
'modal',
'noise_filter',
'otsu',
'percentile',
'pop',
'pop_bilateral',
'pop_percentile',
'subtract_mean',
'subtract_mean_percentile',
'sum',
'sum_bilateral',
'sum_percentile',
'threshold',
'threshold_percentile',
'tophat',
'windowed_histogram',
],
'ridges': [
'compute_hessian_eigenvalues',
'frangi',
'hessian',
'meijering',
'sato',
],
'setup': [
'base_path',
'configuration',
],
'tests': [
'setup',
'teardown',
],
'thresholding': [
'apply_hysteresis_threshold',
'threshold_isodata',
'threshold_li',
'threshold_local',
'threshold_mean',
'threshold_minimum',
'threshold_multiotsu',
'threshold_niblack',
'threshold_otsu',
'threshold_sauvola',
'threshold_triangle',
'threshold_yen',
'try_all_threshold',
],
},
)
def __dir__():
return __all__
__all__ = ['HFARID_WEIGHTS', 'HPREWITT_WEIGHTS', 'HSCHARR_WEIGHTS',
'HSOBEL_WEIGHTS', 'LPIFilter2D', 'PREWITT_EDGE', 'PREWITT_SMOOTH',
'ROBERTS_ND_WEIGHTS', 'ROBERTS_PD_WEIGHTS', 'SCHARR_EDGE',
'SCHARR_SMOOTH', 'SOBEL_EDGE', 'SOBEL_SMOOTH', 'VFARID_WEIGHTS',
'VPREWITT_WEIGHTS', 'VSCHARR_WEIGHTS', 'VSOBEL_WEIGHTS',
'apply_hysteresis_threshold', 'autolevel', 'autolevel_percentile',
'base_path', 'bottomhat', 'compute_hessian_eigenvalues',
'configuration', 'constrained_least_squares', 'd1', 'edges',
'enhance_contrast', 'enhance_contrast_percentile', 'entropy', 'eps',
'equalize', 'farid', 'farid_h', 'farid_v', 'forward', 'frangi',
'geometric_mean', 'gradient', 'gradient_percentile', 'hessian',
'inverse', 'laplace', 'lpi_filter', 'majority', 'maximum', 'mean',
'mean_bilateral', 'mean_percentile', 'median', 'meijering',
'minimum', 'modal', 'noise_filter', 'otsu', 'p', 'percentile',
'pop', 'pop_bilateral', 'pop_percentile', 'prewitt', 'prewitt_h',
'prewitt_v', 'rank', 'ridges', 'roberts', 'roberts_neg_diag',
'roberts_pos_diag', 'sato', 'scharr', 'scharr_h', 'scharr_v',
'setup', 'setup', 'sobel', 'sobel_h', 'sobel_v', 'subtract_mean',
'subtract_mean_percentile', 'sum', 'sum_bilateral',
'sum_percentile', 'teardown', 'tests', 'threshold',
'threshold_isodata', 'threshold_li', 'threshold_local',
'threshold_mean', 'threshold_minimum', 'threshold_multiotsu',
'threshold_niblack', 'threshold_otsu', 'threshold_percentile',
'threshold_sauvola', 'threshold_triangle', 'threshold_yen',
'thresholding', 'tophat', 'try_all_threshold', 'wiener',
'windowed_histogram']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment