[SciPy-User] issue pickling an interp1d object

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] issue pickling an interp1d object

JohannCT
Dear Scipy-ers,
I am using scipy (0.15.1) to interpolate a fairly complicate double
integral for several parameters, for later use in yet a third integral.
The pickling is thus of a dict of interpolators. When I am using
InterpolatedUnivariateSpline my code runs smoothly and dump a pickled
file. But when I use interp1d (with default protocol 0), I crash :
Traceback (most recent call last):
     pickle.dump( interpolators, f )
   File "/usr/lib/python2.7/pickle.py", line 1370, in dump
     Pickler(file, protocol).dump(obj)
   File "/usr/lib/python2.7/pickle.py", line 224, in dump
     self.save(obj)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
     self._batch_setitems(obj.iteritems())
   File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
     save(v)
   File "/usr/lib/python2.7/pickle.py", line 306, in save
     rv = reduce(self.proto)
   File "/usr/lib/python2.7/copy_reg.py", line 77, in _reduce_ex
     raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__
cannot be pickled

When I set the protocol to -1, I get a different crash :
     pickle.dump( interpolators, f, protocol=-1 )
   File "/usr/lib/python2.7/pickle.py", line 1370, in dump
     Pickler(file, protocol).dump(obj)
   File "/usr/lib/python2.7/pickle.py", line 224, in dump
     self.save(obj)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
     self._batch_setitems(obj.iteritems())
   File "/usr/lib/python2.7/pickle.py", line 681, in _batch_setitems
     save(v)
   File "/usr/lib/python2.7/pickle.py", line 331, in save
     self.save_reduce(obj=obj, *rv)
   File "/usr/lib/python2.7/pickle.py", line 419, in save_reduce
     save(state)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 548, in save_tuple
     save(element)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
     self._batch_setitems(obj.iteritems())
   File "/usr/lib/python2.7/pickle.py", line 681, in _batch_setitems
     save(v)
   File "/usr/lib/python2.7/pickle.py", line 331, in save
     self.save_reduce(obj=obj, *rv)
   File "/usr/lib/python2.7/pickle.py", line 396, in save_reduce
     save(cls)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 748, in save_global
     (obj, module, name))
pickle.PicklingError: Can't pickle <type 'instancemethod'>: it's not
found as __builtin__.instancemethod

Does that ring a bell to anyone, before I start simplifying my code to
provide this list with a test case?
Thanks a lot in advance,
Johann
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: issue pickling an interp1d object

Max Shron-2
This isn't exactly an answer, but: 

interp1d itself is stateless, given its inputs. Why not pickle a dictionary of inputs, pass around the pickled dictionary, then give it as **kwargs on the other side? Something like:

pickle.dump({'x': [1,2,3], 'y': [4,5,6], 'kind': 'linear'}, file_obj)

params = pickle.load(file_obj)
ip = interp1d(**params)

On Thu, Sep 10, 2015 at 12:52 PM, Johann Cohen-Tanugi <[hidden email]> wrote:
Dear Scipy-ers,
I am using scipy (0.15.1) to interpolate a fairly complicate double
integral for several parameters, for later use in yet a third integral.
The pickling is thus of a dict of interpolators. When I am using
InterpolatedUnivariateSpline my code runs smoothly and dump a pickled
file. But when I use interp1d (with default protocol 0), I crash :
Traceback (most recent call last):
     pickle.dump( interpolators, f )
   File "/usr/lib/python2.7/pickle.py", line 1370, in dump
     Pickler(file, protocol).dump(obj)
   File "/usr/lib/python2.7/pickle.py", line 224, in dump
     self.save(obj)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
     self._batch_setitems(obj.iteritems())
   File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
     save(v)
   File "/usr/lib/python2.7/pickle.py", line 306, in save
     rv = reduce(self.proto)
   File "/usr/lib/python2.7/copy_reg.py", line 77, in _reduce_ex
     raise TypeError("a class that defines __slots__ without "
TypeError: a class that defines __slots__ without defining __getstate__
cannot be pickled

When I set the protocol to -1, I get a different crash :
     pickle.dump( interpolators, f, protocol=-1 )
   File "/usr/lib/python2.7/pickle.py", line 1370, in dump
     Pickler(file, protocol).dump(obj)
   File "/usr/lib/python2.7/pickle.py", line 224, in dump
     self.save(obj)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
     self._batch_setitems(obj.iteritems())
   File "/usr/lib/python2.7/pickle.py", line 681, in _batch_setitems
     save(v)
   File "/usr/lib/python2.7/pickle.py", line 331, in save
     self.save_reduce(obj=obj, *rv)
   File "/usr/lib/python2.7/pickle.py", line 419, in save_reduce
     save(state)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 548, in save_tuple
     save(element)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
     self._batch_setitems(obj.iteritems())
   File "/usr/lib/python2.7/pickle.py", line 681, in _batch_setitems
     save(v)
   File "/usr/lib/python2.7/pickle.py", line 331, in save
     self.save_reduce(obj=obj, *rv)
   File "/usr/lib/python2.7/pickle.py", line 396, in save_reduce
     save(cls)
   File "/usr/lib/python2.7/pickle.py", line 286, in save
     f(self, obj) # Call unbound method with explicit self
   File "/usr/lib/python2.7/pickle.py", line 748, in save_global
     (obj, module, name))
pickle.PicklingError: Can't pickle <type 'instancemethod'>: it's not
found as __builtin__.instancemethod

Does that ring a bell to anyone, before I start simplifying my code to
provide this list with a test case?
Thanks a lot in advance,
Johann
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user