# lahammond/respan architecture

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

- Interactive diagram: https://gitdiagram.com/lahammond/respan
- Repository: https://github.com/lahammond/respan

## Overview

##### Main Components
- User Interface & Entry Points:
– The GUI: Notably in RESPAN/Scripts/RESPAN_GUI.py, which provides the user-friendly interface.
– Batch Processing and CLI options: Scripts like RESPAN_Batch.py and SelfNet_Inference.py/ SelfNet_Model_Training.py indicate alternative modes of operation.
- Main Controller and Orchestration:
– Located in RESPAN/Main, especially Main.py (with supporting modules such as Timer.py) that likely coordinates the processing pipeline.
- Image Analysis & Processing Modules:
– RESPAN/ImageAnalysis includes files such as ImageAnalysis.py, ModelTraining.py, SpineTracking.py, and Validation.py. These files collectively manage image restoration, segmentation, model training, spine tracking across 3D images, and result validation.
- Parameter and Configuration Management:
– The Elastix_params folder holds parameter files (bspline_params.txt, rigid_params.txt) configuring image registration/processing.
– The Templates folder contains a YAML file (“Analysis_Settings.yaml”) that likely holds additional configurable settings.
- Supporting Scripts & Utilities:
– Other scripts in RESPAN/Scripts (e.g., Dice Score Calculator.py) offer utility functions for metrics computation and additional processing modes.

##### Relationships and Interactions
- The user interacts with the application via the GUI or batch scripts; these trigger the Main Controller (RESPAN/Main/Main.py).
- The Main Controller then orchestrates calls into the Image Analysis components for tasks such as restoration, segmentation, tracking, and validation.
- Image Analysis might call model training or inference routines and interchange data with configured settings and parameters (Elastix_params and analysis templates).
- Utility scripts (calculation of Dice scores, etc.) are triggered at appropriate stages for metric evaluation.
- Underlying deep learning components (as referenced in the README) interact with frameworks like Tensorflow/PyTorch (even if not visible in the file tree, they are inferred as dependencies) to perform GPU-accelerated processing.

##### Architectural Patterns
- Modular Design: The project cleanly separates functionalities into directories – distinct modules for main orchestration, specialized image analysis tasks, parameter/configuration management, and scripting utilities.
- Separation of Concerns: User interface, control logic, computational processing, and configuration are all segregated, supporting easier maintenance and extension.
- Extensibility Points: The inclusion of different training modules for CSBDeep, Self-Net, and nnU-Net suggests plug-in or modular deep learning pipelines.

##### Relevant Technologies and Frameworks
- Python serves as the primary language.
- Deep learning frameworks: Tensorflow and PyTorch (as mentioned in the README) used implicitly by the different training/inference modules.
- Image processing/registration: Elastix parameters point to usage of Elastix or similar image registration libraries.
- YAML for configuration and possible use of standard Python libraries for timer and GUI functionality.

##### What
- Components with clear labels:
– UI Layer (Graphical Interface, Batch Processing scripts)
– Main Controller (Orchestration – Main.py, Timer)
– Image Analysis Module (segmentation, spine tracking, validation)
– Model Training and Inference Module (SelfNet and possibly others)
– Configuration/Parameter Management (Elastix parameters, YAML templates)
– External dependencies (GPU framework, pre-trained model repositories)

