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 1 commit
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
37 changes: 25 additions & 12 deletions simglucose/analysis/risk.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
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):
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)