In Python, the indexing operation for a type is implemented by the __getitem__/__setitem__/__delitem__ magic methods. To understand how indexing syntax is interpreted, and thus how these methods are called, we can simply define our own class:
class test:
def __getitem__(self, idx):
return idx
test()[:, [1,2,3], ...]
# result: (slice(None, None, None), [1, 2, 3], Ellipsis)
This slice type is a built-in type mentioned earlier in the same documentation, with a link to more specific information.
Ellipsis is simply the name of the object created by the literal syntax .... It's what Numpy uses to represent the complete extent of any number of axes - so this lets us avoid worrying about the dimensionality of the array we're working with.
The important thing to realize is that we can use that same index tuple directly, to get the same result.
Thus, given a dimension along which to slice with [ci], we can construct the necessary tuple, and index with it:
all = slice(None)
def make_index(ci, dimension):
return (all,) * dimension + ([ci], Ellipsis)
# Now we can do things like:
arr[make_index(ci, dimension)] = some_data
Answer from Karl Knechtel on Stack OverflowThe syntax is:
Use with AIa[start:stop] # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array
There is also the step value, which can be used with any of the above:
Use with AIa[start:stop:step] # start through not past stop, by step
The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).
The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:
Use with AIa[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
Similarly, step may be a negative number:
Use with AIa[::-1] # all items in the array, reversed
a[1::-1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::-1] # everything except the last two items, reversed
Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.
Relationship with the slice object
A slice object can represent a slicing operation, i.e.:
Use with AIa[start:stop:step]
is equivalent to:
Use with AIa[slice(start, stop, step)]
Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported.
To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].
While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.
The Python tutorial talks about it (scroll down a bit until you get to the part about slicing).
The ASCII art diagram is helpful too for remembering how slices work:
Use with AI +---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5
-6 -5 -4 -3 -2 -1
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n.
I'm making a program that does some processing for material dispersal in bomb explosions, working on numpy arrays to be converted into geo raster files and eventually displayed in ArcGIS.
It was my understanding that a slice of a numpy array is a view, and so altering the data on it should alter it on the original array. But I keep returning arrays with all zero values. If this is not the case, I can't seem to find any easy way to apply a function to just a part of a multidimensional array.
Here is the part of the code in question.