I assume most of the time is taken by constructing the Delaunay
triangulation (scipy.spatial.Delaunay(points)).
> I'd like it if I could
> dump the baked interpolant to disk and then restore it on execution. Of
> course I probably need to generate the interpolant per machine but I can
> deal with that. Is there any cool way to do this?
Ideally, this would work:
import pickle
f = open('file.pck', 'wb')
pickle.dump(interpolator, f)
f.close()
but it doesn't work, since there's a small unnecessary technical snatch
(scipy.spatial.interpnd has the wrong __name__). This will be fixed in
the next version of Scipy, so a small workaround is needed in the
meantime:
class PickleableInterpolator(CloughTocher2DInterpolator):
def __getstate__(self):
return (self.tri, self.values, self.grad, self.is_complex,
self.fill_value, self.values_shape)
def __setstate__(self, data):
self.tri, self.values, self.grad, self.is_complex, \
self.fill_value, self.values_shape = data
self.points = self.tri.points
This kind of mucking around might break in future Scipy versions, so I
suggest you put it in 'if scipy.__version__ == "0.9.0":'
Note that pickling objects like this is quite brittle --- the internal
details of what needs to be pickled may change, so do not expect the
pickle files to work across different Scipy versions.
--
Pauli Virtanen
For most stuff, one should do
pickle.dump(interpolator, f, protocol=2)
so that all Numpy arrays get dumped as binary data rather than first
converted to text in the pickle stream.
> For most stuff, one should do
> pickle.dump(interpolator, f, protocol=2)
> so that all Numpy arrays get dumped as binary data rather than first
> converted to text in the pickle stream.
Or use joblib's pickler subclass:
http://packages.python.org/joblib/generated/joblib.dump.html
which can only be read using joblib's load function, but will pickle
numpy arrays as .npy, as thus be fast at save and load.
G