# pcabrales/prototwin-pet architecture

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

- Interactive diagram: https://gitdiagram.com/pcabrales/prototwin-pet
- Repository: https://github.com/pcabrales/prototwin-pet

## Overview

##### Project Type and Purpose
- This is a scientific, patient-specific deep learning and GPU-accelerated workflow. Its main goal is to simulate dose deviations in proton therapy and verify them using PET simulations, followed by deep learning–based prediction of dose maps and patient setup errors.
- The README clearly indicates the integration of Monte Carlo (FRED), PET simulation (MCGPU-PET), treatment planning (MatRad), and the use of a 3D deep learning model (nnFormer with a Deviation Predicting Branch).

##### File Structure Analysis – Key Components
- Top-level directories such as “generate-dataset”, “config-main”, “data”, “models”, “images”, and “unit-tests” indicate the separation of functionality.
- The “generate-dataset” folder contains scripts and configurations for dataset generation. It leverages submodules like “pet-simulation-reconstruction” (which holds necessary input files, material definitions, and interfaces to MCGPU-PET).
- The “models” folder, along with associated training and testing scripts (main.py, train_model.py, test_model.py), shows the deep learning part of the pipeline.
- Additional configuration files (in “config-main” and “generate-dataset/config-generate-dataset”) are used to set patient-specific and process-specific parameters.
- The presence of utilities (e.g., utils.py, utils_parallelproj.py) supports various parts of the workflow.

##### Insights from the README
- The README details the patient data preparation, the simulation steps (FRED for dose simulation, PET simulation and reconstruction using MCGPU-PET) and then how the dataset is used for deep learning training.
- It also emphasizes the integration with external tools (MatRad, MCGPU-PET executable, FRED) and provides instructions for environment setup, indicating that the proper hardware (GPU, CUDA) and dependencies are vital.
- There are clear step-by-step instructions: selecting or generating a patient plan, dataset generation, and model training/testing.

##### Main Components & Their Relationships
a. Data Ingestion & Preprocessing:
- Patient’s CT/DICOM data and treatment plan data (either provided in “data” or generated via MatRad).
- Config files (in “config-main” and “generate-dataset/config-generate-dataset”) defining simulation and deep-learning parameters.

b. Simulation and Dataset Generation Pipeline:
- FRED Monte Carlo simulation: Provides dose maps.
- MCGPU-PET simulation & reconstruction: Converts simulation outputs to PET images.
- Additional utilities and parallel projection tools (seen in the generate-dataset folder) which organize and generate the final PET–dose pairs dataset.
- Input and timing logs for each simulation step (e.g., “timings” folder) to monitor performance.

c. Deep Learning Training/Inference Pipeline:
- Main processing scripts: main.py, train_model.py, and test_model.py.
- The “models” directory hosts the deep learning model definition (nnFormer and its DPB extension), training losses, test results, and trained model outputs.
- Utility modules (e.g., utils.py) help with dataset management, plotting, and post-processing.

d. External Dependencies & Environment:
- CUDA toolkit and GPU dependencies.
- Third-party simulation software: FRED, MCGPU-PET, and even parts of MatRad for treatment planning.
- Dependencies are managed by the environment file (environment.yml).

