8000 ENH: Make memmaps return arrays in ufuncs and fancy indexing by seberg · Pull Request #2932 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Make memmaps return arrays in ufuncs and fancy indexing #2932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions numpy/core/memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,33 @@ def __array_finalize__(self, obj):
self.offset = None
self.mode = None

def __array_wrap__(self, arr, context=None):
# If the type is not memmap, then a user subclass may do some more
# things that need to be preserved. Otherwise, return a base array
# since it should not p
if type(self) is memmap and not np.may_share_memory(self, arr):
return arr
try:
return super(memmap, self).__array_wrap__(arr, context=context)
except TypeError:
return super(memmap, self).__array_wrap__(arr)

def __array_prepare__(self, arr, context=None):
# There is no need to make an array a memmap, but we still have to do
# it in case this is not of memmap type.
if type(self) is memmap and not np.may_share_memory(self, arr):
return arr
try:
return super(memmap, self).__array_prepare__(arr, context=context)
except TypeError:
return super(memmap, self).__array_prepare__(arr)

def __getitem__(self, index):
res = super(memmap, self).__getitem__(index)
if type(res) is memmap and res._mmap is None:
return res.view(type=ndarray)
return res

def flush(self):
"""
Write any changes in the array to the file on disk.
Expand Down
22 changes: 21 additions & 1 deletion numpy/core/tests/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

from numpy import memmap
from numpy import arange, allclose, asarray
from numpy import arange, allclose, asarray, add
from numpy.testing import *

class TestMemmap(TestCase):
Expand Down Expand Up @@ -115,5 +115,25 @@ def test_view(self):
new_array = asarray(fp)
assert(new_array.base is fp)

def test_to_array_conversions(self):
class DummyInerhitance(np.ndarray):
def __array_wrap__(self, arr):
arr = super(DummyInerhitance, self).__array_wrap__(arr)
arr.info = 'set'
class MMSubclass(memmap, DummyInerhitance):
pass
m = memmap(self.tmpfp, dtype=self.dtype, shape=self.shape)
r = m + 1
assert_(not isinstance(r, memmap))
add(m, 1, out=m)
assert_(isinstance(m, memmap))
assert_(not isinstance(m[:,[0,1]], memmap))
# But for subclasses, preserve in case someone tagged on functions:
m = MMSubclass(self.tmpfp, dtype=self.dtype, shape=self.shape)
r = m + 1
assert_(isinstance(r, MMSubclass))
assert_(hasattr(r, 'info'))
assert_(isinstance(m[:,[0,1]], MMSubclass))

if __name__ == "__main__":
run_module_suite()
0