pykappa.utils

Functions

equilibrated(values, times[, tail_fraction, ...])

Checks whether the magnitude of the slope of the tail of the series relative to the mean is sufficiently small (below tolerance).

rejection_sample(population, excluded[, ...])

Randomly sample an element from population that is not in excluded.

str_table(rows[, header])

Format rows into a table with aligned columns.

Classes

Counted()

Assigns a unique integer ID to each instance, starting from 0.

IndexedSet([iterable])

A subclass of the built-in set, with support for indexing by arbitrary properties of set members and integer indexing to enable random sampling.

OrderedSet([items])

class pykappa.utils.Counted[source]

Assigns a unique integer ID to each instance, starting from 0.

counter = 0
class pykappa.utils.IndexedSet(iterable=[])[source]

A subclass of the built-in set, with support for indexing by arbitrary properties of set members and integer indexing to enable random sampling.

Credit https://stackoverflow.com/a/15993515 for the integer indexing logic.

NOTE: Member ordering is not stable across insertions and deletions.

Example usage: ``` […] # define a SportsTeam class

teams: IndexedSet[SportsTeam] = IndexedSet() teams.create_index(“name”, lambda team: [team.name]) teams.create_index(“color”, lambda team: [team.jersey_color])

[…] # populate the set with teams

teams.lookup_one(“name”, “Manchester”) # Returns the team whose name is “Manchester” teams.lookup(“color”, “blue”) # Returns all teams with blue jerseys ```

Parameters:

iterable (Iterable[T])

add(item)[source]

Add an element to a set.

This has no effect if the element is already present.

Parameters:

item (T)

create_index(name, prop)[source]

Create an index that’s updated when adding and removing members.

Note

Mutating set members outside of interface calls can invalidate indices.

Parameters:
  • name (str)

  • prop (Callable[[T], Iterable[Hashable]])

indices: dict[str, defaultdict[Hashable, Self]]
lookup(name, value)[source]

Return an IndexedSet of all matching items.

Parameters:
  • name (str)

  • value (Any)

Return type:

Self

lookup_one(name, value)[source]

Return a single matching item. Raises if not exactly one match.

Parameters:
  • name (str)

  • value (Any)

Return type:

T

properties: dict[str, Callable[[T], Iterable[Hashable]]]
remove(item)[source]

Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError.

Parameters:

item (T)

remove_by(prop_name, value)[source]

Remove all set members whose given property matches value.

Parameters:
  • prop_name (str)

  • value (Any)

class pykappa.utils.OrderedSet(items=None)[source]
Parameters:

items (Iterable | None)

add(item)[source]
Parameters:

item (Any)

Return type:

None

remove(item)[source]
Parameters:

item (Any)

Return type:

None

pykappa.utils.equilibrated(values, times, tail_fraction=0.1, tolerance=0.01)[source]

Checks whether the magnitude of the slope of the tail of the series relative to the mean is sufficiently small (below tolerance). Time can be provided to account for non-uniform sampling intervals.

Raises:

AssertionError – If there are not enough measurements to assess equilibration.

Parameters:
  • values (list[float])

  • times (list[float] | None)

  • tail_fraction (float)

  • tolerance (float)

Return type:

bool

pykappa.utils.rejection_sample(population, excluded, max_attempts=100)[source]

Randomly sample an element from population that is not in excluded.

Parameters:
  • population (Iterable)

  • excluded (Iterable)

  • max_attempts (int)

pykappa.utils.str_table(rows, header=None)[source]

Format rows into a table with aligned columns.

Parameters:
  • rows (list[list])

  • header (list | None)

Return type:

str