Skip to content

Spatial triadic percolation on a torus

Spatial triadic percolation.

  • Class: SpatialTriadicPercolationSystem
  • Import: from skcomplex.systems.mathematics.networks import SpatialTriadicPercolationSystem
  • Field: mathematics/networks
  • Topics: network, hypergraph, percolation, triadic interactions, spatial network, pattern formation
  • References:
    • Millán, Sun, Torres, and Bianconi 2023
    • Waxman 1988

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.mathematics.networks import SpatialTriadicPercolationSystem

trajectory = SpatialTriadicPercolationSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
side 4.0
density 100.0
c 0.4
positive 0.2
negative 0.2
structural_range 0.25
regulatory_range 0.25
p 0.8
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space hypergraph
Laws update
Randomness stochastic
Domains boolean
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class SpatialTriadicPercolationSystem(BaseSystem):
    """Spatial triadic percolation.

    Nearby signed regulators make the giant component form moving patterns.

    The nodes lie uniformly on a square torus of side `side` with `density` nodes per unit area, and two nodes at the
    distance `d` are linked with probability `c exp(-d / structural_range)`, a Waxman network, Eq. 1 of Millán et al.
    A node regulates a link positively with probability `positive exp(-d / regulatory_range)` and negatively with
    probability `negative exp(-d / regulatory_range)`, never both, from its distance `d` to the midpoint of the link,
    Eq. 2. Every step of the paper runs in two phases. In the phase `regulation` a link is active with probability `p`
    when none of its negative regulators is active and one of its positive regulators is, or, for the rare link
    without positive regulators, when none of its negative regulators is active. In the phase `percolation` a node is
    active when it belongs to the giant component of the active links. All nodes and links start active.

    The regulation is local, so the giant component forms clusters, stripes, and octopus-like patterns that move, and
    its size keeps changing instead of settling. At `p = 0.8` with `positive = negative` the patterns blink: a pattern
    deactivates the links it regulates negatively and the next step activates the region around it, so the active nodes
    of consecutive steps are anti-correlated. For long ranges the model becomes the triadic percolation of Erdős-Rényi
    networks with many regulators per link. The system has the activities `nodes.active` and `links.active`, and
    `links.negatives` and `links.positives`, the numbers of negative and positive regulators of every link, on a network
    whose nodes hold their positions.

    Args:
        side:
            The side of the square torus.

        density:
            The number of nodes per unit area.

        c:
            The prefactor of the probability of a structural link.

        positive:
            The prefactor of the probability of a positive regulation.

        negative:
            The prefactor of the probability of a negative regulation.

        structural_range:
            The typical length of the structural links, `d0`.

        regulatory_range:
            The typical length of the regulations, `dr`.

        p:
            The probability that a link the regulation allows is active.

        random_state:
            The seed or generator of the positions, the links, and the regulators.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import SpatialTriadicPercolationSystem
        >>> trajectory = SpatialTriadicPercolationSystem(1.0).prepare().simulate(until=2, random_state=0)
        >>> giant = trajectory.get_values('nodes.active').mean(axis=1)
        >>> bool(giant[0] == 1.0 and giant[1] < 0.1)
        True
    """

    field = 'mathematics/networks'
    topics = ('network', 'hypergraph', 'percolation', 'triadic interactions', 'spatial network', 'pattern formation')
    references = ('Millán, Sun, Torres, and Bianconi 2023', 'Waxman 1988')
    until = 100
    dt = 1
    observables = (Observable('giant', group='nodes', variable='active'),)

    def __init__(
        self,
        side: float = 4.0,
        *,
        density: float = 100.0,
        c: float = 0.4,
        positive: float = 0.2,
        negative: float = 0.2,
        structural_range: float = 0.25,
        regulatory_range: float = 0.25,
        p: float = 0.8,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.side = side
        self.density = density
        self.c = c
        self.positive = positive
        self.negative = negative
        self.structural_range = structural_range
        self.regulatory_range = regulatory_range
        self.p = p
        self.random_state = random_state

    @staticmethod
    def _get_torus_distances(first: np.ndarray, second: np.ndarray, side: float) -> np.ndarray:
        """Return the distances on a square torus between two arrays of points.

        Every point of the first array is paired with every point of the second.
        """
        offsets = np.abs(first[:, None, :] - second[None, :, :])
        offsets = np.minimum(offsets, side - offsets)
        return np.hypot(offsets[..., 0], offsets[..., 1])

    @staticmethod
    def _get_waxman_torus(n_nodes: int, side: float, c: float, scale: float, rng: np.random.Generator) -> nx.Graph:
        """Return nodes placed uniformly on a square torus.

        Each pair is linked with probability `c exp(-d / scale)`.
        """
        positions = rng.uniform(0.0, side, size=(n_nodes, 2))
        graph = nx.Graph()
        graph.add_nodes_from((node, {'pos': tuple(positions[node])}) for node in range(n_nodes))
        for start in range(0, n_nodes, CHUNK):
            rows = np.arange(start, min(start + CHUNK, n_nodes))
            distances = SpatialTriadicPercolationSystem._get_torus_distances(positions[rows], positions, side)
            linked = (rng.uniform(size=distances.shape) < c * np.exp(-distances / scale)) & (
                np.arange(n_nodes)[None, :] > rows[:, None]
            )
            graph.add_edges_from((int(rows[row]), int(other)) for row, other in zip(*np.nonzero(linked), strict=True))
        return graph

    @staticmethod
    def _get_spatial_regulators(
        graph: nx.Graph,
        side: float,
        chances: tuple[float, float],
        scale: float,
        rng: np.random.Generator,
    ) -> tuple[list[tuple[int, ...]], np.ndarray, np.ndarray]:
        """Return a hyperedge of every link with its regulators, padded to one size.

        The negative regulators come first and then the positive ones.

        A node regulates a link positively with probability `c+ exp(-d / scale)` and negatively with probability `c-
        exp(-d / scale)`, never both, from its distance `d` to the midpoint of the link on the torus. Every hyperedge is
        padded with the first end of its link up to the size of the largest one.
        """
        positive, negative = chances
        positions = np.asarray([graph.nodes[node]['pos'] for node in graph.nodes], dtype=float)
        ends = np.asarray(list(graph.edges), dtype=np.intp).reshape(-1, 2)
        steps = positions[ends[:, 1]] - positions[ends[:, 0]]
        midpoints = (positions[ends[:, 0]] + (steps - side * np.round(steps / side)) / 2) % side
        regulators: list[tuple[int, ...]] = []
        negatives, positives = [], []
        for start in range(0, len(ends), CHUNK):
            decay = np.exp(
                -SpatialTriadicPercolationSystem._get_torus_distances(midpoints[start : start + CHUNK], positions, side)
                / scale,
            )
            draws = rng.uniform(size=decay.shape)
            for row, (chance, draw) in enumerate(zip(decay, draws, strict=True)):
                against = np.flatnonzero((draw >= positive * chance) & (draw < (positive + negative) * chance))
                towards = np.flatnonzero(draw < positive * chance)
                regulators.append((*ends[start + row].tolist(), *against.tolist(), *towards.tolist()))
                negatives.append(against.size)
                positives.append(towards.size)
        width = max((len(item) for item in regulators), default=2)
        hyperedges = [item + (item[0],) * (width - len(item)) for item in regulators]
        return hyperedges, np.asarray(negatives), np.asarray(positives)

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components of the system built from its model parameters."""
        super()._prepare(context)
        rng = np.random.default_rng(self.random_state)
        graph = self._get_waxman_torus(
            round(self.density * self.side**2),
            self.side,
            self.c,
            self.structural_range,
            rng,
        )
        hyperedges, negatives, positives = self._get_spatial_regulators(
            graph,
            self.side,
            (self.positive, self.negative),
            self.regulatory_range,
            rng,
        )
        space = NetworkSpace(graph, hyperedges=hyperedges)
        nodes = PercolatingNodeElement('nodes', space=space)
        links = SignedLinkElement('links', negatives=negatives, positives=positives, space=space)
        interactions = [
            SignedRegulationInteraction('regulation', among='nodes', p=self.p, phase='regulation'),
            PercolationInteraction('percolation', nodes='nodes', links='links', phase='percolation'),
        ]
        self.system_ = System(self.label, elements=[nodes, links], interactions=interactions)

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

  • PercolatingNodeElement, from Element
  • SignedLinkElement, from Element

Interactions:

  • SignedRegulationInteraction, from Interaction
  • PercolationInteraction, from Interaction
class PercolatingNodeElement(Element):
    """Node of a network with spatial triadic regulation.

    The node is active when it belongs to the giant component.

    Args:
        label:
            The label of the node group.

        active:
            1 when the node is in the giant component of the active links, and 0 otherwise.

        space:
            The network whose nodes hold one member each, or `None` for one node.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import PercolatingNodeElement
        >>> PercolatingNodeElement('nodes').active
        1.0
    """

    visual = ACTIVITY_VISUAL

    def __init__(
        self,
        label: str,
        *,
        active: Annotated[float, FiniteEnumeratedSet([0, 1])] = 1.0,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space)
        self.active = active

class SignedLinkElement(Element):
    """Link regulated by signed nodes.

    The link has its activity and the numbers of its negative and positive regulators.

    Args:
        label:
            The label of the link group.

        active:
            1 when the link is active, and 0 otherwise.

        negatives:
            The number of regulating nodes that let the link be active only while they are inactive, the first
            regulators of its hyperedge.

        positives:
            The number of regulating nodes that let the link be active while they are active, the regulators of its
            hyperedge after the negative ones.

        space:
            The network whose links hold one member each, or `None` for one link.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import SignedLinkElement
        >>> SignedLinkElement('links', negatives=2).negatives
        2
    """

    def __init__(
        self,
        label: str,
        *,
        active: Annotated[object, FiniteEnumeratedSet([0, 1])] = 1.0,
        negatives: Annotated[object, RealSet.closed_open(0, oo)] = 0,
        positives: Annotated[object, RealSet.closed_open(0, oo)] = 1,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, space=space, on='edges' if space is not None else None)
        self.active = active
        self.negatives = negatives
        self.positives = positives

class SignedRegulationInteraction(Interaction):
    """Regulation of every link by signed regulating nodes.

    The regulators let the link be active with probability `p` in its phase.

    Every hyperedge `(i, j, r_1, ..., r_m)` holds the two ends of the link `(i, j)` and its regulating nodes, first its
    `negatives` negative regulators and then its `positives` positive ones, which the link reads. Further members pad
    the hyperedges to one size and regulate nothing, so links with several numbers of regulators share one size and one
    law writes every link once. In every step the link becomes active with probability `p` when none of its negative
    regulators is active and at least one of its positive regulators is active, or, for a link without positive
    regulators, when none of its negative regulators is active, and inactive otherwise.

    Args:
        label:
            The label of the interaction, unique among the components of its system.

        among:
            The path of the group on the nodes, whose activity the regulators read.

        along:
            `'hyperedges'`, the hyperedges of the network the regulation runs along.

        p:
            The probability that a link the regulation allows is active.

        phase:
            The phase of the step the regulation runs in, or `None` for the default phase.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import SignedRegulationInteraction
        >>> SignedRegulationInteraction('regulation', among='nodes').p
        0.5
    """

    draws = True

    law_classes = (UpdateLaw,)

    def __init__(
        self,
        label: str,
        *,
        among: Role = 'nodes',
        along: str = 'hyperedges',
        p: Annotated[float, RealSet.closed(0, 1)] = 0.5,
        phase: str | None = None,
    ) -> None:
        super().__init__(label, among=among, along=along)
        self.p = p
        self.phase = phase

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the activity of the link from its regulating nodes, the members of the hyperedge after its ends.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        s = context
        negatives, positives = s.edge.negatives, s.edge.positives
        blocked, silent = SR(1), SR(1)
        for position, regulator in enumerate(s.others[1:]):
            blocked *= 1 - where(negatives > position, regulator.active, 0)
            positive = where(negatives > position, 0, where(negatives + positives > position, 1, 0))
            silent *= 1 - positive * regulator.active
        allowed = blocked * where(positives > 0, 1 - silent, 1)
        self.laws_: list[BaseLaw] = [
            UpdateLaw(s.edge.active, bernoulli('retained', s.p) * allowed, phase=self.phase),
        ]

class PercolationInteraction(Interaction):
    """Activity of every node in its phase.

    The activity is 1 in the giant component of the active links and 0 outside it.

    Args:
        label:
            The label of the interaction, unique among the components of its system.

        nodes:
            The path of the group on the nodes, whose activity the interaction sets.

        links:
            The path of the group on the edges, whose activity selects the links of the components.

        phase:
            The phase of the step the interaction runs in, or `None` for the default phase.

    Examples:
        >>> from skcomplex.systems.mathematics.networks import PercolationInteraction
        >>> PercolationInteraction('percolation', nodes='nodes', links='links').links
        'links'
    """

    law_classes = (UpdateLaw,)

    def __init__(
        self,
        label: str,
        *,
        nodes: Role,
        links: Role,
        phase: str | None = None,
    ) -> None:
        super().__init__(label)
        self.nodes = nodes
        self.links = links
        self.phase = phase

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the membership of the giant component of the active links.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        giant = components(context.links.active, reduce='giant')
        self.laws_: list[BaseLaw] = [UpdateLaw(context.nodes.active, giant, phase=self.phase)]