From bff126aa4c6ba5699f50b756e1cf19ebef25de5a Mon Sep 17 00:00:00 2001 From: Alex Morling Date: Tue, 28 May 2024 16:57:52 +0200 Subject: [PATCH] lazy load ecoscope --- ecoscope/__init__.py | 12 +++++++++--- ecoscope/analysis/UD/__init__.py | 7 ++++++- ecoscope/analysis/__init__.py | 14 ++++++++++---- ecoscope/base/__init__.py | 23 +++++++++++++++-------- ecoscope/io/__init__.py | 13 ++++++++++--- ecoscope/mapping/__init__.py | 23 +++++++++++++++-------- ecoscope/plotting/__init__.py | 23 +++++++++++++++-------- setup.py | 1 + 8 files changed, 81 insertions(+), 35 deletions(-) diff --git a/ecoscope/__init__.py b/ecoscope/__init__.py index f026ace7..d93636e9 100644 --- a/ecoscope/__init__.py +++ b/ecoscope/__init__.py @@ -1,4 +1,9 @@ -from ecoscope import base, io, mapping, plotting, analysis +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submodules=["base", "io", "mapping", "plotting", "analysis"], +) ASCII = """\ _____ @@ -49,6 +54,7 @@ def init(silent=False, selenium=False, force=False): warnings.filterwarnings("ignore", message=".*initial implementation of Parquet.*") import geopandas as gpd + from ecoscope.mapping.map import EcoMap def explore(data, *args, **kwargs): """ @@ -56,9 +62,9 @@ def explore(data, *args, **kwargs): """ initialized = "m" in kwargs if not initialized: - kwargs["m"] = mapping.EcoMap() + kwargs["m"] = EcoMap() - if isinstance(kwargs["m"], mapping.EcoMap): + if isinstance(kwargs["m"], EcoMap): m = kwargs.pop("m") m.add_gdf(data, *args, **kwargs) if not initialized: diff --git a/ecoscope/analysis/UD/__init__.py b/ecoscope/analysis/UD/__init__.py index b8456746..1ab40452 100644 --- a/ecoscope/analysis/UD/__init__.py +++ b/ecoscope/analysis/UD/__init__.py @@ -1,4 +1,9 @@ -from ecoscope.analysis.UD.etd_range import calculate_etd_range +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submod_attrs={"etd_range": ["calculate_etd_range"]}, +) __all__ = [ "calculate_etd_range", diff --git a/ecoscope/analysis/__init__.py b/ecoscope/analysis/__init__.py index fc7b7488..4832a192 100644 --- a/ecoscope/analysis/__init__.py +++ b/ecoscope/analysis/__init__.py @@ -1,7 +1,13 @@ -from ecoscope.analysis import UD, seasons -from ecoscope.analysis.ecograph import Ecograph, get_feature_gdf -from ecoscope.analysis.percentile import get_percentile_area -from ecoscope.analysis.speed import SpeedDataFrame +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submodules=["UD", "seasons", "speed"], + submod_attrs={ + "ecograph": ["Ecograph", "get_feature_gdf"], + "percentile": ["get_percentile_area"], + }, +) __all__ = [ "Ecograph", diff --git a/ecoscope/base/__init__.py b/ecoscope/base/__init__.py index 45e1db4b..caa3755a 100644 --- a/ecoscope/base/__init__.py +++ b/ecoscope/base/__init__.py @@ -1,12 +1,19 @@ -from ecoscope.base._dataclasses import ( - RelocsCoordinateFilter, - RelocsDateRangeFilter, - RelocsDistFilter, - RelocsSpeedFilter, - TrajSegFilter, +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submod_attrs={ + "_dataclasses": [ + "RelocsCoordinateFilter", + "RelocsDateRangeFilter", + "RelocsDistFilter", + "RelocsSpeedFilter", + "TrajSegFilter", + ], + "base": ["EcoDataFrame", "Relocations", "Trajectory"], + "utils": ["cachedproperty", "create_meshgrid", "groupby_intervals", "to_EarthLocation", "is_night"], + }, ) -from ecoscope.base.base import EcoDataFrame, Relocations, Trajectory -from ecoscope.base.utils import cachedproperty, create_meshgrid, groupby_intervals, to_EarthLocation, is_night __all__ = [ "EcoDataFrame", diff --git a/ecoscope/io/__init__.py b/ecoscope/io/__init__.py index a58e2e9c..d06faf42 100644 --- a/ecoscope/io/__init__.py +++ b/ecoscope/io/__init__.py @@ -1,6 +1,13 @@ -from ecoscope.io import earthranger, eetools, raster, utils -from ecoscope.io.earthranger import EarthRangerIO -from ecoscope.io.utils import download_file +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submodules=["earthranger", "eetools", "raster", "utils"], + submod_attrs={ + "earthranger": ["EarthRangerIO"], + "utils": ["download_file"], + }, +) __all__ = [ "earthranger", diff --git a/ecoscope/mapping/__init__.py b/ecoscope/mapping/__init__.py index f4b16799..a9bd07bd 100644 --- a/ecoscope/mapping/__init__.py +++ b/ecoscope/mapping/__init__.py @@ -1,11 +1,18 @@ -from ecoscope.mapping.map import ( - ControlElement, - EcoMap, - FloatElement, - NorthArrowElement, - ScaleElement, - GeoTIFFElement, - PrintControl, +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submod_attrs={ + "map": [ + "ControlElement", + "EcoMap", + "FloatElement", + "NorthArrowElement", + "ScaleElement", + "GeoTIFFElement", + "PrintControl", + ], + }, ) __all__ = [ diff --git a/ecoscope/plotting/__init__.py b/ecoscope/plotting/__init__.py index 23d6dd37..b8abb950 100644 --- a/ecoscope/plotting/__init__.py +++ b/ecoscope/plotting/__init__.py @@ -1,11 +1,18 @@ -from ecoscope.plotting.plot import ( - EcoPlotData, - add_seasons, - ecoplot, - mcp, - nsd, - plot_seasonal_dist, - speed, +import lazy_loader as lazy + +__getattr__, __dir__, __all__ = lazy.attach( + __name__, + submod_attrs={ + "plot": [ + "EcoPlotData", + "add_seasons", + "ecoplot", + "mcp", + "nsd", + "plot_seasonal_dist", + "speed", + ], + }, ) __all__ = [ diff --git a/setup.py b/setup.py index d0ee7fad..a98efadc 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ "geopandas<=0.14.2", "igraph", "kaleido", + "lazy_loader", "mapclassify", "matplotlib", "networkx",