# bu-cisl/sbrnet architecture

> Architecture diagram of the GitHub repository bu-cisl/sbrnet, 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/bu-cisl/sbrnet
- Repository: https://github.com/bu-cisl/sbrnet

## Overview

##### Project Type and Purpose
- This is an open‐source Python package (sbrnet_core) for simulator‐trained deep learning in fluorescence imaging.
- It provides two main capabilities: synthetic data generation (to simulate scattering microscopy measurements) and training a neural network model (SBR‐Net) to reconstruct 3D volumes from 2D measurements.

##### Key Architectural Components
a. CLI/Script Layer
- Entry‐point scripts in `scripts/` (e.g., `run_gen_syn_data.sh`, `run_train.sh`) that wrap calls to:
– `sbrnet_core/synthetic_data/main.py` (generates synthetic datasets)
– `sbrnet_core/sbrnet/main.py` (trains the SBR‐Net model)
- HPC job submission wrappers (`*.qsub`) that schedule these scripts.
b. Core Library (`sbrnet_core/`)
- config_loader.py: loads YAML configuration for both data and training.
- Utils: common constants and utility functions (`utils/`).
- Synthetic Data Module (`synthetic_data/`):
– Data generators (`generate_synthetic_data_v1.py`, deprecated version)
– Composite GT creation (`make_composite_gt.py`)
– CLI entrypoint (`main.py`).
- SBR‐Net Module (`sbrnet/`):
– Dataset abstraction (`dataset.py`) that reads synthetic measurements and ground truth.
– Noise model (`noisemodel.py`) to simulate sensor noise.
– Model definition (`model.py`) for the neural network architecture.
– Loss functions (`losses/pinball.py`).
– Trainer (`trainer.py`) orchestrating epochs, optimization, checkpointing.
– CLI entrypoint (`main.py`).
c. External Dependencies & Resources
- Python runtime, PyTorch (implied by model/trainer), NumPy, YAML, etc., as listed in `requirements.txt`.
- Data storage (e.g., Google Drive for raw datasets).
- Cluster scheduler (via qsub scripts) for large‐scale jobs.

##### Data Flows and Interactions
- User edits a YAML config → config_loader loads settings →
a) Synthetic data pipeline uses noise model → generates and writes synthetic 2D/3D datasets.
b) Training pipeline ingests these datasets via the Dataset class → applies model architecture → trainer uses losses to optimize weights → saves model checkpoints and logs.
- Scripts wrap these pipelines and optionally submit them to a cluster scheduler.

