Partition

class samplomatic.partition.Partition(num_elements_per_part: int, parts: Iterable[tuple[ElementT, ...]] | None = None)[source]

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.

  • partsNone, or the parts to initialize with.

Methods Summary

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(part: tuple[ElementT, ...])[source]

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

Copy this instance.

difference(subtracted: set[ElementT]) Partition[source]

Return the difference with respect to subtracted.

Order is maintained of those remaining parts.

classmethod from_elements(elements: Iterable[ElementT]) T[source]

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(other: T) ndarray[int64][source]

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(other: Partition, strict: bool = False) Partition[source]

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 strict is True and other contains a part that partially overlaps with this partition.

overlaps_with(elements: Iterable[ElementT]) bool[source]

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(required: set[ElementT]) Partition[source]

Restrict to those parts with containment in the required set.

Order is maintained of those remaining parts.

classmethod union(*all_partitions: T) T[source]

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_part are incompatible.

  • BuildError – If a partition partially overlaps another, or is reordered.