# sanoscience/pr-endo architecture

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

- Interactive diagram: https://gitdiagram.com/sanoscience/pr-endo
- Repository: https://github.com/sanoscience/pr-endo

## Overview

#### Purpose and Entry Points
PR-ENDO is a GPU-based research pipeline for reconstructing endoscopic scenes as physically based, relightable 3D Gaussian splats. Training is orchestrated by `run_all.sh` and implemented in `train_viewdir.py`; rendering and evaluation are exposed through `render.py`, `render_light_rotations.py`, `render_body_movement.py`, and `metrics.py`.

#### Dataset and Scene Ingestion
Scene loading supports COLMAP-derived and EndoGSLAM-optimized inputs, converting camera calibration, poses, images, and point-cloud data into the internal scene representation. The data boundary is concentrated in:
- `scene/dataset_readers.py`
- `scene/colmap_loader.py`
- `scene/rnnslam_loader.py`
- `scene/cameras.py`

#### Gaussian Scene Representation
The trainable scene is a collection of 3D Gaussians with geometry, appearance, and relighting-related parameters. `scene/gaussian_model.py` owns this representation and its optimization-facing state. It is paired with neural components in `scene/mlp.py`, including the diffuse/view-light modeling needed to separate tissue properties from illumination.

#### Differentiable Rendering and Relighting
`gaussian_renderer/__init__.py` is the rendering integration boundary: it projects and shades the Gaussian model from a camera viewpoint and produces training/render outputs. Its CUDA-backed rasterization and spatial-neighbor operations come from installed submodules:
- `submodules/depth-diff-gaussian-rasterization`
- `submodules/simple-knn`
- `submodules/gaussian-norms`

The renderer consumes camera state, Gaussian attributes, lighting/view directions, and MLP outputs; gradients flow back to Gaussian and neural parameters during training.

#### Training Control Flow
`train_viewdir.py` drives iterative optimization: load a scene, select training cameras, render predicted frames, calculate image/reconstruction losses, and update Gaussian/MLP parameters. Training-specific operations such as scheduling, densification/pruning support, and loss-related utilities are organized under:
- `utils/train_utils.py`
- `utils/loss_utils.py`
- `utils/general_utils.py`

`arguments/__init__.py` centralizes command-line parameter grouping for the executable scripts.

#### Output Modes
Standard novel-view output is produced by `render.py`. Two specialized rendering paths expose the paper’s controllable effects:
- `render_light_rotations.py` changes light direction/spotlight settings while preserving scene properties.
- `render_body_movement.py` applies Gaussian reparameterization-based body-motion simulation.

Camera trajectories and viewpoint construction are supported by `utils/camera_utils.py` and `utils/spiral_utils.py`; illumination/view calculations are supported by `utils/gaussian_viewing_utils.py`.

#### Runtime and External Dependencies
The system targets Python 3.10, PyTorch 2.1, CUDA 12.1, and an NVIDIA GPU. It depends on custom CUDA extensions for differentiable Gaussian rasterization, KNN, and normal estimation; FAISS GPU for accelerated similarity/search operations; and Tiny-CUDA-NN for optional HashGrid support. Source datasets are externally preprocessed C3VD/EndoGSLAM and RotateColon scene directories, configured through `run_all.sh`.

