You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
dump_session() seems to fail when used with ipython notebook set up for inline plots. When the %matplotlib inline magic is used you get a traceback resulting in PicklingError: Can't pickle 'RendererAgg' object: <RendererAgg object at 0x42a2bb8>
and when using the --pylab=inline option at start up you get a traceback resulting in PicklingError: Can't pickle <class 'matplotlib.axes.AxesSubplot'>: it's not found as matplotlib.axes.AxesSubplot
@niallrobinson: This is a known issue for pickling of matplotlib objects. Several years ago, at a scipy sprint, I tried see how far dill could go with pickling matplotlib objects, and ran into issues on the objects you list above. There's some "tricky" things that are being done in matplotlib with weakrefs and the like that make the objects unpicklable, even with dill, for the moment. I'd need to talk to the matplotlib team. In many cases, it's just a matter of moving a line of code (say an import) to another level in the object hierarchy.
ipython is a good use case for why matplotlib objects should be picklable... also it could allow matplotlib to be sped up by doing parallel computing on each of the different graphics layers. The good news is that once it would work with matplotlib objects, then it'd work in ipython. This will take some time, however, and someone on the matplotlib team's interest in getting it done. It looks like the right guys to ping about this initially are @ivanov and @fperez. There are some things that I can do in dill to help it out, but it's really a case of dev in matplotlib... and that means it be a big help if someone there is interested in it happening.
This one is really for the matplotlib core team, pinging @mdboom so he can pitch in or relay it to the others. I'm not active in the core of mpl to the level this requires, I'm afraid.
As @mmckerns says, once the necessary support goes in for matplotlib, it will just work transparently in IPython.
What version of matplotlib are you on? For 1.3.0, @pelson added a great deal of support for pickling... But I haven't tried what you describe to confirm.
@mdboom: Last I tried was in version 1.1.0 and 1.2.0. So, I'm a few months off the current one. I will update to 1.3.0 and give it a try. I'm not sure about what version of matplotlib the reporter @niallrobinson is on.
If you want to test pickling anything with dill, all you have to do is type: import dill.
Pickle's API is a subset of dill's, and the code above loads dill's known types into pickle's registry...
so you can then go about using pickle if your code is pickling with pickle and not with cPickle.
Install dill from the github site, if you want to give it a try. It's a very small pure python package with no dependencies.
For 1.3.0, @pelson added a great deal of support for pickling
Yep. I think we labelled it "experimental" in the what's new, but certainly anything that isn't too fruity can be pickled in v1.3.0. Anything that remains is an oversight and can be considered a bug, so please fire away bug reports to the mpl tracker and I'll get on to them.
@niallrobinson: This should probably move over to matplotlib's issue tracker -- please bug report your issue there. However, I'd appreciate keeping me in the loop... I may be able to help with the serialization. I'll update and try myself (and I may want to link the related mpl issue from here).
We don't store the RenderAgg object when pickling figures (in fact, one of the cool features is that you can pickle a figure from one backend, and re-attach it to another, that is probably news to even @mdboom 😉) so I think there is probably some deep inspection of all objects which exist in the current session which is being a little too deep, it's uncertain to me where that inspection lies: it could be IPython or it could be dill...
@pelson: So the issue that started this thread is that one of the things that dill provides is a serialization of an entire active interpreter session, where you can start a new interpreter and then resume the pickled session where you left off. That means, if you have an Agg object in the interpreter session (esp. in __main__), dill's going to try to serialize it. So, even if matplotlib doesn't need to use the Agg object to pickle a plot, requiring the Agg object be available in __main__ creates an issue for anyone that's going to try to save the current session... something desirable in IPython. Maybe that might be a focus of discussion going forward. Dill has special handlers for a very few of the IPython objects (e.g. the "exit" singleton) that need to be in __main__ for IPython, but don't need to be serialized to save the state of the interpreter. With the Agg, we may be in a similar boat. The thing to try is to create a plot, and then do:
import dill
dill.dump_session('mpl_test.pkl')
That will show what needs work for an interpreter session with a plot to be serialized.
I have a debug flag that can be turned on with dill._trace(True), but it's the same strings that would be seen in the traceback when something fails.
Hi, @mmckerns, how is this issuing coming along? I recently tried dill.dump_session() in a ipython notebook and still got the same error. I am using ipython 2.1 and matplotlib 1.4. As you mentioned, the ability to serialize entire active interpreter session and then resume the pickled session later is a big plus for ipython notebook. I'd like to know if there is workarounds or alternatives I can try!
@jianlingzh: It would be a very cool feature for IPython... however, this issue is still very much open, and to be honest, I haven't touched it in a long while. It'd take digging into the guts of both matplotlib (and possibly how IPython handles matplotlib)… and I've not done it.
I wasn't able to reproduce this with mpl v1.3.1, IPython 2.2, and dill 0.2.1 - @jianlingzh, could you produce a notebook which I can run to reproduce the problem exactly? (a gist would be great)
@pelson, this isn't hard for me to reproduce. Just a couple of lines:
a = range(10)
dill.dump_session('test.pkl')
will show the error that started this thread. I tried matplotlib 1.4.0, dill 0.2.1 on both IPython 2.1 and 2.2, both created the error. Did you have the pylab = 'inline' config in your ipython notebook config file?
I have added a bunch of pickling error detection and introspection methods since this thread started -- in dill.detect. If you are going to do some poking around, they are good tools. See also the complimentary use of objgraph (#58).
Did you have the pylab = 'inline' config in your ipython notebook config file?
No! This is the reason... I never use pylab, I cannot recommend highly enough just using the proper namespaces. i.e.:
from numpy import np
from matplotlib.pyplot import plt
That said, to reproduce this without IPython (or pylab):
$> python -c "import matplotlib; matplotlib.use('tkagg'); import matplotlib.pyplot as plt; import dill; s = plt.subplot(1, 2, 1); plt.draw(); r = dill.dumps(s);"
That's:
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
import dill
s = plt.subplot(1, 2, 1)
# Put the renderer on the figure.
plt.draw()
r = dill.dumps(s)
And the error:
pickle.PicklingError: Can't pickle 'RendererAgg' object: <RendererAgg object at 0xxxxxxx>
Essentially, I can reproduce this with Pickle alone, so this isn't a IPython or a Dill issue - I'll go ahead and create an issue on matplotlib, and we can close this one.
P.S. The dill.detect module looks really useful! Thanks for the pointer.
@pelson: The above issue is really out of scope for dill, however, if you do find any issues that you are having in trying to do the above, please either report here or in a new ticket. Also anything pickling-related you want to ping me about, please do.
Activity
mmckerns commentedon Sep 18, 2013
@niallrobinson: This is a known issue for pickling of matplotlib objects. Several years ago, at a scipy sprint, I tried see how far dill could go with pickling matplotlib objects, and ran into issues on the objects you list above. There's some "tricky" things that are being done in matplotlib with weakrefs and the like that make the objects unpicklable, even with dill, for the moment. I'd need to talk to the matplotlib team. In many cases, it's just a matter of moving a line of code (say an import) to another level in the object hierarchy.
ipython is a good use case for why matplotlib objects should be picklable... also it could allow matplotlib to be sped up by doing parallel computing on each of the different graphics layers. The good news is that once it would work with matplotlib objects, then it'd work in ipython. This will take some time, however, and someone on the matplotlib team's interest in getting it done. It looks like the right guys to ping about this initially are @ivanov and @fperez. There are some things that I can do in dill to help it out, but it's really a case of dev in matplotlib... and that means it be a big help if someone there is interested in it happening.
fperez commentedon Sep 19, 2013
This one is really for the matplotlib core team, pinging @mdboom so he can pitch in or relay it to the others. I'm not active in the core of mpl to the level this requires, I'm afraid.
As @mmckerns says, once the necessary support goes in for matplotlib, it will just work transparently in IPython.
mdboom commentedon Sep 19, 2013
What version of matplotlib are you on? For 1.3.0, @pelson added a great deal of support for pickling... But I haven't tried what you describe to confirm.
mmckerns commentedon Sep 19, 2013
@mdboom: Last I tried was in version 1.1.0 and 1.2.0. So, I'm a few months off the current one. I will update to 1.3.0 and give it a try. I'm not sure about what version of matplotlib the reporter @niallrobinson is on.
If you want to test pickling anything with dill, all you have to do is type:
import dill.Pickle's API is a subset of dill's, and the code above loads dill's known types into pickle's registry...
so you can then go about using pickle if your code is pickling with
pickleand not withcPickle.Install dill from the github site, if you want to give it a try. It's a very small pure python package with no dependencies.
pelson commentedon Sep 19, 2013
Yep. I think we labelled it "experimental" in the what's new, but certainly anything that isn't too fruity can be pickled in v1.3.0. Anything that remains is an oversight and can be considered a bug, so please fire away bug reports to the mpl tracker and I'll get on to them.
niallrobinson commentedon Sep 19, 2013
I'm currently on 1.2.0rc2, so I'll see if 1.3 sorts any of the problems.
niallrobinson commentedon Sep 19, 2013
Unfortunately I get the same behaviour with matplotlib 1.3
mmckerns commentedon Sep 19, 2013
@niallrobinson: This should probably move over to matplotlib's issue tracker -- please bug report your issue there. However, I'd appreciate keeping me in the loop... I may be able to help with the serialization. I'll update and try myself (and I may want to link the related mpl issue from here).
pelson commentedon Sep 19, 2013
We don't store the RenderAgg object when pickling figures (in fact, one of the cool features is that you can pickle a figure from one backend, and re-attach it to another, that is probably news to even @mdboom 😉) so I think there is probably some deep inspection of all objects which exist in the current session which is being a little too deep, it's uncertain to me where that inspection lies: it could be IPython or it could be dill...
mmckerns commentedon Sep 19, 2013
@pelson: So the issue that started this thread is that one of the things that dill provides is a serialization of an entire active interpreter session, where you can start a new interpreter and then resume the pickled session where you left off. That means, if you have an Agg object in the interpreter session (esp. in
__main__), dill's going to try to serialize it. So, even if matplotlib doesn't need to use the Agg object to pickle a plot, requiring the Agg object be available in__main__creates an issue for anyone that's going to try to save the current session... something desirable in IPython. Maybe that might be a focus of discussion going forward. Dill has special handlers for a very few of the IPython objects (e.g. the "exit" singleton) that need to be in__main__for IPython, but don't need to be serialized to save the state of the interpreter. With the Agg, we may be in a similar boat. The thing to try is to create a plot, and then do:That will show what needs work for an interpreter session with a plot to be serialized.
I have a debug flag that can be turned on with
dill._trace(True), but it's the same strings that would be seen in the traceback when something fails.mmckerns commentedon Sep 19, 2013
I'll be investigating as soon as I can.
niallrobinson commentedon Nov 7, 2013
Does anyone know if anyone has had a chance to look at this? Not meaning to pester, just thought I'd ask in case I'd missed it.
mmckerns commentedon Nov 7, 2013
@niallrobinson: I have not looked at it yet. I've been tied up for a few weeks, but should get to it soon.
qkhhly commentedon Oct 3, 2014
Hi, @mmckerns, how is this issuing coming along? I recently tried dill.dump_session() in a ipython notebook and still got the same error. I am using ipython 2.1 and matplotlib 1.4. As you mentioned, the ability to serialize entire active interpreter session and then resume the pickled session later is a big plus for ipython notebook. I'd like to know if there is workarounds or alternatives I can try!
mmckerns commentedon Oct 3, 2014
@jianlingzh: It would be a very cool feature for IPython... however, this issue is still very much open, and to be honest, I haven't touched it in a long while. It'd take digging into the guts of both matplotlib (and possibly how IPython handles matplotlib)… and I've not done it.
qkhhly commentedon Oct 5, 2014
Thanks, @mmckerns! Is there any way to work around this currently? Like ask dill to ignore certain matplotlib objects when dumping the session?
mmckerns commentedon Oct 5, 2014
I don't know of any current workaround, but having a list or dict of
objects-to-ignoreindump_sessionis an interesting idea. Hmm.pelson commentedon Oct 8, 2014
I wasn't able to reproduce this with mpl v1.3.1, IPython 2.2, and dill 0.2.1 - @jianlingzh, could you produce a notebook which I can run to reproduce the problem exactly? (a gist would be great)
qkhhly commentedon Oct 8, 2014
@pelson, this isn't hard for me to reproduce. Just a couple of lines:
will show the error that started this thread. I tried matplotlib 1.4.0, dill 0.2.1 on both IPython 2.1 and 2.2, both created the error. Did you have the pylab = 'inline' config in your ipython notebook config file?
mmckerns commentedon Oct 8, 2014
I have added a bunch of pickling error detection and introspection methods since this thread started -- in
dill.detect. If you are going to do some poking around, they are good tools. See also the complimentary use ofobjgraph(#58).pelson commentedon Oct 9, 2014
No! This is the reason... I never use pylab, I cannot recommend highly enough just using the proper namespaces. i.e.:
That said, to reproduce this without IPython (or pylab):
That's:
And the error:
pickle.PicklingError: Can't pickle 'RendererAgg' object: <RendererAgg object at 0xxxxxxx>Essentially, I can reproduce this with Pickle alone, so this isn't a IPython or a Dill issue - I'll go ahead and create an issue on matplotlib, and we can close this one.
P.S. The
dill.detectmodule looks really useful! Thanks for the pointer.mmckerns commentedon Oct 9, 2014
@pelson: The above issue is really out of scope for
dill, however, if you do find any issues that you are having in trying to do the above, please either report here or in a new ticket. Also anything pickling-related you want to ping me about, please do.mmckerns commentedon May 31, 2015
FYI: this likely has become a lot easier as of #101.