Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 668
kampu Avatar asked Sep 14 '12 09:09

kampu


People also ask

How do you find the location of an element in a NumPy array?

Using ndenumerate() function to find the Index of value It is usually used to find the first occurrence of the element in the given numpy array.

Can you slice NumPy arrays?

Slicing in python means extracting data from one given index to another given index, however, NumPy slicing is slightly different. Slicing can be done with the help of (:) . A NumPy array slicing object is constructed by giving start , stop , and step parameters to the built-in slicing function.

How do you access elements in a 2d NumPy array?

Indexing a Two-dimensional Array To access elements in this array, use two indices. One for the row and the other for the column. Note that both the column and the row indices start with 0. So if I need to access the value '10,' use the index '3' for the row and index '1' for the column.

What is NumPy slice?

Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end] . We can also define the step, like this: [start:end:step] .


1 Answers

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).

like image 74
seberg Avatar answered Sep 21 '22 16:09

seberg