forked from freesurfer/freesurfer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_optimal_pump_calibration.m
229 lines (200 loc) · 9.41 KB
/
compute_optimal_pump_calibration.m
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
BG0 = [143.5 147.5 155.5 166 216 222.5 240 248.5 264.5 294.5 309.5 327.5 332 330.5 326 325 326 326.5 324 310.5 312.5 306.5 296 293.5 280.5 273.5];
carbs = 50;
bolus = 1.65;
min_per_hour = 60 ;
dt = 5;
time = [-10:dt:240];
time = time ./ min_per_hour ;
dt = dt ./ min_per_hour ;
basal1 = .95*dt ;
basal2 = .75*dt ;
BG = zeros(size(time)) ;
BG(1:2:end) = BG0 ;
for i=2:2:length(BG)-1
BG(i) = (BG(i-1) + BG(i+1))/2;
end
pct_parm_change = 0.5 ;
basal_time = -10:dt:time(end) ;
basal = ones(size(basal_time))*basal1 ;
[foo, basal_change_index] = min(abs(basal_time*60-30));
[foo, bolus_index] = min(abs(basal_time*60-5));
basal(basal_change_index:end) = basal(basal_change_index:end) *basal2/basal1;
total_insulin = basal ;
total_insulin(bolus_index) = total_insulin(bolus_index)+bolus ;
bolus_schedule = zeros(size(time)) ;
[foo, bolus_index] = min(abs(time*60-5));
bolus_schedule(bolus_index) = bolus ;
[foo,time_start_index] = min(abs(basal_time-time(1)));
[foo,time_end_index] = min(abs(basal_time-time(end)));
pump_basal = basal ;
% given at 9:01 AM.
% food eaten at 8:56
estimateIOBTable;
SI_best = 50 ; % insulin sensitivity, BG/insulin
b1_best = basal1 ;
b2_best = basal2 ;
C_best = 10 ; % carb ratio, carbs/insulin
SC_best = SI_best / C_best ;
k_carbs_best = .005 ;
bci_best = basal_change_index ;
k_insulin_best = .025 ;
basal = ones(size(basal_time))*b1_best ;
basal(basal_change_index:end) = basal(basal_change_index:end) *b2_best/b1_best;
[bact, babs] = compute_total_insulin_timecourse(basal, 60*basal_time, k_insulin_best,.001,k_insulin_best);
basal_absorbed = babs(time_start_index:time_end_index) ;
[pump_basal_active_long, pump_basal_absorbed_long] = compute_total_insulin_timecourse(pump_basal, 60*basal_time, k_insulin_best,.001,k_insulin_best);
pump_basal_absorbed = pump_basal_absorbed_long(time_start_index:time_end_index);
basal_difference = pump_basal_absorbed - basal_absorbed ;
[active_insulin, bolus_absorbed] = compute_total_insulin_timecourse(bolus_schedule, 60*time, k_insulin_best,.001, k_insulin_best);
carbs_metabolized = predict_carb_uptake(carbs, .0, k_carbs_best, time) ;
rms_best = compute_bg_error(BG(1),BG, bolus_absorbed, carbs_metabolized, time, SI_best, basal_difference, SC_best);
M0 = BG(1) ;
disp(sprintf('**** new optimum %2.3f found at B1=%2.3f, B2=%2.3f k_carbs=%2.3f, k_insulin=%2.4f, SC=%2.1f, SI=%2.0f, C=%2.1f *****', rms_best,b1_best/dt, b2_best/dt,k_carbs_best,k_insulin_best,SC_best,SI_best, C_best));
rms_best = sqrt((length(time) * 400^2)/length(time)) ;
k_carb_range = 0.005:0.001:0.04;
k_carbs_table = zeros(length(k_carb_range), length(time)) ;
ci = 1 ;
for k=k_carb_range
k_carbs_table(ci,:) = predict_carb_uptake(carbs, .0, k, time) ;
ci = ci+1 ;
end
b1_range = .85*dt:.025*dt:1.1*dt ;
b2_range = .6*dt:.025*dt:.85*dt ;
bci_range = (basal_change_index-(round(1/dt)):round(0.5/dt):(basal_change_index+(round(1/dt))));
k_insulin_range=.015:0.001:.04;
C_range = 15:1:32 ;
SI_range = 50:1:100 ;
SC_range = 1.5:6;
Clen = length(C_range) ;
SIlen = length(SI_range) ;
rms_table = zeros(Clen, SIlen);
pool = parpool(10) ;
for bci=bci_range
for b1=b1_range
for b2=b2_range
disp(sprintf('searching BCI=%2.1f (%d), basal1 = %2.3f, basal2 = %2.3f', dt*(bci-basal_change_index), bci,b1/dt, b2/dt)) ;
basal = ones(size(basal_time))*b1 ;
% basal(basal_change_index:end) = basal(basal_change_index:end) *b2/b1;
basal(bci:end) = basal(bci:end) *b2/b1;
for k_insulin=k_insulin_range
[active_insulin, bolus_absorbed] = compute_total_insulin_timecourse(bolus_schedule, 60*time, k_insulin,.001,k_insulin);
if (active_insulin(end) > 0.1*bolus)
continue
end
[bact, babs] = compute_total_insulin_timecourse(basal, 60*basal_time, k_insulin,.001,k_insulin);
basal_absorbed = babs(time_start_index:time_end_index) ;
[pump_basal_active_long, pump_basal_absorbed_long] = compute_total_insulin_timecourse(pump_basal, 60*basal_time, k_insulin,.001,k_insulin);
pump_basal_absorbed = pump_basal_absorbed_long(time_start_index:time_end_index);
basal_difference = pump_basal_absorbed - basal_absorbed ;
ci = 1 ;
for k_carbs=k_carb_range
% carbs_metabolized = predict_carb_uptake(carbs, .0, k_carbs, time) ;
carbs_metabolized = k_carbs_table(ci,:) ;
ci = ci + 1 ;
if (carbs_metabolized(end) < .9*carbs)
continue
end
parfor CRi=1:Clen
C = C_range(CRi) ;
for SIi=1:SIlen
SI = SI_range(SIi) ;
SC = SI / C ;
if (SC >= SC_range(1) & SC < SC_range(end))
rms = compute_bg_error(BG(1), BG, bolus_absorbed, carbs_metabolized, time, SI, basal_difference, SC);
else
rms = sqrt((length(time) * 400^2)/length(time)) ;
end
rms_table(CRi,SIi) = rms ;
end
end
[test_min_rms,index] = min(rms_table(:)) ;
if (test_min_rms < rms_best)
rms_best = test_min_rms ;
CRi = mod(index,size(rms_table,1)) ;
SIi = ceil(index/size(rms_table,1));
C = C_range(CRi) ;
SI = SI_range(SIi) ;
k_insulin_best = k_insulin ;
k_carbs_best = k_carbs ;
b1_best = b1 ;
b2_best = b2 ;
C_best = C ;
SI_best = SI ;
SC_best = SI_best/C_best ;
bci_best = bci ;
[pump_basal_active_long, pump_basal_absorbed_long] = compute_total_insulin_timecourse(pump_basal, 60*basal_time, k_insulin_best,.001,k_insulin_best);
pump_basal_absorbed = pump_basal_absorbed_long(time_start_index:time_end_index);
basal = ones(size(basal_time))*b1_best ;
basal(bci_best:end) = basal(bci_best:end) *b2_best/b1_best;
[bact, babs] = compute_total_insulin_timecourse(basal, 60*basal_time, k_insulin_best,.001, k_insulin_best);
basal_absorbed = babs(time_start_index:time_end_index) ;
basal_difference = pump_basal_absorbed - basal_absorbed ;
[active_insulin, bolus_absorbed] = compute_total_insulin_timecourse(bolus_schedule, 60*time, k_insulin_best,.001,k_insulin_best);
disp(sprintf('**** new optimum %2.3f found at B1=%2.3f, B2=%2.3f k_carbs=%2.3f, k_insulin=%2.4f, SC=%2.1f, SI=%2.1f, C=%2.2f, BCI=%2.1f *****', rms_best, b1_best/dt, b2_best/dt, k_carbs_best, k_insulin_best, SC_best,SI_best, C_best,dt*(bci_best-basal_change_index)));
[rms, BG_p] = compute_bg_error(BG(1),BG, bolus_absorbed, carbs_metabolized, time, SI_best, basal_difference, SC_best);
figure(1) ;
plot(time*60, BG, 'r') ;
title(sprintf('B1=%2.3f, B2=%2.3f, k=%2.3f, RMS=%2.1f', b1_best/dt, b2_best/dt, k_carbs_best, rms_best)) ;
hold on ;
plot(time*60, BG_p, 'g') ;
ch = get(gca, 'children') ;
set(ch, 'linewidth', 6) ;
hold off;
drawnow ;
figure(2) ;
clf ;
[ax, h1, h2] = plotyy(60*time, bolus_absorbed, 60*time, carbs_metabolized) ;
legend('insulin', 'carbs')
xlabel('time (min)', 'fontsize', 16, 'fontweight', 'bold')
set(ax(1), 'fontsize', 16, 'fontweight', 'bold')
set(ax(2), 'fontsize', 16, 'fontweight', 'bold')
axes(ax(1)) ; ylabel('insulin absorbed (units)') ;
axes(ax(2)) ; ylabel('carbs metabolized (g)') ;
set(get(ax(1), 'children'), 'linewidth', 6) ;
set(get(ax(2), 'children'), 'linewidth', 6) ;
set(ax(1), 'ylim', [0 bolus]) ;
drawnow ;
end
end
end
basal = ones(size(basal_time))*b1_best ;
basal(bci_best:end) = basal(bci_best:end) *b2_best/b1_best;
[bact, babs] = compute_total_insulin_timecourse(basal, 60*basal_time, k_insulin_best,.001, k_insulin_best);
basal_absorbed = babs(time_start_index:time_end_index) ;
[pump_basal_active_long, pump_basal_absorbed_long] = compute_total_insulin_timecourse(pump_basal, 60*basal_time, k_insulin_best,.001,k_insulin_best);
pump_basal_absorbed = pump_basal_absorbed_long(time_start_index:time_end_index);
basal_difference = pump_basal_absorbed - basal_absorbed ;
[active_insulin, bolus_absorbed] = compute_total_insulin_timecourse(bolus_schedule, 60*time, k_insulin_best,.001,k_insulin_best);
carbs_metabolized = predict_carb_uptake(carbs, .0, k_carbs_best, time) ;
[rms, BG_p] = compute_bg_error(BG(1),BG, bolus_absorbed, carbs_metabolized, time, SI_best, basal_difference, SC_best);
figure(1) ;
clf ;
plot(time*60, BG, 'r') ;
title(sprintf('B1=%2.3f, B2=%2.3f, k=%2.3f, RMS=%2.1f', b1_best/dt, b2_best/dt, k_carbs_best, rms_best)) ;
hold on ;
plot(time*60, BG_p, 'g') ;
ch = get(gca, 'children') ;
set(ch, 'linewidth', 6) ;
legend('measured BG', 'model BG')
xlabel('time (min)', 'fontsize', 16, 'fontweight', 'bold')
ylabel('BG (mg/dL)', 'fontsize', 16, 'fontweight', 'bold')
set(gca, 'fontsize', 16, 'fontweight', 'bold')
hold off;
figure(2) ;
clf ;
[ax, h1, h2] = plotyy(time*60, bolus_absorbed, time*60, carbs_metabolized) ;
legend('insulin', 'carbs')
xlabel('time (min)', 'fontsize', 16, 'fontweight', 'bold')
set(ax(1), 'fontsize', 16, 'fontweight', 'bold')
set(ax(2), 'fontsize', 16, 'fontweight', 'bold')
axes(ax(1)) ; ylabel('insulin absorbed (units)') ;
axes(ax(2)) ; ylabel('carbs metabolized (g)') ;
set(get(ax(1), 'children'), 'linewidth', 6) ;
set(get(ax(2), 'children'), 'linewidth', 6) ;
set(ax(1), 'ylim', [0 bolus]) ;
drawnow ;
end
end
end
delete(pool) ;
disp(sprintf('**** global optimum %2.3f found at B1=%2.3f, B2=%2.3f k_carbs=%2.3f, k_insulin=%2.4f, SC=%2.1f, SI=%2.1f, C=%2.2f, BCI=%2.1f hours *****', rms_best, b1_best/dt, b2_best/dt, k_carbs_best, k_insulin_best, SC_best,SI_best, C_best,dt*(bci_best-basal_change_index)));