# alleninstitute/txtox architecture

> Architecture diagram of the GitHub repository alleninstitute/txtox, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alleninstitute/txtox
- Repository: https://github.com/alleninstitute/txtox

## Overview

##### Project type and purpose
- This is an ML research or engineering codebase for spatial transcriptomics. Its core functionality is to train and evaluate graph-based neural networks (GNNs) and MLPs on spatial gene-expression data. It provides:
- Data ingestion and preprocessing (txtox/data)
- Model definitions (txtox/models)
- Training loops and experiment orchestration (txtox/trainers)
- Interactive analyses and prototyping via Jupyter notebooks

##### File structure
- Top-level:
- notebooks/ – dozens of exploratory and result-generation notebooks (data prep, model tests, uncertainty estimation, clustering)
- txtox/ – the Python package, subdivided into:
– data/ (datamodules.py, datasets.py)
– models/ (various GNN variants, simple models)
– trainers/ (one module per model variant + common trainers.py)
– utils.py
- pyproject.toml & config.toml – project metadata and paths
- development artifacts: .gitignore, .pre-commit-config.yaml

##### README for architectural hints
- Dependencies: PyTorch, PyTorch-Geometric (PyG), Lightning, MLflow, TensorBoard
- Config: paths defined in config.toml, loaded by DataModules

a. Main components to represent
1. Environment & Dependencies
– Conda environment with CUDA, PyTorch, PyG, Lightning, MLflow, TensorBoard
2. Configuration
– config.toml / pyproject.toml (data_root path)
3. Data Layer
– Raw data on disk (data_root)
– txtox/data/datasets.py (Dataset classes)
– txtox/data/datamodules.py (Lightning DataModule orchestrator)
4. Model Layer
– txtox/models (all GNN and simple model classes)
5. Training Orchestration
– txtox/trainers (Lightning Trainer wrappers, experiment logic, hyperparameters)
6. Experiment Interface
– notebooks/ (calls into txtox package API, visualizes results)
7. Logging & Monitoring
– MLflow (parameter/metric tracking)
– TensorBoard (visualizations)
8. Utility Layer
– txtox/utils.py (shared helpers, e.g., seed setting, metric functions)

b. Relationships & data flows
- Config → DataModule → Dataset reads raw files
- DataLoader (inside Lightning) feeds batches to Model
- Model forward/backward integrated with Lightning Trainer
- Trainer logs metrics to MLflow & TensorBoard
- Utilities called throughout for preprocessing, metrics, logging setup

c. Architectural patterns & principles
- Layered architecture: Data → Model → Training → Logging
- Separation of concerns via:
– DataModule (data orchestration) vs. Dataset (I/O)
– Model classes decoupled from training logic
– Trainer modules encapsulate experiment loop & hyperparams
- Use of PyTorch Lightning as a framework for clean training loops
- Extensibility: adding a new GNN variant just requires a new model.py + trainer.py

d. Key technologies & libraries
- PyTorch & Torch-Vision (core deep learning)
- PyTorch-Geometric (graph data structures & layers)
- Lightning (structured training loops, callbacks)
- MLflow & TensorBoard (experiment tracking & visualization)
- Jupyter notebooks (interactive prototyping, result analysis)

