|
| 1 | +# SCPN Phase Orchestrator |
| 2 | +# Copyright concepts (c) 1996-2026 Miroslav Sotek. All rights reserved. |
| 3 | +# Copyright code (c) 2026 Miroslav Sotek. All rights reserved. |
| 4 | +# ORCID: https://orcid.org/0009-0009-3560-0851 |
| 5 | +# Contact: www.anulum.li | protoscience@anulum.li |
| 6 | +# License: GNU AGPL v3 | Commercial licensing available |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +from numpy.typing import NDArray |
| 12 | + |
| 13 | +from scpn_phase_orchestrator.actuation.mapper import ControlAction |
| 14 | +from scpn_phase_orchestrator.upde.metrics import UPDEState |
| 15 | + |
| 16 | +__all__ = ["SNNControllerBridge"] |
| 17 | + |
| 18 | +# Abbott 1999, Eq. 1 — LIF time constants |
| 19 | +TAU_RC = 0.02 # s, membrane time constant |
| 20 | +TAU_REF = 0.002 # s, refractory period |
| 21 | + |
| 22 | + |
| 23 | +class SNNControllerBridge: |
| 24 | + """Bridge between UPDE state and spiking neural network controllers. |
| 25 | +
|
| 26 | + Pure-numpy methods work without SNN libraries. Nengo/Lava methods |
| 27 | + require their respective packages. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + n_neurons: int = 100, |
| 33 | + tau_rc: float = TAU_RC, |
| 34 | + tau_ref: float = TAU_REF, |
| 35 | + ) -> None: |
| 36 | + self.n_neurons = n_neurons |
| 37 | + self.tau_rc = tau_rc |
| 38 | + self.tau_ref = tau_ref |
| 39 | + |
| 40 | + def upde_state_to_input_current( |
| 41 | + self, state: UPDEState, i_scale: float = 1.0 |
| 42 | + ) -> NDArray: |
| 43 | + """Map R values from each layer to LIF input currents.""" |
| 44 | + r_values = np.array([ls.R for ls in state.layers], dtype=np.float64) |
| 45 | + return r_values * i_scale |
| 46 | + |
| 47 | + def spike_rates_to_actions( |
| 48 | + self, |
| 49 | + rates: NDArray, |
| 50 | + layer_assignments: list[int], |
| 51 | + threshold_hz: float = 50.0, |
| 52 | + ) -> list[ControlAction]: |
| 53 | + """Convert spike rates to control actions. |
| 54 | +
|
| 55 | + *rates*: 1-D array of mean firing rates (Hz) per neuron group. |
| 56 | + *layer_assignments*: maps each rate index to a layer. |
| 57 | + *threshold_hz*: rates above this trigger coupling boost. |
| 58 | + """ |
| 59 | + actions: list[ControlAction] = [] |
| 60 | + for idx, (rate, layer) in enumerate( |
| 61 | + zip(rates, layer_assignments, strict=False) |
| 62 | + ): |
| 63 | + if rate > threshold_hz: |
| 64 | + excess = (rate - threshold_hz) / threshold_hz |
| 65 | + actions.append( |
| 66 | + ControlAction( |
| 67 | + knob="K", |
| 68 | + scope=f"layer_{layer}", |
| 69 | + value=0.05 * excess, |
| 70 | + ttl_s=5.0, |
| 71 | + justification=f"SNN group {idx}: {rate:.1f} Hz", |
| 72 | + ) |
| 73 | + ) |
| 74 | + return actions |
| 75 | + |
| 76 | + def lif_rate_estimate(self, currents: NDArray) -> NDArray: |
| 77 | + """Analytic LIF steady-state firing rate (Abbott 1999, Eq. 1). |
| 78 | +
|
| 79 | + rate = 1 / (tau_ref - tau_rc * ln(1 - 1/J)) for J > 1 |
| 80 | + """ |
| 81 | + rates = np.zeros_like(currents, dtype=np.float64) |
| 82 | + above = currents > 1.0 |
| 83 | + if above.any(): |
| 84 | + j = currents[above] |
| 85 | + rates[above] = 1.0 / (self.tau_ref - self.tau_rc * np.log(1.0 - 1.0 / j)) |
| 86 | + return rates |
| 87 | + |
| 88 | + def build_nengo_network( |
| 89 | + self, n_layers: int, seed: int = 0, synapse: float = 0.01 |
| 90 | + ) -> object: |
| 91 | + """Build a Nengo network for UPDE-SNN coupling. |
| 92 | +
|
| 93 | + Raises ImportError if nengo is not installed. |
| 94 | + """ |
| 95 | + import nengo |
| 96 | + |
| 97 | + with nengo.Network(seed=seed) as model: |
| 98 | + model.input_node = nengo.Node(size_in=n_layers) |
| 99 | + model.ensemble = nengo.Ensemble( |
| 100 | + n_neurons=self.n_neurons, |
| 101 | + dimensions=n_layers, |
| 102 | + neuron_type=nengo.LIF(tau_rc=self.tau_rc, tau_ref=self.tau_ref), |
| 103 | + ) |
| 104 | + model.output_node = nengo.Node(size_in=n_layers) |
| 105 | + nengo.Connection(model.input_node, model.ensemble, synapse=synapse) |
| 106 | + nengo.Connection(model.ensemble, model.output_node, synapse=synapse) |
| 107 | + return model |
| 108 | + |
| 109 | + def build_lava_process(self, n_layers: int) -> object: |
| 110 | + """Build a Lava LIF process for UPDE-SNN coupling. |
| 111 | +
|
| 112 | + Raises ImportError if lava-nc is not installed. |
| 113 | + """ |
| 114 | + from lava.proc.lif.process import LIF |
| 115 | + |
| 116 | + return LIF( |
| 117 | + shape=(self.n_neurons,), |
| 118 | + du=1.0 / self.tau_rc, |
| 119 | + dv=1.0 / self.tau_ref, |
| 120 | + vth=1.0, |
| 121 | + ) |
0 commit comments