# tonitacker/cimpredict architecture

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

- Interactive diagram: https://gitdiagram.com/tonitacker/cimpredict
- Repository: https://github.com/tonitacker/cimpredict

## Overview

##### Project Type and Purpose
- CIMPredict is a desktop, full-stack application (written in Python) that embeds R modules for statistical computation.
- Purpose: Accepts user input (CIM-blood value and age) via a GUI, runs a trained fracture-risk model (in R), and displays the resulting risk percentage.

##### High-Level Analysis of the File Structure
Top-level files and folders:
- main.py / main.spec – Python entry point and PyInstaller spec for packaging into an .exe
- R-Skripte/ – folder containing encrypted R scripts (RiskAssessmentModule.R, modelCIMAge.encrypted)
- modelCIMAge.RData – serialized R model data used by the R scripts
- encrypt.py + key.key – Python utility and key for decrypting the R modules at runtime
- README.md – user and developer documentation
Key inferences:
– The app decrypts R code on startup, then invokes R (via subprocess or rpy2) to compute the risk.
– PyInstaller bundles Python runtime, all scripts, model data, and the decryption key into CIMPredict.exe.
– There is an auto-update/checker mechanism that fetches dependency updates on first run.

##### Main Components and Layers
System into three logical layers:
A. Presentation Layer
- GUI Module (main.py’s UI)
B. Application / Business Logic Layer
- Decryption Service (encrypt.py + key.key)
- Core Controller (Python code orchestrating input validation, decryption, R invocation, and output handling)
- Auto-update Agent (checks for dependency updates on startup)
C. Data Layer
- R Execution Environment (R interpreter / Rscript binary)
- Model Data Store (modelCIMAge.RData + decrypted R scripts)

##### Data Flows and Interactions
– User launches CIMPredict.exe → Python runtime initializes
– Auto-update Agent checks for new Python/R package versions (Internet)
– GUI prompts user for CIM-value & age → passes inputs to Core Controller
– Core Controller calls Decryption Service to transiently decrypt R scripts into memory or temp files
– Core Controller invokes R engine (Rscript) with:
- decrypted RiskAssessmentModule.R
- modelCIMAge.RData
and the two input parameters
– R script computes fracture risk % → writes result to stdout or file
– Core Controller reads result → updates GUI view → displays risk to user

##### Important Architectural Patterns and Principles
- Layered Architecture: clear separation of UI, orchestration logic, and data/model
- Modularization: decryption, core logic, GUI and update checker are each independent modules
- External Service Integration: R is treated as an external “model service” called via subprocess/rpy2
- Packaging Strategy: PyInstaller bundles all code, data, and interpreter for a portable .exe

##### Relevant Technologies
– Python 3.x (main.py, encrypt.py)
– PyInstaller (main.spec → CIMPredict.exe)
– R (Rscript interpreter, .RData model)
– Encryption library (used in encrypt.py + key.key)
– GUI framework (e.g., tkinter or PyQt in main.py—deduced by examining imports)
– Network module (for update checking)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        GUI["GUI Module"]:::presentation
    end

    %% Logic Layer
    subgraph "Logic / Business Logic Layer"
        Core["Core Controller"]:::logic
        Decrypt["Decryption Service"]:::logic
        Key["key.key"]:::data
        Update["Auto-update Agent"]:::logic
    end

    %% Data Layer
    subgraph "Data Layer"
        ModelData["modelCIMAge.RData"]:::data
        EncModel["R-Skripte/modelCIMAge.encrypted"]:::data
        EncScript["R-Skripte/RiskAssessmentModule.encrypted"]:::data
        DScripts["Decrypted Scripts"]:::data
    end

    %% External Systems
    subgraph "External / Runtime"
        Internet["External Internet"]:::external
        REnv["R Execution Environment"]:::external
    end

    %% Documentation / Packaging
    subgraph "Documentation & Packaging"
        Doc["README.md"]:::documentation
        Spec["PyInstaller Spec (main.spec)"]:::documentation
    end

    %% Connections
    User(("User")):::external -->|"Enter CIM & age"| GUI
    GUI -->|"CIM & age"| Core
    Core -->|"invoke Auto-update"| Update
    Update -->|"check dependencies"| Internet
    Core -->|"Decrypt scripts"| Decrypt
    Decrypt -->|"uses key"| Key
    Decrypt -->|"outputs"| DScripts
    Core -->|"Load model data"| ModelData
    Core -->|"Load encrypted modules"| EncModel
    Core -->|"Load encrypted scripts"| EncScript
    Core -->|"invoke Rscript with inputs, scripts & model"| REnv
    REnv -->|"Risk % output"| Core
    Core -->|"Display risk %"| GUI

    %% Click Events
    click GUI "https://github.com/tonitacker/cimpredict/blob/main/main.py"
    click Core "https://github.com/tonitacker/cimpredict/blob/main/main.py"
    click Decrypt "https://github.com/tonitacker/cimpredict/blob/main/encrypt.py"
    click Key "https://github.com/tonitacker/cimpredict/blob/main/key.key"
    click ModelData "https://github.com/tonitacker/cimpredict/blob/main/modelCIMAge.RData"
    click EncModel "https://github.com/tonitacker/cimpredict/blob/main/R-Skripte/modelCIMAge.encrypted"
    click EncScript "https://github.com/tonitacker/cimpredict/blob/main/R-Skripte/RiskAssessmentModule.encrypted"
    click Spec "https://github.com/tonitacker/cimpredict/blob/main/main.spec"
    click Doc "https://github.com/tonitacker/cimpredict/blob/main/README.md"

    %% Styles
    classDef presentation fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef logic fill:#90EE90,stroke:#333,stroke-width:1px
    classDef data fill:#FFFFE0,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef documentation fill:#F0E68C,stroke:#333,stroke-width:1px
```
