pfun-cma-model

PFun CMA Model

A physiofunctional chronometabolic model implementation (numerical inference for glucose, stress hormones, circadian time series).

PFun Logo
Python 3.12+ FastAPI
API Docs Source Code

What is PFun CMA?

The PFun CMA Model is a generative physiological model that functionally replicates neuroendocrine dynamics โ€” specifically the interplay between cortisol, melatonin, and adiponectin โ€” and their influence on glucose metabolism across the circadian cycle.

In simple terms


Generated CMA Decomposition

The model decomposes glucose time series data into underlying hormonal influences:

Generated Cortisol-Melatonin-Adiponectin decomposition from Glucose time series

The CMA model leverages physiological modeling principles to decompose glucose time series data into underlying hormonal influences โ€” specifically cortisol, melatonin, and adiponectin.


Model Fitting

Fit the CMA model to real CGM data and extract clinically meaningful parameters:

24-hour fit result โ€” blue is model, red is data

After fitting, visualize parameters with automatically generated qualitative descriptions:

Fit result with qualitative descriptions


Live Demos

LLM-Powered Scenario Generation

Generate realistic physiological scenarios using natural language prompts. The LLM translates qualitative descriptions into physiologically valid CMA parameters:

LLM Generate Scenario Demo

LLM Scenario with recommendations


Real-time WebSocket Streaming

Interactive parameter control with live chart updates via WebSocket:

WebSocket streaming demo

WebSocket demo with sliders


Parameter Grid Visualization

Explore the CMA parameter space with precomputed grids:

CMA parameter grid visualization


Quick Start

# Install
pip install pfun-cma-model
# โ€” or with uv โ€”
uv add pfun-cma-model

# Run a quick model fit
uv run pfun-cma-model fit-model --plot

# Generate a scenario via LLM
uv run pfun-cma-model generate-scenario \
  --query "a healthy individual who exercises before sunrise"

# Launch the full dev server
uv run fastapi dev pfun_cma_model/app.py --port 8001

โ†’ Full installation guide


Project Architecture

pfun-cma-model/
โ”œโ”€โ”€ pfun_cma_model/          # Core Python package
โ”‚   โ”œโ”€โ”€ engine/              # CMA model, fit, grid, plotting
โ”‚   โ”œโ”€โ”€ routes/              # FastAPI route handlers
โ”‚   โ”œโ”€โ”€ llm.py               # LLM prompting logic
โ”‚   โ”œโ”€โ”€ cli.py               # Click CLI commands
โ”‚   โ””โ”€โ”€ security.py          # Security middleware
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ notebooks/           # Jupyter notebooks
โ”‚   โ”œโ”€โ”€ screenshots/         # Application screenshots
โ”‚   โ””โ”€โ”€ videos/              # Demo recordings
โ”œโ”€โ”€ docs/                    # This documentation (Zensical)
โ”œโ”€โ”€ tests/                   # Test suite
โ””โ”€โ”€ results/                 # Output artifacts & databases

Resource Link
:material-web: Homepage pfun.one
:material-play-circle: Live Demo PFun Health Tips
:material-github: Source Code pfun-health/pfun-cma-model
:material-file-document: Research Paper Chronometabolic Analysis (PDF)
:material-api: API Swagger http://localhost:8001/docs
:material-api: API ReDoc http://localhost:8001/redoc