-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgd5.html
198 lines (157 loc) · 6.5 KB
/
gd5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={
'modules':[{
'name':'visualization',
'version':'1',
'packages':['corechart']
}]
}"
></script>
</head>
<body>
<script>
function iob(g,idur)
//g is time in minutes from bolus event, idur=insulin duration
//walsh iob curves
{
if(g<=0.0) {tot=100.0} else
if(g>=idur*60.0) {tot=0.0} else {
if(idur==3) {
tot=-3.203e-7*Math.pow(g,4)+1.354e-4*Math.pow(g,3)-1.759e-2*Math.pow(g,2)+9.255e-2*g+99.951} else
if (idur==4) {
tot=-3.31e-8*Math.pow(g,4)+2.53e-5*Math.pow(g,3)-5.51e-3*Math.pow(g,2)-9.086e-2*g+99.95} else
if (idur==5) {
tot=-2.95e-8*Math.pow(g,4)+2.32e-5*Math.pow(g,3)-5.55e-3*Math.pow(g,2)+4.49e-2*g+99.3} else
if (idur==6) {
tot=-1.493e-8*Math.pow(g,4)+1.413e-5*Math.pow(g,3)-4.095e-3*Math.pow(g,2)+6.365e-2*g+99.7}
}
return(tot);
}
function intIOB(x1,x2,idur,g)
//simpsons rule to integrate IOB - can include sf and dbdt as functions of tstar later - assume constants for now
//integrating over flux time tstar
{
var integral;
var dx;
var nn=50; //nn needs to be even
var ii=1;
//initialize with first and last terms of simpson series
dx=(x2-x1)/nn;
integral=iob((g-x1),idur)+iob(g-(x1+nn*dx),idur);
while(ii<nn-2) {
// console.log(i);
// for (i=1;i<nn-1;i=i+2) {
integral = integral + 4*iob(g-(x1+ii*dx),idur)+2*iob(g-(x1+(ii+1)*dx),idur);
ii=ii+2;
}
integral=integral*dx/3.0;
return(integral);
}
function cob(g,ct)
//scheiner gi curves fig 7-8 from Think Like a Pancreas, fit with a triangle shaped absorbtion rate curve
//see basic math pdf on repo for details
//g is time in minutes,gt is carb type
{
var at={high:90.0,medium:180.0,low:240.0};
if(g<=0) {tot=0.0} else
if (g>=at[ct]) {tot=1.0} else
if ((g>0)&&(g<=at[ct]/2.0)) {
tot=2.0/Math.pow(at[ct],2)*Math.pow(g,2)} else
tot=-1.0+4.0/at[ct]*(g-Math.pow(g,2)/(2.0*at[ct]))
return(tot);
}
function deltatempBGI(g,dbdt,sensf,idur,t1,t2)
{
return -dbdt*sensf*((t2-t1)-1/100*intIOB(t1,t2,idur,g));
//return -dbdt*sensf*(t2-t1);
}
function deltaBGC(g,sensf,cratio,camount,ct)
{
return sensf/cratio*camount*cob(g,ct);
}
function deltaBGI(g,bolus,sensf,idur)
{
return -bolus*sensf*(1-iob(g,idur)/100.0);
}
function deltaBG(g,sensf,cratio,camount,ct,bolus,idur)
{
return deltaBGI(g,bolus,sensf,idur)+deltaBGC(g,sensf,cratio,camount,ct);
}
//user parameters - carb ratio, sensitivity factor, insulin duration
var userdata={cratio:8.5,sensf:47.0,idur:3}
var uevent = [];
//user events - insulin and carbs time in min, bolus in units
//program loops through these events and adds BG results from each to reach final curve
//also plots insulin only and carbs only
//use time value to delay events, time in minutes
uevent[0] = {time:0.0,etype:"bolus",units:(5.29)};
uevent[1] = {time:0.0,etype:"carb",grams:45.0,ctype:"high"};
//uevent[2] = {time:150.0,etype:"bolus",units:5.29};
//uevent[3] = {time:150.0,etype:"carb",grams:45.0,ctype:"high"};
//uevent[2]={time:0.0,etype:"tempbasal",t1:0.0,t2:120,dbdt:(-2.0/(120.0))}
var simt = 5*60; //total simulation time in min from zero - need to add adaptive total time and adaptive n based on idur and event timing
//end user inputs
var n=75; //points in simulation
var dt=simt/n;
var simbgc = [];
var simbgi = [];
var simbg = [];
var predata =[];
for (i=0;i<n;i++) {
simbgc[i]=0.0;
simbgi[i]=0.0;
simbg[i]=0.0; }
for (j = 0; j < uevent.length; j++) {
for (i=0; i<n;i++) {
if(uevent[j].etype=="carb") {
simbgc[i] = simbgc[i]+deltaBGC(i*dt-uevent[j].time,userdata.sensf,userdata.cratio,uevent[j].grams,uevent[j].ctype)} else
if(uevent[j].etype=="bolus") {
simbgi[i] = simbgi[i]+deltaBGI(i*dt-uevent[j].time,uevent[j].units,userdata.sensf,userdata.idur) }
else
{
simbgi[i]=simbgi[i]+deltatempBGI((i*dt-uevent[j].time),uevent[j].dbdt,userdata.sensf,userdata.idur,uevent[j].t1,uevent[j].t2)}
}
}//end event loop
//graph
google.setOnLoadCallback(drawChart);
var predata = new google.visualization.DataTable();
predata.addColumn('number', 'Time'); // Implicit domain label col.
predata.addColumn('number', 'Carb Effect'); // Implicit series 1 data col.
predata.addColumn('number', 'Insulin Effect'); // Implicit series 1 data col.
predata.addColumn('number', 'Blood Sugar mg/dl'); // Implicit series 1 data col.
predata.addColumn({type:'string', role:'annotation'}); // annotation role col.
predata.addColumn({type:'string', role:'annotation'}); // annotation role col
for (i=0;i<n;i++) {
//for each time i add up the resultant carb and insulin effects on bg
simbg[i]=simbgc[i]+simbgi[i];
predata.addRow([(dt*i)+1,simbgc[i],simbgi[i],simbg[i],null,null]);
}
//annotation not really visible need to improve graphing package
//annotate event data
// for (i=0;i<uevent.length;i++) {
// if(uevent[i].etype=="bolus") {predata.setValue(Math.ceil(uevent[i].time/simt*n+1),4,(uevent[i].units).toString())+"U"} else
// predata.setValue(Math.ceil(uevent[i].time/simt*n+5),5,(uevent[i].grams).toString()+"g")
// }
function drawChart() {
//var data = google.visualization.arrayToDataTable(predata);
var options = {
title: 'GlucoDyn',
curveType: 'function',
legend: { position: 'bottom' },
hAxis: {
title: 'Time (min)'
},
vAxis: {
title: 'Change in BG mg/dl'
}
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(predata, options);
}
</script>
<div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>