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+')
If your goal is to open arrays you saved with np.save
as memmaps, then you can just use np.load
with the option mmap_mode
:
fA = np.load('A.npy', mmap_mode='r')
fB = np.load('B.npy', mmap_mode='r')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With