-
Notifications
You must be signed in to change notification settings - Fork 17
/
calibration.js
918 lines (734 loc) · 29.6 KB
/
calibration.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
// Rule 1 - Clear calibration records upon CGM Sensor Change/Insert
// Rule 2 - Don't allow any BG calibrations or take in any new calibrations
// within 15 minutes of last sensor insert
// Rule 3 - Only use Single Point Calibration for 1st SENSOR_STABLE hours since Sensor insert
// Rule 4 - Do not use LSR until we have 3 or more calibration points.
// Use SinglePoint calibration only for less than MIN_LSR_PAIRS calibration points.
// SinglePoint simply uses the latest calibration record and assumes
// the intercept is 0.
// Rule 5 - Drop back to SinglePoint calibration if slope is out of bounds
// (>MAXSLOPE or <MINSLOPE)
// only applies to expired calibration
// Rule 6 - TODO: Drop back to SinglePoint calibration if intercept is out of bounds
// (> minimum unfiltered value in calibration record set or
// < - minimum unfiltered value in calibration record set)
const Debug = require('debug');
const log = Debug('calibration:log');
const error = Debug('calibration:error');
const debug = Debug('calibration:debug');
const moment = require('moment');
const _ = require('lodash');
const constants = require('./constants');
const stats = require('./calcStats');
const xDripAPS = require('./xDripAPS')();
module.exports = {};
const calibrationExports = module.exports;
const MAXSLOPE = 12500;
const MINSLOPE = 450;
const SENSOR_STABLE = 12; // hours
const SENSOR_WARM = 2; // hours
const leftPadString = (str, len) => ' '.repeat(Math.max(0, len - str.toString().length)) + str;
const calcGlucose = (sgv, calibration) => {
let glucose = Math.round((sgv.unfiltered - calibration.intercept) / calibration.slope);
// If BG is below 40, set it to 39 so it's displayed correctly in NS
glucose = glucose < 40 ? 39 : glucose;
return glucose;
};
calibrationExports.calcGlucose = calcGlucose;
// calibrationPairs has three values for each array element:
// glucose => the "true" glucose value for the pair
// unfiltered => the sensor's unfiltered glucose value for the pair
// readDateMills => the sensor's read date for the pair in ms since 1/1/1970 00:00
const lsrCalibration = (calibrationPairs) => {
let sumX = 0;
let sumY = 0;
let meanX = 0;
let meanY = 0;
let stddevX = 0;
let stddevY = 0;
let sumXY = 0;
let sumXSq = 0;
let sumYSq = 0;
let sumSqDiffX = 0;
let sumSqDiffY = 0;
let yError = 0;
let slopeError = 0;
const n = calibrationPairs.length;
const tarr = [];
const multipliers = [];
const returnVal = {
slope: 0,
intercept: 0,
calibrationType: 'LeastSquaresRegression',
};
for (let i = 0; i < n; i += 1) {
sumX += calibrationPairs[i].glucose;
sumY += calibrationPairs[i].unfiltered;
}
meanX = sumX / n;
meanY = sumY / n;
for (let i = 0; i < n; i += 1) {
let diff = calibrationPairs[i].glucose - meanX;
sumSqDiffX += diff * diff;
diff = calibrationPairs[i].unfiltered - meanY;
sumSqDiffY += diff * diff;
}
stddevX = Math.sqrt(sumSqDiffX / (n - 1));
stddevY = Math.sqrt(sumSqDiffY / (n - 1));
const firstDate = calibrationPairs[0].readDateMills;
for (let i = 0; i < n; i += 1) {
tarr.push(calibrationPairs[i].readDateMills - firstDate);
}
let multiplier = 1;
for (let i = 0; i < n; i += 1) {
if (i !== 0) {
multiplier = 1 + tarr[i - 1] / (tarr[n - 1] * 2);
// boundary check
if ((multiplier < 1) || (multiplier > 2)) {
multiplier = 1;
}
}
multipliers.push(multiplier);
sumXY = (sumXY + calibrationPairs[i].glucose * calibrationPairs[i].unfiltered) * multiplier;
sumXSq = (sumXSq + calibrationPairs[i].glucose * calibrationPairs[i].glucose) * multiplier;
sumYSq = (sumYSq
+ calibrationPairs[i].unfiltered * calibrationPairs[i].unfiltered) * multiplier;
}
const denominator = Math.sqrt(((n * sumXSq - sumX * sumX) * (n * sumYSq - sumY * sumY)));
if ((denominator === 0) || (stddevX === 0)) {
return null;
}
const r = (n * sumXY - sumX * sumY) / denominator;
returnVal.slope = r * stddevY / stddevX;
returnVal.intercept = meanY - returnVal.slope * meanX;
// calculate error
let varSum = 0;
for (let j = 0; j < n; j += 1) {
const varVal = (
calibrationPairs[j].unfiltered - returnVal.intercept
- returnVal.slope * calibrationPairs[j].glucose
);
debug(`LSR Cal - record ${j},`
+ ` ${moment(calibrationPairs[j].readDateMills).format('ddd YYYY-MM-DDTHH:MM:SSG\\M\\TZZ')},`
+ ` unfiltered: ${leftPadString(Math.round(calibrationPairs[j].unfiltered), 6)},`
+ ` sgv: ${leftPadString(calibrationPairs[j].glucose, 3)},`
+ ` calculated: ${leftPadString(calcGlucose(calibrationPairs[j], returnVal), 3)},`
+ ` multiplier: ${leftPadString(multipliers[j].toFixed(3), 5)}`);
varSum += varVal * varVal;
}
const delta = n * sumXSq - sumX * sumX;
const vari = 1.0 / (n - 2.0) * varSum;
yError = Math.sqrt(vari / delta * sumXSq);
slopeError = Math.sqrt(n / delta * vari);
debug(`LSR Calibration yError: ${yError}, slopeError: ${slopeError}`);
return returnVal;
};
const singlePointCalibration = (calibrationPairs) => {
const returnVal = {
slope: 0,
intercept: 0,
calibrationType: 'SinglePoint',
};
const x = calibrationPairs[calibrationPairs.length - 1].glucose;
const y = calibrationPairs[calibrationPairs.length - 1].unfiltered;
returnVal.intercept = 0;
returnVal.slope = y / x;
return returnVal;
};
const calculateTxmitterCalibration = (
options, lastCal, lastTxmitterCalTime, latestBgCheckTime, sensorInsert, glucoseHist, currSGV,
) => {
log('Calculating transmitter calibration');
// set it to a high number so we upload a new cal
// if we don't have a previous calibration
// Do not calculate a new calibration value
// if we don't have a valid calibrated glucose reading
if (currSGV.glucose > constants.MAX_CAL_SGV
|| currSGV.glucose < constants.MIN_CAL_SGV) {
log(`Current glucose out of range to calibrate: ${currSGV.glucose}`);
return null;
}
let rawDelta = Math.abs(currSGV.unfiltered - currSGV.filtered) * 1.0;
if (rawDelta / Math.min(currSGV.unfiltered, currSGV.filtered) > 0.1) {
log('Current glucose filtered more than 10% apart from unfiltered');
log(`Out of range to calibrate: ${currSGV.unfiltered} ${currSGV.filtered}`);
return null;
}
let calErr = 100;
let calValue;
let i;
let calReturn = null;
const calPairs = [];
if (lastCal) {
calValue = calcGlucose(currSGV, lastCal);
calErr = Math.abs(calValue - currSGV.glucose);
log(
`Current CGM calculated calibration error: ${Math.round(calErr * 10) / 10} `
+ `calibrated value: ${Math.round(calValue * 10) / 10} `
+ `slope: ${Math.round(lastCal.slope * 10) / 10} `
+ `intercept: ${Math.round(lastCal.intercept * 10) / 10}`,
);
}
// Check if we need a calibration
if (!lastCal || (calErr > 5) || (lastCal.type === 'Unity') || (lastCal.type === 'SinglePoint')) {
calPairs.push(currSGV);
// Suitable values need to be:
// 1. less than 300 mg/dl
// 2. greater than 80 mg/dl
// 3. calibrated via Txmitter, not Lookout
// 4. 12 minutes after the last Txmitter calibration time
// (it takes up to 2 readings to reflect calibration updates)
// 5. After the latest sensorInsert (ignore sensorInsert if we didn't get one)
// 6. Unfiltered and Filtered not more than 10% apart
for (i = (glucoseHist.length - 1); ((i >= 0) && (calPairs.length < 10)); i -= 1) {
// Only use up to 10 of the most recent suitable readings
const sgv = glucoseHist[i];
rawDelta = Math.abs(sgv.unfiltered - sgv.filtered) * 1.0;
if (('unfiltered' in sgv)
&& (sgv.readDateMills > (lastTxmitterCalTime + 12 * 60 * 1000))
&& (sgv.glucose < constants.MAX_CAL_SGV)
&& (sgv.glucose > constants.MIN_CAL_SGV)
&& sgv.g5calibrated
&& (!sensorInsert || (sgv.readDateMills > sensorInsert.valueOf()))
&& (rawDelta / Math.min(sgv.unfiltered, sgv.filtered) < 0.1)) {
calPairs.unshift(sgv);
}
}
// If we have at least MIN_LSR_PAIRS good pairs and we are off by more than 5
// OR we have at least 8 and our current cal error is > 1
if (((calErr > 5) && calPairs.length > options.min_lsr_pairs)
|| ((calErr > 1) && (calPairs.length > 8))) {
const calResult = lsrCalibration(calPairs);
if (!calResult) {
error('CGM calculated calibration calculated denominator of zero');
return null;
}
log(`CGM lsrCalibration: numPoints=${calPairs.length}, slope=${calResult.slope}, intercept=${calResult.intercept}`);
if ((calResult.slope > MAXSLOPE) || (calResult.slope < MINSLOPE)) {
// wait until the next opportunity
error(`CGM calculated calibration slope out of range: ${calResult.slope}`);
return null;
}
calReturn = {
date: currSGV.readDateMills,
scale: 1,
intercept: calResult.intercept,
slope: calResult.slope,
type: calResult.calibrationType,
};
} else if (calErr > 5) {
log(`CGM calculated calibration update needed, but only ${calPairs.length} suitable glucose pairs found.`);
return null;
}
}
if (calReturn) {
log(`Calculated new CGM calculated calibration with ${calReturn.type} due to ${calPairs.length} calibration pairs:\n%O`, calReturn);
} else if (lastCal && latestBgCheckTime && (latestBgCheckTime.diff(moment(lastCal.date).subtract(6, 'minutes')) > 0)) {
log('BG Check occurred, but no CGM calculated calibration update needed. Setting calibration date to be after latest BG check time.');
// this disables the CGM calculated calibration until we have
// at least 2 readings showing it doesn't need an update (6 minutes).
calReturn = _.cloneDeep(lastCal);
calReturn.date = Date.now();
} else {
log('No CGM calculated calibration update needed.');
}
return calReturn;
};
calibrationExports.calculateTxmitterCalibration = calculateTxmitterCalibration;
const saveExpiredCal = async (storage, newCal) => {
await storage.setItem('expiredCal', newCal)
.catch(() => {
error('Unable to store new NS Calibration');
});
};
calibrationExports.saveExpiredCal = saveExpiredCal;
const interpolateUnfiltered = (SGVBefore, SGVAfter, valueTime) => {
const totalTime = SGVAfter.readDateMills - SGVBefore.readDateMills;
const totalDelta = SGVAfter.unfiltered - SGVBefore.unfiltered;
const totalFilteredDelta = SGVAfter.filtered - SGVBefore.filtered;
const fractionTime = (valueTime.valueOf() - SGVBefore.readDateMills) / totalTime;
debug(`SGVBefore Time: ${SGVBefore.readDateMills} Unfiltered: ${SGVBefore.unfiltered} Filtered: ${SGVBefore.filtered}`);
debug(` SGVAfter Time: ${SGVAfter.readDateMills} Unfiltered: ${SGVAfter.unfiltered} Filtered: ${SGVAfter.filtered}`);
if (totalTime > 12 * 60000) {
debug(`Total time exceeds 12 minutes: ${totalTime}ms`);
debug('Not interpolating unfiltered values.');
return null;
}
const returnVal = {
unfiltered: totalDelta * fractionTime + SGVBefore.unfiltered,
filtered: totalFilteredDelta * fractionTime + SGVBefore.filtered,
};
debug(` BGCheck Time: ${valueTime.valueOf()}`);
debug(` Unfilter: ${Math.round(returnVal.unfiltered * 1000) / 1000}`);
debug(` Filtered: ${Math.round(returnVal.filtered * 1000) / 1000}`);
debug(` totalTime: ${totalTime} fractionTime: ${Math.round(fractionTime * 100) / 100}`);
debug(` totalDelta: ${Math.round(totalDelta * 1000) / 1000} fractionDelta: ${Math.round(fractionTime * totalDelta)}`);
return returnVal;
};
const getUnfilteredFromNS = async (valueTime) => {
let NSSGVs = null;
const timeStart = moment(valueTime.valueOf()).subtract(11, 'minutes');
const timeEnd = moment(valueTime.valueOf()).add(11, 'minutes');
let SGVBefore = null;
let SGVBeforeTime = null;
let SGVAfter = null;
let SGVAfterTime = null;
log(`Retrieving raw glucose value from NS for ${valueTime.format()}`);
// Get NS SGV immediately before BG Check
NSSGVs = await xDripAPS.SGVsBetween(timeStart, timeEnd, 5)
.catch((err) => {
error(`Unable to get NS SGVs to match unfiltered with BG Check: ${err}`);
});
if (!NSSGVs) {
NSSGVs = [];
}
for (let i = 0; i < NSSGVs.length; i += 1) {
NSSGVs[i].dateMills = moment(NSSGVs[i].date).valueOf();
}
NSSGVs = _.sortBy(NSSGVs, ['dateMills']);
for (let i = 0; i < (NSSGVs.length - 1); i += 1) {
// Is the next SGV after valueTime
// and the current SGV is before valueTime
SGVBeforeTime = NSSGVs[i].dateMills;
SGVAfterTime = NSSGVs[i + 1].dateMills;
if ((SGVBeforeTime < valueTime.valueOf()) && (SGVAfterTime > valueTime.valueOf())) {
SGVBefore = NSSGVs[i];
SGVAfter = NSSGVs[i + 1];
break;
}
}
if (SGVBefore && SGVAfter) {
return interpolateUnfiltered(
xDripAPS.convertEntryToxDrip(SGVBefore), xDripAPS.convertEntryToxDrip(SGVAfter), valueTime,
);
}
debug(`Unable to find bounding SGVs for BG Check at ${valueTime.format()}`);
return null;
};
const getUnfiltered = async (valueTime, glucoseHist, sgv) => {
const rigSGVs = _.map(glucoseHist, value => ({
readDateMills: value.readDateMills,
unfiltered: value.unfiltered,
filtered: value.filtered,
}));
if (rigSGVs && (rigSGVs.length > 1)) {
let SGVBefore = null;
let SGVBeforeTime = null;
let SGVAfter = null;
let SGVAfterTime = null;
if (sgv) {
// if we got an sgv argument
// it is the latest sgv that hasn't
// been appended to the storage array
// yet
rigSGVs.push(sgv);
}
// we can assume they are already sorted
// since we sort before storing them
// Search from the end since in the normal case
// the Txmitter cal is processed within 2 readings
// of the event.
for (let i = (rigSGVs.length - 2); i >= 0; i -= 1) {
// Is the next SGV after valueTime
// and the current SGV is before valueTime
SGVBeforeTime = rigSGVs[i].readDateMills;
SGVAfterTime = rigSGVs[i + 1].readDateMills;
if ((valueTime.valueOf() > SGVBeforeTime) && (SGVAfterTime > valueTime.valueOf())) {
SGVBefore = rigSGVs[i];
SGVAfter = rigSGVs[i + 1];
break;
}
}
if (SGVBefore && SGVAfter) {
return interpolateUnfiltered(SGVBefore, SGVAfter, valueTime);
}
debug(`Unable to find bounding SGVs for calibration at ${valueTime.format()}`);
debug('Looking in Nightscout');
return getUnfilteredFromNS(valueTime);
}
return null;
};
calibrationExports.getUnfiltered = getUnfiltered;
const expiredCalibration = async (
options, storage, bgChecks, lastExpiredCal, sensorInsert, glucoseHist, sgv,
) => {
let calPairs = [];
let calReturn = null;
let calPairsStart = 0;
const maxLsrPairs = options.max_lsr_pairs;
const minLsrPairs = options.min_lsr_pairs;
let maxLsrPairsAge = options.max_lsr_pairs_age;
log('Calculating expired calibration');
debug(`options: {\n maxLsrPairs: ${maxLsrPairs},\n minLsrPairs: ${minLsrPairs},\n maxLsrPairsAge: ${maxLsrPairsAge}\n}`);
// convert to milliseconds
maxLsrPairsAge *= 24 * 60 * 60000;
for (let i = 0; i < bgChecks.length; i += 1) {
let raw = null;
if (!('unfiltered' in bgChecks[i]) || !bgChecks[i].unfiltered) {
// Try to get the unfiltered value if we don't have it
/* eslint-disable-next-line no-await-in-loop */
raw = await getUnfiltered(moment(bgChecks[i].dateMills), glucoseHist, sgv);
} else {
raw = bgChecks[i];
}
if (raw) {
const rawDelta = Math.abs(raw.unfiltered - raw.filtered) * 1.0;
if ((bgChecks[i].type !== 'Unity') && (raw)
&& (bgChecks[i].glucose > constants.MIN_CAL_SGV)
&& (bgChecks[i].glucose < constants.MAX_CAL_SGV)
&& (rawDelta / Math.min(raw.unfiltered, raw.filtered) < 0.1)) {
calPairs.push({
unfiltered: raw.unfiltered,
filtered: raw.filtered,
glucose: bgChecks[i].glucose,
readDateMills: bgChecks[i].dateMills,
});
}
}
}
if (calPairs.length > 0) {
const latestCalTime = calPairs[calPairs.length - 1].readDateMills;
for (let i = 0; i < (calPairs.length - 1); i += 1) {
if (
(sensorInsert
&& (calPairs[i].readDateMills - sensorInsert.valueOf()) < SENSOR_STABLE * 60 * 60000)
|| ((latestCalTime - calPairs[i].readDateMills) > maxLsrPairsAge)) {
calPairsStart = i + 1;
}
}
// save at least two if we have two
if (calPairsStart >= (calPairs.length - 1)) {
calPairsStart = Math.max(calPairs.length - 2, 0);
}
calPairs = calPairs.slice(calPairsStart);
}
// don't use too many
calPairs = calPairs.slice(Math.max(0, calPairs.length - maxLsrPairs + 1));
// If we have at least minLsrPairs good pairs, use LSR
if (calPairs.length >= minLsrPairs) {
const calResult = lsrCalibration(calPairs);
if (calResult && (calResult.slope < MAXSLOPE) && (calResult.slope > MINSLOPE)) {
log(`expired lsrCalibration: numPoints=${calPairs.length}, slope=${calResult.slope}, intercept=${calResult.intercept}`);
calReturn = {
date: calPairs[calPairs.length - 1].readDateMills,
scale: 1,
intercept: calResult.intercept,
slope: calResult.slope,
type: calResult.calibrationType,
};
} else if (calResult) {
log(`Falling back to single point cal due to slope out of range: ${calResult.slope}`);
} else {
log('Falling back to single point cal due to calibration result denominator of zero');
}
}
if (!calReturn && (calPairs.length > 0)) {
const calResult = singlePointCalibration(calPairs);
log(`expired singlePointCalibration: glucose=${calPairs[calPairs.length - 1].glucose}, unfiltered=${calPairs[calPairs.length - 1].unfiltered}, slope=${calResult.slope}, intercept=0`);
calReturn = {
date: calPairs[calPairs.length - 1].readDateMills,
scale: 1,
intercept: calResult.intercept,
slope: calResult.slope,
type: calResult.calibrationType,
};
}
if (!calReturn) {
log('No suitable glucose pairs found for expired calibration.');
}
// Default to sending a new calibration calculation
let slopeDelta = 1;
let interceptDelta = 1;
if (lastExpiredCal && calReturn) {
slopeDelta = Math.abs(calReturn.slope - lastExpiredCal.slope);
interceptDelta = Math.abs(calReturn.intercept - lastExpiredCal.intercept);
}
if ((slopeDelta < 1) && (interceptDelta < 1)) {
log(`No calibration update required: slopeDelta=${Math.round(slopeDelta * 10) / 10} interceptDelta=${Math.round(interceptDelta * 10) / 10}`);
return lastExpiredCal;
}
if (calReturn) {
log(`New expired calibration with ${calReturn.type} due to ${calPairs.length} calibration pairs:\n%O`, calReturn);
}
if (storage) {
saveExpiredCal(storage, calReturn);
}
return calReturn;
};
calibrationExports.expiredCalibration = expiredCalibration;
const getExpiredCal = async (storage) => {
const lastExpiredCal = await storage.getItem('expiredCal')
.catch((err) => {
error(`Unable to obtain current Expired Calibration${err}`);
});
return lastExpiredCal;
};
calibrationExports.getExpiredCal = getExpiredCal;
const getTxmitterCal = async (storage) => {
const lastCal = await storage.getItem('g5Calibration')
.catch((err) => {
error(`Unable to obtain current NS Calibration${err}`);
});
return lastCal;
};
calibrationExports.getTxmitterCal = getTxmitterCal;
const saveTxmitterCal = async (storage, newCal) => {
await storage.setItem('g5Calibration', newCal)
.catch(() => {
error('Unable to store new NS Calibration');
});
};
calibrationExports.saveTxmitterCal = saveTxmitterCal;
const getLastTxmitterCal = (bgChecks) => {
let lastTxmitterCal = null;
if (bgChecks) {
for (let ii = (bgChecks.length - 1); ii >= 0; ii -= 1) {
if ((bgChecks[ii].type === 'Txmitter') || (bgChecks[ii].type === 'Unity')) {
lastTxmitterCal = bgChecks[ii];
break;
}
}
}
return lastTxmitterCal;
};
calibrationExports.getLastTxmitterCal = getLastTxmitterCal;
const getActiveCal = async (options, storage) => {
const lastCal = await getTxmitterCal(storage);
const lastExpiredCal = await getExpiredCal(storage);
if (lastCal && (lastCal.type !== 'Unity')) {
return lastCal;
}
if (lastExpiredCal) {
return lastExpiredCal;
}
return false;
};
calibrationExports.getActiveCal = getActiveCal;
// provide the most recent Txmitter calibration
const getLastCal = async (storage) => {
const bgChecks = await storage.getItem('bgChecks')
.catch((err) => {
error(`Unable to get bgChecks storage item: ${err}`);
});
const lastTxmitterCal = getLastTxmitterCal(bgChecks);
return lastTxmitterCal;
};
calibrationExports.getLastCal = getLastCal;
calibrationExports.clearCalibration = async (storage) => {
await storage.delItem('g5Calibration');
await storage.delItem('expiredCal');
await storage.delItem('bgChecks');
const newCal = {
date: Date.now(),
scale: 1,
intercept: 0,
slope: 1000,
type: 'Unity',
};
saveTxmitterCal(storage, newCal);
};
calibrationExports.haveCalibration = async (storage) => {
const lastCal = await getTxmitterCal(storage);
const lastExpiredCal = await getExpiredCal(storage);
return ((lastCal && (lastCal.type !== 'Unity')) || lastExpiredCal);
};
const validateTxmitterCalibration = (sensorInsert, sensorStop, latestBgCheckTime, lastCal) => {
let bgCheckDelta = 0;
let bgCheckTime = null;
let sensorInsertTime = null;
let sensorInsertDelta = 0;
let sensorStopTime = null;
let sensorStopDelta = 0;
if (!lastCal) {
log('No Transmitter Calibration');
return false;
}
const lastCalTime = moment(lastCal.date).subtract(6, 'minutes');
if (latestBgCheckTime) {
bgCheckDelta = latestBgCheckTime.diff(lastCalTime);
bgCheckTime = latestBgCheckTime.format();
}
if (sensorInsert) {
sensorInsertDelta = sensorInsert.diff(lastCalTime);
sensorInsertTime = sensorInsert.format();
}
if (sensorStop) {
sensorStopDelta = sensorStop.diff(lastCalTime);
sensorStopTime = sensorStop.format();
}
if (!sensorInsert
|| (lastCal.type === 'Unity')
|| (sensorInsertDelta > 0)
|| (sensorStopDelta > 0)
|| (bgCheckDelta > 0)) {
log('\n-----------------------------------------------\n'
+ 'No valid Transmitter Calibration -\n'
+ ` lastCalType: ${lastCal.type}\n`
+ ` lastCal: ${moment(lastCal.date).format()}\n`
+ ` lastBgCheck: ${bgCheckTime} bgCheckDelta: ${bgCheckDelta}\n`
+ `sensorInsert: ${sensorInsertTime} sensorInsertDelta: ${sensorInsertDelta}\n`
+ ` sensorStop: ${sensorStopTime} sensorStopDelta: ${sensorStopDelta}\n`
+ ' - if lastCalType set to "Unity", calibration is not valid to use\n'
+ ' - if bgCheckDelta > 0, latest BG check is after latest calibration calculation, invalidating calibration\n'
+ ' - if sensorInsertDelta > 0, latest sensor insert is after latest calibration calculation, invalidating calibration\n'
+ ' - if sensorStop > 0, latest sensor stop is after last calibration calculation, invalidating calibration\n'
+ '-----------------------------------------------');
return false;
}
log('Have valid Transmitter Calibration');
return true;
};
const validateExpiredCalibration = async (
sensorInsert, sensorStop, lastExpiredCal, options, storage, bgChecks, glucoseHist,
) => {
let sensorInsertTime = null;
let sensorInsertDelta = 0;
let sensorStopTime = null;
let sensorStopDelta = 0;
let expiredCal = lastExpiredCal;
if (!lastExpiredCal) {
log('No Expired Calibration');
// Try to generate an expired calibration
// This is needed on fresh startups when we haven't tried to generate one yet
expiredCal = await expiredCalibration(
options, storage, bgChecks, lastExpiredCal, sensorInsert, glucoseHist,
);
if (!expiredCal) {
return false;
}
saveExpiredCal(storage, expiredCal);
}
const lastExpiredCalTime = moment(expiredCal.date).subtract(6, 'minutes');
if (sensorInsert) {
sensorInsertDelta = sensorInsert.diff(lastExpiredCalTime);
sensorInsertTime = sensorInsert.format();
}
if (sensorStop) {
sensorStopDelta = sensorStop.diff(lastExpiredCalTime);
sensorStopTime = sensorStop.format();
}
if (!sensorInsert || !expiredCal
|| (sensorInsertDelta > 0)
|| (sensorStopDelta > 0)) {
log('\n-----------------------------------------------\n'
+ 'No valid Expired Calibration -\n'
+ `lastExpiredCal: ${moment(expiredCal.date).format()}\n`
+ ` sensorInsert: ${sensorInsertTime} sensorInsertDelta: ${sensorInsertDelta}`
+ ` sensorStop: ${sensorStopTime} sensorStopDelta: ${sensorStopDelta}`
+ ' - if sensorInsertDelta > 0, latest sensor insert is after latest calibration calculation, invalidating calibration\n'
+ ' - if sensorStop > 0, latest sensor stop is after last calibration calculation, invalidating calibration\n'
+ '-----------------------------------------------');
return false;
}
log('Have valid Expired Calibration');
return true;
};
const validateCalibration = async (
options, storage, sensorInsert, sensorStop, latestBgCheckTime,
) => {
const lastCal = await getTxmitterCal(storage);
const lastExpiredCal = await getExpiredCal(storage);
let glucoseHist = await storage.getItem('glucoseHist');
let bgChecks = await storage.getItem('bgChecks');
if (!glucoseHist) {
glucoseHist = [];
}
if (!bgChecks) {
bgChecks = [];
}
return (validateTxmitterCalibration(sensorInsert, sensorStop, latestBgCheckTime, lastCal)
|| validateExpiredCalibration(
sensorInsert, sensorStop, lastExpiredCal, options, storage, bgChecks, glucoseHist,
));
};
calibrationExports.validateCalibration = validateCalibration;
calibrationExports.calibrateGlucose = async (
storage, options, sensorInsert, sensorStop, glucoseHist, uncalibratedSgv,
) => {
let lastCal = await getTxmitterCal(storage);
let expiredCal = await getExpiredCal(storage);
const sgv = _.cloneDeep(uncalibratedSgv);
let bgChecks = await storage.getItem('bgChecks')
.catch((err) => {
error(`Error getting bgChecks: ${err}`);
});
if (!bgChecks) {
bgChecks = [];
}
let lastTxmitterCalTime = 0;
let newCal = null;
let usedCal = null;
const lastTxmitterCal = getLastTxmitterCal(bgChecks);
if (lastTxmitterCal) {
lastTxmitterCalTime = lastTxmitterCal.dateMills;
}
sgv.g5calibrated = true;
sgv.inExtendedSession = false;
sgv.inExpiredSession = false;
let latestBgCheckTime = null;
if (bgChecks.length > 0) {
latestBgCheckTime = moment(bgChecks[bgChecks.length - 1].dateMills);
}
if (glucoseHist.length > 0) {
newCal = calculateTxmitterCalibration(
options, lastCal, lastTxmitterCalTime, latestBgCheckTime, sensorInsert, glucoseHist, sgv,
);
expiredCal = await expiredCalibration(
options, storage, bgChecks, expiredCal, sensorInsert, glucoseHist, sgv,
);
}
if (newCal) {
lastCal = newCal;
}
usedCal = lastCal;
if (!sgv.glucose && options.extend_sensor
&& validateTxmitterCalibration(sensorInsert, sensorStop, latestBgCheckTime, lastCal)) {
sgv.glucose = calcGlucose(sgv, lastCal);
sgv.inExtendedSession = true;
log('Invalid glucose value received from transmitter, replacing with calibrated unfiltered value from Txmitter calibration algorithm');
log(`Calibrated SGV: ${sgv.glucose} unfiltered: ${sgv.unfiltered} slope: ${lastCal.slope} intercept: ${lastCal.intercept}`);
sgv.g5calibrated = false;
}
if (options.expired_cal
&& await validateExpiredCalibration(
sensorInsert, sensorStop, expiredCal, options, storage, bgChecks, glucoseHist,
)) {
const expiredCalGlucose = calcGlucose(sgv, expiredCal);
if (!sgv.glucose) {
sgv.glucose = expiredCalGlucose;
sgv.inExpiredSession = true;
usedCal = expiredCal;
log('Invalid glucose value received from transmitter, replacing with calibrated unfiltered value from expired calibration algorithm');
log(`Calibrated SGV: ${sgv.glucose} unfiltered: ${sgv.unfiltered} slope: ${expiredCal.slope} intercept: ${expiredCal.intercept}`);
sgv.g5calibrated = false;
} else {
const calErr = expiredCalGlucose - sgv.glucose;
log(`Current Expired Cal error: ${Math.round(calErr * 10) / 10} calibrated value: ${Math.round(expiredCalGlucose * 10) / 10} slope: ${Math.round(expiredCal.slope * 10) / 10} intercept: ${Math.round(expiredCal.intercept * 10) / 10} type: ${expiredCal.type}`);
}
}
if (newCal) {
log(`New CGM calibration: slope = ${newCal.slope}, intercept = ${newCal.intercept}, scale = ${newCal.scale}`);
saveTxmitterCal(storage, newCal);
}
if (usedCal) {
// a valid calibration is available to use
sgv.trend = stats.calcTrend(calcGlucose, glucoseHist, usedCal, sgv);
sgv.noise = stats.calcSensorNoise(calcGlucose, glucoseHist, usedCal, sgv);
if ((sgv.noise < 0.4) && sensorInsert
&& (((sgv.readDateMills - sensorInsert.valueOf()) < SENSOR_WARM * 60 * 60000)
|| ((usedCal.dateMills - sensorInsert.valueOf()) < SENSOR_WARM * 60 * 60000))) {
// put in light noise to account for warm up
log(`Setting noise to light because SGV date (${moment(sgv.readDateMills).format()} - ${sensorInsert.format()} < ${SENSOR_WARM} hours`);
sgv.noise = 0.4;
}
} else {
// No way to calculate a trend since we don't know the calibration slope
sgv.trend = 0;
// Put in light noise to account for uncertainty
sgv.noise = 0.4;
}
sgv.nsNoise = stats.calcNSNoise(sgv.noise, glucoseHist);
sgv.noiseString = stats.NSNoiseString(sgv.nsNoise);
log(`Current sensor trend: ${Math.round(sgv.trend * 10) / 10} Sensor Noise: ${Math.round(sgv.noise * 1000) / 1000} NS Noise: ${sgv.nsNoise}`);
return sgv;
};