I have a (4,) arrays that I want to save to the disk (The sizes I am working with can not fit into memory so I need to dynamically load what I need). However, I want to have that in a single numpy.memmap
. Not sure if it is possible but any suggestion would be greatly appreciated.
I have this without numpy.memmap
arr1 = [1,2,3,4]
arr2 = [2,3,4,5]
arr3 = [3,4,5,6]
arr4 = [4,5,6,7]
data = []
data.extend([arr1])
data.extend([arr2])
data.extend([arr3])
data.extend([arr4])
print(data)
[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
I want to be able to do something like this:
import numpy as np
arr1 = np.memmap('./file1', np.dtype('O'), mode='w+', shape=(4,))
arr1[:] = [1,2,3,4]
arr2 = np.memmap('./file2', np.dtype('O'), mode='w+', shape=(4,))
arr2[:] = [2,3,4,5]
arr3 = np.memmap('./file3', np.dtype('O'), mode='w+', shape=(4,))
arr3[:] = [3,4,5,6]
arr4 = np.memmap('./file4', np.dtype('O'), mode='w+', shape=(4,))
arr4[:] = [4,5,6,7]
data = []
data.extend([arr1])
data.extend([arr2])
data.extend([arr3])
data.extend([arr4])
print (data)
[memmap([1, 2, 3, 4], dtype=object), memmap([2, 3, 4, 5], dtype=object), memmap([3, 4, 5, 6], dtype=object), memmap([4, 5, 6, 7], dtype=object)]
This requires me to create different files per array and I really want to have a single memmap
that would handle the entire mini-arrays of 4. Can someone provide a way to do this using memmaps
?
The ability to extend i.e. data.extend()
is important as I don't know how many mini-arrays I have.