Metadata-Version: 2.5
Name: flybrainer
Version: 0.1.0
Summary: A reusable, Numba-JIT accelerated fruit-fly connectome (MaleCNS v1.0) extracted from OpenFly
Project-URL: Homepage, https://github.com/chnak/flybrain
Project-URL: Source, https://github.com/chnak/flybrain
Project-URL: Issues, https://github.com/chnak/flybrain/issues
Project-URL: Changelog, https://github.com/chnak/flybrain/blob/main/docs/USAGE.zh.md
Author: OpenFly contributors
License: MIT License
        
        Copyright (c) 2026 OpenFly contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
        NOTE — The MaleCNS connectome data set (downloaded via the [download] extra)
        is licensed under CC-BY 4.0 by the FlyEM / MaleCNS team. See
        https://www.malecns.org for terms.
License-File: LICENSE
Keywords: connectomics,fruit-fly,neural-simulation,neuroscience,numba
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Life
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: <3.14,>=3.12
Requires-Dist: numba>=0.61
Requires-Dist: numpy<2.5,>=2.2
Requires-Dist: pillow>=10
Provides-Extra: dev
Requires-Dist: pytest-cov>=5; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: download
Requires-Dist: requests>=2.32; extra == 'download'
Provides-Extra: feather
Requires-Dist: pandas>=2.2; extra == 'feather'
Requires-Dist: pyarrow>=17; extra == 'feather'
Provides-Extra: readout
Requires-Dist: joblib>=1.4; extra == 'readout'
Requires-Dist: scikit-learn>=1.5; extra == 'readout'
Requires-Dist: scipy>=1.14; extra == 'readout'
Description-Content-Type: text/markdown

# flybrainer

A reusable, Numba-JIT accelerated **fruit-fly connectome** simulator —
extracted from [OpenFly](https://github.com/marketcalls/openfly).

* **Zero heavy deps** by default: `numpy` + `numba` + `pillow` only.
* **MaleCNS v1.0 connectome** loadable via `[feather,download]` extras.
* **3 encoders** + **2 readouts** + **optional plasticity**.
* **Closable LIF** kernel (Numba `@njit`), 18M+ spikes/sec.

## Why?

OpenFly is a trading system. The brain model inside it is excellent but
inseparable from the broker / market / front-end code. This package
isolates the brain so it can be reused for **any** sensor → readout task:
trading, robotics, audio classification, RL environments, etc.

## Install

```bash
# Minimum (no MaleCNS, no sklearn):
pip install flybrainer

# With everything:
pip install flybrainer[feather,readout,download]
```

## 30-second example

```python
import numpy as np
from flybrainer import Brain, Stimulus

# Build a tiny synthetic graph (no MaleCNS required)
n = 100
g = dict(
    ptr=np.array([0, n], dtype=np.int64),
    post=np.random.randint(0, n, size=n).astype(np.int32),
    weight=np.where(np.random.rand(n) > 0.5, 0.275, -0.275),
    modulatory=np.zeros(n, dtype=bool),
    type=np.array(["L1"]*90 + ["KC"]*4 + ["MBON07"]*3 + ["MBON11"]*3),
    superclass=np.array(["cb_int"]*n),
    r16_uv=np.zeros((20, 2), dtype=np.float32),
    r8_uv=np.zeros((20, 2), dtype=np.float32),
    r16_eye=np.zeros(20, dtype=np.int8),
    r8_eye=np.zeros(20, dtype=np.int8),
    r8_channel=np.ones(20, dtype=np.int8),
    r16=np.arange(20, dtype=np.int32),
    r8=np.arange(20, dtype=np.int32),
)
brain = Brain(graph_path="<test>", graph=g)
stim = Stimulus(r16=np.random.rand(20), r8=np.random.rand(20))
result = brain.observe(stim, neural_ms=500.0)
print("total spikes:", result.counts.sum())
```

See `examples/` for more.

## Architecture

```
src/flybrainer/
├── interfaces.py    # zero-dep contracts (Stimulus, BrainProtocol, SensorFrame)
├── eyemap.py        # EyeMap dataclass + resolver
├── encoders.py      # ChartEncoder, BarsEncoder, FeatureEncoder + make_encoder()
├── kernel.py        # Numba @njit LIF kernel + KernelState
├── plasticity.py    # KC→MBON plasticity (Ormond-style Oja)
├── brain.py          # Brain class (the orchestrator)
├── readout/
│   ├── fixed.py     # FixedDecoder (no training)
│   └── reservoir.py # ReservoirReadout (ridge / logistic; needs [readout])
└── connectome/
    ├── normalize.py # NT sign rules (feather source → arrays; needs [feather])
    ├── compile.py   # arrays → graph.npz (multi-platform)
    ├── sources.py   # MaleCNS v1.0 metadata + SHA256
    ├── download.py  # resumable download (urllib only)
    └── verify.py    # sha256 file/array helpers
```

## Optional dependencies

| extra | adds | when needed |
|---|---|---|
| `feather` | `pandas`, `pyarrow` | `from flybrainer.connectome import normalize, compile_graph` |
| `readout` | `scikit-learn`, `scipy`, `joblib` | `from flybrainer.readout import ReservoirReadout` |
| `download` | `requests` | `download_source(...)` |
| `dev` | `pytest`, `pytest-cov`, `ruff` | testing |

Without `[feather]`, `compile_graph` still works if `.pre` arrays are
already on disk (you just can't re-derive from raw `.feather` files).

## Tests

```bash
pip install flybrainer[feather,readout,dev]
pytest tests/ -v
```

## Citation

* Brain model: Aso & Gorinov — *bioRxiv 2024*
* Connectome: MaleCNS v1.0 — *https://www.malecns.org*
* This extraction: OpenFly — *github.com/marketcalls/openfly*

## Documentation

Detailed Chinese user manual (API contracts, 7 worked scenarios, FAQ):

* [`docs/USAGE.zh.md`](docs/USAGE.zh.md) — 991 lines / 10 chapters

Quick look-up order: 第 3 章 (core concepts) → 第 5 章 (full API) → 第 6 章 (worked scenarios) → 第 10 章 (FAQ).

## License

MIT. The MaleCNS dataset is CC-BY 4.0 (separate license).
