Skip to content

Instantly share code, notes, and snippets.

@gauteh
Created January 23, 2023 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauteh/602f8c9dd88e7bb8772750a4909690c5 to your computer and use it in GitHub Desktop.
Save gauteh/602f8c9dd88e7bb8772750a4909690c5 to your computer and use it in GitHub Desktop.
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