# facebookresearch/tribev2 architecture

> Architecture diagram of the GitHub repository facebookresearch/tribev2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/facebookresearch/tribev2
- Repository: https://github.com/facebookresearch/tribev2

## Overview

1. **What this repo is**
   TRIBE v2 is a multimodal fMRI encoding system: it takes naturalistic stimuli from **video, audio, and text** and predicts brain responses on a common cortical surface representation. The codebase is split between an **inference-facing model package** and a **training/experiment pipeline**.

2. **Primary runtime boundary**
   The core implementation is a **Python 3.11+ / PyTorch** project. Training uses **PyTorch Lightning** (via `pl_module.py`), while inference is exposed through a higher-level `TribeModel` wrapper in `demo_utils.py`. The README also indicates optional integrations for **plotting/visualization** and **training tooling**.

3. **Model subsystem**
   `tribev2/model.py` contains the main `FmriEncoder`, described as a **Transformer-based multimodal → fMRI model**. Architecturally, this is the central compute boundary: upstream stimulus events are converted into features/tokens, then the encoder maps them into predicted response vectors over vertices/voxels.

4. **Stimulus/event preprocessing**
   `tribev2/eventstransforms.py` and `tribev2/demo_utils.py` handle the conversion from raw inputs into model-ready event streams. The README makes clear that text can be converted into speech/transcription-derived word timings, and video/audio inputs are converted into **timed events** that align to the model’s temporal framing.

5. **Inference flow**
   The public inference path is:
   raw stimulus file (`video_path`, `audio_path`, or `text_path`) → event dataframe (`get_events_dataframe`) → prediction (`predict`) → `(preds, segments)`.
   This path is designed around a **pretrained HuggingFace checkpoint** (`facebook/tribev2`) and returns predictions on the **fsaverage5 cortical mesh** with an explicit **5-second lag offset** to compensate for hemodynamic delay.

6. **Training pipeline**
   `tribev2/main.py` and `tribev2/pl_module.py` form the experiment/training boundary. `main.py` likely orchestrates data setup and experiment configuration, while the Lightning module encapsulates optimization, batching, metrics, and training-step behavior. This separation keeps model definition distinct from training mechanics.

7. **Study-specific dataset adapters**
   The `tribev2/studies/` package contains per-dataset definitions for **Algonauts2025, Lahner2024BOLD, Lebel2023BOLD, and Wen2017**. These modules act as dataset adapters, normalizing heterogeneous study formats into the shared training pipeline so the model can be trained/evaluated across multiple neuroscience datasets.

8. **Cross-study loading and splits**
   `tribev2/utils.py` and `tribev2/utils_fmri.py` provide the glue for multi-study work: loading, subject weighting, splitting, and fMRI-specific utilities. `utils_fmri.py` also handles **surface projection** between spaces such as **MNI** and **fsaverage**, plus ROI-oriented analysis support.

9. **Grid search and experiment orchestration**
   The `tribev2/grids/` package is the experiment-launch layer. `defaults.py` centralizes configuration, `configs.py` likely defines experiment variants, and `run_cortical.py`, `run_subcortical.py`, and `test_run.py` provide entry points for **local smoke tests** and **Slurm-based grid execution**. Environment variables like `DATAPATH` and `SAVEPATH` define the external data/output boundary.

10. **Visualization subsystem**
   `tribev2/plotting/` is an optional presentation layer for brain outputs. The README mentions **PyVista** and **Nilearn** backends, implying two rendering paths for cortical/subcortical views. This layer is separate from the modeling pipeline and consumes predicted brain maps plus mesh/ROI metadata.

11. **End-to-end data flow**
   The overall architecture is:
   raw naturalistic stimulus → event construction/transforms → multimodal feature encoding → transformer-based fMRI prediction → cortical surface projection/analysis → optional visualization.
   The same core prediction output can feed either research evaluation or plotting, depending on which boundary is used.

12. **Developer-facing entry points**
   The repository is organized around a small number of clear entry surfaces: `TribeModel` for inference, `main.py`/`pl_module.py` for training, `grids/*` for experiment launch, and `plotting/*` for visualization. The notebook `tribe_demo.ipynb` ties these together for interactive use.

