Analysis code for studying temporal coordination between CA1, mPFC, and VTA dopamine neurons during rapid rule switching in a W-track task. This repository combines MATLAB analysis pipelines with Python/CEBRA workflows for neural manifold learning, visualization, and decoding.
- Behavioral rule-switch analysis on trial and rule-block timescales.
- Neural embedding and decoding workflows using CEBRA.
- MATLAB pipelines for preprocessing, behavior, LFP, spike, replay, and visualization analyses.
- Python modules and notebooks for reproducible multi-session neural manifold analysis.
Learning-FlexibilityProject/
├── MATLAB/
│ ├── Preprocessing/
│ ├── Behavior/
│ ├── LFPAnalysis/
│ ├── SpikeAnalysis/
│ ├── Scripts/
│ └── plot/
├── Python/
│ ├── src/cebra_analysis/
│ ├── notebooks/
│ └── requirements.txt
└── README.md
- MATLAB (recommended: R2021b or newer).
- Python 3.9+.
- Python dependencies listed in
Python/requirements.txt.
Install Python dependencies:
pip install -r Python/requirements.txtNote: Python/requirements.txt currently includes some standard-library module names (pathlib, math, pickle, random). If pip reports errors for those entries, remove/comment them locally and install the remaining packages.
- Clone and enter the repository:
git clone https://github.com/MingxinDing/Learning-FlexibilityProject.git
cd Learning-FlexibilityProject- Create and activate a virtual environment:
python -m venv .venv
source .venv/bin/activate- Install dependencies:
pip install -r Python/requirements.txt- Make Python package imports available:
export PYTHONPATH="$PWD/Python/src:$PYTHONPATH"In notebooks, you can alternatively use:
import sys
from pathlib import Path
sys.path.append(str(Path("../src")))Raw data is not bundled with this repository.
The Python loader expects session files in the directory defined by RAW_DIR in Python/src/cebra_analysis/config.py:
RAW_DIR / "Data"for input dataRAW_DIR / "Model"for saved modelsRAW_DIR / "Embedding"for saved embeddingsRAW_DIR / "Figure"for figures
Default root in code:
RAW_DIR = Path("/Users/mingxinding/Data/RuleSwitch")Update RAW_DIR in Python/src/cebra_analysis/config.py to match your local machine.
Expected per-session data files (load_session(animal, day)):
{animal}data_neural{day:02d}.mat{animal}data_behavior{day:02d}.csv{animal}data_cellinfo{day:02d}.csv
Behavior tables used in Python workflows should include columns used by filtering/labeling code, including:
trialperfruletrajrewardbin
This example loads one session, selects a training subset, trains a CEBRA model, and saves both model and embeddings.
import numpy as np
from cebra_analysis.load_data import load_session
from cebra_analysis.compute_subset import compute_subset
from cebra_analysis.training import build_model, save_model
from cebra_analysis.embedding import save_embedding
animal, day = "TH155", 15
area = "PFC"
neural, behavior, cellinfo = load_session(animal, day)
# Select trials by performance/rule/reward criteria.
train_mask = compute_subset(behavior, pct_threshold=60, rule=2, reward=1)
cell_mask = cellinfo["area"] == area
neural_train = neural[train_mask.to_numpy(), :][:, cell_mask.to_numpy()]
label_train = behavior.loc[train_mask, ["traj", "rule", "bin"]].to_numpy()
model = build_model(max_iterations=1000, batch_size=2048)
model.fit(neural_train, label_train)
save_model(model, f"{animal}_day{day:02d}_{area}_demo.pkl")
embedding = model.transform(neural_train)
save_embedding(embedding, f"{animal}_day{day:02d}_{area}_demo_embedding.pkl")Related Python interfaces:
cebra_analysis.load_data.load_sessioncebra_analysis.compute_subset.compute_subsetcebra_analysis.training.build_modelcebra_analysis.training.save_modelcebra_analysis.training.load_modelcebra_analysis.embedding.save_embeddingcebra_analysis.embedding.load_embeddingcebra_analysis.plotting.plot_rulecebra_analysis.plotting.plot_trajectorycebra_analysis.decode.decoding_classcebra_analysis.decode.decoding_reg
Python/notebooks/CEBRA_training_multianimal.ipynb- Loads multi-animal sessions.
- Applies subset selection criteria.
- Trains multi-session CEBRA models.
- Compares single-animal vs multi-animal embeddings.
Python/notebooks/ruleswitch_cebra_run_multisession.ipynb- End-to-end exploratory analysis notebook for rule-switching manifolds.
- Includes embedding analyses, decoding, and transition-related analyses.
MATLAB analysis is organized by stage/module rather than a single entry script:
MATLAB/Preprocessing/: session-specific preprocessing and trajectory extraction.MATLAB/Behavior/: behavior/performance quantification and rule-switch summaries.MATLAB/LFPAnalysis/: theta, coherence, cross-frequency coupling, SWR analyses.MATLAB/SpikeAnalysis/: firing dynamics, remapping, decoding, replay analyses.MATLAB/Scripts/andMATLAB/plot/: summary scripts and figure generation.
Representative scripts include:
MATLAB/Behavior/PerformanceSummary.mMATLAB/Scripts/ICA_summary.mMATLAB/SpikeAnalysis/PlaceFieldRemapping.mMATLAB/SpikeAnalysis/Replay/replay_summary.m
Output locations are controlled in Python/src/cebra_analysis/config.py:
- Models:
MODEL_DIR = RAW_DIR / "Model" - Embeddings:
EMBEDDING_DIR = RAW_DIR / "Embedding" - Figures:
FIGURE_DIR = RAW_DIR / "Figure"
MATLAB outputs are generated by individual scripts and may vary by workflow and local configuration.
- Path errors:
- Confirm
RAW_DIRinPython/src/cebra_analysis/config.pypoints to your local data root.
- Confirm
- Session loading mismatch:
load_sessionraisesValueErrorwhen neural rows and behavior rows differ.- Verify matching session files and preprocessing consistency.
- Cell filtering issues:
- Ensure
cellinfoincludes anareacolumn with labels such asPFC,CA1, orVTA.
- Ensure
- Missing behavior columns:
- Verify required columns exist (
trial,perf,rule,traj,reward,bin).
- Verify required columns exist (
Contributions from lab collaborators are welcome.
- Open an issue for bugs, reproducibility gaps, or documentation updates.
- Submit pull requests with a clear description of data assumptions and expected outputs.
- Keep analysis scripts explicit about session/day/animal parameters for reproducibility.
- This repository currently does not include a
LICENSEorCITATION.cfffile. - For citation guidance, manuscript linkage, or collaboration requests, contact the repository owner via GitHub: https://github.com/MingxinDing