Repository navigation
Closed
sunpy/sunpy
#4877Labels
API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Consistency of the matplotlib API, including naming, behavior, defaults, …Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: transforms and scales
Milestone
Description
Bug report
Bug summary
As of 0d1d95c on the master branch it is no longer possible to deepcopy a LogNorm instance, which is a regression from 3.3.x.
Code for reproduction
import matplotlib.colors as mcolor
import copy
norm = mcolor.LogNorm()
copy.deepcopy(norm)Actual outcome
Traceback (most recent call last):
File "test.py", line 4, in <module>
copy.deepcopy(norm)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 270, in _reconstruct
state = deepcopy(state, memo)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 146, in deepcopy
y = copier(x, memo)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 230, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 172, in deepcopy
y = _reconstruct(x, memo, *rv)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 270, in _reconstruct
state = deepcopy(state, memo)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 146, in deepcopy
y = copier(x, memo)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 230, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "/Users/dstansby/miniconda3/envs/dev/lib/python3.8/copy.py", line 153, in deepcopy
y = copier(memo)
File "/Users/dstansby/github/matplotlib/lib/matplotlib/transforms.py", line 143, in __copy__
raise NotImplementedError(
NotImplementedError: TransformNode instances can not be copied. Consider using frozen() instead.
Expected outcome
No error
Matplotlib version
- Operating system:
- Matplotlib version:
- Matplotlib backend (
print(matplotlib.get_backend())): - Python version:
- Jupyter version (if applicable):
- Other libraries:
Metadata
Metadata
Assignees
Labels
API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Consistency of the matplotlib API, including naming, behavior, defaults, …Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.topic: transforms and scales
Activity
jklymak commentedon Jul 29, 2020
So norms now derive from scales (#16457), and scales have probably never been deep copyable.
Previously norms did not use the transform stack, but now that they are based on scales, they use it.
I guess a "fix" is to write a
__deepcopy__method for the scales if there is a strong need to calldeepcopy(I guess I can sort of see the use)dstansby commentedon Jul 29, 2020
The specific use case here is passing a norm to a plot method that autoscales vmin/vmax in place, but taking a copy to avoid modifying the vmin/vmax on the original norm object.
jklymak commentedon Jul 29, 2020
Implementing a
__deepcopy__of the scale seems straightforward enough, but it does require keeping a lot more of the scale init info around as extra private variables. Before doing all that (to all the scales) I wonder if anyone else has solutions...ie for
LogScalewe would need to keep track ofaxis_nameandnonpositivefrom the__init__.jklymak commentedon Jul 29, 2020
Ugh, but the same song and dance would have to be done for the
normas well because it holds its transform now. I guess the other alternative is implementing__deepcopy__for the transform.dstansby commentedon Jul 29, 2020
Does one need to implement a custom
__deepcopy__instead of just relying on the python default? I just removed the "Cannot be copied" error fromTransformNodeand at least my simple example worked fine.jklymak commentedon Jul 29, 2020
Yeah, I don't know why
deepcopymight be deemed problematic for transforms, or why thefrozenwas used instead.dstansby commentedon Jul 29, 2020
Looks like it's been around since forever (2007), with no explanation as to why the restriction on copying is there. Maybe I'll remove it and see what happens to the CI...
jklymak commentedon Jul 29, 2020
I doubt it will universally work - the
TransformNodesubclasses all havefrozendefined with special hooks into internal state. But maybe its all obsolete anddeepcopyis more robust these days?tacaswell commentedon Jul 29, 2020
frozenis an internal thing.The issue with transforms is that they are a branching partially-cached call graph. That is the
ax.transDataboth has a cached version of the transform from data space -> physical screen space, it knows that it is a compound of the figure transfrom, the dpi, transform, and the axes transform. If any of it's parents get changed it is notified that it has to be re-computed. I suspect that the number of links in that chain is why it is forbidden (along with the theory that it is better to not allowing something we don't need internally than it is to expose a wrong functionality).A way to implement deepcopy using things we do test is
new = pickle.loads(pickle.dumps(obj))jklymak commentedon Jul 29, 2020
Thanks @tacaswell
It seems to me that scales can use frozen transforms? i.e. they never depend on any children, do they? It seems to me that if a transform was frozen then copying it would be safe. But maybe I'm not following the subtleties.
QuLogic commentedon Jul 30, 2020
They could be frozen, but also, the transforms in
scale.pycould override__copy__? Just likeLogTransformoverrides.invertedto returnInvertedLogTransform.I don't think scales use an transforms outside of the
scale.pyones, except for the identity.QuLogic commentedon Jul 30, 2020
So
frozenjust returnsself, which seems the opposite of what you want forcopy, or at leastdeepcopy, doesn't it?jklymak commentedon Jul 30, 2020
So if I add
to
LogTransformandInvertedLogTransforminscalethenreturns what I'd expect:
I somewhat hesitate to do all this because the transform stack seems delicate, but given that
scale.pyis at the end, I think its OK...dstansby commentedon Dec 31, 2020
I'm going to be a little bit greedy, and mark this as Release critical. I think plotting multiple images with the same norm, but automatically autoscaling each one (which requires individual (deep)copies of the original norm object), is a reasonable use case, and this is a regression from 3.3.x.
jklymak commentedon Dec 31, 2020
I mean there is a PR for it. Does it not fix the problem? @tacaswell seemed to have some issue with it but I frankly did not fully understand what it was.