2

I writting large array so I'm using numpy memmap

At the end of my operation I would like to use flip function but the problem is, the flip function does not apply after using the flush function on memap

import numpy as np
from matplotlib import pyplot as plt

#Create array on disk

fpc = np.memmap("test3.rgb", dtype=np.uint8, mode='w+', shape=(10000,20000,3))
fpc[0:5000] = [255,0,0]     #Top red
fpc[5000:10000] = [0,255,0] #Bottom green


plt.subplot(131)
plt.title("Before flip")
plt.imshow(fpc)


#Apply flip function, so green come on top
fpc = np.flip(fpc,0)

plt.subplot(132)
plt.imshow(fpc)
plt.title("After flip")

#Save changes
fpc.flush()

#Reload array
fpc = np.memmap("test3.rgb", dtype=np.uint8, mode='r', shape=(10000,20000,3))


plt.subplot(133)
plt.title("After reload")
plt.imshow(fpc)
plt.show()

Result:

enter image description here

Is it really possible to use memmap with a flip function?

1 Answer 1

3

np.flip creates a new array in memory. You need to set the one mapped to the file. The current code only change the reference of fpc so to reference the new array. You can fix that with:

fpc[:,:] = np.flip(fpc,0)

UPDATE: note that if you use memmap to reduce the memory footprint, then doing a np.flip will defeat this optimization since a new array is created anyway. You can swap pairs of lines manually so to reduce the memory footprint. For better performance, you can flip&swap chunks of lines.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.