scipy.interpolate.RectBivariateSpline calculates completely wrong values for certain data

Viewed 23

I am creating two numpy 2D arrays, which contain some calculated data (one, named Z, resembles a semisphere, the other one, named F, is geometrical stuff). Since the edges are filled with nans, I convert the nans to the minimum value of each individual array. Then I'd like to evaluate them along a given line, which is here from (x0,y0) to (x0,y). For this I create a spline, using scipy.interpolate.BiRectVariateSpline, and then sample it using the call feature and two numpy arrays for the coordinates.

import numpy as np
import matplotlib.pyplot as plt
import sys
import warnings
import scipy.interpolate as intp
import scipy.integrate as intg

radius = 15.
gridsize = 0.1
curvature = 35.
height = 7.
hmax = np.sqrt(curvature**2-radius**2)
spectrum = np.arange(-radius,radius+gridsize,gridsize)


X,Y = np.meshgrid(spectrum,spectrum)
Z = np.where(np.sqrt(X**2+Y**2)<=radius, np.sqrt(curvature**2-np.sqrt(X**2+Y**2)**2)-hmax+height-curvature+hmax, np.nan)

F = np.where(np.sqrt(X**2+Y**2)<=radius, 10000, np.nan)
F = (np.sin(np.arctan(np.abs(1 / np.gradient(Z, gridsize)[0]))) * F)

dev = Z
middle = int((len(dev[0,:])+1)/2)
dev = np.nan_to_num(dev, nan = np.nanmin(dev[:,middle]))


x0 = 0.
y0 = -15.
y = 15.
spline = intp.RectBivariateSpline(x = X[0,:], y = Y[:,0], z = dev, kx = 3, ky = 3, s = 0.)
ydata = np.arange(y0,y+gridsize,step = gridsize)
xdata = np.full_like(ydata,x0)
data = spline.__call__(x = xdata, y = ydata, grid = False)

plt.plot(ydata,dev[:,middle], color = "orange", label = "original data")
plt.plot(ydata,data, ls = "dashed", color = "blue", label = "interpolated data")
plt.title("Plot of Z")
#plt.xlim(-15.2,-14.7)
#plt.ylim(3.5,4.0)
plt.legend()
#plt.savefig("Zplot.png")
plt.show()

For the data of Z this works fine, resulting in the following plot for the given cut:

Z data and it's all fine.

In addition the edge looks fine, even though it was filled with the minimum value and could thus interfere with the interpolation:

Z edge detail and it's all fine.

However, when calculating the spline over the data of F(achievable by setting dev = F), the same method goes horribly wrong and what I get from this is far from the interpolation quality of Z. Actually it's nowhere close to F for the majority of the data:

Plot of F, which shows the problem.

Looking at the edge, which seems to be the culprit on the first glance, doesn't clear things up:

Edge of the plot of F

I tried playing with the parameters of the split, kx, ky and the smoothing factor s, to solve this problem. kx and ky lead to pretty much the same result. Increasing s will first of all lead to no result at all (spline filled with nans) and, for a very high values of s, approach some kind of parabolic shape:

Plot of F with smoothing factor 8e14

Does anyone have an idea what's wrong either with the calculation or my code?

0 Answers
Related