# InsuLearner: Estimating Insulin Pump Settings with Machine Learning
## Author: Cameron Summers
#### Author's Website: [www.CameronSummers.com](https://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](https://www.CameronSummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning)
where I describe in plain language the machine learning approach I developed to compute
the insulin pump settings for my son.
This library estimates personalized insulin pump settings from historical insulin
and carbohydrate information. Currently, it interfaces with [Tidepool](https://www.tidepool.org)
accounts to retrieve data.
The code uses machine learning to estimate:
- Carbohydate Ratio (CIR)
- Basal Rate
- Insulin Sensivity Factor (ISF)
Historical carbohydrate and insulin data directly estimate CIR and Basal
Rate using Linear Regression. An estimate of Carbohydrate Sensitivity Factor
(CSF) and the estimate CIR is used to estimate ISF.
Here is an example plot of a fitted model and estimated settings:
![alt text](static/example_settings_plot_plus_aace.jpg)
## Dependencies
Developed and tested with Python 3.9
## Installation
For easy CLI tool create a virtual environment and then run:
```
pip install insulearner
```
For digging into the code to understand what's going on (recommended):
```
git clone https://github.com/scaubrey/InsuLearner
```
## Usage (CLI)
The install with pip provides a command line interface so you can run `insulearner`.
#### CLI Examples
If you don't know your Carbohydrate Sensitivity Factor (CSF) I devised
an estimator based on height and weight. Note: this estimator is an
educated guess and *not validated*. It's probably better if you estimate
it yourself.
`insulearner <your_tidepool_email> <your_tidepool_password> --num_days 60 --height_inches 72 --weight_lbs 200 --gender male`
If you do have an estimate of your CSF through your own testing:
`insulearner <your_tidepool_email> <your_tidepool_password> --num_days 60 --CSF 4.2`
#### More CLI Options
`--agg_period_window_size_hours` This is the size of the time period in days over which
aggregate insulin and carb data. I theorized in [my article](https://www.cameronsummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning)
that longer periods would converge to better estimates of CIR and Basal Rate. Default
is 1 day (24 hours) but I have seen good results with many more days.
`--agg_period_hop_size_hours` This is how much to shift over the aggregation
window for each data point. For example, if the window size above is 72 hrs
and this is 24 hours, then the aggregation for the first data point will be days 1-3, the
second data point will be days 2-4, and the third data point will
be days 3-5, and so on.
`--estimate_agg_boundaries` If set this will use an autocorrelation-like algorithm to
estimate the hour of the day when blood glucose movement is the least active, ie
isolating associated insulin and carb effects.
## Usage (code)
Once installed with pip, you can use the functions in your code:
```
>>> from InsuLearner.insulearner import analyze_settings_lr
```
## Algorithms
There are three algorithms I developed that are in this code and worth
being aware of:
1. An autocorrelation-like algorithm to find inactive periods in data
in order to help denoise the data for fitting a model.
2. Linear Regression to estimate Carb Ratio (CIR), Basal Rate, and Insulin
Sensitivity Factor (ISF)
1. This is methodology is described in [my article](https://www.CameronSummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning).
3. Estimating Carb Sensitivity Factor (CSF)
1. CSF is used to derive ISF from the estimated CIR from the Linear Regression.
It's best to estimate CSF through testing, but I've provided an
algorithm to approximate it based on blood volume computed via height
and weight. While based on knowledge of biology this algorithm *has not been validated*.
## Tests
I included a couple of basic regression tests in the `tests` folder of the code that
can be run with pytest.
## Acknowledgements
Special thanks to [Tidepool](https://www.tidepool.org) for doing
great things for the diabetes community.
## How to Contribute
### Contribute Code:
Hello! Thanks for considering contributing.
- Step 1. Please open an Issue with a description of what you're trying to add/fix/change
- Step 2. Fork and create a feature branch in the format <some-description>/<your issue number>
- Step 3. Please squash all your commits into one with a good commit message before opening a pull request
- Step 4. Open a pull request, reference your original issue, and provide a concise description of how your changes fixed the issue
- Step 5. Your PR requires approval before it can be merged.
### Contribute Results:
If you've run this and had success or issues, please consider sharing so
it can be improved. You can message me on [my website](https://www.cameronsummers.com/contact).
## License: [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html)
Raw data
{
"_id": null,
"home_page": "https://www.cameronsummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning",
"name": "insulearner",
"maintainer": "",
"docs_url": null,
"requires_python": ">3.8",
"maintainer_email": "",
"keywords": "insulin,diabetes,pump,settings,type 1,carbohydrate ratio,insulin sensitivity factor,basa rate",
"author": "Cameron Summers",
"author_email": "contact@cameronsummers.com",
"download_url": "https://files.pythonhosted.org/packages/ca/18/b583e8cfd96f6e464c63db2c25dd6db6a18b36f1d08183af319e0f104c9e/insulearner-0.1.6.tar.gz",
"platform": null,
"description": "# InsuLearner: Estimating Insulin Pump Settings with Machine Learning\n## Author: Cameron Summers\n#### Author's Website: [www.CameronSummers.com](https://www.CameronSummers.com)\n\n---\n\n### _Warning_:\n*_This code can have significant impact on insulin dosing. \nThere are no guardrails included here so it's possible to get poor\nresults in some circumstances.\nCheck with your doctor before making any changes to dosing settings._*\n\n\n### Overview:\n\nThis is the code underlying [my article](https://www.CameronSummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning) \nwhere I describe in plain language the machine learning approach I developed to compute \nthe insulin pump settings for my son.\n\nThis library estimates personalized insulin pump settings from historical insulin\nand carbohydrate information. Currently, it interfaces with [Tidepool](https://www.tidepool.org)\naccounts to retrieve data.\n\nThe code uses machine learning to estimate:\n\n- Carbohydate Ratio (CIR)\n- Basal Rate\n- Insulin Sensivity Factor (ISF)\n\nHistorical carbohydrate and insulin data directly estimate CIR and Basal \nRate using Linear Regression. An estimate of Carbohydrate Sensitivity Factor \n(CSF) and the estimate CIR is used to estimate ISF.\n\nHere is an example plot of a fitted model and estimated settings:\n\n![alt text](static/example_settings_plot_plus_aace.jpg)\n\n## Dependencies\n\nDeveloped and tested with Python 3.9\n\n## Installation\n\nFor easy CLI tool create a virtual environment and then run:\n```\npip install insulearner\n```\n\nFor digging into the code to understand what's going on (recommended):\n\n```\ngit clone https://github.com/scaubrey/InsuLearner\n```\n\n## Usage (CLI)\n\nThe install with pip provides a command line interface so you can run `insulearner`.\n\n#### CLI Examples\n\nIf you don't know your Carbohydrate Sensitivity Factor (CSF) I devised\nan estimator based on height and weight. Note: this estimator is an\neducated guess and *not validated*. It's probably better if you estimate\nit yourself.\n\n`insulearner <your_tidepool_email> <your_tidepool_password> --num_days 60 --height_inches 72 --weight_lbs 200 --gender male`\n\nIf you do have an estimate of your CSF through your own testing:\n\n`insulearner <your_tidepool_email> <your_tidepool_password> --num_days 60 --CSF 4.2`\n\n#### More CLI Options\n\n`--agg_period_window_size_hours` This is the size of the time period in days over which\naggregate insulin and carb data. I theorized in [my article](https://www.cameronsummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning) \nthat longer periods would converge to better estimates of CIR and Basal Rate. Default\nis 1 day (24 hours) but I have seen good results with many more days.\n\n`--agg_period_hop_size_hours` This is how much to shift over the aggregation\nwindow for each data point. For example, if the window size above is 72 hrs \nand this is 24 hours, then the aggregation for the first data point will be days 1-3, the\nsecond data point will be days 2-4, and the third data point will\nbe days 3-5, and so on.\n\n`--estimate_agg_boundaries` If set this will use an autocorrelation-like algorithm to\nestimate the hour of the day when blood glucose movement is the least active, ie \nisolating associated insulin and carb effects.\n\n\n## Usage (code)\n\nOnce installed with pip, you can use the functions in your code:\n\n```\n>>> from InsuLearner.insulearner import analyze_settings_lr\n```\n\n\n## Algorithms\n\nThere are three algorithms I developed that are in this code and worth\nbeing aware of:\n\n1. An autocorrelation-like algorithm to find inactive periods in data\n in order to help denoise the data for fitting a model.\n2. Linear Regression to estimate Carb Ratio (CIR), Basal Rate, and Insulin\n Sensitivity Factor (ISF)\n 1. This is methodology is described in [my article](https://www.CameronSummers.com/how_I_calculate_my_sons_insulin_pump_settings_with_machine_learning).\n3. Estimating Carb Sensitivity Factor (CSF)\n 1. CSF is used to derive ISF from the estimated CIR from the Linear Regression. \n It's best to estimate CSF through testing, but I've provided an\n algorithm to approximate it based on blood volume computed via height\n and weight. While based on knowledge of biology this algorithm *has not been validated*. \n\n## Tests\n\nI included a couple of basic regression tests in the `tests` folder of the code that\ncan be run with pytest.\n\n## Acknowledgements\n\nSpecial thanks to [Tidepool](https://www.tidepool.org) for doing\ngreat things for the diabetes community.\n\n## How to Contribute\n\n### Contribute Code:\n\nHello! Thanks for considering contributing.\n\n- Step 1. Please open an Issue with a description of what you're trying to add/fix/change\n- Step 2. Fork and create a feature branch in the format <some-description>/<your issue number>\n- Step 3. Please squash all your commits into one with a good commit message before opening a pull request\n- Step 4. Open a pull request, reference your original issue, and provide a concise description of how your changes fixed the issue\n- Step 5. Your PR requires approval before it can be merged.\n\n### Contribute Results:\n\nIf you've run this and had success or issues, please consider sharing so \nit can be improved. You can message me on [my website](https://www.cameronsummers.com/contact).\n\n\n## License: [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html)\n\n\n",
"bugtrack_url": null,
"license": "GPL-3.0-only",
"summary": "Estimate Insulin Pump Settings using Machine Learning",
"version": "0.1.6",
"split_keywords": [
"insulin",
"diabetes",
"pump",
"settings",
"type 1",
"carbohydrate ratio",
"insulin sensitivity factor",
"basa rate"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5ec9e9f20ec5b1f4e955bb2d18cb37f32384702ae1797df7cb41a228e2077bc8",
"md5": "b3778f900c457d611e12b9a7a086c5db",
"sha256": "3d9fc82492622aa421df89a6e753fc3981e49560e9986af1395cd4c993852bc6"
},
"downloads": -1,
"filename": "insulearner-0.1.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b3778f900c457d611e12b9a7a086c5db",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">3.8",
"size": 36092,
"upload_time": "2023-04-05T21:29:16",
"upload_time_iso_8601": "2023-04-05T21:29:16.129548Z",
"url": "https://files.pythonhosted.org/packages/5e/c9/e9f20ec5b1f4e955bb2d18cb37f32384702ae1797df7cb41a228e2077bc8/insulearner-0.1.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ca18b583e8cfd96f6e464c63db2c25dd6db6a18b36f1d08183af319e0f104c9e",
"md5": "7f415b11bdea2967fbaf4a27df471d6c",
"sha256": "8d368dc8df11b9c5aad10c4448b9b999dcf05e68121f53a022b7fcfe886027c3"
},
"downloads": -1,
"filename": "insulearner-0.1.6.tar.gz",
"has_sig": false,
"md5_digest": "7f415b11bdea2967fbaf4a27df471d6c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">3.8",
"size": 46977,
"upload_time": "2023-04-05T21:29:17",
"upload_time_iso_8601": "2023-04-05T21:29:17.822054Z",
"url": "https://files.pythonhosted.org/packages/ca/18/b583e8cfd96f6e464c63db2c25dd6db6a18b36f1d08183af319e0f104c9e/insulearner-0.1.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-05 21:29:17",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "insulearner"
}