# wm1995/tcup architecture

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

## Overview

##### Project Overview
- The project is an open-source statistical modeling tool focused on robust linear regression.
- It bundles components for both Pyro-based inference (in the tcup/numpyro.py module) and Stan-based inference (under tcup/stan), allowing users to choose the inference engine.
- It integrates with XDGMM (an external Python package implementing the extreme deconvolution algorithm), which is mentioned in the README.

##### Key Components
a. Core Modules:
- tcup Package: The heart of the project with the following subcomponents:
- numpyro.py: Handles the robust regression implementation using NumPyro.
- preprocess.py: Contains data pre-processing utilities.
- priors.py: Encapsulates the definition of prior distributions.
- scale.py: Deals with scaling methods or data transformations.
- utils.py: Miscellaneous utility functions.
- stan Subpackage:
▫ __init__.py: Package setup.
▫ model.py: High-level interfacing or abstraction for Stan-based modeling.
▫ src Directory: Contains actual Stan model files (ncup.stan, tcup.stan, template.stan) which define the statistical models.

b. Testing and CI:
- tests Directory: A comprehensive suite of tests for validating each module (numyprof, scale, stan integration, etc.).
- .github/workflows: Contains CI configuration (for example, pull-request.yaml) ensuring that automated tests and checks are run.

c. Configuration and Misc:
- pyproject.toml: Manages build and dependency setup.
- Pre-commit, GitHub, and VSCode config files indicate a focus on code quality and tool integration for development.

##### Relationships and Interactions
- The core computation modules (numpyro, priors, preprocess, scale, and utils) are interrelated to build the robust regression pipeline. The workflow is from data pre-processing → applying prior information and scaling → executing regression via either NumPyro or Stan.
- The Stan subpackage represents a parallel, specialized flow where the model specifications are handled using Stan’s language (with model.py abstracting/simplifying interactions).
- Dependencies clearly exist between the components: for example, preprocess prepares data that is then used by both the numpyro and stan approaches.
- The CI pipelines in the .github directory ensure that code commits trigger the tests in the tests directory.

##### Architectural Patterns and Design Principles
- Modular Design: The separation between different inference backends (NumPyro vs. Stan) makes for a modular architecture, making it straightforward to see which components serve which purpose.
- Separation of Concerns: Core modeling classes and helper functions are separated from data pre-processing and testing logic.
- Extensibility: With a clear directory structure and separate modules for different responsibilities, adding additional models or inference methods should be straightforward.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top-level raw input
    RD["Raw Data Input"]:::external

    %% Core Package Subgraph
    subgraph "tcup Package"
      direction TB
      Pkg["tcup Package"]:::core
      DP["Data Preprocessing Utilities"]:::core
      PD["Prior Distributions"]:::core
      SC["Scaling/Transformation"]:::core
      UT["Utility Functions"]:::utility
      NI["NumPyro Inference"]:::core
      SI["Stan-based Inference"]:::core

      %% Stan Modeling Subpackage inside Stan-based Inference
      subgraph "Stan Modeling Subpackage"
        direction TB
        SI_init["Stan Package Initialization"]:::core
        SI_abs["Stan Model Abstraction"]:::core
        SI_files["Stan Model Files"]:::core
      end
    end

    %% Supporting Infrastructure Subgraph
    subgraph "Supporting Infrastructure"
      direction TB
      TS["Test Suite"]:::test
      CI["CI Configuration"]:::test
      PC["Project Configuration"]:::test
    end

    %% External Dependency
    XDM["XDGMM External Dependency"]:::external

    %% Data Flow
    RD --> Pkg
    Pkg --> DP
    DP --> PD
    PD --> SC
    SC --> NI
    SC --> SI
    UT --> NI
    UT --> SI

    %% Stan Modeling flow within Stan-based Inference
    SI --> SI_init
    SI_init --> SI_abs
    SI_abs --> SI_files

    %% External dependency interactions (dashed lines)
    XDM --- NI
    XDM --- SI

    %% Supporting Infrastructure connections (from Utility Functions as central support)
    UT --> TS
    UT --> CI
    UT --> PC

    %% Click Events
    click Pkg "https://github.com/wm1995/tcup/tree/main/tcup/"
    click DP "https://github.com/wm1995/tcup/blob/main/tcup/preprocess.py"
    click PD "https://github.com/wm1995/tcup/blob/main/tcup/priors.py"
    click SC "https://github.com/wm1995/tcup/blob/main/tcup/scale.py"
    click UT "https://github.com/wm1995/tcup/blob/main/tcup/utils.py"
    click NI "https://github.com/wm1995/tcup/blob/main/tcup/numpyro.py"
    click SI "https://github.com/wm1995/tcup/tree/main/tcup/stan/"
    click SI_init "https://github.com/wm1995/tcup/blob/main/tcup/stan/__init__.py"
    click SI_abs "https://github.com/wm1995/tcup/blob/main/tcup/stan/model.py"
    click SI_files "https://github.com/wm1995/tcup/tree/main/tcup/stan/src/"
    click TS "https://github.com/wm1995/tcup/tree/main/tests/"
    click CI "https://github.com/wm1995/tcup/tree/main/.github/workflows/"
    click PC "https://github.com/wm1995/tcup/blob/main/pyproject.toml"
    click XDM "https://github.com/wm1995/tcup/tree/main/XDGMM"

    %% Styles
    classDef core fill:#AEDFF7,stroke:#333,stroke-width:2px;
    classDef utility fill:#D1E8E2,stroke:#333,stroke-width:2px;
    classDef external fill:#F7D6AE,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef test fill:#F9F7AE,stroke:#333,stroke-width:2px;
```
