# dssg/triage architecture

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

- Interactive diagram: https://gitdiagram.com/dssg/triage
- Repository: https://github.com/dssg/triage

## Overview

##### Main Components
- CLI & Entry Points: The primary user interaction is via the CLI (see manage.py and src/triage/cli.py). This command line interface is a key entry point where users initiate experiments.
- Experiment Runner: The “experiments” module within src/triage/experiments (for example, SingleThreadedExperiment, multicore, etc.) encapsulates the experiment execution logic.
- Modular Components: Triage is organized into multiple functional components found in src/triage/component. These include:
- Architect: Handles cohort selection, feature generation, labeling, and planning (e.g. in src/triage/component/architect).
- Audition: Manages scoring, model evaluation, thresholding, and rule generation.
- Catwalk: Provides baselines, model training, feature importance, and prediction utilities.
- Collate: Responsible for data aggregation and transformation routines.
- Postmodeling: Focuses on generating predictions, crosstabs, and fairness analysis.
- Results Schema: Manages database migrations and schema consistency with alembic.
- Timechop: Deals with temporal partitioning and visualization.
- Configuration Management: The system is highly configurable, using YAML files (such as database.yaml, experiment.yaml, and others in the example/config directory) to drive how the components behave.
- Data Storage and Database: Triage requires a PostgreSQL database to store source data, matrices, and experiment outputs. This dependency is evident in configuration files and database provisioning scripts (e.g. triage db up, Dockerfiles for the food_db subproject).
- Documentation and Tutorials: Although not part of the runtime, the extensive docs (in docs and example directories) explain usage, configuration, and tutorial examples (like the Dirty Duck tutorial).

##### Relationships and Interactions
- The user starts a Triage experiment via the CLI or by importing the package in a Python script.
- The CLI passes configuration information (from YAML files) to the Experiment Runner.
- The Experiment Runner orchestrates calls to various modular components:
- The Architect module generates data matrices (e.g., feature generation, label creation) and plans which features to build.
- Audition and Catwalk modules work on model training, evaluation, and generating performance metrics.
- Postmodeling takes the outputs, refines predictions, and generates reports (with fairness checks).
- The Results Schema ensures the underlying PostgreSQL database is structured correctly to persist experiment data.
- Timechop handles temporal aspects such as training/test splits and visualization of time-based trends.
- Each component interacts with a shared configuration and logging system (using src/triage/config) and accesses the database using a common set of utility functions.
- There is also a clear separation between “core experimental logic” and “infrastructure/data management” (with separate directories for configuration, experiments, and database reflection utilities).

##### Architectural Patterns and Design Principles
- Modularity: The architecture is broken into clearly defined components (architect, audition, catwalk, collate, etc.) that encapsulate specific steps in the ML workflow.
- Configurability: Heavy use of YAML configuration allows users to tailor experiments and run different data science pipelines without altering code.
- Reusability and Extensibility: The design encourages plugging in new algorithms or analysis routines by adding to the component modules.
- Integration with External Services: Triage supports running experiments locally or via cloud/Docker setups, evidenced by Dockerfiles and AWS batch examples.
- Robust Testing: The extensive “src/tests” directory shows that unit tests and integration tests are key, indicating a design that favors a tested, reliable structure.

