PFun CMA Model
A physiofunctional chronometabolic model implementation (numerical inference for glucose, stress hormones, circadian time series).
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
- ๐ Phase-based dimensionality reduction โ Compress weeks or months of CGM time-series data into a compact phase vector (
โฅ 1024b in memory). - ๐ฎ Interpretable & Quantifiable โ Translate between qualitative states ("mood", "stress") and biophysical neuroendocrine dynamics ("cortisol levels", "glucose variability").
- ๐ High-speed circadian mapping โ Understand how circadian rhythm maps to glucose values in real time.
Generated CMA Decomposition
The model decomposes glucose time series data into underlying hormonal influences:

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:

After fitting, visualize parameters with automatically generated 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:


Real-time WebSocket Streaming
Interactive parameter control with live chart updates via WebSocket:


Parameter Grid Visualization
Explore the CMA parameter space with precomputed grids:

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
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
Key Links
| 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 (Markdown) ยท PDF |
| :material-api: API Swagger | http://localhost:8001/docs |
| :material-api: API ReDoc | http://localhost:8001/redoc |