jooooanna
jooooanna

Reputation: 27

Interpolation out of the range in Python

I am doing interpolation for some lists.

Some of my data is out of the range, how could I make a simple limit to solve this problem?

For example, one of my list look like this:

testList = [[(0.0, -0.9960135495794032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]]

I would like to do interpolation to find (x, y=0) and (x, y=-1).

I know for the left part [(0.0, -0.9960135495794032), (0.5, -1.0)] is out of the range for (x, y=0).

How could edit my code to make the result which list is out of the range directly equals to (x= 0, y=0)?

This is my code:

from scipy import interpolate

res_j = []
for j in range(0, 2, 1):
    res_k = []
    for k in range(0, 2, 1): 
        testList = [
                    [[(0.0, -0.9960135495794032), (0.5, -1.0)], [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]],
                    [[(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0)], [(5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)]],
                    ]

        x = [c[0] for c in testList[j][k]]
        y = [c[1] for c in testList[j][k]]
        # Enter y to find x: f(y) = x
        f = interpolate.interp1d(y, x)
        interpolate_result = (f(0), f(-1))
        res_k.append(interpolate_result)
    res_j.append(res_k)

print(res_j)

I am trying to get the result like:

[[(array(0), array(0.5)), (array(2.06474439), array(0.5))], [(array(4.57078944), array(5.)), (array(5.37357663), array(5.))]]

Upvotes: 1

Views: 10056

Answers (1)

mapf
mapf

Reputation: 2068

For these kind of questions, I generally recommend to first look at the documentation of the method / class / function / etc. that you are using. In this case, the documentation for scipy.interpolate.interp1d tells us all we need to know:

bounds_error: bool, optional

If True, a ValueError is raised any time interpolation is attempted on a value outside of the range of x (where extrapolation is necessary). If False, out of bounds values are assigned fill_value. By default, an error is raised unless fill_value="extrapolate".

and:

fill_value: array-like or (array-like, array_like) or “extrapolate”, optional

  • if a ndarray (or float), this value will be used to fill in for requested points outside of the data range. If not provided, then the default is NaN. The array-like must broadcast properly to the dimensions of the non-interpolation axes.

Since you want values outside the value range to default to 0, this is all you need:

from scipy import interpolate

testList = [
    [
        [(0.0, -0.9960135495794032), (0.5, -1.0)],
        [(0.5, -1.0), (2.0, -0.16138322487766676), (2.5, 1.0849272141417852)]
    ],
    [
        [(4.0, 3.3149805356833015), (4.5, 0.1649293864654484), (5.0, -1.0)],
        [(5.0, -1.0), (5.5, 0.33841349597101744), (6.0, 4.702347949145297)]
    ],
]

results = []
for subset in testList:
    sub_result = []
    for dataset in subset:
        x = [coord[0] for coord in dataset]
        y = [coord[1] for coord in dataset]
        # Enter y to find x: f(y) = x
        f = interpolate.interp1d(y, x, bounds_error=False, fill_value=0)
        interpolate_result = (f(0), f(-1))
        sub_result.append(interpolate_result)
    results.append(sub_result)

print(results)

If the documentation doesn't tell you what you are looking for, simply searching the internet for the error code can produce helpful results, because chances are, somebody else has already inquired about a similar problem. In your case, when searching for ValueError: A value in x_new is above the interpolation range., this is the first result. Searching for the error code usually means much less effort (for everybody involved), than asking the question yourself.

Upvotes: 4

Related Questions