Main entry points: [`gaussian_renderer/__init__.py`](https://github.com/sanoscience/pr-endo/blob/main/gaussian_renderer/__init__.py) (Gaussian renderer), [`train_viewdir.py`](https://github.com/sanoscience/pr-endo/blob/main/train_viewdir.py) (Training loop), [`scene/gaussian_model.py`](https://github.com/sanoscience/pr-endo/blob/main/scene/gaussian_model.py) (Gaussian scene model), [`scene/dataset_readers.py`](https://github.com/sanoscience/pr-endo/blob/main/scene/dataset_readers.py) (Dataset readers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entrypoints"]
  node_pipeline["Pipeline launcher<br/>shell workflow<br/>[run_all.sh]"]
  node_args["CLI parameters<br/>argument groups<br/>[__init__.py]"]
  node_training["Training loop<br/>Python trainer<br/>[train_viewdir.py]"]
end

subgraph group_scene["Scene and learning"]
  node_ingestion["Dataset readers<br/>scene ingestion<br/>[dataset_readers.py]"]
  node_colmap["COLMAP loader<br/>format loader<br/>[colmap_loader.py]"]
  node_rnnslam["EndoGSLAM loader<br/>format loader<br/>[rnnslam_loader.py]"]
  node_cameras["Camera state<br/>camera model<br/>[cameras.py]"]
  node_gaussians[("Gaussian scene model<br/>trainable 3D splats<br/>[gaussian_model.py]")]
  node_mlp["Appearance MLPs<br/>neural shading<br/>[mlp.py]"]
  node_training_utils["Optimization utilities<br/>training support<br/>[train_utils.py]"]
end

subgraph group_rendering["Differentiable rendering"]
  node_renderer["Gaussian renderer<br/>render integration<br/>[__init__.py]"]
  node_cuda_extensions{{"CUDA extensions<br/>native GPU kernels"}}
  node_view_lighting["View and lighting utilities<br/>relighting support"]
end

subgraph group_outputs["Rendering modes"]
  node_trajectories["Camera trajectory utilities<br/>viewpoint construction<br/>[camera_utils.py]"]
  node_novel_view["Novel-view renderer<br/>render executable<br/>[render.py]"]
  node_light_rotation["Light rotation renderer<br/>relighting executable"]
  node_body_motion["Body movement renderer<br/>motion executable"]
  node_metrics["Evaluation<br/>metrics executable<br/>[metrics.py]"]
end

node_datasets[("External scene datasets<br/>preprocessed inputs")]

node_pipeline -->|"launches"| node_training
node_pipeline -->|"configures"| node_datasets
node_args -->|"configures"| node_training
node_datasets -->|"source scenes"| node_ingestion
node_colmap -->|"COLMAP input"| node_ingestion
node_rnnslam -->|"EndoGSLAM input"| node_ingestion
node_ingestion -->|"builds"| node_cameras
node_ingestion -->|"initializes"| node_gaussians
node_training -->|"renders iterations"| node_renderer
node_cameras -->|"viewpoint"| node_renderer
node_gaussians -->|"splat attributes"| node_renderer
node_mlp -->|"shading outputs"| node_renderer
node_view_lighting -->|"view/light directions"| node_renderer
node_cuda_extensions -->|"GPU kernels"| node_renderer
node_renderer -->|"frames and gradients"| node_training
node_training_utils -->|"optimization support"| node_training
node_training -->|"updates"| node_gaussians
node_training -->|"updates"| node_mlp
node_gaussians -->|"trained scene"| node_novel_view
node_gaussians -->|"trained scene"| node_light_rotation
node_gaussians -->|"trained scene"| node_body_motion
node_trajectories -->|"camera paths"| node_novel_view
node_renderer -->|"render backend"| node_novel_view
node_renderer -->|"render backend"| node_light_rotation
node_renderer -->|"render backend"| node_body_motion
node_novel_view -->|"rendered outputs"| node_metrics

click node_pipeline "https://github.com/sanoscience/pr-endo/blob/main/run_all.sh"
click node_args "https://github.com/sanoscience/pr-endo/blob/main/arguments/__init__.py"
click node_training "https://github.com/sanoscience/pr-endo/blob/main/train_viewdir.py"
click node_ingestion "https://github.com/sanoscience/pr-endo/blob/main/scene/dataset_readers.py"
click node_colmap "https://github.com/sanoscience/pr-endo/blob/main/scene/colmap_loader.py"
click node_rnnslam "https://github.com/sanoscience/pr-endo/blob/main/scene/rnnslam_loader.py"
click node_cameras "https://github.com/sanoscience/pr-endo/blob/main/scene/cameras.py"
click node_gaussians "https://github.com/sanoscience/pr-endo/blob/main/scene/gaussian_model.py"
click node_mlp "https://github.com/sanoscience/pr-endo/blob/main/scene/mlp.py"
click node_training_utils "https://github.com/sanoscience/pr-endo/blob/main/utils/train_utils.py"
click node_renderer "https://github.com/sanoscience/pr-endo/blob/main/gaussian_renderer/__init__.py"
click node_cuda_extensions "https://github.com/sanoscience/pr-endo/tree/main/submodules/depth-diff-gaussian-rasterization"
click node_view_lighting "https://github.com/sanoscience/pr-endo/blob/main/utils/gaussian_viewing_utils.py"
click node_trajectories "https://github.com/sanoscience/pr-endo/blob/main/utils/camera_utils.py"
click node_novel_view "https://github.com/sanoscience/pr-endo/blob/main/render.py"
click node_light_rotation "https://github.com/sanoscience/pr-endo/blob/main/render_light_rotations.py"
click node_body_motion "https://github.com/sanoscience/pr-endo/blob/main/render_body_movement.py"
click node_metrics "https://github.com/sanoscience/pr-endo/blob/main/metrics.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_pipeline,node_args,node_training toneBlue
class node_ingestion,node_colmap,node_rnnslam,node_cameras,node_gaussians,node_mlp,node_training_utils toneAmber
class node_renderer,node_cuda_extensions,node_view_lighting toneMint
class node_trajectories,node_novel_view,node_light_rotation,node_body_motion,node_metrics toneRose
class node_datasets toneNeutral
```

## Components

### Entrypoints

- [Pipeline launcher](https://github.com/sanoscience/pr-endo/blob/main/run_all.sh): shell workflow · Configures datasets and runs reconstruction stages. · `run_all.sh`
- [CLI parameters](https://github.com/sanoscience/pr-endo/blob/main/arguments/__init__.py): argument groups · `arguments/__init__.py`
- [Training loop](https://github.com/sanoscience/pr-endo/blob/main/train_viewdir.py): Python trainer · Iteratively renders, computes losses, and updates scene parameters. · `train_viewdir.py`

### Scene and learning

- [Dataset readers](https://github.com/sanoscience/pr-endo/blob/main/scene/dataset_readers.py): scene ingestion · Converts supported source formats into the internal scene. · `scene/dataset_readers.py`
- [COLMAP loader](https://github.com/sanoscience/pr-endo/blob/main/scene/colmap_loader.py): format loader · `scene/colmap_loader.py`
- [EndoGSLAM loader](https://github.com/sanoscience/pr-endo/blob/main/scene/rnnslam_loader.py): format loader · `scene/rnnslam_loader.py`
- [Camera state](https://github.com/sanoscience/pr-endo/blob/main/scene/cameras.py): camera model · `scene/cameras.py`
- [Gaussian scene model](https://github.com/sanoscience/pr-endo/blob/main/scene/gaussian_model.py): trainable 3D splats · Owns geometry, appearance, relighting, and optimization state. · `scene/gaussian_model.py`
- [Appearance MLPs](https://github.com/sanoscience/pr-endo/blob/main/scene/mlp.py): neural shading · Models diffuse and view/light-dependent appearance. · `scene/mlp.py`
- [Optimization utilities](https://github.com/sanoscience/pr-endo/blob/main/utils/train_utils.py): training support · Scheduling, densification, pruning, losses, and general utilities. · `utils/train_utils.py`

### Differentiable rendering

- [Gaussian renderer](https://github.com/sanoscience/pr-endo/blob/main/gaussian_renderer/__init__.py): render integration · Projects and shades splats, producing differentiable frame outputs. · `gaussian_renderer/__init__.py`
- [CUDA extensions](https://github.com/sanoscience/pr-endo/tree/main/submodules/depth-diff-gaussian-rasterization): native GPU kernels · Rasterization, spatial KNN, and normal estimation. · `submodules/depth-diff-gaussian-rasterization`
- [View and lighting utilities](https://github.com/sanoscience/pr-endo/blob/main/utils/gaussian_viewing_utils.py): relighting support · `utils/gaussian_viewing_utils.py`

### Rendering modes

- [Camera trajectory utilities](https://github.com/sanoscience/pr-endo/blob/main/utils/camera_utils.py): viewpoint construction · `utils/camera_utils.py`
- [Novel-view renderer](https://github.com/sanoscience/pr-endo/blob/main/render.py): render executable · `render.py`
- [Light rotation renderer](https://github.com/sanoscience/pr-endo/blob/main/render_light_rotations.py): relighting executable · Changes illumination while preserving scene properties. · `render_light_rotations.py`
- [Body movement renderer](https://github.com/sanoscience/pr-endo/blob/main/render_body_movement.py): motion executable · Applies Gaussian reparameterization-based body-motion simulation. · `render_body_movement.py`
- [Evaluation](https://github.com/sanoscience/pr-endo/blob/main/metrics.py): metrics executable · `metrics.py`

### Other components

- **External scene datasets**: preprocessed inputs · C3VD, EndoGSLAM, and RotateColon scene directories.

## Connections

- Pipeline launcher → Training loop: launches
- Pipeline launcher → External scene datasets: configures
- CLI parameters → Training loop: configures
- External scene datasets → Dataset readers: source scenes
- COLMAP loader → Dataset readers: COLMAP input
- EndoGSLAM loader → Dataset readers: EndoGSLAM input
- Dataset readers → Camera state: builds
- Dataset readers → Gaussian scene model: initializes
- Training loop → Gaussian renderer: renders iterations
- Camera state → Gaussian renderer: viewpoint
- Gaussian scene model → Gaussian renderer: splat attributes
- Appearance MLPs → Gaussian renderer: shading outputs
- View and lighting utilities → Gaussian renderer: view/light directions
- CUDA extensions → Gaussian renderer: GPU kernels
- Gaussian renderer → Training loop: frames and gradients
- Optimization utilities → Training loop: optimization support
- Training loop → Gaussian scene model: updates
- Training loop → Appearance MLPs: updates
- Gaussian scene model → Novel-view renderer: trained scene
- Gaussian scene model → Light rotation renderer: trained scene
- Gaussian scene model → Body movement renderer: trained scene
- Camera trajectory utilities → Novel-view renderer: camera paths
- Gaussian renderer → Novel-view renderer: render backend
- Gaussian renderer → Light rotation renderer: render backend
- Gaussian renderer → Body movement renderer: render backend
- Novel-view renderer → Evaluation: rendered outputs
