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.
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.
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.
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.
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] .
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With