Skip to content

Conversation

@cvanelteren
Copy link
Collaborator

@cvanelteren cvanelteren commented Sep 8, 2025

This PR changes the colormap loading to lazy loading -- effectively reducing the import time from 0.38s to 0.00082s for the colormaps.

@cvanelteren cvanelteren requested a review from Copilot September 8, 2025 07:53

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@cvanelteren cvanelteren marked this pull request as draft September 8, 2025 08:01
@codecov

This comment was marked as resolved.

@cvanelteren cvanelteren marked this pull request as ready for review September 8, 2025 08:47
@cvanelteren cvanelteren requested a review from Copilot September 8, 2025 08:48

This comment was marked as resolved.

@cvanelteren
Copy link
Collaborator Author

hmm this test that is failing is passing locally.. strange

@cvanelteren cvanelteren requested a review from beckermr September 8, 2025 10:23
# by overriding matplotlib's behavior
for name in tuple(self._cmaps.keys()):
self.register(self._cmaps[name], name=name)
super().__init__({k.lower(): v for k, v in kwargs.items()})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why switch to k.lower() here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We perform a lower command internally later when the colormap is registered.

# such that ultraplot knows what these objects are. We piggy back on the registering mechanism
# by overriding matplotlib's behavior
for name in tuple(self._cmaps.keys()):
self.register(self._cmaps[name], name=name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we are no longer using the register method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch. We do need it as we override the ColormapRegistery. Moved the lazy loading from file to a private function to make this distinction clearer and still rely on register

Comment on lines 3203 to 3216
raise ValueError(
f"Invalid colormap type {type!r} for key {key!r} in file {path!r}. "
"Expected 'continuous' or 'discrete'."
)

if cmap:
if is_default and cmap.name.lower() in CMAPS_CYCLIC:
cmap.set_cyclic(True)
self._cmaps[key] = cmap
value = cmap
else: # failed to load
# remove from registry to avoid trying again
del self._cmaps[key]
raise KeyError(f"Failed to load colormap {key!r} from {path!r}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code warned on failures at least according to the keyword. This code appears to raise an error?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it back to a warn.

@cvanelteren cvanelteren merged commit 6ab7434 into Ultraplot:main Sep 9, 2025
25 checks passed
@cvanelteren cvanelteren deleted the fix-cmap-loading branch September 9, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants