-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy patharray_mmap_proxy.py
38 lines (27 loc) · 1.01 KB
/
array_mmap_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
import tempfile
import atexit
class ArrayMMapProxy(object):
__slots__ = ('_array', '_params',)
def __init__(self, array):
if isinstance(array, np.memmap):
order = 'F' if np.isfortran(array) else 'C'
self._params = (array.filename, array.dtype, array.mode,
array.offset, array.shape, order)
else:
raise TypeError('Unsupported type %s' % type(array))
self._array = array
@classmethod
def fromarray(cls, array, tmpdir=None, mode='r'):
f = tempfile.NamedTemporaryFile(dir=tmpdir)
atexit.register(lambda: f.close())
array.tofile(f.name) # just f does not work?
return cls(np.memmap(f.name, array.dtype, mode,
0, array.shape, 'C'))
def __getstate__(self):
return self._params
def __setstate__(self, state):
self._params = state
self._array = np.memmap(*self._params)
def get(self):
return self._array