GittingGud
GittingGud

Reputation: 335

Fast interpolation of a scattered DataFrame

TL;DR: Question: Is there a fast way to interpolate a scattered 2D-dataset at specific coordinates?

And if so could someone provide an example with the provided sample data and variables used from "Current Solution" (as I'm apparently to stupid to implement it myself).


Problem:

I need to interpolate (and if possible also extrapolate) a DataFrame (size = (34, 18)) of scattered data at specific coordinate points. The DataFrame stays always the same.

The interpolation need to be fast as it is done more than 10.000 times in a loop.

The coordinates at which will be interpolated are not know in advance as they change every loop.


Current Solution:

def Interpolation(a, b):

    #import external modules
    import pandas as pd
    from scipy import interpolate

    #reading .xlsx file into DataFrame
    file  = pd.ExcelFile(file_path)
    mr_df = file.parse('Model_References')
    matrix = mr_df.set_index(mr_df.columns[0])

    #interpolation at specific coordinates
    matrix = Matrix.stack().reset_index().values
    value = interpolate.griddata(matrix[:,0:2], matrix[:,2], (a, b), method='cubic')

    return(value)

This method is not acceptable for long time use as only the two lines of code under #interpolation at specific coordinates is more than 95% of the execution time.


My Ideas:


Sample Data:

          0.0     0.1     0.2     0.3
0.0      -407    -351    -294    -235
0.0001   -333    -285    -236    -185
0.0002   -293    -251    -206    -161
0.00021  -280    -239    -196    -151

no

Upvotes: 1

Views: 1099

Answers (1)

GittingGud
GittingGud

Reputation: 335

Thanks to @Jdog's comment I was able to figure it out:

The creation of a spline once before the loop with scipy.interpolate.RectBivariateSpline and the read out of specific coordinates with scipy.interpolate.RectBivariateSpline.ev decreased the execution time of the interpolation from 255s to 289ms.

def Interpolation(mesh, a, b):

    #interpolation at specific coordinates
    value = mesh.ev(stroke, current)

    return(value)

#%%

#import external modules
import pandas as pd
from scipy import interp

#reading .xlsx file into DataFrame
file  = pd.ExcelFile(file_path)
mr_df = file.parse('Model_References')
matrix = mr_df.set_index(mr_df.columns[0])

mesh = interp.RectBivariateSpline(a_index, b_index, matrix)

for iterations in loop:
    value = Interpolation(mesh, a, b)

Upvotes: 1

Related Questions