Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update risk.py #67

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions simglucose/analysis/risk.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import numpy as np
import warnings


def risk_index(BG, horizon):
# BG is in mg/dL
# horizon in samples
with warnings.catch_warnings():
warnings.simplefilter('ignore')
BG_to_compute = BG[-horizon:]
fBG = 1.509 * (np.log(BG_to_compute)**1.084 - 5.381)
rl = 10 * fBG[fBG < 0]**2
rh = 10 * fBG[fBG > 0]**2
LBGI = np.nan_to_num(np.mean(rl))
HBGI = np.nan_to_num(np.mean(rh))
RI = LBGI + HBGI
BG_to_compute = BG[-horizon:]
risks =[risk(r) for r in BG_to_compute]
LBGI = np.mean([r[0] for r in risks])
HBGI = np.mean([r[1] for r in risks])
RI = np.mean([r[2] for r in risks])

return (LBGI, HBGI, RI)

def risk(BG):
"""
Risk is a percentage - ranging from 0 to 100%.
The 20 and 600 mg/dl are just the values to which the risk formula was fit.
The aim is to make the risk maximum when it is either 20 or 600.
The units in the paper below are different (mmol/l), but in our units (mg/dl) these limits are 20 and 600.

Reference, in particular see appendix for the derivation of risk:
https://diabetesjournals.org/care/article/20/11/1655/21162/Symmetrization-of-the-Blood-Glucose-Measurement

"""
MIN_BG = 20.0
MAX_BG = 600.0
if BG <= MIN_BG:
return (100.0, 0.0, 100.0)
drozzy marked this conversation as resolved.
Show resolved Hide resolved
if BG >= MAX_BG:
return (0.0, 100.0, 100.0)

U = 1.509 * (np.log(BG)**1.084 - 5.381)

ri = 10 * U**2

rl, rh = 0.0, 0.0
if U <= 0:
rl = ri
if U >= 0:
rh = ri
return (rl, rh, ri)