scipy 0.11.0 to 0.12.0 changes scipy.interpolate.interp1d, breaks constantly updated interpolator
Hello all, I have been playing around with a package that uses a linear scipy.interpolate.interp1d to create a history function for the ode solver in scipy, described here<http://zulko.wordpress.com/2013/03/01/delay-differential-equations-easy-with-python/> . The relevant bit of code goes something like def update(self, ti, Y): """ Add one new (ti, yi) to the interpolator """ self.itpr.x = np.hstack([self.itpr.x, [ti]]) yi = np.array([Y]).T self.itpr.y = np.hstack([self.itpr.y, yi]) self.itpr.fill_value = Y Where "self.itpr" is initialized in __init__: def __init__(self, g, tc=0): """ g(t) = expression of Y(t) for t<tc """ self.g = g self.tc = tc # We must fill the interpolator with 2 points minimum self.itpr = scipy.interpolate.interp1d( np.array([tc-1, tc]), # X np.array([self.g(tc), self.g(tc)]).T, # Y kind='linear', bounds_error=False, fill_value = self.g(tc)) Where g is some function that returns an array of values that are solutions to a set of differential equations and tc is the current time. This seems nice to me because a new interpolator object doesn't have to be re-created every time I want to update the ranges of values (which happens at each explicit time step during a simulation). This method of updating the interpolator works well under scipy v 0.11.0. However, after updating to v 0.12.0 I ran into issues. I see that the new interpolator now includes an array _y<https://github.com/scipy/scipy/blob/master/scipy/interpolate/interpolate.py#L390>. Is it safe and/or sane to just update _y as outlined above as well? Is there a simpler, more pythonic way to address this that would hopefully be more robust to future updates in scipy? Again, in v 0.11 everything works well and expected results are produced, and in v 0.12 I get an IndexError when _y is referenced<https://github.com/scipy/scipy/blob/master/scipy/interpolate/interpolate.py#L409>as it isn't updated in my function while y itself is. Any help/pointers would be appreciated! Thanks! Ben Evans
On Fri, Jul 5, 2013 at 10:46 PM, Benjamin Evans <b.evans@yale.edu> wrote:
Hello all,
I have been playing around with a package that uses a linear scipy.interpolate.interp1d to create a history function for the ode solver in scipy, described here<http://zulko.wordpress.com/2013/03/01/delay-differential-equations-easy-with-python/> .
The relevant bit of code goes something like
def update(self, ti, Y): """ Add one new (ti, yi) to the interpolator """ self.itpr.x = np.hstack([self.itpr.x, [ti]]) yi = np.array([Y]).T self.itpr.y = np.hstack([self.itpr.y, yi]) self.itpr.fill_value = Y
Where "self.itpr" is initialized in __init__:
def __init__(self, g, tc=0): """ g(t) = expression of Y(t) for t<tc """
self.g = g self.tc = tc # We must fill the interpolator with 2 points minimum self.itpr = scipy.interpolate.interp1d( np.array([tc-1, tc]), # X np.array([self.g(tc), self.g(tc)]).T, # Y kind='linear', bounds_error=False, fill_value = self.g(tc))
Where g is some function that returns an array of values that are solutions to a set of differential equations and tc is the current time.
This seems nice to me because a new interpolator object doesn't have to be re-created every time I want to update the ranges of values (which happens at each explicit time step during a simulation). This method of updating the interpolator works well under scipy v 0.11.0. However, after updating to v 0.12.0 I ran into issues. I see that the new interpolator now includes an array _y<https://github.com/scipy/scipy/blob/master/scipy/interpolate/interpolate.py#L390>. Is it safe and/or sane to just update _y as outlined above as well? Is there a simpler, more pythonic way to address this that would hopefully be more robust to future updates in scipy? Again, in v 0.11 everything works well and expected results are produced, and in v 0.12 I get an IndexError when _y is referenced<https://github.com/scipy/scipy/blob/master/scipy/interpolate/interpolate.py#L409>as it isn't updated in my function while y itself is.
Any help/pointers would be appreciated!
There's a ticket with discussion here: https://github.com/scipy/scipy/issues/2621 Ralf
05.07.2013 23:46, Benjamin Evans kirjoitti: [clip]
Is it safe and/or sane to just update _y as outlined above as well? Is there a simpler, more pythonic way to address this that would hopefully be more robust to future updates in scipy? Again, in v 0.11 everything works well and expected results are produced, and in v 0.12 I get an IndexError when _y is referenced <https://github.com/scipy/scipy/blob/master/scipy/interpolate/interpolate.py#L409> as it isn't updated in my function while y itself is.
The short answer is that interp1d does not currently support what you are trying to do. Since it supports also spline interpolants, it is in general not possible to update the interpolant online. -- Pauli Virtanen
20.07.2013 18:04, Pauli Virtanen kirjoitti: [clip]
The short answer is that interp1d does not currently support what you are trying to do. Since it supports also spline interpolants, it is in general not possible to update the interpolant online.
To clarify: the easiest way is to recompute the spline coefficients when points are added, but this the same cost as reconstructing the whole interpolant so it's not really an on-line operation. It's probably possible to extend B-splines cheaply when points are added, but implementing this takes some work. If someone wants to take on adding an `add_points` method to the interpolator, that would be useful. For this particular use case, it can be useful even if it works only for linear interpolants (and raises an exception for the spline ones). -- Pauli Virtanen
participants (3)
-
Benjamin Evans
-
Pauli Virtanen
-
Ralf Gommers