# juglab/n2v architecture

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

- Interactive diagram: https://gitdiagram.com/juglab/n2v
- Repository: https://github.com/juglab/n2v

## Overview

##### Main Components Identification
- Library Core (“n2v” folder): This encapsulates the principal modules:
– Internals (data generators and wrappers such as N2V_DataGenerator.py and N2V_DataWrapper.py) that handle dataset preparation and on‐the‐fly data manipulation.
– Models (in “n2v/models”) where network configuration and standard network definitions (n2v_standard.py) reside.
– Nets (in “n2v/nets”) which include the U‑Net architecture, specialized blocks, and modifications (e.g. blurpool, unet_blocks) used to build the denoising network.
– Utils (in “n2v/utils”) that provide evaluation, export, and TensorFlow-specific helper functions.
- Scripts (“scripts” folder): Contain standalone CLI utilities (e.g., trainN2V.py, predictN2V.py) that serve as entry points into the training and prediction pipelines.
- Examples (“examples” folder): Provide Jupyter notebooks that demonstrate various scenarios (2D/3D, different modalities such as RGB, SEM, and even a structured noise2void variant); these serve both as documentation and as practical demos for end‑users.
- Tests (“tests” folder): Functional and unit tests ensuring that individual modules (e.g., data generators, utilities) and overall pipelines (training and prediction) operate as intended.
- CI/CD and Configuration: The workflows set up via GitHub Actions (in .github/workflows) along with configuration files (pyproject.toml, setup.py, tox.ini) which ensure integration, packaging, and deployment.

##### Component Relationships and Data Flow
- Input image data is read, and then passed to data generator/wrapper components contained within the “n2v/internals” modules.
- The training pipeline then configures the network parameters using “n2v/models” (and potentially network modifications in “n2v/nets”) where the U‑Net (with optional modifications like BlurPool for N2V2) is set up.
- Utilities in “n2v/utils” facilitate evaluation, export of models, logging, and TensorFlow-related helper functions.
- Prediction involves a similar flow, starting from data ingestion, passing through the network, and ending as output predictions.
- Testing components interact with the core modules to validate both training and prediction flows.

##### Architectural Patterns and Design Principles
- Modularity: The clear separation into "internals", "models", "nets", and "utils" encourages reusability and easy testing.
- Pipeline/Layer Abstraction: Training and prediction pipelines are neatly delineated, highlighting data input, processing, network inference, and output handling.
- Separation between core logic (the “n2v” package) and usage examples (Jupyter notebooks) as well as CLI scripts, making the system both a library and a ready-to-use application.
- Test-Driven Design: The existence of both unit and functional tests reinforces the modular design and provides a safety net for future development.

##### Technologies and Frameworks
- TensorFlow (versions 2.7, 2.10, and 2.13 supported; carefully managed with version-specific installation instructions).
- Python (Python 3.9 is standard throughout).
- CI/CD with GitHub Actions (found in .github/workflows).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interface Subgraph
    subgraph "User Interface"
        UI1a["Train CLI"]
        UI1b["Predict CLI"]
        UI2["Example Notebooks"]
    end

    %% Library Core - Data Management (Internals)
    subgraph "Library Core - Data Management (Internals)"
        IN1["N2V_DataGenerator"]
        IN2["N2V_DataWrapper"]
        IN3["n2v_losses"]
    end

    %% Library Core - Model & Network Configuration (Models & Nets)
    subgraph "Library Core - Model & Network Configuration (Models & Nets)"
        MO1["n2v_standard"]
        MO2["n2v_config"]
        NE1["U-Net Architecture"]
        NE2["unet_blocks"]
        NE3["blurpool"]
    end

    %% Library Core - Utilities & Evaluation (Utils)
    subgraph "Library Core - Utilities & Evaluation (Utils)"
        UT1["evaluation_utils"]
        UT2["export_utils"]
        UT3["n2v_utils"]
        UT4["tf helper"]
    end

    %% Testing & CI Subgraph
    subgraph "Testing & CI (Tests & Config)"
        T1["Tests Suite"]
        T2["CI/CD & Config"]
    end

    %% Main Data Flow Connections
    UI1a -->|"CLI_Input"| IN1
    UI1b -->|"CLI_Input"| IN1
    UI2 -->|"RawImageData"| IN1
    IN3 -->|"ProcessedBatches"| MO1
    NE3 -->|"TrainedModel/Predictions"| UT1

    %% Testing Connections (side arrows)
    T1 ---|"Testing"| IN1
    T1 ---|"Testing"| MO1
    T1 ---|"Testing"| UT1
    T2 ---|"ConfigCI"| IN1
    T2 ---|"ConfigCI"| MO1
    T2 ---|"ConfigCI"| UT1

    %% Click Events for User Interface
    click UI1a "https://github.com/juglab/n2v/blob/main/scripts/trainN2V.py"
    click UI1b "https://github.com/juglab/n2v/blob/main/scripts/predictN2V.py"
    click UI2 "https://github.com/juglab/n2v/tree/main/examples/"

    %% Click Events for Data Management (Internals)
    click IN1 "https://github.com/juglab/n2v/blob/main/n2v/internals/N2V_DataGenerator.py"
    click IN2 "https://github.com/juglab/n2v/blob/main/n2v/internals/N2V_DataWrapper.py"
    click IN3 "https://github.com/juglab/n2v/blob/main/n2v/internals/n2v_losses.py"

    %% Click Events for Model & Network Configuration (Models & Nets)
    click MO1 "https://github.com/juglab/n2v/blob/main/n2v/models/n2v_standard.py"
    click MO2 "https://github.com/juglab/n2v/blob/main/n2v/models/n2v_config.py"
    click NE1 "https://github.com/juglab/n2v/blob/main/n2v/nets/unet.py"
    click NE2 "https://github.com/juglab/n2v/blob/main/n2v/nets/unet_blocks.py"
    click NE3 "https://github.com/juglab/n2v/blob/main/n2v/nets/blurpool.py"

    %% Click Events for Utilities & Evaluation (Utils)
    click UT1 "https://github.com/juglab/n2v/blob/main/n2v/utils/evaluation_utils.py"
    click UT2 "https://github.com/juglab/n2v/blob/main/n2v/utils/export_utils.py"
    click UT3 "https://github.com/juglab/n2v/blob/main/n2v/utils/n2v_utils.py"
    click UT4 "https://github.com/juglab/n2v/blob/main/n2v/utils/tf.py"

    %% Click Events for Testing & CI
    click T1 "https://github.com/juglab/n2v/tree/main/tests/"
    click T2 "https://github.com/juglab/n2v/blob/main/.github/workflows/test_and_deploy.yml"

    %% Class Definitions for Colors
    classDef ui stroke:#1f77b4,fill:#aec7e8,stroke-width:2px;
    classDef internals stroke:#2ca02c,fill:#98df8a,stroke-width:2px;
    classDef model stroke:#ff7f0e,fill:#ffbb78,stroke-width:2px;
    classDef utils stroke:#9467bd,fill:#c5b0d5,stroke-width:2px;
    classDef test stroke:#d62728,fill:#ff9896,stroke-width:2px;

    %% Assign Classes
    class UI1a,UI1b,UI2 ui;
    class IN1,IN2,IN3 internals;
    class MO1,MO2,NE1,NE2,NE3 model;
    class UT1,UT2,UT3,UT4 utils;
    class T1,T2 test;
```
