Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pickling scipy interp1d spline

Tags:

python

scipy

I wonder if there is a simple way to pickle an 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 raises following 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
like image 902
cel Avatar asked Oct 01 '15 09:10

cel


People also ask

How does Scipy interp1d work?

The interp1d() function of scipy. interpolate package is used to interpolate a 1-D function. It takes arrays of values such as x and y to approximate some function y = f(x) and then uses interpolation to find the value of new points.

What is a spline in Scipy?

Spline interpolation is a type of piecewise polynomial interpolation method. The SciPy API provides several functions to implement the interpolation method for a given dataset. In this tutorial, you'll learn how to apply interpolation for a given dataset by using SciPy API functions in Python.

What does interp1d do python?

The function interp1d() is used to interpolate a distribution with 1 variable. It takes x and y points and returns a callable function that can be called with new x and returns corresponding y .

What is interpolation in Scipy?

Interpolation is a technique of constructing data points between given data points. The scipy. interpolate is a module in Python SciPy consisting of classes, spline functions, and univariate and multivariate interpolation classes. Interpolation is done in many ways some of them are : 1-D Interpolation.


2 Answers

maybe wrapping it in another class with __getstate__ and __setstate__ methods:

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])
like image 62
Vahid Avatar answered Oct 15 '22 14:10

Vahid


Hi if you are willing to use a different package you could use dill instead of pickle:

import dill as pickle
import scipy.interpolate as interpolate
import numpy as np 

interpolation = interpolate.interp1d(np.arange(0,10), np.arange(0,10))
with open("test_interp", "wb") as dill_file:
     pickle.dump(inv_cdf, dill_file)
with open("test_interp", "rb") as dill_file:
     interpolation = pickle.load(dill_file)

Works on my Python 3.6

like image 37
Sebastiano1991 Avatar answered Oct 15 '22 15:10

Sebastiano1991