indexer.py

Classes that handle array indexing.

class openmdao.utils.indexer.ArrayIndexer(arr, convert=True)[source]

Bases: openmdao.utils.indexer.ShapedArrayIndexer

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

__init__(arr, convert=True)

Initialize attributes.

Parameters
arrndarray

The index array.

convertbool

If True, conversion of arrays to slices and ellipses to multi-indexers is allowed.

as_array(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 array into a flat array.

as_slice()[source]

Return a slice into a flat array.

Returns
slice

The slice into a flat array.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()

Return the shape of the indices.

Returns
tuple

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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)[source]

Bases: openmdao.utils.indexer.Indexer

Abstract multi indexer class that is ‘shaped’.

Attributes
_tuptuple

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

__init__(tup)[source]

Initialize attributes.

Parameters
tuptuple

Tuple of indices/slices.

as_array(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 array into a flat array.

as_slice()[source]

Return a tuple of slices into a multidimensional array.

Returns
tuple of slices

The slice into a multidimensional array.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()

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

Returns
tuple

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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.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[source]

Bases: object

Abstract indexing class.

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.

__init__()[source]

Initialize attributes.

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.

set_src_shape(shape)[source]

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()[source]

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

Returns
tuple

The shape of the indices.

shaped()[source]

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)[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.

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, ‘:’, or ‘…’.

shaped_slice()[source]

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()[source]

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

to_json()[source]

Return a JSON serializable version of self.

class openmdao.utils.indexer.IntIndexer(idx)[source]

Bases: openmdao.utils.indexer.ShapedIntIndexer

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

__init__(idx)

Initialize attributes.

Parameters
idxint

The index.

as_array(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 array into a flat array.

as_slice()[source]

Return this index as a slice.

Returns
slice

A slice that represents this index.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()

Return the shape of the index ().

Returns
tuple

The shape of the index.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

to_json()

Return a JSON serializable version of self.

Returns
int

int version of self.

class openmdao.utils.indexer.MultiIndexer(tup)[source]

Bases: openmdao.utils.indexer.ShapedMultiIndexer

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

__init__(tup)

Initialize attributes.

Parameters
tuptuple

Tuple of indices/slices.

as_array(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 array into a flat array.

as_slice()

Return a tuple of slices into a multidimensional array.

Returns
tuple of slices

The slice into a multidimensional array.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()

Return the shape of the indices.

Returns
tuple

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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, convert=True)[source]

Bases: openmdao.utils.indexer.Indexer

Abstract index array class that is ‘shaped’.

Attributes
_arrndarray

The wrapped index array object.

_convertbool

If True, conversion of arrays to slices and ellipses to multi-indexers is allowed.

__init__(arr, convert=True)[source]

Initialize attributes.

Parameters
arrndarray

The index array.

convertbool

If True, conversion of arrays to slices and ellipses to multi-indexers is allowed.

as_array(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 array into a flat array.

as_slice()[source]

Return a slice into a flat array.

This always fails because if it were possible, we would have already replaced this array indexer with a slice 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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()[source]

Return the shape of the indices.

Returns
tuple

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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, ‘:’, or ‘…’.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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)[source]

Bases: openmdao.utils.indexer.Indexer

Int indexing class.

Attributes
_idxint

The integer index.

__init__(idx)[source]

Initialize attributes.

Parameters
idxint

The index.

as_array(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 array into a flat array.

as_slice()[source]

Return a slice into a flat array.

Returns
slice

The slice into a flat array.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()[source]

Return the shape of the index ().

Returns
tuple

The shape of the index.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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, ‘:’, or ‘…’.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()[source]

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

to_json()[source]

Return a JSON serializable version of self.

Returns
int

int version of self.

class openmdao.utils.indexer.ShapedMultiIndexer(tup)[source]

Bases: openmdao.utils.indexer.Indexer

Abstract multi indexer class that is ‘shaped’.

Attributes
_tuptuple

The wrapped tuple of indices/slices.

_idx_listlist

List of Indexers.

__init__(tup)[source]

Initialize attributes.

Parameters
tuptuple

Tuple of indices/slices.

as_array(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 array into a flat array.

as_slice()[source]

Return a tuple of slices into a multidimensional array.

Returns
tuple of slices

The slice into a multidimensional array.

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.

set_src_shape(shape)[source]

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()[source]

Return the shape of the indices.

Returns
tuple

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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, ‘:’, or ‘…’.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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)[source]

Bases: openmdao.utils.indexer.Indexer

Abstract slice class that is ‘shaped’.

Attributes
_sliceslice

The wrapped slice object.

__init__(slc)[source]

Initialize attributes.

Parameters
slcslice

The slice.

as_array(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 array into a flat array.

as_slice()[source]

Return a slice into a flat array.

Returns
slice

The slice into a flat array.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()[source]

Return the shape of the indices.

Returns
int

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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, ‘:’, or ‘…’.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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)[source]

Bases: openmdao.utils.indexer.ShapedSliceIndexer

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

__init__(slc)

Initialize attributes.

Parameters
slcslice

The slice.

as_array(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 array into a flat array.

as_slice()

Return a slice into a flat array.

Returns
slice

The slice into a flat array.

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.

set_src_shape(shape)

Set the shape of the ‘source’ array .

Parameters
shapetuple or int

The shape of the ‘source’ array.

Returns
Indexer

Self is returned to allow chaining.

shape()[source]

Return the shape of the indices.

Returns
int

The shape of the indices.

shaped()

Return a version of the indices that index into a flattened array.

Could be either a slice or an index array.

Returns
slice or ndarray or int

Version of these indices that index into a flattened array.

shaped_array(copy=False)

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.

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.

shaped_slice()

Return a slice version (if possible) of the indices that index into a flattened array.

Raises an exception if a slice can’t be returned.

Returns
slice

Slice ersion of these indices that index into a flattened array.

size()

Return the size of the flattened indices.

Returns
int

Size of flattened indices.

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 array to be represented as a slice.

Returns
slice or None

If slice conversion is possible, return the slice, else return None