Skip to content

lixiang1076/fly-brain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿชฐ Chat with a Fruit Fly Brain

Talk to 138,000 neurons. Ask a fly to walk, taste sugar, or run for its life.

A whole-brain computational model of the adult Drosophila melanogaster (fruit fly), built on the FlyWire connectome (~138k neurons, ~5M synapses). Translates natural language into neural stimulations, runs a leaky integrate-and-fire (LIF) simulation across the entire brain, and describes what the fly would do.

Based on Shiu et al. (2023) โ€” "A leaky integrate-and-fire computational model based on the connectome of the entire adult Drosophila brain reveals insights into sensorimotor processing".

โœจ What Can You Do?

Stimulus What Happens
๐Ÿฌ Sweet taste Fly extends proboscis โ†’ feeding
๐Ÿ’€ Bitter taste Fly retracts โ†’ rejection
๐Ÿฆถ Walk forward Coordinated leg movement
๐Ÿ”™ Walk backward Moonwalker retreat
๐Ÿš€ Escape Giant fiber activation โ†’ emergency takeoff
๐Ÿงน Groom Antenna cleaning with front legs
๐Ÿ‘ Looming shadow Visual threat โ†’ triggers escape
๐Ÿ‘‚ Sound Auditory processing via Johnston's organ
๐Ÿ‘ƒ Danger smell Olfactory avoidance (geosmin detection)

Combine them: Walk + looming shadow, sweet + bitter (watch the fly get confused)

Learning: The fly has a mushroom body with dopamine-modulated plasticity. Punish it (โšก electric shock) or reward it (๐Ÿฐ) and it remembers!

๐Ÿš€ Quick Start

1. Clone (includes connectome data via Git LFS)

git clone https://github.com/lixiang1076/fly-brain.git
cd fly-brain

๐Ÿ’ก Large data files (~200 MB) are stored with Git LFS. Make sure you have git-lfs installed. If data files appear as small pointer files, run git lfs pull.

2. Install dependencies

conda env create -f environment.yml
conda activate brain-fly

3. Chat with the fly!

Interactive mode (recommended):

python fly_chat.py

Then type in Chinese or English:

๐Ÿชฐ > ็ป™ๆžœ่‡ๅฐ็”œ็š„
๐Ÿ”ฌ ็†่งฃ: ็ป™ๆžœ่‡ๅฐ็”œ็š„
   ๅผ€ๅง‹ไปฟ็œŸ 13.8ไธ‡ไธช็ฅž็ปๅ…ƒ...

==================================================
๐Ÿชฐ  ๆžœ่‡ๅคง่„‘ไปฟ็œŸๅฎŒๆˆ๏ผ
    ไปฟ็œŸๆ—ถ้•ฟ: 0.1็ง’
    ๆดป่ทƒ็ฅž็ปๅ…ƒ: 324/138639
    ๆ€ป่„‰ๅ†ฒๆ•ฐ: 1571
==================================================

๐Ÿง  ๆžœ่‡็š„ๅๅบ”:
    ๐Ÿ‘… ๆžœ่‡ไผธๅ‡บไบ†ๅฃๅ™จ๏ผˆproboscis๏ผ‰๏ผๅฎƒๅœจ็งฏๆž่ฟ›้ฃŸ๏ผŒ็œ‹่ตทๆฅๅพˆไบซๅ—ใ€‚

๐Ÿ“Š ๅ…ณ้”ฎ่พ“ๅ‡บ็ฅž็ปๅ…ƒๆดปๅŠจ:
    MN9_left              80.0 Hz  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ  (ๅฃๅ™จ่ฟๅŠจ (่ฟ›้ฃŸ))
    MN9_right             60.0 Hz  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ  (ๅฃๅ™จ่ฟๅŠจ (่ฟ›้ฃŸ))

Command-line mode (JSON output):

# Single stimulus
python chat_with_fly.py --stim taste_sweet --duration 0.1 --pretty

# Combo: walking + visual threat
python chat_with_fly.py --stim walk_forward,vision_looming --freq 100,200 --duration 0.1 --pretty

# Silence a sense (blind the fly, then make it walk)
python chat_with_fly.py --stim walk_forward --silence vision_looming --duration 0.1 --pretty

# Stimulate by neuron ID directly
python chat_with_fly.py --neuron-ids 720575940622838154,720575940632499757 --freq 200 --duration 0.1 --pretty

๐Ÿง  Learning & Memory

The fly has a mushroom body โ€” the insect equivalent of associative memory. It supports dopamine-modulated learning:

๐Ÿชฐ > ็ป™ๆžœ่‡ๅฐ็”œ็š„     โ† present a stimulus
๐Ÿชฐ > ็”ตๅ‡ป              โ† punish! (PPL1 dopamine neurons fire)
โšก ๆžœ่‡ๅญฆไผšไบ†๏ผšไธŠๆฌก็š„ๅˆบๆฟ€ = ๅฑ้™ฉ๏ผ

๐Ÿชฐ > ็ป™ๆžœ่‡ๅฐ็”œ็š„     โ† same stimulus again
๐Ÿง  ๆžœ่‡่ฎฐๅพ—่ฟ™ไธชๆฐ”ๅ‘ณๆ˜ฏๅฑ้™ฉ็š„๏ผๅผบ็ƒˆๅ›ž้ฟๅๅบ”  โ† learned avoidance!

๐Ÿชฐ > ่ฎฐๅฟ†              โ† check what the fly has learned
๐Ÿชฐ > ๅคฑๅฟ†              โ† wipe its memory (amnesia)

