Repository navigation
Description
Bug summary
The new matplotlib.colors.make_norm_from_scale helper dynamically generates a norm class from a scale class. Currently, in the codebase, it is only used as a decorator to create "toplevel" classes (e.g., it is used to generate LogNorm from LogScale, etc.), but it can also be used within other functions to dynamically generate a norm class based on a user-given arbitrary scale (see #20752 for an example of application, which is however not necessary to understand for what follows). In the latter case, the dynamically generate class is currently not picklable (because pickling of classes relies on the existence of global names, see e.g. "classes are pickled by named reference"). It would be generally useful to get rid of this restriction, which can be done by implementing the __reduce__ protocol; there's already other examples in the codebase of dynamically generated classes that use the same mechanism).
I'm tagging this as "good first issue" because there's no API design and the eng goal is clear, but medium (perhaps hard) difficulty because it requires somewhat sophisticated understanding of the details of the pickling process.
Code for reproduction
pickle.dumps(matplotlib.colors.make_norm_from_scale(matplotlib.scale.LogitScale, matplotlib.colors.Normalize))Actual outcome
Can't pickle <class 'matplotlib.colors.Normalize'>: it's not the same object as matplotlib.colors.Normalize
(Note the additional confusion here: there's two classes that are both named matplotlib.colors.Normalize -- the original one and the dynamically generated one -- but they are different.)
Expected outcome
A correct round-trippable pickle.
Operating system
No response
Matplotlib Version
master (unreleased, pre 3.5)
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Other libraries
No response
Installation
source
Conda channel
No response
Activity
Bart-del commentedon Sep 7, 2021
Hey, I want to try and fix this bug. Will you assign me or should I just start working on it?
anntzer commentedon Sep 7, 2021
Go for it.
greglucas commentedon Feb 27, 2022
I think this wasn't fully solved for the built-in classes of
matplotlib.colors. Adding this test totest_dynamic_norm:Fails with:
greglucas commentedon Mar 20, 2022
I tried to look into this a bit, and I'm not sure you can use a decorator in this situation to wrap a class definition and keep it picklable at the module-level. I thought you could possibly update the definition in the
globals()dictionary, but even this doesn't seem to help here. Explicitly calling the decorator as a function, makes things picklable, but then you lose the extra class docstring information that was defined. i.e. here I get information about functools.partial in the docstring.I guess I'm somewhat curious if there is some more magic here that can be done to make this actually work... but I'm also wondering how much this is actually gaining over just explicitly defining these named classes in the module (and defining
self._scale/self._trfmanually). Note that I'm not saying to remove the function for easily making these norms from scales, but rather just explicitly defining the 4 module-level Norms that use the decorator may be the better route here.anntzer commentedon Mar 20, 2022
I guess the way out may(?) be to just explicitly check for the condition we want:
(then _picklable_norm_constructor can be rewritten to use _create_empty_object_of_class) (also, probably needs some error checking e.g. getattr failing should just fall back to the old path)
At least the following now holds:
As usual, feel free to pick up the patch.