Skip to content

speed up import and load hvplot lazily #1539

Closed

Description

@Sevans711

Proposed new feature or change:

I found a tiny change we can make to speed up import uxarray. On my machine the import takes 1.7 seconds before this change, and 0.7 seconds after it. See also: #1260

The change is to import hvplot.pandas and hvplot.xarray lazily in uxarray.plot.accessor.py, i.e. only import them the first time any of the PlotAccessor objects' __init__ methods actually get called. These lazy imports should also make it easier to move hvplot to be an optional dependency instead of a required dependency later (see #1224).

This also delays when these strange gray bars appear in Jupyter (they are apparently caused by import hvplot.pandas and hvplot.xarray). Now they only appear the first time you plot something, instead of immediately upon importing uxarray for the first time.

Previous behavior:
Image
(with first cell taking ~1.7s, second cell ~0.1s)

New behavior:
Image
(first cell faster, second cell slower. Rerunning the second cell takes ~0.1s and does not produce the gray bars.)

Activity

  1. added
    new feature
    on Jun 30, 2026
  2. self-assigned this
    on Jun 30, 2026
  3. rajeeja commented on Jun 30, 2026

    @rajeeja
    Contributor

    Thanks @Sevans711 — I've been meaning to look into this too, as I've found the import times to be pretty high again. hvplot is the biggest single chunk (~0.8s, ~40%), so this is the right core fix, and it also unblocks making hvplot optional (#1224).

    Some history: #1265 (with @philipc2) did an earlier import-time pass but deliberately left hvplot at module top, so your change picks up where that left off. A couple of those wins have since regressed too — cartopy got re-added at module top by later viz features (#1271, #1454), and re-deferring it (plus holoviews/pyproj) is worth another ~0.5s. Locally I see import uxarray drop from ~2.0s to ~1.0s with all of it.

    One suggestion so it doesn't rot again: a small test asserting cartopy/hvplot/holoviews aren't in sys.modules after import uxarray would have caught those regressions.

  4. added a commit that references this issue on Jun 30, 2026
  5. Sevans711 commented on Jun 30, 2026

    @Sevans711
    CollaboratorAuthor

    @rajeeja it looks like the test fails in #1541, but it is failing before even reaching the uxarray import. The code looks like this:

    import sys
    
    def test_hvplot_optional():
        """Test that hvplot is actually optional and not imported by default.
        (hvplot in particular is "slow" to import (~1s to import hvplot.pandas),
        so it should not be imported until actually plotting something.)
        """
        assert "hvplot" not in sys.modules  # else, it's a test environment issue..?
        import uxarray as ux
        assert "hvplot" not in sys.modules

    and the failure is at the first assert statement, before the import uxarray as ux line. When I run pytest locally, with just this file (pytest test/test_dependencies.py) it passes, but when I run it locally for all tests (pytest test/) it crashes. Maybe it's related to pytest fixtures? I can try running this down a bit more but also wanted to check, do you have any guesses here?

  6. moved this from 📚 Backlog to 🏗 In progress in UXarray Developmenton Jun 30, 2026
  7. added
    improvement
    and removed
    new feature
    on Jul 8, 2026
  8. moved this from 🏗 In progress to 👀 In review in UXarray Developmenton Jul 8, 2026
  9. moved this from 👀 In review to ✅ Done in UXarray Developmenton Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

improvement

Type

No type

Fields

Priority

None yet

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions