Partition
class samplomatic.partition.Partition(num_elements_per_part: int, parts: Iterable[tuple[ElementT, ...]] | None = None)
Bases: Generic[ElementT]
A partition of a sequence of elements into equally-sized, non-overlapping subsets.
The Partition class preserves the insertion order of the parts, as well as the order of the elements within the parts.
Parameters
- num_elements_per_part – How many elements each part must have.
- parts –
None, or the parts to initialize with.
Methods Summary
Column 1 | Column 2 |
|---|---|
add(part) | Add a new part to this partition. |
copy() | Copy this instance. |
difference(subtracted) | Return the difference with respect to subtracted. |
from_elements(elements) | Construct a new instance in the special case that num_elements_per_part is one. |
get_indices(other) | Get the indices of the parts of other in this partition. |
intersection(other[, strict]) | Return a new partition that is the intersection with the other. |
overlaps_with(elements) | Whether any iterable of elements overlaps with these elements. |
restrict(required) | Restrict to those parts with containment in the required set. |
union(*all_partitions) | Take the union of one or more partitions. |
Methods Documentation
add
add(part: tuple[ElementT, ...])
Add a new part to this partition.
Parameters
part – The part to add.
Raises
- BuildError – If the part has the incorrect size.
- BuildError – If the part partially overlaps an existing part.
copy
difference
difference(subtracted: set[ElementT]) → Partition
Return the difference with respect to subtracted.
Order is maintained of those remaining parts.
from_elements
classmethod from_elements(elements: Iterable[ElementT]) → T
Construct a new instance in the special case that num_elements_per_part is one.
Parameters
elements – The elements for each subsystem.
Returns
A new partition where each part is an element from elements.
get_indices
get_indices(other: T) → ndarray[int64]
Get the indices of the parts of other in this partition.
Parameters
other – Some other partition whose elements are present in this partition.
Returns
A list of indices indicating the position of the parts of other in this partition.
intersection
intersection(other: Partition, strict: bool = False) → Partition
Return a new partition that is the intersection with the other.
The order of this partition is maintained, whereas the order of the other partition is irrelevant.
Parameters
- other – The other partition to take the intersection with.
- strict – Whether to error on partial overlaps.
Raises
- BuildError – If the two partitions have different numbers of elements per part.
- BuildError – If
strictisTrueandothercontains a part that partially overlaps with this partition.
overlaps_with
overlaps_with(elements: Iterable[ElementT]) → bool
Whether any iterable of elements overlaps with these elements.
Parameters
elements – Any iterable of elements to check against.
Returns
Whether there is any overlap.
restrict
restrict(required: set[ElementT]) → Partition
Restrict to those parts with containment in the required set.
Order is maintained of those remaining parts.
union
classmethod union(*all_partitions: T) → T
Take the union of one or more partitions.
Order is maintained with earlier all_partitions taking precedence.
Parameters
all_partitions – The partitions to take the union of.
Returns
The union of all the partitions.
Raises
- BuildError – If no partitions are given.
- BuildError – If the
num_elements_per_partare incompatible. - BuildError – If a partition partially overlaps another, or is reordered.