[13865] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[14310] | 4 | using System.Collections.ObjectModel;
|
---|
[13865] | 5 | using System.Collections.Specialized;
|
---|
| 6 | using System.Drawing.Design;
|
---|
| 7 | using System.Linq;
|
---|
[14310] | 8 | using HeuristicLab.Common;
|
---|
[13865] | 9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 10 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[13870] | 11 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[13865] | 12 |
|
---|
| 13 | namespace HeuristicLab.Problems.GeneticProgramming.GlucosePrediction {
|
---|
| 14 | public static class Interpreter {
|
---|
| 15 | private class Data {
|
---|
| 16 | public double[] realGluc;
|
---|
| 17 | public double[] realIns;
|
---|
| 18 | public double[] realCh;
|
---|
[14310] | 19 | public Dictionary<ISymbolicExpressionTreeNode, double[]> precalculatedValues;
|
---|
[13865] | 20 | }
|
---|
| 21 |
|
---|
| 22 | public static IEnumerable<double> Apply(ISymbolicExpressionTreeNode model, IDataset dataset, IEnumerable<int> rows) {
|
---|
| 23 | double[] targetGluc = dataset.GetDoubleValues("Glucose_target", rows).ToArray(); // only for skipping rows for which we should not produce an output
|
---|
| 24 |
|
---|
| 25 | var data = new Data {
|
---|
| 26 | realGluc = dataset.GetDoubleValues("Glucose_Interpol", rows).ToArray(),
|
---|
| 27 | realIns = dataset.GetDoubleValues("Insuline", rows).ToArray(),
|
---|
| 28 | realCh = dataset.GetDoubleValues("CH", rows).ToArray(),
|
---|
[14310] | 29 | precalculatedValues = CreatePrecalculatedValues(model, dataset)
|
---|
[13865] | 30 | };
|
---|
[14310] | 31 | var predictions = new double[targetGluc.Length];
|
---|
| 32 | var rowsEnumerator = rows.GetEnumerator();
|
---|
[14311] | 33 | rowsEnumerator.MoveNext();
|
---|
[14310] | 34 | for (int k = 0; k < predictions.Length; k++, rowsEnumerator.MoveNext()) {
|
---|
[13865] | 35 | if (double.IsNaN(targetGluc[k])) {
|
---|
[14310] | 36 | predictions[k] = double.NaN;
|
---|
[13865] | 37 | } else {
|
---|
[14310] | 38 | var rawPred = InterpretRec(model, data, rowsEnumerator.Current);
|
---|
[14311] | 39 | predictions[k] = rawPred;
|
---|
[13865] | 40 | }
|
---|
| 41 | }
|
---|
[14310] | 42 | return predictions;
|
---|
[13865] | 43 | }
|
---|
| 44 |
|
---|
[14310] | 45 | private static Dictionary<ISymbolicExpressionTreeNode, double[]> CreatePrecalculatedValues(ISymbolicExpressionTreeNode root, IDataset dataset) {
|
---|
| 46 | var dict = new Dictionary<ISymbolicExpressionTreeNode, double[]>();
|
---|
| 47 | // here we integrate ins or ch inputs over the whole day to generate smoothed ins/ch values with the same number of rows
|
---|
| 48 | // the integrated values are reset to zero whenever a new evluation period starts
|
---|
| 49 | foreach (var node in root.IterateNodesPrefix()) {
|
---|
| 50 | var curvedInsNode = node as CurvedInsVariableTreeNode;
|
---|
| 51 | var curvedChNode = node as CurvedChVariableTreeNode;
|
---|
| 52 | if (curvedInsNode != null) {
|
---|
| 53 | dict.Add(curvedInsNode, Integrate(curvedInsNode, dataset));
|
---|
| 54 | } else if (curvedChNode != null) {
|
---|
| 55 | dict.Add(curvedChNode, Integrate(curvedChNode, dataset));
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | return dict;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private static double[] Integrate(CurvedInsVariableTreeNode node, IDataset dataset) {
|
---|
| 62 | // d Q1 / dt = ins(t) - alpha * Q1(t)
|
---|
| 63 | // d Q2 / dt = alpha * (Q1(t) - Q2(t))
|
---|
[14311] | 64 | // d Q3 / dt = alpha * Q2(t) - beta * Q3(t)
|
---|
[14310] | 65 | var alpha = node.Alpha;
|
---|
[14311] | 66 | var beta = node.Beta;
|
---|
[14310] | 67 |
|
---|
| 68 | var ins = dataset.GetReadOnlyDoubleValues("Insuline");
|
---|
| 69 | var time = dataset.GetReadOnlyDoubleValues("HourMin").ToArray();
|
---|
| 70 |
|
---|
[14311] | 71 | double q1, q2, q3, q1_prev, q2_prev, q3_prev;
|
---|
[14310] | 72 | // starting values: zeros
|
---|
[14311] | 73 | q1 = q2 = q3 = q1_prev = q2_prev = q3_prev = 0;
|
---|
[14310] | 74 | double[] s = new double[dataset.Rows];
|
---|
| 75 |
|
---|
| 76 | for (int t = 1; t < dataset.Rows; t++) {
|
---|
| 77 | if (IsStartOfNewPeriod(time, t)) {
|
---|
[14311] | 78 | q1 = q2 = q3 = q1_prev = q2_prev = q3_prev = 0;
|
---|
[14310] | 79 | }
|
---|
| 80 | q1 = q1_prev + ins[t] - alpha * q1_prev;
|
---|
| 81 | q2 = q2_prev + alpha * (q1_prev - q2_prev);
|
---|
[14311] | 82 | q3 = q3_prev + alpha * q2_prev - beta * q3_prev;
|
---|
| 83 | s[t] = q3;
|
---|
[14310] | 84 | q1_prev = q1;
|
---|
| 85 | q2_prev = q2;
|
---|
[14311] | 86 | q3_prev = q3;
|
---|
[14310] | 87 |
|
---|
| 88 | }
|
---|
| 89 | return s;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private static bool IsStartOfNewPeriod(double[] time, int t) {
|
---|
| 93 | return t == 0 ||
|
---|
| 94 | (time[t].IsAlmost(2005) && !time[t - 1].IsAlmost(2000));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | private static double[] Integrate(CurvedChVariableTreeNode node, IDataset dataset) {
|
---|
| 99 | // d Q1 / dt = ins(t) - alpha * Q1(t)
|
---|
| 100 | // d Q2 / dt = alpha * (Q1(t) - Q2(t))
|
---|
[14311] | 101 | // d Q3 / dt = alpha * Q2(t) - beta * Q3(t)
|
---|
[14310] | 102 | var alpha = node.Alpha;
|
---|
[14311] | 103 | var beta = node.Beta;
|
---|
[14310] | 104 |
|
---|
[14311] | 105 | var ins = dataset.GetReadOnlyDoubleValues("CH");
|
---|
[14310] | 106 | var time = dataset.GetReadOnlyDoubleValues("HourMin").ToArray();
|
---|
| 107 |
|
---|
[14311] | 108 | double q1, q2, q3, q1_prev, q2_prev, q3_prev;
|
---|
[14310] | 109 | // starting values: zeros
|
---|
[14311] | 110 | q1 = q2 = q3 = q1_prev = q2_prev = q3_prev = 0;
|
---|
[14310] | 111 | double[] s = new double[dataset.Rows];
|
---|
| 112 |
|
---|
| 113 | for (int t = 1; t < dataset.Rows; t++) {
|
---|
| 114 | if (IsStartOfNewPeriod(time, t)) {
|
---|
[14311] | 115 | q1 = q2 = q3 = q1_prev = q2_prev = q3_prev = 0;
|
---|
[14310] | 116 | }
|
---|
[14311] | 117 | q1 = q1_prev + ins[t] - alpha * q1_prev;
|
---|
[14310] | 118 | q2 = q2_prev + alpha * (q1_prev - q2_prev);
|
---|
[14311] | 119 | q3 = q3_prev + alpha * q2_prev - beta * q3_prev;
|
---|
| 120 | s[t] = q3;
|
---|
[14310] | 121 | q1_prev = q1;
|
---|
| 122 | q2_prev = q2;
|
---|
[14311] | 123 | q3_prev = q3;
|
---|
[14310] | 124 |
|
---|
| 125 | }
|
---|
| 126 | return s;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[13865] | 129 | private static double InterpretRec(ISymbolicExpressionTreeNode node, Data data, int k) {
|
---|
| 130 | if (node.Symbol is SimpleSymbol) {
|
---|
| 131 | switch (node.Symbol.Name) {
|
---|
| 132 | case "+":
|
---|
| 133 | case "+Ins":
|
---|
| 134 | case "+Ch": {
|
---|
| 135 | return InterpretRec(node.GetSubtree(0), data, k) + InterpretRec(node.GetSubtree(1), data, k);
|
---|
| 136 | }
|
---|
| 137 | case "-":
|
---|
| 138 | case "-Ins":
|
---|
| 139 | case "-Ch": {
|
---|
| 140 | return InterpretRec(node.GetSubtree(0), data, k) - InterpretRec(node.GetSubtree(1), data, k);
|
---|
| 141 | }
|
---|
| 142 | case "*":
|
---|
| 143 | case "*Ins":
|
---|
| 144 | case "*Ch": {
|
---|
| 145 | return InterpretRec(node.GetSubtree(0), data, k) * InterpretRec(node.GetSubtree(1), data, k);
|
---|
| 146 | }
|
---|
| 147 | case "/Ch":
|
---|
| 148 | case "/Ins":
|
---|
| 149 | case "/": {
|
---|
| 150 | return InterpretRec(node.GetSubtree(0), data, k) / InterpretRec(node.GetSubtree(1), data, k);
|
---|
| 151 | }
|
---|
| 152 | case "Exp":
|
---|
| 153 | case "ExpIns":
|
---|
| 154 | case "ExpCh": {
|
---|
| 155 | return Math.Exp(InterpretRec(node.GetSubtree(0), data, k));
|
---|
| 156 | }
|
---|
| 157 | case "Sin":
|
---|
| 158 | case "SinIns":
|
---|
| 159 | case "SinCh": {
|
---|
| 160 | return Math.Sin(InterpretRec(node.GetSubtree(0), data, k));
|
---|
| 161 | }
|
---|
| 162 | case "CosCh":
|
---|
| 163 | case "CosIns":
|
---|
| 164 | case "Cos": {
|
---|
| 165 | return Math.Cos(InterpretRec(node.GetSubtree(0), data, k));
|
---|
| 166 | }
|
---|
| 167 | case "LogCh":
|
---|
| 168 | case "LogIns":
|
---|
| 169 | case "Log": {
|
---|
| 170 | return Math.Log(InterpretRec(node.GetSubtree(0), data, k));
|
---|
| 171 | }
|
---|
| 172 | case "Func": {
|
---|
| 173 | // <exprgluc> + <exprch> - <exprins>
|
---|
| 174 | return InterpretRec(node.GetSubtree(0), data, k)
|
---|
| 175 | + InterpretRec(node.GetSubtree(1), data, k)
|
---|
| 176 | - InterpretRec(node.GetSubtree(2), data, k);
|
---|
| 177 | }
|
---|
| 178 | case "ExprGluc": {
|
---|
| 179 | return InterpretRec(node.GetSubtree(0), data, k);
|
---|
| 180 | }
|
---|
| 181 | case "ExprCh": {
|
---|
| 182 | return InterpretRec(node.GetSubtree(0), data, k);
|
---|
| 183 | }
|
---|
| 184 | case "ExprIns": {
|
---|
| 185 | return InterpretRec(node.GetSubtree(0), data, k);
|
---|
| 186 | }
|
---|
| 187 | default: {
|
---|
| 188 | throw new InvalidProgramException("Found an unknown symbol " + node.Symbol);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | } else if (node.Symbol is PredictedGlucoseVariableSymbol) {
|
---|
[14310] | 192 | throw new NotSupportedException();
|
---|
[13865] | 193 | } else if (node.Symbol is RealGlucoseVariableSymbol) {
|
---|
| 194 | var n = (RealGlucoseVariableTreeNode)node;
|
---|
[14310] | 195 | if (k + n.RowOffset < 0 || k + n.RowOffset >= data.realGluc.Length) return double.NaN;
|
---|
| 196 | return data.realGluc[k + n.RowOffset];
|
---|
[13865] | 197 | } else if (node.Symbol is CurvedChVariableSymbol) {
|
---|
[14310] | 198 | return data.precalculatedValues[node][k];
|
---|
[13865] | 199 | } else if (node.Symbol is RealInsulineVariableSymbol) {
|
---|
[14310] | 200 | throw new NotSupportedException();
|
---|
[13865] | 201 | } else if (node.Symbol is CurvedInsVariableSymbol) {
|
---|
[14310] | 202 | return data.precalculatedValues[node][k];
|
---|
[13870] | 203 | } else if (node.Symbol is Constant) {
|
---|
| 204 | var n = (ConstantTreeNode)node;
|
---|
| 205 | return n.Value;
|
---|
[13865] | 206 | } else {
|
---|
| 207 | throw new InvalidProgramException("found unknown symbol " + node.Symbol);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | }
|
---|
| 212 | }
|
---|