##### Final Advice
- Be as detailed as possible in representing each module, making sure to capture the modular design that separates image restoration, segmentation, and quantification tasks.
- That elements like configuration files and parameter management are prominently featured to illustrate how they integrate and configure the pipeline.
- Don’t miss out on portraying the integration of both training and inference pipelines, as well as the support for GPU-accelerated processing which is critical to the design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface & Entry Points
    subgraph "User Interface & Entry Points"
        A1["GUI (RESPAN_GUI.py)"]:::ui
        A2["Batch Processing (RESPAN_Batch.py)"]:::ui
        A3["SelfNet Inference (SelfNet_Inference.py)"]:::ui
        A4["SelfNet Model Training (SelfNet_Model_Training.py)"]:::ui
    end

    %% Main Controller & Orchestration
    subgraph "Main Controller & Orchestration"
        B1["Main Controller (Main.py)"]:::controller
        B2["Timer Module (Timer.py)"]:::controller
    end

    %% Image Analysis & Processing Modules
    subgraph "Image Analysis & Processing Modules"
        C1["Image Analysis (ImageAnalysis.py)"]:::processing
        C2["Model Training (ModelTraining.py)"]:::processing
        C3["Spine Tracking (SpineTracking.py)"]:::processing
        C4["Validation (Validation.py)"]:::processing
    end

    %% Parameter & Configuration Management
    subgraph "Parameter & Configuration Management"
        D1["Elastix Params - bspline (bspline_params.txt)"]:::config
        D2["Elastix Params - rigid (rigid_params.txt)"]:::config
        D3["Analysis Settings (Analysis_Settings.yaml)"]:::config
    end

    %% Supporting Scripts & Utilities
    subgraph "Supporting Scripts & Utilities"
        E1["Dice Score Calculator (Dice Score Calculator.py)"]:::utility
    end

    %% External Dependencies
    subgraph "External Dependencies"
        F1["GPU Framework (TensorFlow/PyTorch)"]:::external
    end

    %% Connections: User Interface triggers Main Controller
    A1 -->|"triggers"| B1
    A2 -->|"triggers"| B1
    A3 -->|"triggers"| B1
    A4 -->|"triggers"| B1

    %% Main Controller orchestrates Image Analysis modules
    B1 -->|"orchestrates"| C1
    B1 -->|"orchestrates"| C2
    B1 -->|"orchestrates"| C3
    B1 -->|"orchestrates"| C4

    %% Image Analysis uses Parameter & Configuration settings
    C1 -->|"uses"| D1
    C1 -->|"uses"| D2
    C1 -->|"uses"| D3

    %% Processing modules leverage GPU Framework
    C2 -->|"acceleratedBy"| F1
    C3 -->|"acceleratedBy"| F1

    %% Validation module triggers Utility for metrics
    C4 -->|"evaluatedBy"| E1

    %% Styles
    classDef ui fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef controller fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef processing fill:#ffa07a,stroke:#000,stroke-width:2px;
    classDef config fill:#ffff99,stroke:#000,stroke-width:2px;
    classDef utility fill:#dda0dd,stroke:#000,stroke-width:2px;
    classDef external fill:#ffcccb,stroke:#000,stroke-width:2px;

    %% Click Events
    click A1 "https://github.com/lahammond/respan/blob/main/RESPAN/Scripts/RESPAN_GUI.py"
    click A2 "https://github.com/lahammond/respan/blob/main/RESPAN/Scripts/RESPAN_Batch.py"
    click A3 "https://github.com/lahammond/respan/blob/main/RESPAN/Scripts/SelfNet_Inference.py"
    click A4 "https://github.com/lahammond/respan/blob/main/RESPAN/Scripts/SelfNet_Model_Training.py"
    click B1 "https://github.com/lahammond/respan/blob/main/RESPAN/Main/Main.py"
    click B2 "https://github.com/lahammond/respan/blob/main/RESPAN/Main/Timer.py"
    click C1 "https://github.com/lahammond/respan/blob/main/RESPAN/ImageAnalysis/ImageAnalysis.py"
    click C2 "https://github.com/lahammond/respan/blob/main/RESPAN/ImageAnalysis/ModelTraining.py"
    click C3 "https://github.com/lahammond/respan/blob/main/RESPAN/ImageAnalysis/SpineTracking.py"
    click C4 "https://github.com/lahammond/respan/blob/main/RESPAN/ImageAnalysis/Validation.py"
    click D1 "https://github.com/lahammond/respan/blob/main/RESPAN/Elastix_params/bspline_params.txt"
    click D2 "https://github.com/lahammond/respan/blob/main/RESPAN/Elastix_params/rigid_params.txt"
    click D3 "https://github.com/lahammond/respan/blob/main/RESPAN/Templates/Analysis_Settings.yaml"
    click E1 "https://github.com/lahammond/respan/blob/main/RESPAN/Scripts/Dice Score Calculator.py"
```
