# netmanaiops/donut architecture

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

## Overview

##### Project Type and Purpose
- Donut is a Python library for anomaly detection in seasonal KPIs using a variational autoencoder (VAE) based approach.
- It provides modules for data preprocessing, model construction, training, prediction, and missing data reconstruction.
- The README shows example API usage, citation details, and guidance on installation and integration with TensorFlow, TFSnippet, and ZhuSuan.

##### File Structure
- Top-level files like .travis.yml, .gitignore, requirements*, and setup.py suggest standard package management, CI/CD integration, and testing.
- The “donut” directory holds the core modules:
– __init__.py: Package initialization.
– preprocessing.py & augmentation.py: Likely handle raw data transformations, timestamp completion, and augmentation.
– model.py: Defines the core VAE model structure.
– training.py: Contains logic to train the model (wrapped by DonutTrainer).
– prediction.py: Implements inference using DonutPredictor.
– reconstruction.py: Manages enhanced reconstruction (e.g., MCMC-based missing data imputation).
– utils.py: Utility functions used across the package.
- The “tests” directory mirrors the core modules to validate functionality.
- “sample_data” provides example CSV datasets to illustrate file format and usage.

##### Key Architecture Components/Blocks
a. Data Preparation Layer:
– Components: Preprocessing (donut/preprocessing.py) and Augmentation (donut/augmentation.py)
– Role: Completing timestamps, standardizing KPIs, and handling missing data
b. Model Core:
– Component: Model definition (donut/model.py)
– Role: Building the VAE architecture using TensorFlow and Keras layers (as seen in the API usage in the README)
c. Training Module:
– Component: Training logic in donut/training.py wrapped by a DonutTrainer class
– Role: Handling model training workflows, loss computations, and optimization
d. Prediction / Inference Module:
– Component: Prediction logic in donut/prediction.py, organized as DonutPredictor
– Role: Generating anomaly scores on new data
e. Reconstruction and Missing Data Imputation:
– Component: Reconstruction in donut/reconstruction.py
– Role: Incorporating MCMC iterative reconstruction approaches for missing data correction
f. Utility Functions:
– Component: donut/utils.py
– Role: Supporting common tasks across modules such as variable management and session handling

##### Relationships and Interactions
- Data flows from raw input through preprocessing/augmention to the model core.
- The model is constructed in a modular way where the encoder (built with Keras Dense layers) and the decoder are set up for VAE processes.
- Training and prediction modules directly wrap around the model core, managing sessions and TensorFlow variable scopes.
- The reconstruction module interfaces with both the model and utilities to provide advanced outputs (like MCMC-based reconstruction).
- External services include TensorFlow, TFSnippet, and ZhuSuan, which are critical dependencies influencing how the model is structured and executed.

##### Architectural Design Patterns and Principles
- A modular design: Separates concerns between data preparation, model definition, training, prediction, and post-processing.
- Dependency injection: The design makes it possible to plug in different neural network architectures (as seen in the API where h_for_p_x and h_for_q_z are defined).
- Clear separation of training vs. inference logic, which is reflected in both file and function organization.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Data Preparation Layer
    subgraph "Data Preparation Layer"
        preproc["Preprocessing\n(donut/preprocessing.py)"]:::prep
        augment["Data Augmentation\n(donut/augmentation.py)"]:::prep
    end

    %% Core Model Layer
    subgraph "Core Model Layer"
        model["VAE-based Donut Model\n(donut/model.py)"]:::model
    end

    %% Operational Modules
    subgraph "Operational Modules"
        trainer["DonutTrainer\n(donut/training.py)"]:::operation
        predictor["DonutPredictor\n(donut/prediction.py)"]:::operation
        recon["Reconstruction (MCMC-based)\n(donut/reconstruction.py)"]:::operation
    end

    %% Utilities and External Dependencies
    subgraph "Utility & External"
        utils["Utility Functions\n(donut/utils.py)"]:::utility
        external["External Dependencies\nTensorFlow, TFSnippet, ZhuSuan"]:::external
    end

    %% Raw data input
    data_in["Raw Data"]

    %% Data flow
    data_in -->|"processed_by"| preproc
    data_in -->|"processed_by"| augment
    preproc -->|"feeds"| model
    augment -->|"feeds"| model
    model -->|"trains_with"| trainer
    model -->|"infers_with"| predictor
    model -->|"reconstructs_with"| recon

    %% Utility interactions
    utils -.->|"supports"| model
    utils -.->|"supports"| trainer
    utils -.->|"supports"| predictor
    utils -.->|"supports"| recon

    %% External dependencies usage
    trainer -->|"uses"| external
    predictor -->|"uses"| external
    recon -->|"uses"| external

    %% Click Events
    click preproc "https://github.com/netmanaiops/donut/blob/master/donut/preprocessing.py"
    click augment "https://github.com/netmanaiops/donut/blob/master/donut/augmentation.py"
    click model "https://github.com/netmanaiops/donut/blob/master/donut/model.py"
    click trainer "https://github.com/netmanaiops/donut/blob/master/donut/training.py"
    click predictor "https://github.com/netmanaiops/donut/blob/master/donut/prediction.py"
    click recon "https://github.com/netmanaiops/donut/blob/master/donut/reconstruction.py"
    click utils "https://github.com/netmanaiops/donut/blob/master/donut/utils.py"

    %% Styling
    classDef prep fill:#F4A460,stroke:#000,stroke-width:2px;
    classDef model fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef operation fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef utility fill:#DDA0DD,stroke:#000,stroke-width:2px;
    classDef external fill:#FFB6C1,stroke:#000,stroke-width:2px;
```
