# jsxlei/scale architecture

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

## Overview

The SCALE project is a Python-based tool for single-cell ATAC-seq analysis that uses deep learning (implemented in PyTorch) to perform latent feature extraction. The project is structured as a library (located in the “scale” package) along with a command-line entry point (SCALE.py), a setup script, and accompanying configuration files (like requirements.txt).

##### Main Components
- Command-line entry point: The SCALE.py script serves as the main execution driver. It parses input arguments, manages runtime options (like choosing between binary or imputed data generation), and triggers the analysis flow.
- Data Ingestion and Preprocessing: The dataset.py module is critical here. It handles importing single-cell ATAC-seq data from multiple input formats (e.g., h5ad files, mtx folders, count matrix files). Include reference to external file formats and data preparation.
- Neural Network Components:
- Model Construction: Represent the model.py module, which builds the deep learning model architecture.
- Network Layers: Use the layer.py module showing that custom neural network layers are defined.
- Loss Calculation: Denote the loss.py module that encapsulates the loss functions for training.
- Utilities and Helpers: The utils.py module provides auxiliary functions, and specifity.py likely encodes specialized operations or metrics.
- Visualization: The plot.py module handles plotting results (e.g., UMAP visualization outputs).
- Packaging and Deployment: The setup.py file and requirements.txt serve as the project's installation and dependency management components.

##### Relationships and Data Flow
- The SCALE.py entry point first ingests input data (using dataset.py) and configures experiment parameters (through parsing CLI options).
- Data then flows into the neural network part:
○ The dataset is preprocessed before being fed into the model (constructed in model.py using components from layer.py).
○ The loss is computed via loss.py to drive model optimization.
- Trained model outputs, such as the latent feature matrix, saved model file (model.pt) and downstream results (adata.h5ad, umap.pdf) are then generated.
- Visualization functions in plot.py extract and display performance and embedding results.
- Utilities and specific functions (in utils.py and specifity.py) support these processes (e.g., filtering peaks, handling metrics).

##### Architectural Patterns & Design Principles
- Modularity: Each distinct responsibility (input handling, model building, loss computation, visualization) is separated into its own module.
- CLI-Driven Execution: SCALE.py acts as a controller that orchestrates data loading, model training, and output generation, following a pipeline pattern.
- Reproducibility: The project emphasizes output reproducibility through saved artifacts (model.pt and adata.h5ad) along with comprehensive runtime options.
- Integration with External Libraries: It clearly leverages PyTorch as the core framework and integrates with analysis libraries like scanpy for handling h5ad files.

##### Relevant Technologies
- Python and PyTorch form the development backbone.
- External libraries for single-cell analysis (e.g., scanpy).
- Data formats include h5ad, txt/tsv/csv, and mtx.
- Visualization (e.g., UMAP)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Command-line Controller
    subgraph "Command-line Controller"
        SCALE("SCALE.py"):::cli
    end

    %% Data Ingestion & Preprocessing
    subgraph "Data Ingestion & Preprocessing"
        DATASET("dataset.py"):::data
        INPUT("Input Data Files (h5ad, mtx, count matrix)"):::data
    end

    %% Neural Network Components
    subgraph "Neural Network Components"
        MODEL("model.py"):::neural
        LAYER("layer.py"):::neural
        LOSS("loss.py"):::neural
    end

    %% Utilities & Helpers
    subgraph "Utilities & Helpers"
        UTILS("utils.py"):::util
        SPECIFITY("specifity.py"):::util
    end

    %% Visualization & Output
    subgraph "Visualization & Output"
        PLOT("plot.py"):::viz
        ARTIFACTS("Output Artifacts: model.pt, adata.h5ad, umap.pdf"):::artifact
    end

    %% Packaging & Deployment
    subgraph "Packaging & Deployment"
        SETUP("setup.py"):::pkg
        REQ("requirements.txt"):::pkg
    end

    %% External Libraries
    PYTORCH("PyTorch"):::ext
    SCANPY("Scanpy"):::ext

    %% Flow Connections
    SCALE -->|"parses args"| INPUT
    SCALE -->|"triggers"| DATASET
    INPUT -->|"loads & preps"| DATASET
    DATASET -->|"preprocessed data"| MODEL
    MODEL -->|"assembles layers"| LAYER
    MODEL -->|"loss calculation"| LOSS
    UTILS -.->|"supports"| DATASET
    UTILS -.->|"helps"| MODEL
    SPECIFITY -.->|"computes metrics"| MODEL
    MODEL -->|"training outputs"| PLOT
    LOSS -->|"loss signals"| PLOT
    PLOT -->|"generates"| ARTIFACTS
    
    %% External Library dependencies
    PYTORCH --- MODEL
    SCANPY --- DATASET

    %% Packaging connection (deployment/integration)
    SCALE -->|"deployment info"| SETUP

    %% Click Events
    click SCALE "https://github.com/jsxlei/scale/blob/master/SCALE.py"
    click DATASET "https://github.com/jsxlei/scale/blob/master/scale/dataset.py"
    click MODEL "https://github.com/jsxlei/scale/blob/master/scale/model.py"
    click LAYER "https://github.com/jsxlei/scale/blob/master/scale/layer.py"
    click LOSS "https://github.com/jsxlei/scale/blob/master/scale/loss.py"
    click UTILS "https://github.com/jsxlei/scale/blob/master/scale/utils.py"
    click SPECIFITY "https://github.com/jsxlei/scale/blob/master/scale/specifity.py"
    click PLOT "https://github.com/jsxlei/scale/blob/master/scale/plot.py"
    click SETUP "https://github.com/jsxlei/scale/blob/master/setup.py"
    click REQ "https://github.com/jsxlei/scale/blob/master/requirements.txt"
    
    %% Styling Definitions
    classDef cli fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef data fill:#F9E79F,stroke:#9A7D0A,stroke-width:2px;
    classDef neural fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef util fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
    classDef viz fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef artifact fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef pkg fill:#D6EAF8,stroke:#2874A6,stroke-width:2px;
    classDef ext fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
```
