rangemapper.py#
A collection of classes for mapping indices to variable names and vice versa.
- class openmdao.utils.rangemapper.FlatRangeMapper(sizes)[source]
Bases:
RangeMapper
A flat list mapping indices to variable key and relative indices.
- Parameters:
- sizeslist of (key, size)
Ordered list of (key, size) tuples. key must be hashable.
- Attributes:
- rangeslist of (key, start, stop)
List of (key, start, stop) tuples, where start and stop define the range of indices for that key. Ranges must be contiguous. key must be hashable.
- __getitem__(idx)[source]
Find the key corresponding to the given index.
- Parameters:
- idxint
The index into the full array.
- Returns:
- object or None
The key corresponding to the given index, or None if not found.
- __init__(sizes)[source]
Initialize a FlatRangeMapper.
- __iter__()[source]
Iterate over (key, start, stop) tuples.
- Yields:
- (obj, int, int)
(key, start index, stop index), where key is a hashable object.
- between_iter(start_key, stop_key)
Iterate over (key, start, stop) tuples between the given start and stop keys.
- Parameters:
- start_keyobject
Key corresponding to an index range.
- stop_keyobject
Key corresponding to an index range.
- Yields:
- (obj, int, int)
(key, relative start index, relative stop index), where key is a hashable object.
- static create(sizes, max_flat_range_size=10000)
Return a mapper that maps indices to variable names and relative indices.
- Parameters:
- sizesiterable of (key, size)
Iterable of (key, size) tuples.
- max_flat_range_sizeint
If the total array size is less than this, a FlatRangeMapper will be returned instead of a RangeTree.
- Returns:
- FlatRangeMapper or RangeTree
A mapper that maps indices to variable key and relative indices.
- dump()
Dump the contents of the mapper to stdout.
- index2key_rel(idx)[source]
Find the key and relative index corresponding to the matched range.
- Parameters:
- idxint
The index into the full array.
- Returns:
- object or None
The key corresponding to the matched range, or None if not found.
- int or None
The relative index into the matched range, or None if not found.
- inds2keys(inds)
Find the set of keys corresponding to the given indices.
- Parameters:
- indsiter of int
The array indices.
- Returns:
- set of object
The set of keys corresponding to the given indices.
- key2range(key)
Get the range corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Data corresponding to an index range.
- Returns:
- tuple of (int, int)
The range of indices corresponding to the given key.
- key2size(key)
Get the size corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Key corresponding to an index range.
- Returns:
- int
The size corresponding to the given key.
- overlap_iter(key, other)
Find the set of keys that overlap between this mapper and another.
- Parameters:
- keyobject
Key corresponding to an index range.
- otherRangeMapper
Another mapper.
- Yields:
- (obj, int, int, obj, int, int)
(key, start, stop, otherkey, otherstart, otherstop).
- class openmdao.utils.rangemapper.RangeMapper(sizes)[source]
Bases:
object
A mapper of indices to variable names and vice versa.
- Parameters:
- sizesiterable of (key, size) tuples
Iterable of (key, size) tuples. key must be hashable.
- Attributes:
- sizeint
Total size of all of the sizes combined.
- _key2rangedict
Dictionary mapping key to an index range.
- __getitem__(idx)[source]
Find the key corresponding to the given index.
- Parameters:
- idxint
The index into the full array.
- __init__(sizes)[source]
Initialize a RangeMapper.
- __iter__()[source]
Iterate over (key, start, stop) tuples.
- Yields:
- (obj, int, int)
(key, start index, stop index), where key is a hashable object.
- between_iter(start_key, stop_key)[source]
Iterate over (key, start, stop) tuples between the given start and stop keys.
- Parameters:
- start_keyobject
Key corresponding to an index range.
- stop_keyobject
Key corresponding to an index range.
- Yields:
- (obj, int, int)
(key, relative start index, relative stop index), where key is a hashable object.
- static create(sizes, max_flat_range_size=10000)[source]
Return a mapper that maps indices to variable names and relative indices.
- Parameters:
- sizesiterable of (key, size)
Iterable of (key, size) tuples.
- max_flat_range_sizeint
If the total array size is less than this, a FlatRangeMapper will be returned instead of a RangeTree.
- Returns:
- FlatRangeMapper or RangeTree
A mapper that maps indices to variable key and relative indices.
- dump()[source]
Dump the contents of the mapper to stdout.
- inds2keys(inds)[source]
Find the set of keys corresponding to the given indices.
- Parameters:
- indsiter of int
The array indices.
- Returns:
- set of object
The set of keys corresponding to the given indices.
- key2range(key)[source]
Get the range corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Data corresponding to an index range.
- Returns:
- tuple of (int, int)
The range of indices corresponding to the given key.
- key2size(key)[source]
Get the size corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Key corresponding to an index range.
- Returns:
- int
The size corresponding to the given key.
- overlap_iter(key, other)[source]
Find the set of keys that overlap between this mapper and another.
- Parameters:
- keyobject
Key corresponding to an index range.
- otherRangeMapper
Another mapper.
- Yields:
- (obj, int, int, obj, int, int)
(key, start, stop, otherkey, otherstart, otherstop).
- class openmdao.utils.rangemapper.RangeTree(sizes)[source]
Bases:
RangeMapper
A binary search tree of sizes, mapping key to an index range.
Allows for fast lookup of the key corresponding to a given index. The sizes must be contiguous, but they can be of different sizes.
Search complexity is O(log2 n). Uses less memory than FlatRangeMapper when total array size is large.
- Parameters:
- sizeslist of (key, start, stop)
Ordered list of (key, start, stop) tuples, where start and stop define the range of indices for the key. Ranges must be contiguous. key must be hashable.
- Attributes:
- rootRangeTreeNode
Root node of the binary search tree.
- __getitem__(idx)[source]
Find the key corresponding to the given index.
- Parameters:
- idxint
The index into the full array.
- Returns:
- object or None
The key corresponding to the given index, or None if not found.
- int or None
The rank corresponding to the given index, or None if not found.
- __init__(sizes)[source]
Initialize a RangeTree.
- __iter__()[source]
Iterate over (key, start, stop) tuples.
- Yields:
- (obj, int, int)
(key, start index, stop index), where key is a hashable object.
- between_iter(start_key, stop_key)
Iterate over (key, start, stop) tuples between the given start and stop keys.
- Parameters:
- start_keyobject
Key corresponding to an index range.
- stop_keyobject
Key corresponding to an index range.
- Yields:
- (obj, int, int)
(key, relative start index, relative stop index), where key is a hashable object.
- build(ranges)[source]
Build a binary search tree to map indices to variable key.
- Parameters:
- rangeslist of (key, start, stop)
List of (key, start, stop) tuples, where start and stop define the range of indices for the key. Ranges must be ordered and contiguous. key must be hashable.
- Returns:
- RangeTreeNode
Root node of the binary search tree.
- static create(sizes, max_flat_range_size=10000)
Return a mapper that maps indices to variable names and relative indices.
- Parameters:
- sizesiterable of (key, size)
Iterable of (key, size) tuples.
- max_flat_range_sizeint
If the total array size is less than this, a FlatRangeMapper will be returned instead of a RangeTree.
- Returns:
- FlatRangeMapper or RangeTree
A mapper that maps indices to variable key and relative indices.
- dump()
Dump the contents of the mapper to stdout.
- index2key_rel(idx)[source]
Find the key and relative index corresponding to the matched range.
- Parameters:
- idxint
The index into the full array.
- Returns:
- obj or None
The key corresponding to the matched range, or None if not found.
- int or None
The relative index into the matched range, or None if not found.
- inds2keys(inds)
Find the set of keys corresponding to the given indices.
- Parameters:
- indsiter of int
The array indices.
- Returns:
- set of object
The set of keys corresponding to the given indices.
- key2range(key)
Get the range corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Data corresponding to an index range.
- Returns:
- tuple of (int, int)
The range of indices corresponding to the given key.
- key2size(key)
Get the size corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Key corresponding to an index range.
- Returns:
- int
The size corresponding to the given key.
- overlap_iter(key, other)
Find the set of keys that overlap between this mapper and another.
- Parameters:
- keyobject
Key corresponding to an index range.
- otherRangeMapper
Another mapper.
- Yields:
- (obj, int, int, obj, int, int)
(key, start, stop, otherkey, otherstart, otherstop).
- class openmdao.utils.rangemapper.RangeTreeNode(key, start, stop)[source]
Bases:
RangeMapper
A node in a binary search tree of sizes, mapping key to an index range.
- Parameters:
- keyobject
Data corresponding to an index range.
- startint
Starting index of the variable.
- stopint
Ending index of the variable.
- Attributes:
- keyobject
Data corresponding to an index range.
- startint
Starting index of the variable.
- stopint
Ending index of the variable.
- leftRangeTreeNode or None
Left child node.
- rightRangeTreeNode or None
Right child node.
- __getitem__(idx)
Find the key corresponding to the given index.
- Parameters:
- idxint
The index into the full array.
- __init__(key, start, stop)[source]
Initialize a RangeTreeNode.
- __iter__()
Iterate over (key, start, stop) tuples.
- Yields:
- (obj, int, int)
(key, start index, stop index), where key is a hashable object.
- between_iter(start_key, stop_key)
Iterate over (key, start, stop) tuples between the given start and stop keys.
- Parameters:
- start_keyobject
Key corresponding to an index range.
- stop_keyobject
Key corresponding to an index range.
- Yields:
- (obj, int, int)
(key, relative start index, relative stop index), where key is a hashable object.
- static create(sizes, max_flat_range_size=10000)
Return a mapper that maps indices to variable names and relative indices.
- Parameters:
- sizesiterable of (key, size)
Iterable of (key, size) tuples.
- max_flat_range_sizeint
If the total array size is less than this, a FlatRangeMapper will be returned instead of a RangeTree.
- Returns:
- FlatRangeMapper or RangeTree
A mapper that maps indices to variable key and relative indices.
- dump()
Dump the contents of the mapper to stdout.
- inds2keys(inds)
Find the set of keys corresponding to the given indices.
- Parameters:
- indsiter of int
The array indices.
- Returns:
- set of object
The set of keys corresponding to the given indices.
- key
- key2range(key)
Get the range corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Data corresponding to an index range.
- Returns:
- tuple of (int, int)
The range of indices corresponding to the given key.
- key2size(key)
Get the size corresponding to the given key.
- Parameters:
- keyobject (must be hashable)
Key corresponding to an index range.
- Returns:
- int
The size corresponding to the given key.
- left
- overlap_iter(key, other)
Find the set of keys that overlap between this mapper and another.
- Parameters:
- keyobject
Key corresponding to an index range.
- otherRangeMapper
Another mapper.
- Yields:
- (obj, int, int, obj, int, int)
(key, start, stop, otherkey, otherstart, otherstop).
- right
- start
- stop