python - How can I make np.save work for an ndarray subclass? -


i want able save array subclass npy file, , recover result later.

something like:

>>> class myarray(np.ndarray): pass >>> data = myarray(np.arange(10)) >>> np.save('fname', data) >>> data2 = np.load('fname') >>> assert isinstance(data2, myarray)  # raises assertionerror 

the docs says (emphasis mine):

the format explicitly not need to:

  • [...]
  • fully handle arbitrary subclasses of numpy.ndarray. subclasses accepted writing, array data written out. regular numpy.ndarray object created upon reading file. the api can used build format particular subclass, out of scope general npy format.

so possible make above code not raise assertionerror?

i don't see evidence np.save handles array subclasses.

i tried save np.matrix it, , got ndarray.

i tried save np.ma array, , got error

notimplementederror: maskedarray.tofile() not implemented yet. 

saving done np.lib.npyio.format.write_array, does

_write_array_header()   # save dtype, shape etc 

if dtype object uses pickle.dump(array, fp ...)

otherwise array.tofile(fp). tofile handles writing data buffer.

i think pickle.dump of array ends using np.save, don't recall how that's triggered.

i can example pickle array, , load it:

in [657]: f=open('test','wb') in [658]: pickle.pickler(f).dump(x) in [659]: f.close() in [660]: np.load('test') in [664]: f=open('test','rb') in [665]: pickle.load(f) 

this pickle dump/load sequence works test np.ma, np.matrix , sparse.coo_matrix cases. that's direction explore own subclass.

searching on numpy , pickle found preserve custom attributes when pickling subclass of numpy array. answer involves custom .__reduce__ , .__setstate__.


Comments