##### Additional Architectural Patterns
- Pipeline nature of the workflow (pre-processing → simulation → dataset generation → deep learning).
- Principles of modularity (separate folders for config, simulation, models, tests) and reusability (utility modules, external tool integrations).
- Parallelism or timing components if relevant (for instance, the “timings” folder indicates where performance metrics are collected).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Preparation & Input
    subgraph "Data Preparation & Input"
        dp["Patient Data / CT & RTSTRUCTs"]
        conf1["Main Config Files"]
        conf2["Generate-Dataset Config Files"]
    end

    %% Simulation and Dataset Generation Pipeline
    subgraph "Simulation & Dataset Generation Pipeline"
        s1["FRED Simulation"]
        s2["MCGPU-PET Simulation & Reconstruction"]
        s3["Dataset Generation Script"]
        s4["Utility Module: utils.py"]
        s5["Utility Module: utils_parallelproj.py"]
        s6["Timing Logs"]
    end

    %% Deep Learning Training/Inference Pipeline
    subgraph "Deep Learning Pipeline"
        dl1["Core Script: main.py"]
        dl2["Core Script: train_model.py"]
        dl3["Core Script: test_model.py"]
        dl4["Deep Learning Model (nnFormer+DPB)"]
        dl5["Losses"]
        dl6["Test Results"]
        dl7["Trained Models"]
        dl8["Training Times"]
    end

    %% External Tools & Environment
    subgraph "External Tools & Environment"
        ext1["Environment Dependencies"]
        ext2["External Tool: FRED Simulation"]
        ext3["MCGPU-PET Executable"]
        ext4["External Tool: MatRad"]
    end

    %% Connections from Data Preparation to Simulation Pipeline
    dp --> s1
    dp --> s2
    conf1 --> s1
    conf1 --> s2
    conf2 --> s1
    conf2 --> s2

    %% Connections within Simulation Pipeline
    s1 --> s3
    s2 --> s3
    s4 --- s3
    s5 --- s3
    s6 --- s3

    %% Connections from Simulation Pipeline to Deep Learning Pipeline
    s3 --> dl1
    s3 --> dl2
    s3 --> dl3
    s3 --> dl4

    %% Connections within Deep Learning Pipeline (outputs)
    dl2 --> dl5
    dl3 --> dl6
    dl1 --> dl7
    dl4 --> dl8

    %% External Tools influence both Simulation and Deep Learning Pipelines
    ext1 --- s3
    ext1 --- dl1
    ext2 --- s1
    ext3 --- s2
    ext4 --- dp

    %% Class definitions for color styling
    class dp,conf1,conf2 dataIngestion
    class s1,s2,s3,s4,s5,s6 simulation
    class dl1,dl2,dl3,dl4,dl5,dl6,dl7,dl8 deepLearning
    class ext1,ext2,ext3,ext4 external

    %% Click Events for Data Preparation & Input
    click dp "https://github.com/pcabrales/prototwin-pet/tree/main/data"
    click conf1 "https://github.com/pcabrales/prototwin-pet/tree/main/config-main"
    click conf2 "https://github.com/pcabrales/prototwin-pet/tree/main/generate-dataset/config-generate-dataset"

    %% Click Events for Simulation and Dataset Generation Pipeline
    click s3 "https://github.com/pcabrales/prototwin-pet/blob/main/generate-dataset/generate_dataset.py"
    click s2 "https://github.com/pcabrales/prototwin-pet/tree/main/generate-dataset/pet-simulation-reconstruction"
    click s4 "https://github.com/pcabrales/prototwin-pet/blob/main/generate-dataset/utils.py"
    click s5 "https://github.com/pcabrales/prototwin-pet/blob/main/generate-dataset/utils_parallelproj.py"
    click s6 "https://github.com/pcabrales/prototwin-pet/tree/main/generate-dataset/timings"

    %% Click Events for Deep Learning Pipeline
    click dl1 "https://github.com/pcabrales/prototwin-pet/blob/main/main.py"
    click dl2 "https://github.com/pcabrales/prototwin-pet/blob/main/train_model.py"
    click dl3 "https://github.com/pcabrales/prototwin-pet/blob/main/test_model.py"
    click dl4 "https://github.com/pcabrales/prototwin-pet/tree/main/models/nnFormer"
    click dl5 "https://github.com/pcabrales/prototwin-pet/tree/main/models/losses"
    click dl6 "https://github.com/pcabrales/prototwin-pet/tree/main/models/test-results"
    click dl7 "https://github.com/pcabrales/prototwin-pet/tree/main/models/trained-models"
    click dl8 "https://github.com/pcabrales/prototwin-pet/tree/main/models/training-times"

    %% Click Events for External Tools & Environment
    click ext1 "https://github.com/pcabrales/prototwin-pet/blob/main/environment.yml"
    click ext2 "https://github.com/pcabrales/prototwin-pet/blob/main/generate-dataset/original-fred.inp"
    click ext3 "https://github.com/pcabrales/prototwin-pet/tree/main/generate-dataset/pet-simulation-reconstruction/mcgpu-pet"

%% Style definitions
classDef dataIngestion fill:#ADD8E6,stroke:#000,stroke-width:1px;
classDef simulation fill:#90EE90,stroke:#000,stroke-width:1px;
classDef deepLearning fill:#FFDAB9,stroke:#000,stroke-width:1px;
classDef external fill:#FFB6C1,stroke:#000,stroke-width:1px;
```
