Lazy-load heavy dependencies to speed up import huracanpy#188
Draft
stella-bourdin with Copilot wants to merge 3 commits into
Draft
Lazy-load heavy dependencies to speed up import huracanpy#188stella-bourdin with Copilot wants to merge 3 commits into
import huracanpy#188stella-bourdin with Copilot wants to merge 3 commits into
Conversation
Move heavy dependencies (geopandas, cartopy, seaborn) from module-level imports to inside the functions that use them. This means `import huracanpy` no longer triggers loading of geopandas, cartopy, or seaborn; these are only imported on first use. Files changed: - huracanpy/_basins.py: add _LazyBasins class; GeoDataFrames created on demand - huracanpy/info/_geography.py: lazy _natural_earth_feature_cache; defer geopandas/cartopy/metpy - huracanpy/convert/_geopandas.py: move geopandas/cartopy/shapely imports inside to_geodataframe() - huracanpy/calc/_translation.py: move cartopy imports inside _latlon_to_xy() - huracanpy/plot/_tracks.py: move cartopy/matplotlib/seaborn imports inside tracks() - huracanpy/plot/_density.py: move cartopy/matplotlib imports inside density() - huracanpy/plot/_fancyline.py: move matplotlib/cartopy imports inside functions - huracanpy/plot/_doughnut.py: move matplotlib import inside doughnut() - huracanpy/plot/_venn.py: move matplotlib_venn imports inside helper functions - huracanpy/plot/_pressure_wind.py: lazy metpy decorator + seaborn import - huracanpy/__init__.py: add comment explaining lazy-import strategy
Copilot
AI
changed the title
[WIP] Implement lazy imports for heavy submodules in huracanpy
Lazy-load heavy dependencies to speed up Jun 5, 2026
import huracanpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
import huracanpywas slow (~3–7s) because all submodules were eagerly imported, immediately pulling ingeopandas,cartopy,metpy,matplotlib, andseaborn— even when only usinghuracanpy.load().Approach
Move heavy imports from module level to inside the functions that need them. No new dependencies added.
_basins.py— new_LazyBasinsclassPreviously created
gpd.GeoDataFrameobjects at module level (requiring geopandas + cartopy on import). Now stores raw shapely geometry dicts and defers GeoDataFrame construction until first access:info/_geography.pygeopandas,cartopy,metpyimports_natural_earth_feature_cachefrom an eager dict comprehension overbasins.items()to a lazily-populated{}, with basin entries created inside_cache_natural_earth_featureon first request@preprocess_and_wrappattern for metpy-decorated functions (decorator applied inside function body on a nested_impl)calc/_translation.pyMoved
from cartopy.crs import Geodetic, Orthographicinside_latlon_to_xy().convert/_geopandas.pyMoved
geopandas,cartopy,shapelyimports insideto_geodataframe().plot/— all plot modulesMoved
cartopy,matplotlib,seaborn,matplotlib_vennimports inside the functions that use them (tracks,density,fancyline,doughnut,_venn_*,_setup_grid,pressure_wind_relation).Result
After these changes,
geopandas,cartopy, andseabornare not imported at startup.metpyis still imported viacalc(used for unit handling throughout), which also pulls inmatplotlibvia pint — deferring those would require broader refactoring of core calc functionality.Original prompt
Problem
import huracanpyis slow becausehuracanpy/__init__.pyeagerly imports all submodules at once:This triggers immediate import of heavy dependencies even if the user only needs basic functionality (e.g.,
huracanpy.load()). The main offenders are:huracanpy/info/_geography.py: importsgeopandas,cartopy, andmetpyat module levelhuracanpy/plot/_tracks.py: importscartopy,matplotlib, andseabornat module levelhuracanpy/convert/_geopandas.py: likely importsgeopandasat module levelTypical import times for these libraries:
geopandas: ~1–3scartopy: ~0.5–2smetpy: ~0.5–1smatplotlib+seaborn: ~0.5–1sGoal
Implement lazy imports for the heavy submodules so that their dependencies are only loaded when actually used. The approach should:
Use inline/deferred imports inside functions in the submodules that use heavy dependencies (primarily
huracanpy/info/_geography.py,huracanpy/plot/*.py,huracanpy/convert/_geopandas.py), OR use thelazy_loaderpackage at the top-level__init__.py.The preferred approach is inline imports inside functions (no new dependency needed), since
lazy_loaderwould add a new dependency. Move the heavy top-level imports (geopandas,cartopy,metpy,matplotlib,seaborn) to inside the functions that use them, in these files:huracanpy/info/_geography.pyhuracanpy/plot/_tracks.pyhuracanpy/plot/_density.pyhuracanpy/plot/_fancyline.pyhuracanpy/plot/_doughnut.pyhuracanpy/plot/_venn.pyhuracanpy/plot/_pressure_wind.pyhuracanpy/convert/_geopandas.pyMake sure all existing tests still pass.
Do not change any public API or function signatures.
Add a brief note in the changelog or a comment in
__init__.pyexplaining the change.Notes
_accessor.pyfile also importsfrom . import calc, info, plot, tcat module level. Since_accessoritself is eagerly imported in__init__.py, the same lazy-import approach should be applied there too, or the accessor import in__init__.pyshould be deferred.@preprocess_and_wrapdecorator frommetpyin_geography.py— the import ofmetpyneeds to happen before the decorator is applied, so it must be imported at the top of the function or the decorator must be handled differently (e.g., by importingmetpyat the top of the module but usingimportliblazy loading, or by restructuring the decorated functions).geopandas,cartopy,metpy.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: I mean, make suggestions for publishing the code associated to this as a python package
Assistant: I'd love to help with suggestions for publishing your code as a Python package! However, I don't have context about which repository or project you're referring to. Could you please share the repository (e.g.,
owner/repo) or link to the code you have in mind?User: Here is. description :
This paper presents a proof of concept for a new analogue-based framework for the detection and attribution of hurricane-related hazards. This framework addresses two important limitations of existing analogue-based methodologies: the lack of observed similar events, and the unsuitability of the distance metrics for hurricanes. To do so, we use a track-based metric, and we make use of synthetic tracks catalogues. We show that our method allows for selecting a sufficient number of suitable analogues, and we apply it to nine hurricane cases. Our analysis does not reveal any robust changes in wind hazards, translation speed, seasonality, or frequency over recent decades, consistent with current literature. This framework provides a reliable alternative to traditional analogue-based methods in the case of hurricanes, complementing and potentially enhancing efforts in addressing extreme weather event attribution.
Assistant: Thanks for the description! That's a great research project. However, I still need the GitHub repository link or
owner/repoto look at the actual code structure before making specific suggestions.Could you share the repository? For example:
stella-bourdin/my-hurricane-repoUser: there is no repo yet, I'm wondering how to name the repo
Assistant: Based on the description, here are some name suggestions:
Concise & Descriptive
pyHATTR– Python Hurricane Attribution via Track-based analogueshurricane-analogues– straightforward and searchabletrack-analogues– emphasizes the track-based metricAcronym-based
HAAF– Hurricane Analogue Attribution FrameworkHABITAT– Hurricane Attribution By Iterative Track Analogue ToolkitTHAAF– Tra...This pull request was created from Copilot chat.