index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Prep step before autotune.js can run; pulls in meal (carb) data and calls categorize.js
  2. var find_meals = require('../meal/history');
  3. var categorize = require('./categorize');
  4. function generate (inputs) {
  5. //console.error(inputs);
  6. var treatments = find_meals(inputs);
  7. var opts = {
  8. treatments: treatments
  9. , profile: inputs.profile
  10. , pumpHistory: inputs.history
  11. , glucose: inputs.glucose
  12. //, prepped_glucose: inputs.prepped_glucose
  13. , basalprofile: inputs.profile.basalprofile
  14. , pumpbasalprofile: inputs.pumpprofile.basalprofile
  15. , categorize_uam_as_basal: inputs.categorize_uam_as_basal
  16. };
  17. var autotune_prep_output = categorize(opts);
  18. if (inputs.tune_insulin_curve) {
  19. if (opts.profile.curve === 'bilinear') {
  20. console.error('--tune-insulin-curve is set but only valid for exponential curves');
  21. } else {
  22. var minDeviations = 1000000;
  23. var newDIA = 0;
  24. var diaDeviations = [];
  25. var peakDeviations = [];
  26. var currentDIA = opts.profile.dia;
  27. var currentPeak = opts.profile.insulinPeakTime;
  28. var consoleError = console.error;
  29. console.error = function() {};
  30. var startDIA=currentDIA - 2;
  31. var endDIA=currentDIA + 2;
  32. for (var dia=startDIA; dia <= endDIA; ++dia) {
  33. var sqrtDeviations = 0;
  34. var deviations = 0;
  35. var deviationsSq = 0;
  36. opts.profile.dia = dia;
  37. var curve_output = categorize(opts);
  38. var basalGlucose = curve_output.basalGlucoseData;
  39. for (var hour=0; hour < 24; ++hour) {
  40. for (var i=0; i < basalGlucose.length; ++i) {
  41. var BGTime;
  42. if (basalGlucose[i].date) {
  43. BGTime = new Date(basalGlucose[i].date);
  44. } else if (basalGlucose[i].displayTime) {
  45. BGTime = new Date(basalGlucose[i].displayTime.replace('T', ' '));
  46. } else if (basalGlucose[i].dateString) {
  47. BGTime = new Date(basalGlucose[i].dateString);
  48. } else {
  49. consoleError("Could not determine last BG time");
  50. }
  51. var myHour = BGTime.getHours();
  52. if (hour === myHour) {
  53. //console.error(basalGlucose[i].deviation);
  54. sqrtDeviations += Math.pow(parseFloat(Math.abs(basalGlucose[i].deviation)), 0.5);
  55. deviations += Math.abs(parseFloat(basalGlucose[i].deviation));
  56. deviationsSq += Math.pow(parseFloat(basalGlucose[i].deviation), 2);
  57. }
  58. }
  59. }
  60. var meanDeviation = Math.round(Math.abs(deviations/basalGlucose.length)*1000)/1000;
  61. var SMRDeviation = Math.round(Math.pow(sqrtDeviations/basalGlucose.length,2)*1000)/1000;
  62. var RMSDeviation = Math.round(Math.pow(deviationsSq/basalGlucose.length,0.5)*1000)/1000;
  63. consoleError('insulinEndTime', dia, 'meanDeviation:', meanDeviation, 'SMRDeviation:', SMRDeviation, 'RMSDeviation:',RMSDeviation, '(mg/dL)');
  64. diaDeviations.push({
  65. dia: dia,
  66. meanDeviation: meanDeviation,
  67. SMRDeviation: SMRDeviation,
  68. RMSDeviation: RMSDeviation,
  69. });
  70. autotune_prep_output.diaDeviations = diaDeviations;
  71. deviations = Math.round(deviations*1000)/1000;
  72. if (deviations < minDeviations) {
  73. minDeviations = Math.round(deviations*1000)/1000;
  74. newDIA = dia;
  75. }
  76. }
  77. // consoleError('Optimum insulinEndTime', newDIA, 'mean deviation:', Math.round(minDeviations/basalGlucose.length*1000)/1000, '(mg/dL)');
  78. //consoleError(diaDeviations);
  79. minDeviations = 1000000;
  80. var newPeak = 0;
  81. opts.profile.dia = currentDIA;
  82. //consoleError(opts.profile.useCustomPeakTime, opts.profile.insulinPeakTime);
  83. if ( ! opts.profile.useCustomPeakTime === true && opts.profile.curve === "ultra-rapid" ) {
  84. opts.profile.insulinPeakTime = 55;
  85. } else if ( ! opts.profile.useCustomPeakTime === true ) {
  86. opts.profile.insulinPeakTime = 75;
  87. }
  88. opts.profile.useCustomPeakTime = true;
  89. var startPeak=opts.profile.insulinPeakTime - 10;
  90. var endPeak=opts.profile.insulinPeakTime + 10;
  91. for (var peak=startPeak; peak <= endPeak; peak=(peak+5)) {
  92. sqrtDeviations = 0;
  93. deviations = 0;
  94. deviationsSq = 0;
  95. opts.profile.insulinPeakTime = peak;
  96. curve_output = categorize(opts);
  97. basalGlucose = curve_output.basalGlucoseData;
  98. for (hour=0; hour < 24; ++hour) {
  99. for (i=0; i < basalGlucose.length; ++i) {
  100. if (basalGlucose[i].date) {
  101. BGTime = new Date(basalGlucose[i].date);
  102. } else if (basalGlucose[i].displayTime) {
  103. BGTime = new Date(basalGlucose[i].displayTime.replace('T', ' '));
  104. } else if (basalGlucose[i].dateString) {
  105. BGTime = new Date(basalGlucose[i].dateString);
  106. } else {
  107. consoleError("Could not determine last BG time");
  108. }
  109. myHour = BGTime.getHours();
  110. if (hour === myHour) {
  111. //console.error(basalGlucose[i].deviation);
  112. sqrtDeviations += Math.pow(parseFloat(Math.abs(basalGlucose[i].deviation)), 0.5);
  113. deviations += Math.abs(parseFloat(basalGlucose[i].deviation));
  114. deviationsSq += Math.pow(parseFloat(basalGlucose[i].deviation), 2);
  115. }
  116. }
  117. }
  118. console.error(deviationsSq);
  119. meanDeviation = Math.round(deviations/basalGlucose.length*1000)/1000;
  120. SMRDeviation = Math.round(Math.pow(sqrtDeviations/basalGlucose.length,2)*1000)/1000;
  121. RMSDeviation = Math.round(Math.pow(deviationsSq/basalGlucose.length,0.5)*1000)/1000;
  122. consoleError('insulinPeakTime', peak, 'meanDeviation:', meanDeviation, 'SMRDeviation:', SMRDeviation, 'RMSDeviation:',RMSDeviation, '(mg/dL)');
  123. peakDeviations.push({
  124. peak: peak,
  125. meanDeviation: meanDeviation,
  126. SMRDeviation: SMRDeviation,
  127. RMSDeviation: RMSDeviation,
  128. });
  129. autotune_prep_output.diaDeviations = diaDeviations;
  130. deviations = Math.round(deviations*1000)/1000;
  131. if (deviations < minDeviations) {
  132. minDeviations = Math.round(deviations*1000)/1000;
  133. newPeak = peak;
  134. }
  135. }
  136. //consoleError('Optimum insulinPeakTime', newPeak, 'mean deviation:', Math.round(minDeviations/basalGlucose.length*1000)/1000, '(mg/dL)');
  137. //consoleError(peakDeviations);
  138. autotune_prep_output.peakDeviations = peakDeviations;
  139. console.error = consoleError;
  140. }
  141. }
  142. return autotune_prep_output;
  143. }
  144. exports = module.exports = generate;