kappybara.mixture#

Functions

grouped(components)

Group components by isomorphism.

neighborhood(agents, radius)

Get all agents within a distance radius of the given agents.

Classes

ComponentMixture([patterns])

A mixture that explicitly tracks connected components.

Edge(site1, site2)

Represents bonds between sites.

Mixture([patterns])

A collection of agents and their connections.

MixtureUpdate([agents_to_add, ...])

Specifies changes to be applied to a mixture.

class kappybara.mixture.ComponentMixture(patterns=None)[source]#

A mixture that explicitly tracks connected components.

Parameters:

patterns (Iterable[Pattern] | None)

components#

Indexed set of all components in the mixture.

Type:

kappybara.utils.IndexedSet[kappybara.pattern.Component]

apply_update(update)[source]#

Apply a collection of changes to the mixture.

Parameters:

update (MixtureUpdate) – MixtureUpdate specifying changes to apply.

Return type:

None

components: IndexedSet[Component]#
embeddings_in_component(match_pattern, mixture_component)[source]#

Get embeddings of a pattern within a specific component.

Parameters:
  • match_pattern (Component) – Pattern to find embeddings for.

  • mixture_component (Component) – Component to search within.

Returns:

List of embeddings within the specified component.

Return type:

list[dict[Agent, Agent]]

track_component(component)[source]#

Start tracking embeddings of a component pattern.

Parameters:

component (Component) – Component pattern to track.

class kappybara.mixture.Edge(site1, site2)[source]#

Represents bonds between sites.

Note

Edge(x, y) is the same as Edge(y, x).

Parameters:
site1#

First site in the bond.

Type:

kappybara.pattern.Site

site2#

Second site in the bond.

Type:

kappybara.pattern.Site

site1: Site#
site2: Site#
class kappybara.mixture.Mixture(patterns=None)[source]#

A collection of agents and their connections.

Parameters:

patterns (Iterable[Pattern] | None)

agents#

Indexed set of all agents in the mixture.

Type:

kappybara.utils.IndexedSet[kappybara.pattern.Agent]

_embeddings#

Cache of embeddings for tracked components.

Type:

dict[kappybara.pattern.Component, kappybara.utils.IndexedSet[kappybara.pattern.Embedding]]

_max_embedding_width#

Maximum diameter of tracked components.

Type:

int

add(component)[source]#

Add a component to the mixture.

Parameters:

component (Component) – Component to add with its agents and connections.

Return type:

None

agents: IndexedSet[Agent]#
apply_update(update)[source]#

Apply a collection of changes to the mixture.

Parameters:

update (MixtureUpdate) – MixtureUpdate specifying changes to apply.

Return type:

None

embeddings(component)[source]#

Get embeddings of a tracked component.

Notes

Returns the number of matches directly returned by subgraph isomorphism, i.e. not accounting for symmetries.

Parameters:

component (Component) – Component to get embeddings for.

Returns:

Set of embeddings for the component.

Raises:

KeyError – If component is not being tracked.

Return type:

IndexedSet[Embedding]

classmethod from_kappa(patterns)[source]#

Create a mixture from Kappa pattern strings and counts.

Parameters:

patterns (dict[str, int]) – Dictionary mapping pattern strings to copy counts.

Returns:

New Mixture with instantiated patterns.

Return type:

Self

instantiate(pattern, n_copies=1)[source]#

Add instances of a pattern to the mixture.

Parameters:
  • pattern (Pattern | str) – Pattern to instantiate, or Kappa string.

  • n_copies (int) – Number of copies to create.

Raises:

AssertionError – If pattern is underspecified.

Return type:

None

property kappa_str: str#

The mixture representation in Kappa format.

Returns:

Kappa string with %init declarations for each component type.

remove(component)[source]#

Remove a component from the mixture.

Parameters:

component (Component) – Component to remove.

Return type:

None

track_component(component)[source]#

Start tracking embeddings of a component.

Parameters:

component (Component) – Component pattern to track.

class kappybara.mixture.MixtureUpdate(agents_to_add=<factory>, agents_to_remove=<factory>, edges_to_add=<factory>, edges_to_remove=<factory>, agents_changed=<factory>)[source]#

Specifies changes to be applied to a mixture.

Parameters:
  • agents_to_add (list[Agent])

  • agents_to_remove (list[Agent])

  • edges_to_add (set[Edge])

  • edges_to_remove (set[Edge])

  • agents_changed (set[Agent])

agents_to_add#

Agents to be added to the mixture.

Type:

list[kappybara.pattern.Agent]

agents_to_remove#

Agents to be removed from the mixture.

Type:

list[kappybara.pattern.Agent]

edges_to_add#

Edges to be created.

Type:

set[kappybara.mixture.Edge]

edges_to_remove#

Edges to be removed.

Type:

set[kappybara.mixture.Edge]

agents_changed#

Agents with internal state changes.

Type:

set[kappybara.pattern.Agent]

agents_changed: set[Agent]#
agents_to_add: list[Agent]#
agents_to_remove: list[Agent]#
connect_sites(site1, site2)[source]#

Specify to create an edge between two sites.

If the sites are bound to other sites, indicates to remove those edges.

Parameters:
  • site1 (Site) – First site to connect.

  • site2 (Site) – Second site to connect.

Return type:

None

create_agent(agent)[source]#

Create a new agent based on a template.

Note

Sites in the created agent will be emptied.

Parameters:

agent (Agent) – Template agent to base the new agent on.

Returns:

New agent with empty sites.

Return type:

Agent

disconnect_site(site)[source]#

Specify that a site should be unbound.

Parameters:

site (Site) – Site to disconnect from its partner.

Return type:

None

edges_to_add: set[Edge]#
edges_to_remove: set[Edge]#
register_changed_agent(agent)[source]#

Register an agent as having internal state changes.

Parameters:

agent (Agent) – Agent that has been internally modified.

Return type:

None

remove_agent(agent)[source]#

Specify to remove an agent and its edges from the mixture.

Parameters:

agent (Agent) – Agent to remove.

Return type:

None

property touched_after: set[Agent]#

The agents that will be changed or added after this update.

Returns:

Set of agents affected by the update.

property touched_before: set[Agent]#

The agents that will be changed or removed by this update.

Returns:

Set of agents affected before the update is applied.

kappybara.mixture.grouped(components)[source]#

Group components by isomorphism.

Parameters:

components (Iterable[Component]) – Components to group.

Returns:

Dictionary mapping representative components to lists of isomorphic components.

Return type:

dict[Component, list[Component]]

kappybara.mixture.neighborhood(agents, radius)[source]#

Get all agents within a distance radius of the given agents.

Parameters:
  • agents (Iterable[Agent]) – Starting agents for the neighborhood.

  • radius (int) – Maximum distance to include.

Returns:

Set of all agents within the specified radius.

Return type:

set[Agent]