Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Core/3.3/Results.cs @ 2139

Last change on this file since 2139 was 2139, checked in by gkronber, 15 years ago

Implemented filtering of result entries in bubble chart and table view. #691 (CEDMA result views should allow filtering of displayed results)

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Collections;
28using HeuristicLab.CEDMA.DB.Interfaces;
29using System.Xml;
30using System.Runtime.Serialization;
31using System.IO;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Data;
34using HeuristicLab.DataAnalysis;
35using System.Drawing;
36
37namespace HeuristicLab.CEDMA.Core {
38  public class Results : ItemBase {
39    private const int PAGE_SIZE = 1000;
40    private string[] categoricalVariables = null;
41    public string[] CategoricalVariables {
42      get {
43        if (categoricalVariables == null) {
44          LoadModelAttributes();
45        }
46        return categoricalVariables;
47      }
48    }
49
50    private string[] ordinalVariables = null;
51    public string[] OrdinalVariables {
52      get {
53        if (ordinalVariables == null) {
54          LoadModelAttributes();
55        }
56        return ordinalVariables;
57      }
58    }
59
60    private string[] multiDimensionalOrdinalVariables = new string[] { "VariableImpacts: EvaluationImpact", "VariableImpacts: QualityImpact" };
61    public string[] MultiDimensionalOrdinalVariables {
62      get { return multiDimensionalOrdinalVariables; }
63    }
64
65    private string[] multiDimensionalCategoricalVariables = new string[] { "VariableImpacts: InputVariableName" };
66    public string[] MultiDimensionalCategoricalVariables {
67      get { return multiDimensionalCategoricalVariables; }
68    }
69
70    private IStore store;
71    public IStore Store {
72      get { return store; }
73      set {
74        store = value;
75      }
76    }
77
78    private Dictionary<string, Dictionary<object, double>> categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();
79
80    public Results(IStore store) {
81      this.store = store;
82    }
83
84    private List<ResultsEntry> entries = null;
85    private bool cached = false;
86    public IEnumerable<ResultsEntry> GetEntries() {
87      if (!cached)
88        return SelectRows();
89      return entries.AsEnumerable();
90    }
91
92    long nStatements;
93    DateTime start, stop;
94    private IEnumerable<ResultsEntry> SelectRows() {
95      start = DateTime.Now;
96      nStatements = 0;
97      int page = 0;
98      int resultsReturned = 0;
99      entries = new List<ResultsEntry>();
100      if (store == null) return entries;
101      do {
102        var allModels = store.Query(
103          "?Model <" + Ontology.InstanceOf + "> <" + Ontology.TypeModel + "> ." +
104          "?Model <" + Ontology.TargetVariable + "> ?TargetVariable ." +
105          "?Model <" + Ontology.TestMeanSquaredError + "> ?TestMSE .",
106          0, 3000)
107          .Select(modelBinding => new {
108            Model = ((Entity)modelBinding.Get("Model")).Uri,
109            TestMSE = (double)((Literal)modelBinding.Get("TestMSE")).Value,
110            TargetVariable = (string)((Literal)modelBinding.Get("TargetVariable")).Value
111          })
112          .Distinct()
113          .GroupBy(m => m.TargetVariable)
114          .Select(grouping => grouping.OrderBy(m => m.TestMSE));
115        foreach (var targetVariableBindings in allModels) {
116          resultsReturned = targetVariableBindings.Count();
117          nStatements += resultsReturned;
118          int nModels = Math.Max(10, resultsReturned * 10 / 100);
119          nModels = Math.Min(nModels, resultsReturned);
120          foreach (var modelBindings in targetVariableBindings.Take(nModels)) {
121            ResultsEntry entry = new ResultsEntry();
122            entry.Uri = modelBindings.Model;
123            entries.Add(entry);
124            SetModelAttributes(entry, modelBindings.Model);
125            entry.Set("VariableImpacts", SelectVariableImpacts(modelBindings.Model));
126          }
127        }
128        page++;
129      } while (resultsReturned == PAGE_SIZE);
130      stop = DateTime.Now;
131      FireChanged();
132      cached = true;
133      return entries;
134    }
135
136    private void SetModelAttributes(ResultsEntry entry, string modelUri) {
137      var modelBindings = store.Query(
138        "<" + modelUri + "> ?Attribute ?Value .",
139        0, PAGE_SIZE);
140      nStatements += modelBindings.Count();
141      foreach (var binding in modelBindings) {
142        if (binding.Get("Value") is Literal) {
143          string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
144          if (entry.Get(name) == null) {
145            object value = ((Literal)binding.Get("Value")).Value;
146            entry.Set(name, value);
147          }
148        }
149      }
150    }
151
152    private IEnumerable<ResultsEntry> SelectVariableImpacts(string modelUri) {
153      var inputVariableNameBindings = store.Query(
154          "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
155          "?InputVariable <" + Ontology.Name + "> ?InputName .",
156          0, PAGE_SIZE);
157
158      var qualityImpactBindings = store.Query(
159          "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
160          "?InputVariable <" + Ontology.QualityImpact + "> ?QualityImpact .",
161          0, PAGE_SIZE);
162
163      var evaluationImpactBindings = store.Query(
164           "<" + modelUri + "> <" + Ontology.HasInputVariable + "> ?InputVariable ." +
165           "?InputVariable <" + Ontology.EvaluationImpact + "> ?EvaluationImpact .",
166           0, PAGE_SIZE);
167      Dictionary<object, ResultsEntry> inputVariableAttributes = new Dictionary<object, ResultsEntry>();
168      nStatements += inputVariableNameBindings.Count();
169      nStatements += qualityImpactBindings.Count();
170      nStatements += evaluationImpactBindings.Count();
171
172      foreach (var inputVariableNameBinding in inputVariableNameBindings) {
173        object inputVariable = inputVariableNameBinding.Get("InputVariable");
174        object name = ((Literal)inputVariableNameBinding.Get("InputName")).Value;
175        if (!inputVariableAttributes.ContainsKey(inputVariable)) {
176          inputVariableAttributes[inputVariable] = new ResultsEntry();
177          inputVariableAttributes[inputVariable].Set("InputVariableName", name);
178        }
179      }
180
181      foreach (var qualityImpactBinding in qualityImpactBindings) {
182        double qualityImpact = (double)((Literal)qualityImpactBinding.Get("QualityImpact")).Value;
183        object inputVariable = qualityImpactBinding.Get("InputVariable");
184        if (!IsAlmost(qualityImpact, 1.0)) {
185          if (inputVariableAttributes[inputVariable].Get("QualityImpact") == null)
186            inputVariableAttributes[inputVariable].Set("QualityImpact", qualityImpact);
187        } else inputVariableAttributes.Remove(inputVariable);
188      }
189
190      foreach (var evaluationImpactBinding in evaluationImpactBindings) {
191        double evaluationImpact = (double)((Literal)evaluationImpactBinding.Get("EvaluationImpact")).Value;
192        object inputVariable = evaluationImpactBinding.Get("InputVariable");
193        if (!IsAlmost(evaluationImpact, 0.0)) {
194          if (inputVariableAttributes.ContainsKey(inputVariable) && inputVariableAttributes[inputVariable].Get("EvaluationImpact") == null)
195            inputVariableAttributes[inputVariable].Set("EvaluationImpact", evaluationImpact);
196        } else inputVariableAttributes.Remove(inputVariable);
197      }
198
199      return inputVariableAttributes.Values;
200    }
201
202    private bool IsAlmost(double x, double y) {
203      return Math.Abs(x - y) < 1.0E-12;
204    }
205
206    internal IEnumerable<string> SelectModelAttributes() {
207      return CategoricalVariables.Concat(OrdinalVariables);
208    }
209
210    private void LoadModelAttributes() {
211      this.ordinalVariables =
212        store
213          .Query(
214            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
215            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .", 0, 100)
216          .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
217          .Distinct()
218          .ToArray();
219      this.categoricalVariables =
220        store
221          .Query(
222            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
223            "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .", 0, 100)
224          .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
225          .Distinct()
226          .ToArray();
227    }
228
229    public double IndexOfCategoricalValue(string variable, object value) {
230      if (value == null) return double.NaN;
231      Dictionary<object, double> valueToIndexMap;
232      if (categoricalValueIndices.ContainsKey(variable)) {
233        valueToIndexMap = categoricalValueIndices[variable];
234      } else {
235        valueToIndexMap = new Dictionary<object, double>();
236        categoricalValueIndices[variable] = valueToIndexMap;
237      }
238      if (!valueToIndexMap.ContainsKey(value)) {
239        if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
240        else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
241      }
242      return valueToIndexMap[value];
243    }
244  }
245}
Note: See TracBrowser for help on using the repository browser.