Manual pickling of numpy array with integer dtype if we don't have to fix import.
This is because if the an object has a reduce method, pickle will use it.
numpy.ndarray.__reduce__
's doc https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.reduce.html
"How numpy ndarray get pickled" does not reference the source code: How does Python 3 know how to pickle extension types, especially Numpy arrays?
In anceint time, Pickling with protocol 0 is not portable for floats, NaN, Inf:
https://mail.python.org/pipermail/tutor/2010-May/075980.html
Official doc on pickling floats on python 3.1 with text protocol:
https://docs.python.org/3/whatsnew/3.1.html
The new algorithm depends on certain features in the underlying
floating point implementation. If the required features are not found,
the old algorithm will continue to be used. Also, the text pickle
protocols assure cross-platform portability by using the old
algorithm.
numpy's source code for writing to a file is in format.write_array
and npyio.save
(https://github.com/numpy/numpy/blob/v1.13.0/numpy/lib/format.py,
https://github.com/numpy/numpy/blob/v1.13.0/numpy/lib/npyio.py#L435-L512)
The header can be saved as text.
The data is simply a pickle.dump
if pickling argument is true, which is the default value for numpy.save
.
In format.write_array
, I found:
pickle.dump(array, fp, protocol=2, **pickle_kwargs)
format.py
also says:
The .npy
format is the standard binary file format in NumPy for
persisting a single arbitrary NumPy array on disk. The format stores
all of the shape and dtype information necessary to reconstruct the
array correctly even on another machine with a different architecture.
So manual pickling of numpy array is cross platform (if we don't have to fix imports) because np.save
also use pickle and is cross platform.
np.save
uses protocol 2. The pickle.DEFAULT_PROTOCOL
is 3 for the two python 3 on the two machines.