Find the location of a slice in a numpy array

I have a subclass of ndarray that implements loading / saving one or more records into a flat binary. After downloading the records, I can access them in normal NumPy mode.

My question is what will happen when I slice the result (or, indeed, any NumPy array). Usually this creates a "view", i.e. an array that refers to the same buffer as the parent array.

Once I have this view, is there a way to determine the position of the view V in array A? More precisely, I would like to know the byte offset (starting from the beginning of the data buffer) from which V. begins. This will allow me to write the slice back to disk with the correct offset.

Here is a sample 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. 
+4
source share
1 answer

Informational information is displayed via array.__array_interface__ (maybe somewhere better), however I think you should probably just use memmaps to start with it, and not get confused with this. Check, for example, numpy code for the np.may_share_memory function (or actually np.byte_bounds ).

+4
source

Source: https://habr.com/ru/post/1434158/


All Articles