I'm trying to create random matrix and save it in binary file using numpy.save
Then I try to map this file using numpy.memmap, but it seems it maps it wrong.
How to fix it?
It seems it read .npy header and I need to scip some bytes from begining.
rows=6
cols=4
def create_matrix(rows,cols):
data = (np.random.rand(rows,cols)*100).astype('uint8') #type for image [0 255] int8?
return data
def save_matrix(filename, data):
np.save(filename, data)
def load_matrix(filename):
data= np.load(filename)
return data
def test_mult_ram():
A= create_matrix(rows,cols)
A[1][2]= 42
save_matrix("A.npy", A)
A= load_matrix("A.npy")
print A
B= create_matrix(cols,rows)
save_matrix("B.npy", B)
B= load_matrix("B.npy")
print B
fA = np.memmap('A.npy', dtype='uint8', mode='r', shape=(rows,cols))
fB = np.memmap('B.npy', dtype='uint8', mode='r', shape=(cols,rows))
print fA
print fB
UPDATE:
I just found that already np.lib.format.open_memmap function exist.
usage: a = np.lib.format.open_memmap('A.npy', dtype='uint8', mode='r+')
The npy format has a header that must be skipped when using
np.memmap
. It starts with an 6-byte magic string,'\x93NUMPY'
, 2 byte version number, followed by 2 bytes header length, followed by header data.So if you open the file, find the header length, then you can compute the offset to pass to np.memmap:
If your goal is to open arrays you saved with
np.save
as memmaps, then you can just usenp.load
with the optionmmap_mode
:This way you actually benefit from the header stored in the
.npy
files, in the sense that it keeps track of the shape and dtype of the array.