[I don't think this affects Loop:master in practice as it only calls this function on new data points]
Consider the following data, when looking at a 4 hour interval, the insulin effect only has data from 21:10 to 22:20 (14 samples), the glucoseData exists from 18:20 to 22:20, but insulinCounteractionEffects gets calculated from 18:20 until 19:30 .
I would have expected the insulinCounteractionEffects data to be filled in from 21:20 to 22:20, aligned with the interval of the insulin effect data.
Afaict there is a bug in GlucoseKit counteractionEffects which assumes that the time series are aligned. Due to missing pump data / app error (in this case simulator) this isn't always the case.
Now I'm not sure how to best handle this situation, but I assume some kind of alignment between the time series would be best. Some kind of piece-wise linear interpolation might be best. But it is way too late today to think straight about this.
Example in pseudo code:
for glucose in self {
...
for effect in effects {
if startGlucose.startDate.timeIntervalSince(effect.startDate) ~ (closest) {
< pick startEffect >
}
if endGlucose.startDate.timeIntervalSince(effect.startDate) ~ (closest) {
< pick endEffect >
}
}
...
let effectChange = endEffect - startEffect
// the effect and glucose interval can be different, so we can't directly substract the glucose and effect change
let effectVelocity = effectChange / endEffect.timeintervalSince(startEffect)
let glucoseVelocity = glucoseChange / glucoseTimeInterval
let averageVelocity = glucoseVelocity - effectVelocity
...
}
Example data to reproduce the bug:
insulinEffect
2020-04-22 00:20:51.032128+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:10:00 +0000, 2020-04-21 21:10:00 +0000, 0.0
2020-04-22 00:20:51.032252+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:15:00 +0000, 2020-04-21 21:15:00 +0000, 0.0
2020-04-22 00:20:51.032473+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:20:00 +0000, 2020-04-21 21:20:00 +0000, 0.0
2020-04-22 00:20:51.032969+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:25:00 +0000, 2020-04-21 21:25:00 +0000, -0.081685128695172
2020-04-22 00:20:51.033191+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:30:00 +0000, 2020-04-21 21:30:00 +0000, -0.36103537027385335
2020-04-22 00:20:51.033422+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:35:00 +0000, 2020-04-21 21:35:00 +0000, -1.5976956099214026
2020-04-22 00:20:51.033641+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:40:00 +0000, 2020-04-21 21:40:00 +0000, -5.891232659531845
2020-04-22 00:20:51.033716+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:45:00 +0000, 2020-04-21 21:45:00 +0000, -12.859588711661342
2020-04-22 00:20:51.033933+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:50:00 +0000, 2020-04-21 21:50:00 +0000, -22.011075161695867
2020-04-22 00:20:51.034114+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:55:00 +0000, 2020-04-21 21:55:00 +0000, -32.90420668651475
2020-04-22 00:20:51.034330+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:00:00 +0000, 2020-04-21 22:00:00 +0000, -45.154685239968984
2020-04-22 00:20:51.034563+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:05:00 +0000, 2020-04-21 22:05:00 +0000, -58.434309126591906
2020-04-22 00:20:51.034646+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:10:00 +0000, 2020-04-21 22:10:00 +0000, -72.46940686250046
2020-04-22 00:20:51.034750+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:15:00 +0000, 2020-04-21 22:15:00 +0000, -87.01067143023353
2020-04-22 00:20:51.034830+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:20:00 +0000, 2020-04-21 22:20:00 +0000, -101.84457648262465
2020-04-22 00:20:51.034907+0200 Loop[6132:162087] [AutoAdjust]
glucoseSamples
2020-04-22 00:20:51.048520+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:20:59 +0000, 2020-04-21 18:20:59 +0000, 107.09608722918229
2020-04-22 00:20:51.048892+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:25:59 +0000, 2020-04-21 18:25:59 +0000, 103.84534813220924
2020-04-22 00:20:51.049660+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:30:59 +0000, 2020-04-21 18:30:59 +0000, 100.7175556903945
2020-04-22 00:20:51.050938+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:35:59 +0000, 2020-04-21 18:35:59 +0000, 97.73651429283414
2020-04-22 00:20:51.051191+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:40:59 +0000, 2020-04-21 18:40:59 +0000, 94.92491146456645
2020-04-22 00:20:51.051282+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:45:59 +0000, 2020-04-21 18:45:59 +0000, 92.30414520080494
2020-04-22 00:20:51.051355+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:50:59 +0000, 2020-04-21 18:50:59 +0000, 89.89416111528047
2020-04-22 00:20:51.051473+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:55:59 +0000, 2020-04-21 18:55:59 +0000, 87.7133006420705
2020-04-22 00:20:51.051674+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:00:59 +0000, 2020-04-21 19:00:59 +0000, 85.77816144621713
2020-04-22 00:20:51.051789+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:05:59 +0000, 2020-04-21 19:05:59 +0000, 84.10347110546851
2020-04-22 00:20:51.051863+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:10:59 +0000, 2020-04-21 19:10:59 +0000, 82.70197502452413
2020-04-22 00:20:51.051969+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:15:59 +0000, 2020-04-21 19:15:59 +0000, 81.58433943480674
2020-04-22 00:20:51.052233+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:20:59 +0000, 2020-04-21 19:20:59 +0000, 80.7590702180002
2020-04-22 00:20:51.052360+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:25:59 +0000, 2020-04-21 19:25:59 +0000, 80.23244817115611
2020-04-22 00:20:51.052755+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:30:59 +0000, 2020-04-21 19:30:59 +0000, 80.00848120603371
2020-04-22 00:20:51.052864+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:35:59 +0000, 2020-04-21 19:35:59 +0000, 80.08887384647265
2020-04-22 00:20:51.052964+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:40:59 +0000, 2020-04-21 19:40:59 +0000, 80.47301425593687
2020-04-22 00:20:51.053067+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:45:59 +0000, 2020-04-21 19:45:59 +0000, 81.15797889396003
2020-04-22 00:20:51.053139+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:50:59 +0000, 2020-04-21 19:50:59 +0000, 82.13855476605346
2020-04-22 00:20:51.053226+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:55:59 +0000, 2020-04-21 19:55:59 +0000, 83.40727909774327
2020-04-22 00:20:51.053301+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:00:59 +0000, 2020-04-21 20:00:59 +0000, 84.95449613078776
2020-04-22 00:20:51.053431+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:05:59 +0000, 2020-04-21 20:05:59 +0000, 86.76843060933129
2020-04-22 00:20:51.053585+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:10:59 +0000, 2020-04-21 20:10:59 +0000, 88.8352773967074
2020-04-22 00:20:51.053655+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:15:59 +0000, 2020-04-21 20:15:59 +0000, 91.13930654086924
2020-04-22 00:20:51.053751+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:20:59 +0000, 2020-04-21 20:20:59 +0000, 93.66298298881831
2020-04-22 00:20:51.053820+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:25:59 +0000, 2020-04-21 20:25:59 +0000, 96.3871000389473
2020-04-22 00:20:51.054014+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:30:59 +0000, 2020-04-21 20:30:59 +0000, 99.2909255156396
2020-04-22 00:20:51.054430+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:35:59 +0000, 2020-04-21 20:35:59 +0000, 102.35235955363797
2020-04-22 00:20:51.054636+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:40:59 +0000, 2020-04-21 20:40:59 +0000, 105.54810279137082
2020-04-22 00:20:51.055133+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:45:59 +0000, 2020-04-21 20:45:59 +0000, 108.8538336931544
2020-04-22 00:20:51.055832+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:50:59 +0000, 2020-04-21 20:50:59 +0000, 112.24439365077338
2020-04-22 00:20:51.056331+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 20:55:59 +0000, 2020-04-21 20:55:59 +0000, 115.69397845567201
2020-04-22 00:20:51.056705+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:00:59 +0000, 2020-04-21 21:00:59 +0000, 119.17633468457099
2020-04-22 00:20:51.057097+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:10:42 +0000, 2020-04-21 21:10:42 +0000, 125.93279155416747
2020-04-22 00:20:51.057210+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:21:32 +0000, 2020-04-21 21:21:32 +0000, 133.26299984489768
2020-04-22 00:20:51.057297+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:26:32 +0000, 2020-04-21 21:26:32 +0000, 136.50155505985032
2020-04-22 00:20:51.057447+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:31:32 +0000, 2020-04-21 21:31:32 +0000, 139.61453004765505
2020-04-22 00:20:51.057529+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:36:32 +0000, 2020-04-21 21:36:32 +0000, 142.57822197066272
2020-04-22 00:20:51.057604+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:41:32 +0000, 2020-04-21 21:41:32 +0000, 145.37007605422457
2020-04-22 00:20:51.057702+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:46:32 +0000, 2020-04-21 21:46:32 +0000, 147.96884779131102
2020-04-22 00:20:51.057765+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:51:32 +0000, 2020-04-21 21:51:32 +0000, 150.35475920832462
2020-04-22 00:20:51.057859+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 21:56:32 +0000, 2020-04-21 21:56:32 +0000, 152.5096454760867
2020-04-22 00:20:51.057934+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:10:38 +0000, 2020-04-21 22:10:38 +0000, 157.20984533356892
2020-04-22 00:20:51.058019+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 22:20:50 +0000, 2020-04-21 22:20:50 +0000, 159.2202392371572
insulinCounteractionEffects
2020-04-22 00:20:51.034995+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:20:59 +0000, 2020-04-21 18:25:59 +0000, -0.6501478193946097, -195.04434581838294
2020-04-22 00:20:51.035092+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:25:59 +0000, 2020-04-21 18:30:59 +0000, -0.6255584883629467, -187.66754650888402
2020-04-22 00:20:51.035176+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:30:59 +0000, 2020-04-21 18:35:59 +0000, -0.5798712537730377, -173.96137613191132
2020-04-22 00:20:51.035259+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:35:59 +0000, 2020-04-21 18:40:59 +0000, -0.5064505173378027, -151.9351552013408
2020-04-22 00:20:51.035342+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:40:59 +0000, 2020-04-21 18:45:59 +0000, -0.2768212048227921, -83.04636144683764
2020-04-22 00:20:51.035442+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:45:59 +0000, 2020-04-21 18:50:59 +0000, 0.3767105928171943, 113.0131778451583
2020-04-22 00:20:51.035531+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:50:59 +0000, 2020-04-21 18:55:59 +0000, 0.9574991157839042, 287.2497347351713
2020-04-22 00:20:51.035649+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 18:55:59 +0000, 2020-04-21 19:00:59 +0000, 1.4432694508362316, 432.98083525086946
2020-04-22 00:20:51.036008+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:00:59 +0000, 2020-04-21 19:05:59 +0000, 1.8436882368140532, 553.1064710442159
2020-04-22 00:20:51.036113+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:05:59 +0000, 2020-04-21 19:10:59 +0000, 2.1697964945019703, 650.9389483505911
2020-04-22 00:20:51.036223+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:10:59 +0000, 2020-04-21 19:15:59 +0000, 2.432397659381105, 729.7192978143315
2020-04-22 00:20:51.036304+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:15:59 +0000, 2020-04-21 19:20:59 +0000, 2.641965703820405, 792.5897111461215
2020-04-22 00:20:51.036404+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:20:59 +0000, 2020-04-21 19:25:59 +0000, 2.8029285041777934, 840.8785512533381
2020-04-22 00:20:51.036483+0200 Loop[6132:162087] [AutoAdjust] * 2020-04-21 19:25:59 +0000, 2020-04-21 19:30:59 +0000, 2.921987617453743, 876.5962852361229