Test dir rename, again
[pyplotsuite.git] / plotfile / interpolation.py
blob6d6514779107aadb997817d3f40f121ca2220fc4
1 """
2 Provides some UnivariateSpline child classes for alternative interpolation
3 methods.
4 """
6 from numpy import log, exp, e
7 import scipy.interpolate as SI
9 class eSpline(SI.UnivariateSpline):
10 """
11 Spline in Y log scale (with fized e base). Simple prototype class.
12 """
14 def __init__(self, x, y, w=None, bbox=[None, None], k=3, s=None):
15 """
16 For a description of the parameters see UnivariateSpline class.
17 """
19 y_ = log(y)
20 SI.UnivariateSpline.__init__(self, x=x, y=y_, s=s, k=k, bbox=bbox, w=w)
22 def __call__(self, x, nu=None):
23 res = SI.UnivariateSpline.__call__(self, x, nu)
24 return exp(res)
27 class aSpline(SI.UnivariateSpline):
28 """
29 Base class for splines calculated in a generic coordinate system.
30 """
31 def __init__(self, x, y, fun, inv, both=False,
32 w=None, bbox=[None, None], k=3, s=None):
33 """
34 Besised the starndard UnivariateSpline parameters we have:
35 @fun: the function to be applied to the input data 'y' and 'x'
36 @inv: the inverse function to be applied to the returned data
37 @both: if the transformation should be applied to both axes or only Y
38 """
39 self.__fun = fun
40 self.__inv = inv
41 self.both = both
42 y_ = fun(y)
43 if both:
44 x_ = fun(x)
45 else:
46 x_ = x
47 SI.UnivariateSpline.__init__(self, x=x_, y=y_, s=s, k=k,
48 bbox=bbox, w=w)
50 def __call__(self, x, nu=None):
51 """
52 Same parameters as UnivariateSpline.
53 """
54 if self.both:
55 x_ = self.__fun(x)
56 else:
57 x_ = x
58 res = SI.UnivariateSpline.__call__(self, x_, nu)
59 return self.__inv(res)
62 class eeSpline(aSpline):
63 """
64 Exponential spline: a spline calculated in log scale.
65 """
67 def __init__(self, x, y, base=e, w=None, bbox=[None, None], k=3, s=None):
68 """
69 Besides usual UnivariateSpline parameters a 'base' for the exponetial
70 transformation can be specified.
71 """
72 self.base = base
74 # Workaround for bad interpolation with s=0
75 if s == 0: s = len(x)*1e-6
77 aSpline.__init__(self, x=x, y=y, both=False,
78 fun = lambda x: log(x)/log(self.base),
79 inv = lambda x: self.base**x,
80 s=s, k=k, bbox=bbox, w=w)