Implementation of pickle serialization protocol in `SpatialImage`
For gnomon we need to be able to serialize SpatialImage
and TissueImage3D
. For that purpose we use pickle which is the standard way to serialize an object in python and in general it works well enough. And at first it seemed to work just fine, after all we had the same array before and after the serialization but it was just the array that was restored. The reason why is because numpy
implements two hooks of pickle's protocol: __reduce__()
and __setstate__()
. More specifically, pickle
will call __reduce__()
on the object to be serialized in order get the information necessary on how to recreate the object but it doesn't report anything on child class.
To fix this issue we implement those two methods (__reduce__()
and __setstate__()
) in SpatialImage
so that they are called instead of their numpy
counterparts to call those counterparts and complement them with info regarding SpatialImage
specifically.
As the __dict__
of the object is mostly included in the new state recorded it mimics the default behavior of pickle
and thus it seem to extend to child class too.
For more info regarding pickle see https://docs.python.org/fr/3/library/pickle.html#pickling-class-instances