Hans
Hans

Reputation: 1379

How best to store a Python object for later evaluation?

I have written the following python code using a scipy function. The particular form of function f is irrelevant.

import numpy as np
from scipy.interpolate.interpnd import LinearNDInterpolator    

def f(x, y):
    s = np.hypot(x, y)
    phi = np.arctan2(y, x)
    tau = s + s*(1-s)/5 * np.sin(6*phi)
    return 5*(1-tau) + tau

npts = 200
px, py = np.random.choice(x, npts), np.random.choice(y, npts)
ip = LinearNDInterpolator((px, py), f(px,py))

What kind of object is ip? I would like to store it in a file and evaluate it later as for example below.

x = np.linspace(-1,1,100)
y = np.linspace(-1,1,100)
X, Y = np.meshgrid(x,y)
Ti = ip((X, Y))

What is a good way to store the object ip in a file?

Upvotes: 2

Views: 492

Answers (3)

Xukrao
Xukrao

Reputation: 8633

Your object ip is an instance of the class scipy.interpolate.interpnd.LinearNDInterpolator. If you want to store the complete object to file, then you could use the pickle module from python's standard library.

Pickling an object and writing it to a (binary) file is quite straightforward:

import pickle
with open('data.pickle', 'wb') as f:
    # Write object `ip` to file
    pickle.dump(ip, f, pickle.HIGHEST_PROTOCOL)

Later loading the object again from file is equally straightforward:

with open('data.pickle', 'rb') as f:
    # Read out object `ip` from file
    ip = pickle.load(f)

Pickling does have a few disadvantages that are worth mentioning. Firstly, the pickle file that you write is not human-readable. Secondly, pickle files received from an untrusted source should never be loaded because of security risks.

Upvotes: 5

Mad Physicist
Mad Physicist

Reputation: 114290

Given that you know f and have access to scipy, I would posit that the simplest thing to do would be to just store x and y. This would avoid many of the pitfalls of a binary dump and allow you to store in a text format.

To store, you could use np.savetxt to store the data:

np.savetxt('mystash.txt', np.stack((px, py), axis=1))

You can then have a simple loading procedure based on np.loadtxt:

def load_interp(fname):
    px, py = np.loadtxt(fname, unpack=True)
    return LinearNDInterpolator((px, py), f(px,py))

This solution produces a very human readable text file with two columns of data, corresponding to the matching entries of px and py. The file is completely portable and editable. It is language independent, all at the cost of one extra line in the loader.

Upvotes: 3

ycx
ycx

Reputation: 3211

You can consider pickle for Python specific object serialization. Otherwise, you have to consider a json or xml format. Visit the link to understand more

Upvotes: -1

Related Questions