Etched scipy interp1d spline

I wonder if there is an easy way to uncover the interp1d object in scipy. The naive approach does not seem to work.

 import pickle import numpy as np from scipy.interpolate import interp1d x = np.linspace(0,1,10) y = np.random.rand(10) sp = interp1d(x, y) with open("test.pickle", "wb") as handle: pickle.dump(sp, handle) 

This happens after PicklingError:

 --------------------------------------------------------------------------- PicklingError Traceback (most recent call last) <ipython-input-1-af4e3326e7d1> in <module>() 10 11 with open("test.pickle", "wb") as handle: ---> 12 pickle.dump(sp, handle) PicklingError: Can't pickle <function interp1d._call_linear at 0x1058abf28>: attribute lookup _call_linear on scipy.interpolate.interpolate failed 
+6
source share
1 answer

maybe wrap it in another class using the __getstate__ and __setstate__ :

 from scipy.interpolate import interp1d class interp1d_picklable: """ class wrapper for piecewise linear function """ def __init__(self, xi, yi, **kwargs): self.xi = xi self.yi = yi self.args = kwargs self.f = interp1d(xi, yi, **kwargs) def __call__(self, xnew): return self.f(xnew) def __getstate__(self): return self.xi, self.yi, self.args def __setstate__(self, state): self.f = interp1d(state[0], state[1], **state[2]) 
+3
source

All Articles