Skip to main content

Estimate Insulin Pump Settings using Machine Learning

Project description

InsuLearner: Estimating Insulin Pump Settings with Machine Learning

Author: Cameron Summers

Author's Website: www.CameronSummers.com


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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

insulearner-0.2.0.tar.gz (44.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

insulearner-0.2.0-py3-none-any.whl (48.3 kB view details)

Uploaded Python 3

File details

Details for the file insulearner-0.2.0.tar.gz.

File metadata

  • Download URL: insulearner-0.2.0.tar.gz
  • Upload date:
  • Size: 44.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.9.10 Darwin/23.2.0

File hashes

Hashes for insulearner-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fbb54467bc59fc3f15a0184510f4b12746cf316ce3a9a2a6e3dbf05d16b67ef2
MD5 c9868bae10743700d75e513ce69f9234
BLAKE2b-256 4cbee0eee70544ae996bbc5f1fd6bf0468681b485c7825f87db80e26b1c861cb

See more details on using hashes here.

File details

Details for the file insulearner-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: insulearner-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 48.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.9.10 Darwin/23.2.0

File hashes

Hashes for insulearner-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bcd59c79e5049427c0943d23f156a1e967662798c2b5585f1f0dfb1991059b1
MD5 fdb4eb0d6f8fcd53a7e10ec9646d7ec0
BLAKE2b-256 07f27a0a9123c225a8b1ff0f63e50daa22150bd899561edebe9307208b427aee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page