[Numpy-discussion] Pickling ufuncs?
Travis Oliphant
oliphant at ee.byu.edu
Mon Jan 29 17:55:06 EST 2007
James A. Bednar wrote:
>Hi,
>
>Does anyone know whether it is possible to pickle and unpickle numpy
>ufuncs?
>
Not directly. Ufuncs are a built-in type and do not have the required
__reduce__ method needed to be pickleable. It could be added, but
hasn't been.
> I can't find anything about that on scipy.org or the mailing
>list archives. I have several important pieces of code that accept a
>numpy ufunc as an argument and later apply it to some data, while
>keeping a copy of the ufunc in an attribute. So far I have been able
>to pickle this code only by doing some very ugly hacks and
>workarounds.
>
>
Is storing the name considered an ugly hack?
>The ufuncs from Numeric 24 and earlier did not even deepcopy, which
>caused us lots of other problems, but deepcopying them works now in
>numpy 1.0.1. However, they still don't seem to pickle with the regular
>picklers. Is this deliberately disabled for some reason, or is there
>some workaround?
>
>
No, nothing has been "disabled." The feature was never added.
>Here's an example that illustrates the problem:
>
>
>I have a class Test defined in test.py:
>
>class Test(object):
> def __init__(self,a):
> self.a = a
>
>
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.
Alternatively you can write your own __reduce__ function for the Test
class.
Direct pickling of ufuncs is not a trivial issue as ufuncs contain
code-pointers at their core which cannot really be pickled. It's not a
simple problem to over-come in general. We could store the name of the
ufunc, but for user-defined ufuncs these might come from a different
package and which package the ufunc lives under is not stored as part of
the ufunc.
-Travis
More information about the NumPy-Discussion
mailing list