# indexer.py

# indexer.py¶

Classes that handle array indexing.

classopenmdao.utils.indexer.ArrayIndexer(arr,flat_src=None)[source]Bases:

`openmdao.utils.indexer.ShapedArrayIndexer`

Abstract index array class that may or may not be ‘shaped’.

- Parameters

arrndarrayThe index array.

flat_srcbool or NoneIf True, treat source as flat.

- __init__(
arr,flat_src=None)Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
The index array.

- copy()
Copy this Indexer.

- Returns

- Indexer
A copy of this Indexer.

- flat(
copy=False)Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
The index into a flat array.

propertyindexed_src_shapeReturn the shape of the result of indexing into the source.

- Returns

- tuple
The shape of the index.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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.

- Returns

- ShapedArrayIndexer or None
Will return a ShapedArrayIndexer if possible, else None.

- to_json()
Return a JSON serializable version of self.

- Returns

- list of int or int
List or int version of self.

classopenmdao.utils.indexer.EllipsisIndexer(tup,flat_src=None)[source]Bases:

`openmdao.utils.indexer.Indexer`

Abstract multi indexer class that is ‘shaped’.

- Parameters

tuptupleTuple of indices/slices.

flat_srcboolIf True, treat source array as flat.

- Attributes

_tuptupleThe wrapped tuple of indices/slices (it contains an ellipsis).

- __init__(
tup,flat_src=None)[source]Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)[source]Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
An index array into a flat array.

propertyindexed_src_shapeReturn the shape of the result if the indices were applied to a source array.

- Returns

- tuple
The shape of the result.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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.

- 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.

classopenmdao.utils.indexer.IndexMaker[source]Bases:

`object`

A Factory for Indexer objects.

- __getitem__(
idx)[source]Return an Indexer based on idx.

- Parameters

idxint, ndarray, slice or tupleThe passed indices/slices.

- Returns

- Indexer
The Indexer instance we created based on the args.

classopenmdao.utils.indexer.Indexer(flat_src=None)[source]Bases:

`object`

Abstract indexing class.

- Parameters

flat_srcboolTrue if we’re treating the source as flat.

- Attributes

_src_shapetuple or NoneShape 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 NoneCached shaped_instance if we’ve computed it before.

_flat_srcboolIf True, index is into a flat source array.

_dist_shapetupleDistributed shape of the source.

- __init__(
flat_src=None)[source]Initialize attributes.

- apply(
subidxer)[source]Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- copy(
*args)[source]Copy this Indexer.

- Parameters

*argsposition argsArgs 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

copyboolIf True, make sure the array returned is a copy.

propertyindexed_src_shapeReturn the shape of the result if the indices were applied to a source array.

- Returns

- tuple
The shape of the result.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

- set_src_shape(
shape,dist_shape=None)[source]Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf 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

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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.

classopenmdao.utils.indexer.IntIndexer(idx,flat_src=None)[source]Bases:

`openmdao.utils.indexer.ShapedIntIndexer`

Int indexing class that may or may not be ‘shaped’.

- Parameters

idxintThe index.

flat_srcbool or NoneIf True, treat source as flat.

- __init__(
idx,flat_src=None)Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
The index array.

- copy()
Copy this Indexer.

- Returns

- Indexer
A copy of this Indexer.

- flat(
copy=False)Return index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
The index into a flat array.

propertyindexed_src_shapeReturn the shape of the index ().

- Returns

- tuple
The shape of the index.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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.

- Returns

- ShapedIntIndexer or None
Will return a ShapedIntIndexer if possible, else None.

- to_json()
Return a JSON serializable version of self.

- Returns

- int
Int version of self.

classopenmdao.utils.indexer.MultiIndexer(tup,flat_src=False)[source]Bases:

`openmdao.utils.indexer.ShapedMultiIndexer`

Abstract multi indexer class that may or may not be ‘shaped’.

- Parameters

tuptupleTuple of indices/slices.

flat_srcboolIf True, treat source array as flat.

- __init__(
tup,flat_src=False)Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
The index array into a flat array.

- copy()
Copy this Indexer.

- Returns

- Indexer
A copy of this Indexer.

- flat(
copy=False)Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
An index array into a flat array.

propertyindexed_src_shapeReturn the shape of the result if the indices were applied to a source array.

- Returns

- tuple
The shape of the result.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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.

- Returns

- ShapedMultiIndexer or None
Will return a ShapedMultiIndexer if possible, else None.

- to_json()
Return a JSON serializable version of self.

- Returns

- list of int or int
List or int version of self.

classopenmdao.utils.indexer.ShapedArrayIndexer(arr,flat_src=None)[source]Bases:

`openmdao.utils.indexer.Indexer`

Abstract index array class that knows its source shape.

- Parameters

arrndarrayThe index array.

flat_srcboolIf True, source is treated as flat.

- Attributes

_arrndarrayThe wrapped index array object.

- __init__(
arr,flat_src=None)[source]Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)[source]Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
The index into a flat array.

propertyindexed_src_shapeReturn the shape of the result if the indices were applied to a source array.

- Returns

- tuple
The shape of the result.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
Version of these indices that index into a flattened array.

- shaped_instance()
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.

- Returns

- list of int or int
List or int version of self.

classopenmdao.utils.indexer.ShapedIntIndexer(idx,flat_src=None)[source]Bases:

`openmdao.utils.indexer.Indexer`

Int indexing class.

- Parameters

idxintThe index.