Main entry points: [`tribev2/utils.py`](https://github.com/facebookresearch/tribev2/blob/main/tribev2/utils.py) (Core utils), [`tribev2/plotting/base.py`](https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/base.py) (Plot base), [`tribev2/model.py`](https://github.com/facebookresearch/tribev2/blob/main/tribev2/model.py) (FmriEncoder), [`tribev2/demo_utils.py`](https://github.com/facebookresearch/tribev2/blob/main/tribev2/demo_utils.py) (TribeModel)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_inference["Inference"]
  node_node_demo_utils["TribeModel<br/>inference wrapper<br/>[demo_utils.py]"]
  node_node_eventstransforms["Event transforms<br/>event preprocessing"]
  node_node_model["FmriEncoder<br/>multimodal encoder<br/>[model.py]"]
end

subgraph group_group_training["Training"]
  node_node_main["Main entry<br/>training entrypoint<br/>[main.py]"]
  node_node_pl_module["Lightning module<br/>training logic<br/>[pl_module.py]"]
end

subgraph group_group_data["Data"]
  node_node_utils["Core utils<br/>shared helpers<br/>[utils.py]"]
  node_node_utils_fmri["fMRI utils<br/>surface utilities<br/>[utils_fmri.py]"]
  node_node_algonauts["Algonauts2025<br/>study adapter<br/>[algonauts2025.py]"]
  node_node_lahner["Lahner2024<br/>study adapter<br/>[lahner2024bold.py]"]
  node_node_lebel["Lebel2023<br/>study adapter<br/>[lebel2023bold.py]"]
  node_node_wen["Wen2017<br/>study adapter<br/>[wen2017.py]"]
end

subgraph group_group_vis["Visualization"]
  node_node_plot_base["Plot base<br/>render base<br/>[base.py]"]
  node_node_plot_cortical["Cortical plot<br/>rendering<br/>[cortical.py]"]
  node_node_plot_cortical_pv["PV cortical<br/>rendering<br/>[cortical_pv.py]"]
  node_node_plot_subcortical["Subcortical plot<br/>rendering<br/>[subcortical.py]"]
  node_node_plot_utils["Plot utils<br/>render helpers<br/>[utils.py]"]
end

subgraph group_group_launch["Launch"]
  node_node_grids_defaults["Defaults<br/>grid config<br/>[defaults.py]"]
  node_node_grids_configs["Configs<br/>grid variants<br/>[configs.py]"]
  node_node_run_cortical["Cortical run<br/>grid launcher<br/>[run_cortical.py]"]
  node_node_run_subcortical["Subcortical run<br/>grid launcher<br/>[run_subcortical.py]"]
  node_node_test_run["Smoke test<br/>launcher test<br/>[test_run.py]"]
end

node_node_demo_utils -->|"build events"| node_node_eventstransforms
node_node_demo_utils -->|"predict"| node_node_model
node_node_eventstransforms -->|"tokens"| node_node_model
node_node_main -->|"train"| node_node_pl_module
node_node_main -->|"setup"| node_node_utils
node_node_pl_module -->|"wraps"| node_node_model
node_node_utils -->|"fMRI ops"| node_node_utils_fmri
node_node_utils -->|"load"| node_node_algonauts
node_node_utils -->|"load"| node_node_lahner
node_node_utils -->|"load"| node_node_lebel
node_node_utils -->|"load"| node_node_wen
node_node_grids_defaults -->|"parameterize"| node_node_grids_configs
node_node_grids_configs -->|"launch"| node_node_run_cortical
node_node_grids_configs -->|"launch"| node_node_run_subcortical
node_node_grids_defaults -->|"smoke"| node_node_test_run
node_node_plot_base -->|"extend"| node_node_plot_cortical
node_node_plot_base -->|"extend"| node_node_plot_subcortical
node_node_plot_base -->|"extend"| node_node_plot_cortical_pv
node_node_plot_utils -->|"support"| node_node_plot_cortical
node_node_plot_utils -->|"support"| node_node_plot_subcortical
node_node_utils_fmri -->|"analyze"| node_node_plot_base
node_node_model -->|"project"| node_node_utils_fmri
node_node_demo_utils -.->|"visualize"| node_node_plot_base

click node_node_demo_utils "https://github.com/facebookresearch/tribev2/blob/main/tribev2/demo_utils.py"
click node_node_eventstransforms "https://github.com/facebookresearch/tribev2/blob/main/tribev2/eventstransforms.py"
click node_node_model "https://github.com/facebookresearch/tribev2/blob/main/tribev2/model.py"
click node_node_main "https://github.com/facebookresearch/tribev2/blob/main/tribev2/main.py"
click node_node_pl_module "https://github.com/facebookresearch/tribev2/blob/main/tribev2/pl_module.py"
click node_node_utils "https://github.com/facebookresearch/tribev2/blob/main/tribev2/utils.py"
click node_node_utils_fmri "https://github.com/facebookresearch/tribev2/blob/main/tribev2/utils_fmri.py"
click node_node_algonauts "https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/algonauts2025.py"
click node_node_lahner "https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/lahner2024bold.py"
click node_node_lebel "https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/lebel2023bold.py"
click node_node_wen "https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/wen2017.py"
click node_node_grids_defaults "https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/defaults.py"
click node_node_grids_configs "https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/configs.py"
click node_node_run_cortical "https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/run_cortical.py"
click node_node_run_subcortical "https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/run_subcortical.py"
click node_node_test_run "https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/test_run.py"
click node_node_plot_base "https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/base.py"
click node_node_plot_cortical "https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/cortical.py"
click node_node_plot_cortical_pv "https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/cortical_pv.py"
click node_node_plot_subcortical "https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/subcortical.py"
click node_node_plot_utils "https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/utils.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_demo_utils,node_node_eventstransforms,node_node_model toneBlue
class node_node_main,node_node_pl_module toneAmber
class node_node_utils,node_node_utils_fmri,node_node_algonauts,node_node_lahner,node_node_lebel,node_node_wen toneMint
class node_node_plot_base,node_node_plot_cortical,node_node_plot_cortical_pv,node_node_plot_subcortical,node_node_plot_utils toneRose
class node_node_grids_defaults,node_node_grids_configs,node_node_run_cortical,node_node_run_subcortical,node_node_test_run toneIndigo
```

## Components

### Inference

Public stimulus-to-prediction path and pretrained model wrapper.

- [TribeModel](https://github.com/facebookresearch/tribev2/blob/main/tribev2/demo_utils.py): inference wrapper · High-level inference API for loading stimuli, building events, and producing predictions. · `tribev2/demo_utils.py`
- [Event transforms](https://github.com/facebookresearch/tribev2/blob/main/tribev2/eventstransforms.py): event preprocessing · Converts raw video, audio, and text inputs into timed model-ready event streams. · `tribev2/eventstransforms.py`
- [FmriEncoder](https://github.com/facebookresearch/tribev2/blob/main/tribev2/model.py): multimodal encoder · Transformer-based core model that maps multimodal stimulus features to fMRI responses. · `tribev2/model.py`

### Training

Experiment entry points and Lightning training boundary.

- [Main entry](https://github.com/facebookresearch/tribev2/blob/main/tribev2/main.py): training entrypoint · Top-level training or experiment orchestration entry for running model workflows. · `tribev2/main.py`
- [Lightning module](https://github.com/facebookresearch/tribev2/blob/main/tribev2/pl_module.py): training logic · PyTorch Lightning module that encapsulates optimization, stepping, and training behavior. · `tribev2/pl_module.py`

### Data

Cross-study adapters and fMRI loading/projection utilities.

- [Core utils](https://github.com/facebookresearch/tribev2/blob/main/tribev2/utils.py): shared helpers · General repository utilities used across training, loading, and study workflows. · `tribev2/utils.py`
- [fMRI utils](https://github.com/facebookresearch/tribev2/blob/main/tribev2/utils_fmri.py): surface utilities · fMRI-specific loading, surface projection, ROI handling, and cross-space utilities. · `tribev2/utils_fmri.py`
- [Algonauts2025](https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/algonauts2025.py): study adapter · Dataset adapter for the Algonauts 2025 study format. · `tribev2/studies/algonauts2025.py`
- [Lahner2024](https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/lahner2024bold.py): study adapter · Dataset adapter for the Lahner 2024 BOLD study format. · `tribev2/studies/lahner2024bold.py`
- [Lebel2023](https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/lebel2023bold.py): study adapter · Dataset adapter for the Lebel 2023 BOLD study format. · `tribev2/studies/lebel2023bold.py`
- [Wen2017](https://github.com/facebookresearch/tribev2/blob/main/tribev2/studies/wen2017.py): study adapter · Dataset adapter for the Wen 2017 study format. · `tribev2/studies/wen2017.py`

### Visualization

Optional brain-map rendering backends.

- [Plot base](https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/base.py): render base · Shared plotting foundation for brain visualization backends. · `tribev2/plotting/base.py`
- [Cortical plot](https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/cortical.py): rendering · Cortical brain visualization utilities. · `tribev2/plotting/cortical.py`
- [PV cortical](https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/cortical_pv.py): rendering · PyVista-based cortical visualization path. · `tribev2/plotting/cortical_pv.py`
- [Subcortical plot](https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/subcortical.py): rendering · Subcortical brain visualization utilities. · `tribev2/plotting/subcortical.py`
- [Plot utils](https://github.com/facebookresearch/tribev2/blob/main/tribev2/plotting/utils.py): render helpers · Shared helpers for plotting and rendering data preparation. · `tribev2/plotting/utils.py`

### Launch

Grid search and runtime orchestration.

- [Defaults](https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/defaults.py): grid config · Central configuration defaults for experiment launch and grid runs. · `tribev2/grids/defaults.py`
- [Configs](https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/configs.py): grid variants · Named experiment variants and launch configurations. · `tribev2/grids/configs.py`
- [Cortical run](https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/run_cortical.py): grid launcher · Entry point for cortical grid execution and batch runs. · `tribev2/grids/run_cortical.py`
- [Subcortical run](https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/run_subcortical.py): grid launcher · Entry point for subcortical grid execution and batch runs. · `tribev2/grids/run_subcortical.py`
- [Smoke test](https://github.com/facebookresearch/tribev2/blob/main/tribev2/grids/test_run.py): launcher test · Small local execution path for validating grid or run setup. · `tribev2/grids/test_run.py`

## Connections

- TribeModel → Event transforms: build events · Converts raw stimulus inputs into timed event data.
- TribeModel → FmriEncoder: predict · Feeds events into the pretrained encoder to generate responses.
- Event transforms → FmriEncoder: tokens · Produces structured temporal inputs consumed by the model.
- Main entry → Lightning module: train · Main orchestration delegates optimization and step logic to Lightning.
- Main entry → Core utils: setup · Uses shared utilities for experiment and data preparation.
- Lightning module → FmriEncoder: wraps · Training module encapsulates the core encoder.
- Core utils → fMRI utils: fMRI ops · General utilities feed fMRI-specific loading and projection logic.
- Core utils → Algonauts2025: load · Shared pipeline consumes study-specific dataset adapters.
- Core utils → Lahner2024: load · Shared pipeline consumes study-specific dataset adapters.
- Core utils → Lebel2023: load · Shared pipeline consumes study-specific dataset adapters.
- Core utils → Wen2017: load · Shared pipeline consumes study-specific dataset adapters.
- Defaults → Configs: parameterize · Defaults underpin named experiment variants.
- Configs → Cortical run: launch · Configurations drive cortical experiment runs.
- Configs → Subcortical run: launch · Configurations drive subcortical experiment runs.
- Defaults → Smoke test: smoke · Defaults support the local validation path.
- Plot base → Cortical plot: extend · Shared plotting primitives are specialized for cortical views.
- Plot base → Subcortical plot: extend · Shared plotting primitives are specialized for subcortical views.
- Plot base → PV cortical: extend · Shared plotting primitives are specialized for PyVista cortical views.
- Plot utils → Cortical plot: support · Helpers supply common rendering data and formatting.
- Plot utils → Subcortical plot: support · Helpers supply common rendering data and formatting.
- fMRI utils → Plot base: analyze · Projected fMRI outputs and ROI metadata feed visualization.
- FmriEncoder → fMRI utils: project · Predictions are interpreted on cortical surface representations.
- TribeModel → Plot base: visualize · Inference outputs can be handed off to plotting backends.
