0
\$\begingroup\$

I have a csv file with many columns (e.g. A,B,C,D,E and F), and I want to create an 2D interpolant that can calculate new values based on interpolating inbetween the data.

All interpolants are created using the same csv file, but I only want to instantiate each interpolant once (e.g on import).

Here's what I have so far:

# interpolants.py

import numpy as np
import pandas as pd
from scipy.interpolate import griddata


def create_interpolant(filepath, columns):
    df = pd.read_csv(filepath)
    data = df[columns]

    # Split up the data into three separate arrays
    [x, y, z] = np.array_split(data.drop_duplicates().values, 3, axis=1)

    # Flatten all 2D arrays into 1D arrays
    x = x.flatten()
    y = y.flatten()
    z = z.flatten()

    def fun(a, b):

        # Create array of values at which to sample
        # FIXME: np.vstack needs a sequence, not a generator
        samples = np.vstack(np.broadcast(a, b))

        # Create griddata interpolant
        # FIXME: np.vstack needs a sequence, not a generator
        out = griddata(np.vstack(np.broadcast(x, y)), z, xi=samples, method="linear")
        return out

    return fun


calcC = create_interpolant(
    filepath='my.csv',
    columns=["A", "B", "C"],
)

calcD = create_interpolant(
    filepath='my.csv',
    columns=["A", "B", "D"],
)
.
.
.

And then I would like to be able to use these interpolants like so:

# main.py

import numpy as np
from interpolants import calcC, calcD

x = np.random.rand(100)
y = np.random.rand(100)

c = calcC(x, y)
d = calcD(x, y)

The problem is that I can't add any kind of docstring to these interpolants. I would like to be able to add unique docstrings and other personalized features to each interpolant, without having to rewrite each independently. How can I improve on what I have here?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.