[Numpy-discussion] Pickling ufuncs?
Robert Kern
robert.kern at gmail.com
Mon Jan 29 19:52:36 EST 2007
Travis Oliphant wrote:
> Why don't you store the name of the ufunc instead:
>
> def __init__(self, a):
> self._a = a.__name__
>
> Then, whenever you are going to use the ufunc you do
>
> import numpy
> func = getattr(numpy,self._a)
>
> Then, pickle should work.
Or you can register pickler/unpickler functions for ufuncs:
In [24]: import copy_reg
In [25]: import numpy
In [26]: def ufunc_pickler(ufunc):
return ufunc.__name__
....:
In [28]: def ufunc_unpickler(name):
import numpy
return getattr(numpy, name)
....:
In [31]: copy_reg.pickle(numpy.ufunc, ufunc_pickler, ufunc_unpickler)
In [32]: import cPickle
In [33]: cPickle.dumps(numpy.add)
Out[33]: 'cnumpy.core.umath\nadd\np1\n.'
In [34]: cPickle.loads(cPickle.dumps(numpy.add))
Out[34]: <ufunc 'add'>
Note that this is a hack. It won't work for the ufuncs in scipy.special, for
example.
We should look into including __module__ information in ufuncs. This is how
regular functions are pickled.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list