slice_from_string

ccdproc.utils.slices.slice_from_string(string, fits_convention=False)[source]

Convert a string to a tuple of slices.

Parameters:
stringstr

A string that can be converted to a slice.

fits_conventionbool, optional

If True, assume the input string follows the FITS convention for indexing: the indexing is one-based (not zero-based) and the first axis is that which changes most rapidly as the index increases.

Returns:
slice_tupletuple of slice objects

A tuple able to be used to index a numpy.array

Notes

The string argument can be anything that would work as a valid way to slice an array in Numpy. It must be enclosed in matching brackets; all spaces are stripped from the string before processing.

Examples

>>> import numpy as np
>>> arr1d = np.arange(5)
>>> a_slice = slice_from_string('[2:5]')
>>> arr1d[a_slice]
array([2, 3, 4])
>>> a_slice = slice_from_string('[ : : -2] ')
>>> arr1d[a_slice]
array([4, 2, 0])
>>> arr2d = np.array([arr1d, arr1d + 5, arr1d + 10])
>>> arr2d
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a_slice = slice_from_string('[1:-1, 0:4:2]')
>>> arr2d[a_slice]
array([[5, 7]])
>>> a_slice = slice_from_string('[0:2,0:3]')
>>> arr2d[a_slice]
array([[0, 1, 2],
       [5, 6, 7]])