Joshua
Joshua

Reputation: 165

create ndarray from numpy memmap slice

I am wondering how to determine if an object is a direct memmap object or a slice descendent of one.

The easiest way to as the question is with an example:

>>> import numpy as np
>>> filename = '../sandbox/test.bin'
>>> a = np.memmap(filename, dtype='float32', offset=0, shape=(4,2), order='F')
>>> print a
[[ 1.  5.]
 [ 2.  6.]
 [ 3.  7.]
 [ 4.  8.]]
>>> a.filename
'Z:\\CNI\\sandbox\\test.bin'
>>> a.shape
(4L, 2L)
>>> a.offset
0
>>>
>>> b = a[:,1]
>>> print b
[ 5.  6.  7.  8.]
>>> b.filename
'Z:\\CNI\\sandbox\\test.bin'
>>> b.shape
(4L,)
>>> b.offset
0
>>>

how would I determine that b is not a memmap object, but is instead a slice descendent of one? Or at the very least that b's offset is wrong. (in this example the offset should be 4)

Upvotes: 1

Views: 2038

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58935

In this example you can check the OWNDATA parameter of the:

b.flags

attribute, which is False for a slice...

Upvotes: 1

Related Questions