##### Architectural Patterns & Design Principles
- Modular package structure separating concerns:
- Configuration (config_loader)
- Data generation
- Model training
- Utilities
- Clear CLI boundary for batch jobs vs. library functions.
- Extensibility points: new noise models, loss functions, or network architectures can be added by dropping files into the respective subdirectories.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CLI / Scripts Layer
    subgraph "CLI / Scripts Layer"
        direction TB
        RunGenSyn["run_gen_syn_data.sh"]:::frontend
        RunTrain["run_train.sh"]:::frontend
        RunArrayTrain["run_array_train.sh"]:::frontend
        GenSynQsub["gen_syn_data.qsub"]:::frontend
        TrainQsub["train.qsub"]:::frontend
        ArrayTrainQsub["array_train.qsub"]:::frontend
    end

    %% Core Library
    subgraph "sbrnet_core" 
        direction TB
        ConfigLoader["config_loader.py"]:::lib
        subgraph "Utils Module"
            direction TB
            UtilsDir["utils/"]:::lib
            Constants["constants.py"]:::lib
            SbrnetUtils["sbrnet_utils.py"]:::lib
        end
        subgraph "Synthetic Data Module"
            direction TB
            SynDataDir["synthetic_data/"]:::lib
            GenV1["generate_synthetic_data_v1.py"]:::lib
            GenDep["generate_synthetic_data_deprecated.py"]:::lib
            CompositeGT["make_composite_gt.py"]:::lib
            SynMain["main.py"]:::lib
        end
        subgraph "SBR-Net Module"
            direction TB
            SbrNetDir["sbrnet/"]:::lib
            Dataset["dataset.py"]:::lib
            NoiseModel["noisemodel.py"]:::lib
            ModelDef["model.py"]:::lib
            Trainer["trainer.py"]:::lib
            LossPinball["pinball.py"]:::lib
            SbrMain["main.py"]:::lib
        end
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        Cluster["Cluster Scheduler"]:::external
        DataStore["External Data Store (Google Drive)"]:::external
        Requirements["requirements.txt"]:::external
        Setup["setup.py"]:::external
    end

    %% Outputs
    subgraph "Outputs"
        direction TB
        SynDatasets["Synthetic Datasets"]:::output
        ModelChkpts["Model Checkpoints & Logs"]:::output
    end

    %% Connections - Control Flow
    RunGenSyn -->|"invokes"| SynMain
    RunTrain -->|"invokes"| SbrMain
    RunArrayTrain -->|"invokes"| SbrMain
    GenSynQsub -.->|"submits HPC job"| Cluster
    TrainQsub -.->|"submits HPC job"| Cluster
    ArrayTrainQsub -.->|"submits HPC job"| Cluster

    %% Connections - Data Flow
    SynMain -->|"loads YAML config"| ConfigLoader
    SbrMain -->|"loads YAML config"| ConfigLoader

    ConfigLoader -->|"provides config"| SynDataDir
    ConfigLoader -->|"provides config"| SbrNetDir

    SynDataDir -->|"uses noise model"| NoiseModel
    SynDataDir -->|"generates datasets"| SynDatasets

    SynDatasets -->|"reads data"| Dataset
    Dataset -->|"feeds"| ModelDef
    ModelDef -->|"optimized by"| Trainer
    Trainer -->|"uses loss"| LossPinball
    Trainer -->|"outputs checkpoints"| ModelChkpts

    DataStore -->|"provides raw data"| SynDataDir

    %% Click Events
    click RunGenSyn "https://github.com/bu-cisl/sbrnet/blob/master-refactor/scripts/run_gen_syn_data.sh"
    click RunTrain "https://github.com/bu-cisl/sbrnet/blob/master-refactor/scripts/run_train.sh"
    click RunArrayTrain "https://github.com/bu-cisl/sbrnet/blob/master-refactor/scripts/run_array_train.sh"
    click GenSynQsub "https://github.com/bu-cisl/sbrnet/blob/master-refactor/scripts/gen_syn_data.qsub"
    click TrainQsub "https://github.com/bu-cisl/sbrnet/blob/master-refactor/scripts/train.qsub"
    click ArrayTrainQsub "https://github.com/bu-cisl/sbrnet/blob/master-refactor/scripts/array_train.qsub"

    click ConfigLoader "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/config_loader.py"
    click UtilsDir "https://github.com/bu-cisl/sbrnet/tree/master-refactor/sbrnet_core/utils/"
    click Constants "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/utils/constants.py"
    click SbrnetUtils "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/utils/sbrnet_utils.py"

    click SynDataDir "https://github.com/bu-cisl/sbrnet/tree/master-refactor/sbrnet_core/synthetic_data/"
    click GenV1 "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/synthetic_data/generate_synthetic_data_v1.py"
    click GenDep "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/synthetic_data/generate_synthetic_data_deprecated.py"
    click CompositeGT "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/synthetic_data/make_composite_gt.py"
    click SynMain "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/synthetic_data/main.py"

    click SbrNetDir "https://github.com/bu-cisl/sbrnet/tree/master-refactor/sbrnet_core/sbrnet/"
    click Dataset "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/sbrnet/dataset.py"
    click NoiseModel "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/sbrnet/noisemodel.py"
    click ModelDef "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/sbrnet/model.py"
    click Trainer "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/sbrnet/trainer.py"
    click LossPinball "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/sbrnet/losses/pinball.py"
    click SbrMain "https://github.com/bu-cisl/sbrnet/blob/master-refactor/sbrnet_core/sbrnet/main.py"

    click Requirements "https://github.com/bu-cisl/sbrnet/blob/master-refactor/requirements.txt"
    click Setup "https://github.com/bu-cisl/sbrnet/blob/master-refactor/setup.py"

    %% Styles
    classDef lib fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef frontend fill:#90EE90,stroke:#333,stroke-width:1px
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px
    classDef output fill:#EEEEEE,stroke:#333,stroke-width:1px
```