flat_srcboolIf True, source is treated as flat.

- Attributes

_idxintThe integer index.

- __init__(
idx,flat_src=None)[source]Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)[source]Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
The index into a flat array.

propertyindexed_src_shapeReturn the shape of the index ().

- Returns

- tuple
The shape of the index.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
Version of these indices that index into a flattened array.

- shaped_instance()
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.

- Returns

- int
Int version of self.

classopenmdao.utils.indexer.ShapedMultiIndexer(tup,flat_src=False)[source]Bases:

`openmdao.utils.indexer.Indexer`

Abstract multi indexer class that is ‘shaped’.

- Parameters

tuptupleTuple of indices/slices.

flat_srcboolIf True, treat source array as flat.

- Attributes

_tuptupleThe wrapped tuple of indices/slices.

_idx_listlistList of Indexers.

- __init__(
tup,flat_src=False)[source]Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)[source]Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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

copyboolIf True, make sure the array returned is a copy.

- Returns

- ndarray
An index array into a flat array.

propertyindexed_src_shapeReturn the shape of the result if the indices were applied to a source array.

- Returns

- tuple
The shape of the result.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn 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 intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
Version of these indices that index into a flattened array.

- shaped_instance()
Return a ‘shaped’ version of this Indexer type.

This should be overridden for all non-shaped derived classes.

- Returns

- Indexer

- to_json()[source]
Return a JSON serializable version of self.

- Returns

- list of int or int
List or int version of self.

classopenmdao.utils.indexer.ShapedSliceIndexer(slc,flat_src=None)[source]Bases:

`openmdao.utils.indexer.Indexer`

Abstract slice class that is ‘shaped’.

- Parameters

slcsliceThe slice.

flat_srcboolIf True, source is treated as flat.

- Attributes

_slicesliceThe wrapped slice object.

- __init__(
slc,flat_src=None)[source]Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)[source]Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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

copyboolIf True, make sure the array returned is a copy.

- Returns

- slice
The slice into a flat array.

propertyindexed_src_shapeReturn the shape of the result if the indices were applied to a source array.

- Returns

- tuple
The shape of the result.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
Version of these indices that index into a flattened array.

- shaped_instance()
Return a ‘shaped’ version of this Indexer type.

This should be overridden for all non-shaped derived classes.

- Returns

- Indexer

- to_json()[source]
Return a JSON serializable version of self.

- Returns

- list of int or int
List or int version of self.

classopenmdao.utils.indexer.SliceIndexer(slc,flat_src=None)[source]Bases:

`openmdao.utils.indexer.ShapedSliceIndexer`

Abstract slice class that may or may not be ‘shaped’.

- Parameters

slcsliceThe slice.

flat_srcbool or NoneIf True, treat source as flat.

- __init__(
slc,flat_src=None)Initialize attributes.

- apply(
subidxer)Apply a sub-Indexer to this Indexer and return the resulting indices.

- Parameters

subidxerIndexerThe Indexer to be applied to this one.

- Returns

- ndarray
The resulting indices (always flat).

- as_array(
copy=False,flat=True)[source]Return an index array into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf True, return a flat array.

- Returns

- ndarray
The index array.

- copy()
Copy this Indexer.

- Returns

- Indexer
A copy of this Indexer.

- flat(
copy=False)Return a slice into a flat array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

- Returns

- slice
The slice into a flat array.

propertyindexed_src_shapeReturn the shape of the result of indexing into the source.

- Returns

- tuple
The shape of the index.

propertyindexed_src_sizeReturn the size of the result if the index were applied to the source.

- Returns

- int
Size of flattened indices.

propertymin_src_dimReturn the number of source dimensions.

- Returns

- int
The number of dimensions expected in the source array.

- set_src_shape(
shape,dist_shape=None)Set the shape of the ‘source’ array .

- Parameters

shapetuple or intThe shape of the ‘source’ array.

dist_shapetuple or NoneIf not None, the full distributed shape of the source.

- Returns

- Indexer
Self is returned to allow chaining.

- shaped_array(
copy=False,flat=True)Return an index array version of the indices that index into a flattened array.

- Parameters

copyboolIf True, make sure the array returned is a copy.

flatboolIf 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.

- Returns

- ShapedSliceIndexer or None
Will return a ShapedSliceIndexer if possible, else None.

- to_json()
Return a JSON serializable version of self.

- Returns

- list of int or int
List or int version of self.

classopenmdao.utils.indexer.Slicer[source]Bases:

`object`

Helper 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 objectsIndices or slice to return.

- Returns

indicesint or slice object or tuples of slice objectsIndices or slice to return.

- openmdao.utils.indexer.array2slice(
arr)[source]Try to convert an array to slice.

Conversion is only attempted for a 1D array.

- Parameters

arrndarrayThe index array to be represented as a slice.

- Returns

- slice or None
If slice conversion is possible, return the slice, else return None.

classopenmdao.utils.indexer.resolve_shape(shape)[source]Bases:

`object`

Class that computes the result shape from a source shape and an index.

- Parameters

shapetupleThe shape of the source.

- Attributes

_shapetupleThe shape of the source.

- __getitem__(
idx)[source]Return the shape of the result of indexing into the source with index idx.

- Parameters

idxint, slice, tuple, ndarrayThe index into the source.

- Returns

- tuple
The shape after indexing.

- __init__(
shape)[source]Initialize attributes.

- Parameters

shapetuple or intShape of the source.