indexer.py#
Classes that handle array indexing.
- class openmdao.utils.indexer.ArrayIndexer(arr, flat_src=None)[source]
Bases:
ShapedArrayIndexerAbstract index array class that may or may not be ‘shaped’.
- Parameters:
- arrndarray
The index array.
- flat_srcbool or None
If True, treat source as flat.
- Attributes:
indexed_src_shapeReturn the shape of the result of indexing into the source.
indexed_src_sizeReturn the size of the result if the index were applied to the source.
min_src_dimReturn the number of source dimensions.
Methods
__call__()Return this index array.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return an index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- property indexed_src_shape
Return the shape of the result of indexing into the source.
- Returns:
- tuple
The shape of the index.
- shaped_instance()[source]
Return a ‘shaped’ version of this Indexer type.
- Returns:
- ShapedArrayIndexer or None
Will return a ShapedArrayIndexer if possible, else None.
- class openmdao.utils.indexer.EllipsisIndexer(tup, flat_src=None)[source]
Bases:
IndexerAbstract multi indexer class that is ‘shaped’.
- Parameters:
- tuptuple
Tuple of indices/slices.
- flat_srcbool
If True, treat source array as flat.
- Attributes:
- _tuptuple
The wrapped tuple of indices/slices (it contains an ellipsis).
Methods
__call__()Return the 'default' form of the indices.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return an index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- __init__(tup, flat_src=None)[source]
Initialize attributes.
- apply_offset(offset, flat=True)[source]
Apply an offset to this index.
- Parameters:
- offsetint
The offset to apply.
- flatbool
If True, return a flat index.
- Returns:
- ndarray
The offset array.
- as_array(copy=False, flat=True)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
The index array.
- copy()[source]
Copy this Indexer.
- Returns:
- EllipsisIndexer
A copy of this Indexer.
- flat(copy=False)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- Returns:
- ndarray
An index array into a flat array.
- property min_src_dim
Return the number of source dimensions.
- Returns:
- int
The number of dimensions expected in the source array.
- shaped_instance()[source]
Return a ‘shaped’ version of this Indexer type.
- Returns:
- A shaped Indexer or None
Will return some kind of shaped Indexer if possible, else None.
- to_json()[source]
Return a JSON serializable version of self.
- Returns:
- list of int or int
A list or int version of self.
- class openmdao.utils.indexer.IndexMaker[source]
Bases:
objectA Factory for Indexer objects.
Methods
__call__(idx[, src_shape, flat_src, try_slice])Return an Indexer instance based on the passed indices/slices.
- __getitem__(idx)[source]
Return an Indexer based on idx.
- Parameters:
- idxint, ndarray, slice or tuple
The passed indices/slices.
- Returns:
- Indexer
The Indexer instance we created based on the args.
- class openmdao.utils.indexer.Indexer(flat_src=None)[source]
Bases:
objectAbstract indexing class.
- Parameters:
- flat_srcbool
True if we’re treating the source as flat.
- Attributes:
- _src_shapetuple or None
Shape of the ‘source’. Used to determine actual index or slice values when indices are negative or slice contains negative start or stop values or ‘:’ or ‘…’.
- _shaped_instIndexer or None
Cached shaped_instance if we’ve computed it before.
- _flat_srcbool
If True, index is into a flat source array.
- _dist_shapetuple
Distributed shape of the source.
Methods
__call__()Return the indices in their most efficient form.
copy(*args)Copy this Indexer.
flat([copy])Return index array or slice into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- __init__(flat_src=None)[source]
Initialize attributes.
- copy(*args)[source]
Copy this Indexer.
- Parameters:
- *argsposition args
Args that are specific to initialization of a derived Indexer.
- Returns:
- Indexer
A copy of this Indexer.
- flat(copy=False)[source]
Return index array or slice into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- property indexed_src_shape
Return the shape of the result if the indices were applied to a source array.
- Returns:
- tuple
The shape of the result.
- property indexed_src_size
Return the size of the result if the index were applied to the source.
- Returns:
- int
Size of flattened indices.
- indexed_val(arr)[source]
Return the value of the indices in the array.
- Parameters:
- arrndarray
The array to index into.
- Returns:
- ndarray
The result of indexing into the array.
- indexed_val_set(arr, val)[source]
Set the value of the indices in the array.
- Parameters:
- arrndarray
The array to index into.
- valndarray
The value to set.
- is_full_slice()[source]
- set_src_shape(shape, dist_shape=None)[source]
Set the shape of the ‘source’ array .
- Parameters:
- shapetuple or int
The shape of the ‘source’ array.
- dist_shapetuple or None
If not None, the full distributed shape of the source.
- Returns:
- Indexer
Self is returned to allow chaining.
- shaped_array(copy=False, flat=True)[source]
Return an index array version of the indices that index into a flattened array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
Version of these indices that index into a flattened array.
- shaped_instance()[source]
Return a ‘shaped’ version of this Indexer type.
This should be overridden for all non-shaped derived classes.
- Returns:
- Indexer
The ‘shaped’ Indexer type. ‘shaped’ Indexers know the extent of the array that they are indexing into, or they don’t care what the extent is because they don’t contain negative indices, negative start or stop, ‘:’, or ‘…’.
- to_json()[source]
Return a JSON serializable version of self.
- class openmdao.utils.indexer.IntIndexer(idx, flat_src=None)[source]
Bases:
ShapedIntIndexerInt indexing class that may or may not be ‘shaped’.
- Parameters:
- idxint
The index.
- flat_srcbool or None
If True, treat source as flat.
- Attributes:
indexed_src_shapeReturn the shape of the result if the indices were applied to a source array.
indexed_src_sizeReturn the size of the result if the index were applied to the source.
min_src_dimReturn the number of source dimensions.
Methods
__call__()Return this index.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- shaped_instance()[source]
Return a ‘shaped’ version of this Indexer type.
- Returns:
- ShapedIntIndexer or None
Will return a ShapedIntIndexer if possible, else None.
- class openmdao.utils.indexer.MultiIndexer(tup, flat_src=False)[source]
Bases:
ShapedMultiIndexerAbstract multi indexer class that may or may not be ‘shaped’.
- Parameters:
- tuptuple
Tuple of indices/slices.
- flat_srcbool
If True, treat source array as flat.
- Attributes:
indexed_src_shapeReturn the shape of the result if the indices were applied to a source array.
indexed_src_sizeReturn the size of the result if the index were applied to the source.
min_src_dimReturn the number of source dimensions.
Methods
__call__()Return this multidimensional index.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return an index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- shaped_instance()[source]
Return a ‘shaped’ version of this Indexer type.
- Returns:
- ShapedMultiIndexer or None
Will return a ShapedMultiIndexer if possible, else None.
- class openmdao.utils.indexer.ShapedArrayIndexer(arr, flat_src=None)[source]
Bases:
IndexerAbstract index array class that knows its source shape.
- Parameters:
- arrndarray
The index array.
- flat_srcbool
If True, source is treated as flat.
- Attributes:
- _arrndarray
The wrapped index array object.
Methods
__call__()Return this index array.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return an index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- __init__(arr, flat_src=None)[source]
Initialize attributes.
- apply_offset(offset, flat=True)[source]
Apply an offset to this index.
- Parameters:
- offsetint
The offset to apply.
- flatbool
If True, return a flat index.
- Returns:
- slice
The offset slice.
- as_array(copy=False, flat=True)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
The index array.
- copy()[source]
Copy this Indexer.
- Returns:
- Indexer
A copy of this Indexer.
- flat(copy=False)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- Returns:
- ndarray
The index into a flat array.
- property min_src_dim
Return the number of source dimensions.
- Returns:
- int
The number of dimensions expected in the source array.
- to_json()[source]
Return a JSON serializable version of self.
- Returns:
- list of int or int
List or int version of self.
- class openmdao.utils.indexer.ShapedIntIndexer(idx, flat_src=None)[source]
Bases:
IndexerInt indexing class.
- Parameters:
- idxint
The index.
- flat_srcbool
If True, source is treated as flat.
- Attributes:
- _idxint
The integer index.
Methods
__call__()Return this index.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- __init__(idx, flat_src=None)[source]
Initialize attributes.
- apply_offset(offset, flat=True)[source]
Apply an offset to this index.
- Parameters:
- offsetint
The offset to apply.
- flatbool
If True, return a flat index.
- Returns:
- int
The offset index.
- as_array(copy=False, flat=True)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
The index array.
- copy()[source]
Copy this Indexer.
- Returns:
- Indexer
A copy of this Indexer.
- flat(copy=False)[source]
Return index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- Returns:
- ndarray
The index into a flat array.
- property min_src_dim
Return the number of source dimensions.
- Returns:
- int
The number of dimensions expected in the source array.
- to_json()[source]
Return a JSON serializable version of self.
- Returns:
- int
Int version of self.
- class openmdao.utils.indexer.ShapedMultiIndexer(tup, flat_src=False)[source]
Bases:
IndexerAbstract multi indexer class that is ‘shaped’.
- Parameters:
- tuptuple
Tuple of indices/slices.
- flat_srcbool
If True, treat source array as flat.
- Attributes:
- _tuptuple
The wrapped tuple of indices/slices.
- _idx_listlist
List of Indexers.
- _remove_extra_bracketsbool
If True, remove extra brackets from the string representation.
Methods
__call__()Return this multidimensional index.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return an index array into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- __init__(tup, flat_src=False)[source]
Initialize attributes.
- apply_offset(offset, flat=True)[source]
Apply an offset to this index.
- Parameters:
- offsetint
The offset to apply.
- flatbool
If True, return a flat index.
- Returns:
- ndarray
The offset array.
- as_array(copy=False, flat=True)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
The index array into a flat array.
- copy()[source]
Copy this Indexer.
- Returns:
- Indexer
A copy of this Indexer.
- flat(copy=False)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- Returns:
- ndarray
An index array into a flat array.
- property min_src_dim
Return the number of source dimensions.
- Returns:
- int
The number of dimensions expected in the source array.
- set_src_shape(shape, dist_shape=None)[source]
Set the shape of the ‘source’ array .
- Parameters:
- shapetuple or int
The shape of the ‘source’ array.
- dist_shapetuple or None
If not None, the full distributed shape of the source.
- Returns:
- Indexer
Self is returned to allow chaining.
- to_json()[source]
Return a JSON serializable version of self.
- Returns:
- list of int or int
List or int version of self.
- class openmdao.utils.indexer.ShapedSliceIndexer(slc, flat_src=None)[source]
Bases:
IndexerAbstract slice class that is ‘shaped’.
- Parameters:
- slcslice
The slice.
- flat_srcbool
If True, source is treated as flat.
- Attributes:
- _sliceslice
The wrapped slice object.
Methods
__call__()Return this slice.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return a slice into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- __init__(slc, flat_src=None)[source]
Initialize attributes.
- apply_offset(offset, flat=True)[source]
Apply an offset to this index.
- Parameters:
- offsetint
The offset to apply.
- flatbool
If True, return a flat index.
- Returns:
- slice
The offset slice.
- as_array(copy=False, flat=True)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
The index array.
- copy()[source]
Copy this Indexer.
- Returns:
- Indexer
A copy of this Indexer.
- flat(copy=False)[source]
Return a slice into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- Returns:
- slice
The slice into a flat array.
- is_full_slice()[source]
- property min_src_dim
Return the number of source dimensions.
- Returns:
- int
The number of dimensions expected in the source array.
- to_json()[source]
Return a JSON serializable version of self.
- Returns:
- list of int or int
List or int version of self.
- class openmdao.utils.indexer.SliceIndexer(slc, flat_src=None)[source]
Bases:
ShapedSliceIndexerAbstract slice class that may or may not be ‘shaped’.
- Parameters:
- slcslice
The slice.
- flat_srcbool or None
If True, treat source as flat.
- Attributes:
indexed_src_shapeReturn the shape of the result if the indices were applied to a source array.
indexed_src_sizeReturn the size of the result if the index were applied to the source.
min_src_dimReturn the number of source dimensions.
Methods
__call__()Return this slice.
apply_offset(offset[, flat])Apply an offset to this index.
as_array([copy, flat])Return an index array into a flat array.
copy()Copy this Indexer.
flat([copy])Return a slice into a flat array.
indexed_val(arr)Return the value of the indices in the array.
indexed_val_set(arr, val)Set the value of the indices in the array.
set_src_shape(shape[, dist_shape])Set the shape of the 'source' array .
shaped_array([copy, flat])Return an index array version of the indices that index into a flattened array.
shaped_instance()Return a 'shaped' version of this Indexer type.
to_json()Return a JSON serializable version of self.
is_full_slice
- as_array(copy=False, flat=True)[source]
Return an index array into a flat array.
- Parameters:
- copybool
If True, make sure the array returned is a copy.
- flatbool
If True, return a flat array.
- Returns:
- ndarray
The index array.
- shaped_instance()[source]
Return a ‘shaped’ version of this Indexer type.
- Returns:
- ShapedSliceIndexer or None
Will return a ShapedSliceIndexer if possible, else None.
- class openmdao.utils.indexer.Slicer[source]
Bases:
objectHelper class that can be used when a slice is needed for indexing.
- __getitem__(val)[source]
Pass through indices or slice.
- Parameters:
- valint or slice object or tuples of slice objects
Indices or slice to return.
- Returns:
- indicesint or slice object or tuples of slice objects
Indices or slice to return.
- openmdao.utils.indexer.apply_idx_list(arr, idx_list)[source]
Apply a sequential list of indexers to an array.
- openmdao.utils.indexer.array2slice(arr)[source]
Try to convert an array to slice.
Conversion is only attempted for a 1D array.
- Parameters:
- arrndarray
The index array to be represented as a slice.
- Returns:
- slice or None
If slice conversion is possible, return the slice, else return None.
- openmdao.utils.indexer.combine_ranges(ranges)[source]
Combine a list of (start, end) tuples into the smallest possible list of contiguous ranges.
The ranges are assumed to be non-overlapping and in ascending order.
- Parameters:
- rangeslist
List of (start, end) tuples.
- Returns:
- list of tuples
List of combined ranges.
- openmdao.utils.indexer.get_virtual_array(shape)[source]
Return a ‘virtual’ array of the given shape.
Only one element is actually allocated in memory.
- Parameters:
- shapetuple
Shape of the desired array.
- Returns:
- ndarray
The virtual array.
- openmdao.utils.indexer.idx_list_to_index_array(idx_list)[source]
Convert a sequential list of indexers to an equivalent array indexer.
- Parameters:
- idx_listlist of indexers
List of indexers.
- Returns:
- ndarray
The equivalent index array.
- openmdao.utils.indexer.idx_list_to_shape(idx_list, src_shape)[source]
Convert a sequential list of indexers to an equivalent shape.
- openmdao.utils.indexer.ranges2indexer(ranges, src_shape=None)[source]
Convert a list of ranges to an indexer.
- Parameters:
- rangeslist
List of (start, end) tuples.
- src_shapetuple or None
The shape of the source array.
- Returns:
- Indexer
Indexer object.