##### Detailed level of granularity
- Inside “txtox/models”, optionally show 2–3 representative classes (e.g., gnn.py, simple.py, gnn_het_reg.py)
- Inside “txtox/trainers”, highlight the generic Trainer base vs. model-specific trainers
- How config.toml is loaded by DataModule at initialization
- Interaction with external GPU/CUDA resources via the environment block

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Environment
    Env["Environment: Conda (CUDA, PyTorch, PyG, Lightning)"]:::env

    %% Configuration
    Pyproject["pyproject.toml"]:::config
    ConfigToml["config.toml"]:::config

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        RawData["Raw data (data_root)"]:::data
        Dataset["Datasets\n(txtox/data/datasets.py)"]:::data
        DataModule["DataModule\n(txtox/data/datamodules.py)"]:::data
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        SimpleModel["MLP\n(txtox/models/simple.py)"]:::model
        GNN["GNN variants\n(txtox/models/gnn.py)"]:::model
        Het["GNN Het Reg\n(txtox/models/gnn_het_reg*.py)"]:::model
        TXGauss["GNN TX Gauss\n(txtox/models/gnn_tx_gauss.py)"]:::model
        TXMSL["GNN TX MSL\n(txtox/models/gnn_tx_msl.py)"]:::model
        NeighborStats["NeighborStats\n(txtox/models/neighborstats.py)"]:::model
    end

    %% Training Orchestration
    subgraph "Training Orchestration"
        direction TB
        TrainerUtil["Trainer Utils\n(txtox/trainers/trainers.py)"]:::orchestration
        GNNTrainer["GNN Trainer\n(txtox/trainers/gnn*.py)"]:::orchestration
        HetTrainer["Het Reg Trainer\n(txtox/trainers/gnn_het_reg*.py)"]:::orchestration
        Neighborhood["Neighborhood Trainer\n(txtox/trainers/neighborhood_v0.py)"]:::orchestration
    end

    %% Experiment Interface
    Notebook["Notebooks\n(notebooks/*.ipynb)"]:::notebook

    %% Logging & Monitoring
    subgraph "Logging & Monitoring"
        direction TB
        MLflow["MLflow"]:::logging
        TensorBoard["TensorBoard"]:::logging
        Logs["Log Store"]:::logging
    end

    %% Utility Layer
    Utils["Utilities\n(txtox/utils.py)"]:::utils

    %% Connections
    Env --> Pyproject
    Env --> ConfigToml

    Pyproject --> ConfigToml
    ConfigToml --> DataModule

    DataModule --> Dataset
    Dataset --> RawData

    DataModule -->|"batches"| SimpleModel
    DataModule -->|"batches"| GNN
    DataModule -->|"batches"| Het
    DataModule -->|"batches"| TXGauss
    DataModule -->|"batches"| TXMSL

    SimpleModel --> GNNTrainer
    GNN --> GNNTrainer
    TXGauss --> GNNTrainer
    TXMSL --> GNNTrainer
    Het --> HetTrainer

    TrainerUtil --> GNNTrainer
    TrainerUtil --> HetTrainer
    TrainerUtil --> Neighborhood

    GNNTrainer --> MLflow
    GNNTrainer --> TensorBoard
    HetTrainer --> MLflow
    HetTrainer --> TensorBoard
    Neighborhood --> MLflow
    Neighborhood --> TensorBoard

    MLflow --> Logs
    TensorBoard --> Logs

    Notebook --> DataModule
    Notebook --> GNN
    Notebook --> SimpleModel
    Notebook --> GNNTrainer
    Notebook --> MLflow
    Notebook --> TensorBoard

    Utils --> DataModule
    Utils --> SimpleModel
    Utils --> GNN
    Utils --> TrainerUtil

    %% Click Events
    click Pyproject "https://github.com/alleninstitute/txtox/blob/main//pyproject.toml"
    click ConfigToml "https://github.com/alleninstitute/txtox/blob/main//config.toml"
    click Dataset "https://github.com/alleninstitute/txtox/blob/main/txtox/data/datasets.py"
    click DataModule "https://github.com/alleninstitute/txtox/blob/main/txtox/data/datamodules.py"
    click SimpleModel "https://github.com/alleninstitute/txtox/blob/main/txtox/models/simple.py"
    click GNN "https://github.com/alleninstitute/txtox/blob/main/txtox/models/gnn.py"
    click Het "https://github.com/alleninstitute/txtox/blob/main/txtox/models/gnn_het_reg*.py"
    click TXGauss "https://github.com/alleninstitute/txtox/blob/main/txtox/models/gnn_tx_gauss.py"
    click TXMSL "https://github.com/alleninstitute/txtox/blob/main/txtox/models/gnn_tx_msl.py"
    click NeighborStats "https://github.com/alleninstitute/txtox/blob/main/txtox/models/neighborstats.py"
    click TrainerUtil "https://github.com/alleninstitute/txtox/blob/main/txtox/trainers/trainers.py"
    click GNNTrainer "https://github.com/alleninstitute/txtox/blob/main/txtox/trainers/gnn*.py"
    click HetTrainer "https://github.com/alleninstitute/txtox/blob/main/txtox/trainers/gnn_het_reg*.py"
    click Neighborhood "https://github.com/alleninstitute/txtox/blob/main/txtox/trainers/neighborhood_v0.py"
    click Notebook "https://github.com/alleninstitute/txtox/blob/main/notebooks/07_gnn_predictions.ipynb"
    click Utils "https://github.com/alleninstitute/txtox/blob/main/txtox/utils.py"

    %% Styles
    classDef env fill:#C8F7C5,stroke:#333,stroke-width:1px
    classDef config fill:#F2F2F2,stroke:#333,stroke-width:1px
    classDef data fill:#D0E8FF,stroke:#333,stroke-width:1px
    classDef model fill:#D8FFE0,stroke:#333,stroke-width:1px
    classDef orchestration fill:#FFE0B3,stroke:#333,stroke-width:1px
    classDef logging fill:#E5D5FF,stroke:#333,stroke-width:1px
    classDef notebook fill:#F7F1C5,stroke:#333,stroke-width:1px
    classDef utils fill:#CCCCCC,stroke:#333,stroke-width:1px
```