This mirrors real fly learning: Kenyon Cells (KC) encode stimulus identity, Dopamine Neurons (DAN) carry reward/punishment signals, and KCโ†’MBON synaptic weights are modified accordingly.

  • 4,133 Kenyon Cells (KCฮณ + KCฮฑฮฒ)
  • 96 Mushroom Body Output Neurons (MBON)
  • 307 PAM dopamine neurons (reward)
  • 24 PPL1 dopamine neurons (punishment)

๐Ÿ“ Project Structure

fly-brain/
โ”œโ”€โ”€ fly_chat.py              # ๐ŸŽฎ Interactive chat interface (start here!)
โ”œโ”€โ”€ chat_with_fly.py         # ๐Ÿ”ง CLI simulation wrapper (JSON output)
โ”œโ”€โ”€ dopamine_learning.py     # ๐Ÿง  Mushroom body learning (KCโ†’MBON plasticity)
โ”œโ”€โ”€ fast_learning.py         # โšก Fast post-hoc learning (no recompile needed)
โ”œโ”€โ”€ neuron_atlas.json        # ๐Ÿ—บ๏ธ Stimulusโ†’neuron mappings & output definitions
โ”œโ”€โ”€ main.py                  # ๐Ÿ“Š Multi-framework benchmark runner
โ”œโ”€โ”€ environment.yml          # ๐Ÿ“ฆ Conda environment
โ”œโ”€โ”€ code/
โ”‚   โ”œโ”€โ”€ benchmark.py         # Benchmark orchestrator
โ”‚   โ”œโ”€โ”€ paper-phil-drosophila/
โ”‚   โ”‚   โ”œโ”€โ”€ model.py         # Core LIF model (Brian2)
โ”‚   โ”‚   โ”œโ”€โ”€ utils.py         # Analysis utilities
โ”‚   โ”‚   โ”œโ”€โ”€ example.ipynb    # Tutorial notebook
โ”‚   โ”‚   โ””โ”€โ”€ figures.ipynb    # Reproduce paper figures
โ”‚   โ”œโ”€โ”€ run_brian2_cuda.py   # Brian2/Brian2CUDA runner
โ”‚   โ”œโ”€โ”€ run_pytorch.py       # PyTorch runner
โ”‚   โ””โ”€โ”€ run_nestgpu.py       # NEST GPU runner
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ 2025_Completeness_783.csv      # Neuron list (FlyWire v783, LFS)
โ”‚   โ”œโ”€โ”€ 2025_Connectivity_783.parquet  # Synapse connectivity (LFS)
โ”‚   โ”œโ”€โ”€ mushroom_body_neurons.json     # MB neuron IDs
โ”‚   โ”œโ”€โ”€ flywire_annotations.tsv       # Neuron annotations (LFS)
โ”‚   โ””โ”€โ”€ archive/                       # Legacy v630 data (LFS)
โ””โ”€โ”€ scripts/
    โ”œโ”€โ”€ download_data.py     # Verify data files
    โ””โ”€โ”€ setup_WSL_CUDA.sh    # WSL2 + CUDA setup guide

๐Ÿ“Š Benchmark: 4 Frameworks Compared

The same LIF model runs on four simulation backends:

Framework Backend Notes
Brian2 C++ standalone (CPU) Reference implementation
Brian2CUDA CUDA (GPU) GPU-accelerated Brian2
PyTorch CUDA (GPU) Sparse tensor operations
NEST GPU CUDA (custom kernel) Requires separate build
python main.py --pytorch --t_run 1 --n_run 1    # single backend
python main.py                                    # all backends, all durations

๐Ÿ“ฆ Data

FlyWire connectome v783 (public release). Stored via Git LFS.

File Description Size
2025_Completeness_783.csv 138,639 neuron IDs + metadata 3.3 MB
2025_Connectivity_783.parquet ~5M synaptic connections + weights 96 MB
mushroom_body_neurons.json 4,133 KC + 96 MBON + 331 DAN neuron IDs 120 KB
flywire_annotations.tsv Neuron type annotations 31 MB
archive/ Legacy v630 data (for paper figure reproduction) 86 MB

โš™๏ธ Requirements

  • Minimum: Python 3.10+, 8 GB RAM (Brian2 CPU backend)
  • Recommended: NVIDIA GPU with CUDA 12.x (for PyTorch / Brian2CUDA backends)
  • First simulation takes ~60โ€“90s (Brian2 C++ compilation); subsequent runs reuse the compiled model

๐Ÿ”ฌ How It Works

  1. Input โ€” specify neurons to stimulate (natural language, stimulus keys, or FlyWire IDs)
  2. Mapping โ€” neuron_atlas.json resolves stimuli to specific FlyWire neuron IDs
  3. Simulation โ€” Brian2 runs a LIF simulation across all 138,639 neurons with 0.1ms timestep
  4. Analysis โ€” output neuron firing rates โ†’ behavioral predictions
  5. Learning โ€” mushroom body KCโ†’MBON weights modified by dopamine signals

Neuron model parameters from Kakaria & de Bivort (2017):

Parameter Value
Resting potential โˆ’52 mV
Spike threshold โˆ’45 mV
Membrane time constant 20 ms
Synaptic time constant 5 ms
Refractory period 2.2 ms
Synaptic delay 1.8 ms

๐Ÿ“œ Citation

If you use this code, please cite:

@article{shiu2023lif,
  title={A leaky integrate-and-fire computational model based on the connectome
         of the entire adult Drosophila brain reveals insights into sensorimotor processing},
  author={Shiu, Philip K and others},
  journal={bioRxiv},
  year={2023},
  doi={10.1101/2023.05.02.539144}
}

๐Ÿ“„ License

MIT License. See LICENSE for details.

The FlyWire connectome data is subject to the FlyWire Terms of Use.

About

๐Ÿชฐ Chat with 138,000 neurons โ€” whole-brain fruit fly simulation with natural language interface and dopamine learning

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors