Created
January 23, 2023 13:13
-
-
Save gauteh/602f8c9dd88e7bb8772750a4909690c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from numpy.lib.format import open_memmap | |
shape = (1, 20, 600, 2160) | |
dtype = np.float32 | |
# Open a memory mapped numpy formatted file for a temporary variable | |
data = open_memmap('temp.npy', dtype = dtype, mode = 'w+', shape = shape) | |
data[:] = np.random.rand(*shape) | |
data.flush() # force write disk | |
test = np.array(data) # copy to memory for checking below | |
print(data) | |
del data # release memory mapped file | |
# load temporary variable as memory mappe filed (no data is loaded into memory) | |
data2 = np.load('temp.npy', mmap_mode='r') | |
np.testing.assert_array_equal(test, data2) # read and compare as necessary | |
# create a second output file, also memory mapped and store result of computation | |
data3 = open_memmap('out.npy', dtype = dtype, mode = 'w+', shape = shape) | |
data3[:] = data2*40. + data2**4 | |
del data3 # flushes to disk | |
# load using memory map (no data is loaded yet) | |
data4 = np.load('out.npy') | |
# assert that contents match computed values above | |
np.testing.assert_array_equal((data2*40. + data2**4), data4) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment