How can I create a slice object for Numpy array?
Yes you can use numpy.s_
:
Example:
>>> a = np.arange(10).reshape(2, 5)
>>>
>>> m = np.s_[0:2, 3:4]
>>>
>>> a[m]
array([[3],
[8]])
And in this case:
my_slice = np.s_[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
a1 = array1[my_slice]
a2 = array2[my_slice]
a3 = array3[my_slice]
You can also use numpy.r_
in order to translates slice objects to concatenation along the first axis.
You can index a multidimensional array by using a tuple of slice
objects.
window = slice(col_start, col_stop), slice(row_start, row_stop)
a1 = array1[window]
a2 = array2[window]
This is not specific to numpy
and is simply how subscription/slicing syntax works in python.
class mock_array:
def __getitem__(self, key):
print(key)
m = mock_array()
m[1:3, 7:9] # prints tuple(slice(1, 3, None), slice(7, 9, None))