##### Relevant Technologies, Frameworks, and Libraries
- Python 3.9+ (core language)
- PostgreSQL (for data storage)
- Docker/Docker Compose (for database provisioning and environment consistency)
- YAML (for configuration files)
- SQLAlchemy (for database engine creation and connection)
- Alembic (for database migrations within the results_schema component)
- PyTest (for testing)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        CLI1["manage.py"]:::cli
        CLI2["CLI: src/triage/cli.py"]:::cli
    end

    %% Configuration Management
    subgraph "Configuration Management"
        CM1["Example Config"]:::config
        CM2["Logging Config"]:::config
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        ER["Experiment Runner (src/triage/experiments)"]:::experiment
        ER -->|"orchestrates"| AM["Architect Module (src/triage/component/architect)"]:::component
        ER -->|"orchestrates"| AU["Audition Module (src/triage/component/audition)"]:::component
        ER -->|"orchestrates"| CW["Catwalk Module (src/triage/component/catwalk)"]:::component
        ER -->|"orchestrates"| CO["Collate Module (src/triage/component/collate)"]:::component
        ER -->|"orchestrates"| PM["Postmodeling Module (src/triage/component/postmodeling)"]:::component
        ER -->|"orchestrates"| RS["Results Schema Module (src/triage/component/results_schema)"]:::component
        ER -->|"orchestrates"| TC["Timechop Module (src/triage/component/timechop)"]:::component
    end

    %% Data Storage Layer
    subgraph "Data Storage Layer"
        DR["Database Reflection (src/triage/database_reflection.py)"]:::db
        DB["Database (PostgreSQL)"]:::db
        FD["Food DB Setup (dirtyduck/food_db)"]:::db
        DC["Database Config (example/config/database.yaml)"]:::db
    end

    %% Documentation & Tutorials
    subgraph "Documentation & Tutorials"
        DOC["Documentation (docs)"]:::docs
        DD["Dirtyduck Tutorial (dirtyduck)"]:::docs
    end

    %% Flow Connections
    CLI1 -->|"starts"| CLI2
    CLI2 --> ER
    CM1 -->|"feeds_config"| ER
    CM2 -->|"feeds_config"| ER

    %% Business Logic utilization of configurations
    AM ---|"uses_config"| CM1
    AM ---|"uses_config"| CM2
    AU ---|"uses_config"| CM1
    AU ---|"uses_config"| CM2
    CW ---|"uses_config"| CM1
    CW ---|"uses_config"| CM2
    CO ---|"uses_config"| CM1
    CO ---|"uses_config"| CM2
    PM ---|"uses_config"| CM1
    PM ---|"uses_config"| CM2
    TC ---|"uses_config"| CM1
    TC ---|"uses_config"| CM2

    %% Data interactions
    RS <-->|"migrations"| DB
    DR --> DB
    DC --> DB
    FD --> DB
    PM -->|"stores_results"| DB

    %% Documentation references
    DOC ---|"references"| CM1
    DD --- DOC

    %% Click Events
    click CLI1 "https://github.com/dssg/triage/blob/master/manage.py"
    click CLI2 "https://github.com/dssg/triage/blob/master/src/triage/cli.py"
    click ER "https://github.com/dssg/triage/tree/master/src/triage/experiments/"
    click AM "https://github.com/dssg/triage/tree/master/src/triage/component/architect/"
    click AU "https://github.com/dssg/triage/tree/master/src/triage/component/audition/"
    click CW "https://github.com/dssg/triage/tree/master/src/triage/component/catwalk/"
    click CO "https://github.com/dssg/triage/tree/master/src/triage/component/collate/"
    click PM "https://github.com/dssg/triage/tree/master/src/triage/component/postmodeling/"
    click RS "https://github.com/dssg/triage/tree/master/src/triage/component/results_schema/"
    click TC "https://github.com/dssg/triage/tree/master/src/triage/component/timechop/"
    click CM1 "https://github.com/dssg/triage/tree/master/example/config/"
    click CM2 "https://github.com/dssg/triage/blob/master/src/triage/config/logging.yaml"
    click DR "https://github.com/dssg/triage/blob/master/src/triage/database_reflection.py"
    click FD "https://github.com/dssg/triage/tree/master/dirtyduck/food_db/"
    click DC "https://github.com/dssg/triage/blob/master/example/config/database.yaml"
    click DOC "https://github.com/dssg/triage/tree/master/docs/"
    click DD "https://github.com/dssg/triage/tree/master/dirtyduck/"

    %% Styles
    classDef cli fill:#add8e6,stroke:#00008b,stroke-width:2px;
    classDef experiment fill:#d2e7ff,stroke:#0055a5,stroke-width:2px;
    classDef component fill:#e0ffd2,stroke:#2d7a2d,stroke-width:2px;
    classDef config fill:#ffe6cc,stroke:#cc6600,stroke-width:2px;
    classDef db fill:#ffd2d2,stroke:#a52a2a,stroke-width:2px;
    classDef docs fill:#e2ccff,stroke:#6a0dad,stroke-width:2px;
```
