indexer.py

Classes that handle array indexing.

class openmdao.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
arrndarray

The index array.

flat_srcbool or None

If 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
subidxerIndexer

The 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
copybool

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

flatbool

If 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
copybool

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

Returns
ndarray

The index into a flat array.

property indexed_src_shape

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

Returns
tuple

The shape of the index.

property indexed_src_size

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

Returns
int

Size of flattened indices.

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)

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)

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.

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.

class openmdao.utils.indexer.EllipsisIndexer(tup, flat_src=None)[source]

Bases: openmdao.utils.indexer.Indexer

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

__init__(tup, flat_src=None)[source]

Initialize attributes.

apply(subidxer)

Apply a sub-Indexer to this Indexer and return the resulting indices.

Parameters
subidxerIndexer

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

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)

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)

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.

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: object

A Factory for Indexer objects.

__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: object

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

__init__(flat_src=None)[source]

Initialize attributes.

apply(subidxer)[source]

Apply a sub-Indexer to this Indexer and return the resulting indices.

Parameters
subidxerIndexer

The Indexer to be applied to this one.

Returns
ndarray

The resulting indices (always flat).

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.

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: openmdao.utils.indexer.ShapedIntIndexer

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

Parameters
idxint

The index.

flat_srcbool or None

If 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
subidxerIndexer

The 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
copybool

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

flatbool

If 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
copybool

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

Returns
ndarray

The index into a flat array.

property indexed_src_shape

Return the shape of the index ().

Returns
tuple

The shape of the index.

property indexed_src_size

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

Returns
int

Size of flattened indices.

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)

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)

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.

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.

class openmdao.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
tuptuple

Tuple of indices/slices.

flat_srcbool

If 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
subidxerIndexer

The 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
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()

Copy this Indexer.

Returns
Indexer

A copy of this Indexer.

flat(copy=False)

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

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)

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)

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.

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.

class openmdao.utils.indexer.ShapedArrayIndexer(arr, flat_src=None)[source]

Bases: openmdao.utils.indexer.Indexer

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

__init__(arr, flat_src=None)[source]

Initialize attributes.

apply(subidxer)

Apply a sub-Indexer to this Indexer and return the resulting indices.

Parameters
subidxerIndexer

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

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)

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)

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

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.

class openmdao.utils.indexer.ShapedIntIndexer(idx, flat_src=None)[source]

Bases: openmdao.utils.indexer.Indexer

Int indexing class.

Parameters
idxint

The index.

flat_srcbool

If True, source is treated as flat.

Attributes
_idxint

The 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
subidxerIndexer

The 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
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 indexed_src_shape

Return the shape of the index ().

Returns
tuple

The shape of the index.

property indexed_src_size

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

Returns
int

Size of flattened indices.

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)

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)

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

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.

class openmdao.utils.indexer.ShapedMultiIndexer(tup, flat_src=False)[source]

Bases: openmdao.utils.indexer.Indexer

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

__init__(tup, flat_src=False)[source]

Initialize attributes.

apply(subidxer)

Apply a sub-Indexer to this Indexer and return the resulting indices.

Parameters
subidxerIndexer

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

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.

shaped_array(copy=False, flat=True)

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

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.

class openmdao.utils.indexer.ShapedSliceIndexer(slc, flat_src=None)[source]

Bases: openmdao.utils.indexer.Indexer

Abstract slice class that is ‘shaped’.

Parameters
slcslice

The slice.

flat_srcbool

If True, source is treated as flat.

Attributes
_sliceslice

The 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
subidxerIndexer

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

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.

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)

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)

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

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.

class openmdao.utils.indexer.SliceIndexer(slc, flat_src=None)[source]

Bases: openmdao.utils.indexer.ShapedSliceIndexer

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

Parameters
slcslice

The slice.

flat_srcbool or None

If 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
subidxerIndexer

The 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
copybool

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

flatbool

If 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
copybool

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

Returns
slice

The slice into a flat array.

property indexed_src_shape

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

Returns
tuple

The shape of the index.

property indexed_src_size

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

Returns
int

Size of flattened indices.

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)

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)

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.

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.

class openmdao.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 objects

Indices or slice to return.

Returns
indicesint or slice object or tuples of slice objects

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

class openmdao.utils.indexer.resolve_shape(shape)[source]

Bases: object

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

Parameters
shapetuple

The shape of the source.

Attributes
_shapetuple

The 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, ndarray

The index into the source.

Returns
tuple

The shape after indexing.

__init__(shape)[source]

Initialize attributes.

Parameters
shapetuple or int

Shape of the source.