简体   繁体   中英

Find location of slice in numpy array

I have an ndarray subclass which implements loading/saving of one or more records into a flat binary file. After the records are loaded, I can access them in the normal NumPy fashion.

My question is about what happens when I slice the result (or indeed, any NumPy array). This normally produces a 'view' ie. an array that refers to the same buffer as the parent array.

Once I have this view, is there any way to determine the position of the view V in the array A? More precisely, I would like to know the byte offset (from the start of A's data buffer) at which V begins. This would allow me to write the slice back onto disk at the right offset.

Here's some example code to show the situation :

# Imagine a as consisting of 4 4-byte records...
a = np.arange(16, dtype='B').reshape(4,4)

# I select the first record
v = a[0]

print (v)

# [0 1 2 3]

# I can determine that v is a subarray:

is_subarray = v.base != None

# I can determine which dimension the slice spans..

whichdim = v.base.strides.index (v.strides[-1])

# But not its position along that dimension.

The informaiton is exposed through array.__array_interface__ (maybe somewhere better too), however I think you should probably just use memmaps to begin with and not mess around with this. Check for example the numpy code to the np.may_share_memory function (or actually np.byte_bounds ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM