Skip to main content

InsuLearner: Estimating Insulin Pump Settings with Machine Learning

Author: Cameron Summers

Author's Website: www.CameronSummers.com

Project Surfaces


Warning:

This code can have significant impact on insulin dosing. There are no guardrails included here so it's possible to get poor results in some circumstances. Check with your doctor before making any changes to dosing settings.

Overview

This is the code underlying my article, where I describe the machine-learning approach used to estimate pump settings.

InsuLearner estimates:

  • Carbohydrate Ratio (CIR)
  • Basal Rate
  • Insulin Sensitivity Factor (ISF)

Supported data sources:

The model fits linear regression over aggregated carb/insulin windows and derives CIR + basal, then uses CSF (K) and CIR to estimate ISF.

Example settings plot

Dependencies

  • Python >=3.9,<4.0

Installation

pip install insulearner

Or for development:

git clone https://github.com/scaubrey/InsuLearner
cd InsuLearner
pip install -e .

Usage (CLI)

The package installs a CLI entrypoint: insulearner.

Tidepool Example

insulearner <your_tidepool_email> <your_tidepool_password> \
  --num_days 60 \
  --height_inches 72 \
  --weight_lbs 200 \
  --gender male

If you already know CSF:

insulearner <your_tidepool_email> <your_tidepool_password> --num_days 60 --CSF 4.2

Nightscout Example

insulearner --source nightscout \
  --nightscout_url https://your-site.example.com \
  --nightscout_token <token-if-used> \
  --nightscout_api_secret <api-secret-if-used> \
  --num_days 30 \
  --CSF 12.5

Notes:

  • --source defaults to tidepool.
  • For --source tidepool, positional tp_username tp_password are required.
  • For --source nightscout, --nightscout_url is required.

Nightscout compatibility note:

  • InsuLearner supports the main Nightscout data needed for analysis: entries, treatments, and profile.
  • Nightscout deployments vary in how they store and filter entries data. InsuLearner now handles common variants using date, mills, and created_at, with a client-side fallback when server-side filtering returns no rows.
  • Basal reconstruction remains the most deployment-specific part of Nightscout support, so results can still vary across custom setups, plugins, and treatment event conventions.
  • If you run into a compatibility issue, please open a GitHub issue and include a redacted sample of the relevant Nightscout JSON shape.

Key CLI Options

  • --num_days: number of days to analyze
  • --agg_period_window_size_hours: aggregation window size in hours (default: 24)
  • --agg_period_hop_size_hours: hop size in hours (default: 24)
  • --estimate_agg_boundaries: estimate aggregation boundaries via autocorrelation-like logic

Python API

from InsuLearner import (
    TidepoolUser,
    NightscoutUser,
    TidepoolAPI,
    NightscoutAPI,
    analyze_settings_lr,
)

Environment Variables for Tests (PyCharm-friendly)

Use .env.test in the repo root so you do not re-enter credentials each run.

  1. Copy template:
cp .env.test.example .env.test
  1. Fill values in .env.test.

  2. Run tests from PyCharm or terminal. tests/conftest.py auto-loads .env.test.

Important:

  • .env.test is git-ignored.
  • .env.test.example is safe to commit.

Test Suite

Run all non-live tests:

pytest -q -m "not live"

Run all tests including live (requires credentials):

pytest -q

Run only live parity tests:

pytest -q -m "live"

Run lint:

ruff check .

CI

  • PR/Push CI runs non-live tests only.
  • Live API parity tests are split into a manual GitHub Actions workflow.

Release Process

See RELEASE.md for the checklist.

Acknowledgements

Special thanks to Tidepool for serving the diabetes community.

License

GPLv3

Metadata

Release files for insulearner 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for insulearner 0.2.1
File Size Uploaded Provenance
insulearner-0.2.1.tar.gz 45.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for insulearner 0.2.1
File Interpreter ABI Platform
insulearner-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 94.7 kB

Release files / insulearner-0.2.1.tar.gz

Download URL insulearner-0.2.1.tar.gz
Size 45.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2362ab8e51419f13071774b82bfe41249cc7873ae3438c1cbcddced153b4e912
BLAKE2b-256 checksum
How to use checksums
ec6e03d25e663b5e0bccb3f4f4f4b97fceb582abb1edb106183cf8ebb0d35b97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.9.10 Darwin/23.2.0

Release files / insulearner-0.2.1-py3-none-any.whl

Download URL insulearner-0.2.1-py3-none-any.whl
Size 49.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e623c925a9e44553d87b53c4c5df3d14f0998bce5936b5dadb19d1eb707349d2
BLAKE2b-256 checksum
How to use checksums
c0f5fb22e06ca5e0e8c37cff10048b32539d353fc46eb37813488e1320847217
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.9.10 Darwin/23.2.0

Security for insulearner 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page