|
| 1 | +"""High level information flow pipeline without vendor extensions. |
| 2 | +
|
| 3 | +The raw `ext/` directory contains uncurated drops of the historical |
| 4 | +implementation. The active codebase builds a much smaller, deterministic |
| 5 | +subset so tests and exercises can run without importing from those raw files. |
| 6 | +
|
| 7 | +`InformationFlow` stitches together the bio, emotion, field and memory |
| 8 | +components into a reproducible pipeline. The class exposes a single ``step`` |
| 9 | +method that accepts an arbitrary iterable of floats representing a sensor |
| 10 | +signal. The signal is filtered, projected into the intention field, analysed |
| 11 | +by the emotional core and finally persisted to long term memory. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +from dataclasses import dataclass, field |
| 17 | +from pathlib import Path |
| 18 | +from typing import Any, Dict, Iterable |
| 19 | + |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +from bio.crystal_receiver import CrystalFieldReceiver |
| 23 | +from bio.eeg_processor import EEGProcessor |
| 24 | +from bio.forcing_field import ForcingField |
| 25 | +from emotion.eeg_mapper import EEGEmotionMapper |
| 26 | +from emotion.emotion_core import EmotionCore |
| 27 | +from fields.intention_field import IntentionField |
| 28 | +from fields.soul_invariant import SoulInvariant |
| 29 | +from memory.long_term_memory import LongTermMemory |
| 30 | + |
| 31 | + |
| 32 | +@dataclass(slots=True) |
| 33 | +class InformationFlow: |
| 34 | + """Compose the lightweight subsystems into a single pipeline.""" |
| 35 | + |
| 36 | + receiver: CrystalFieldReceiver |
| 37 | + forcing: ForcingField |
| 38 | + eeg: EEGProcessor |
| 39 | + mapper: EEGEmotionMapper |
| 40 | + emotion: EmotionCore |
| 41 | + memory: LongTermMemory |
| 42 | + soul: SoulInvariant = field(default_factory=SoulInvariant) |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def build( |
| 46 | + cls, |
| 47 | + *, |
| 48 | + storage_path: Path, |
| 49 | + intention_seed: int | None = None, |
| 50 | + baseline: float = 0.0, |
| 51 | + sample_rate: float = 128.0, |
| 52 | + ) -> "InformationFlow": |
| 53 | + """Create a fully wired pipeline with deterministic defaults.""" |
| 54 | + |
| 55 | + intention = IntentionField(seed=intention_seed) |
| 56 | + return cls( |
| 57 | + receiver=CrystalFieldReceiver(intention), |
| 58 | + forcing=ForcingField(intention), |
| 59 | + eeg=EEGProcessor(sample_rate=sample_rate), |
| 60 | + mapper=EEGEmotionMapper(), |
| 61 | + emotion=EmotionCore(baseline=baseline), |
| 62 | + memory=LongTermMemory(storage_path), |
| 63 | + ) |
| 64 | + |
| 65 | + def step(self, signal: Iterable[float]) -> Dict[str, Any]: |
| 66 | + """Process *signal* through the entire information pipeline.""" |
| 67 | + |
| 68 | + filtered = self.eeg.filter(signal) |
| 69 | + prepared = self._match_channels(filtered) |
| 70 | + intention_vector = self.receiver.receive(prepared) |
| 71 | + modulated = self.forcing.stimulate(prepared) |
| 72 | + emotional_input = modulated if modulated.size else intention_vector |
| 73 | + |
| 74 | + distribution = self.mapper.map(emotional_input) |
| 75 | + emotion_state = self.emotion.process(emotional_input) |
| 76 | + invariant_value = self.soul.compute(self._reshape_for_invariant(emotional_input)) |
| 77 | + |
| 78 | + entry = { |
| 79 | + "filtered": prepared.tolist(), |
| 80 | + "intention": intention_vector.tolist(), |
| 81 | + "modulated": emotional_input.tolist(), |
| 82 | + "distribution": distribution, |
| 83 | + "emotion": emotion_state, |
| 84 | + "soul_invariant": invariant_value, |
| 85 | + } |
| 86 | + self.memory.store(entry) |
| 87 | + return entry |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def _reshape_for_invariant(vector: np.ndarray) -> np.ndarray: |
| 91 | + """Create a 2D matrix compatible with :class:`SoulInvariant`.""" |
| 92 | + |
| 93 | + size = vector.size |
| 94 | + if size == 0: |
| 95 | + return np.zeros((2, 2)) |
| 96 | + side = int(np.ceil(np.sqrt(size))) |
| 97 | + padded = np.pad(vector, (0, side * side - size), mode="constant", constant_values=0.0) |
| 98 | + return padded.reshape(side, side) |
| 99 | + |
| 100 | + def _match_channels(self, signal: np.ndarray) -> np.ndarray: |
| 101 | + """Fit ``signal`` to the intention channel count.""" |
| 102 | + |
| 103 | + channels = self.receiver.intention.channels |
| 104 | + if signal.size >= channels: |
| 105 | + return signal[:channels] |
| 106 | + if signal.size == 0: |
| 107 | + return signal |
| 108 | + return np.pad(signal, (0, channels - signal.size), mode="constant", constant_values=0.0) |
| 109 | + |
| 110 | + |
| 111 | +__all__ = ["InformationFlow"] |
0 commit comments