source: branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.cs @ 2232

Last change on this file since 2232 was 2232, checked in by gkronber, 13 years ago

Implemented export of variable impacts and changed GP Tree exporter to use variable names instead of indexes. #715

File size: 8.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using SemWeb;
6using HeuristicLab.GP;
7using HeuristicLab.Core;
8using HeuristicLab.GP.StructureIdentification;
9using System.IO;
10using HeuristicLab.Data;
11using SVM;
12using HeuristicLab.DataAnalysis;
13
14namespace CedmaExporter {
15  class CedmaExporter {
16
17    public static void Export(string inputFileName, string outputFileName) {
18
19      string rdfConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";
20      using (StreamWriter writer = File.CreateText(outputFileName)) {
21        using (Store store = Store.Create(rdfConnectionString)) {
22          var dsEntities = store.SelectSubjects(Ontology.InstanceOf, Ontology.TypeDataSet);
23          HeuristicLab.CEDMA.Core.Problem prob = (HeuristicLab.CEDMA.Core.Problem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(((Literal)store.SelectObjects(dsEntities[0], Ontology.SerializedData)[0]).Value));
24          Dataset ds = prob.Dataset;
25          WriteColumnHeaders(writer);
26          List<string> inputVariables = WriteVariableImpactHeaders(store, writer);
27          writer.WriteLine();
28          WriteModels(store, writer, inputVariables, new ModelExporter(ds, Path.GetDirectoryName(outputFileName), false));
29        }
30      }
31    }
32
33    private static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
34      List<string> inputVarNames = new List<string>();
35      Statement template = new Statement();
36      template.Predicate = Ontology.HasInputVariable;
37      var inputVars = store.Select(template).Select(x => x.Object);
38      foreach (Entity inputVar in inputVars) {
39        var names = store.SelectObjects(inputVar, Ontology.Name);
40        if (names.Count() > 0) {
41          var inputVarName = ((Literal)names[0]).Value;
42          if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
43        }
44      }
45      inputVarNames.Sort();
46      foreach (string inputVarName in inputVarNames) {
47        writer.Write("EvaluationImpact ("); writer.Write(inputVarName); writer.Write("); ");
48      }
49      foreach (string inputVarName in inputVarNames) {
50        writer.Write("QualityImpact ("); writer.Write(inputVarName); writer.Write("); ");
51      }
52      writer.WriteLine();
53      return inputVarNames;
54    }
55
56    private static void WriteModels(Store store, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {
57      var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri));
58      int i = 0;
59      foreach (var model in subjects) {
60        try {
61          writer.Write(i++); writer.Write("; ");
62          string targetVariable = LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString();
63          string algoName = LiteralValue(store.SelectObjects(model, Ontology.Name)[0]).ToString();
64          string modelFileName = "model_" + targetVariable + "_" + i.ToString("000");
65          writer.Write(modelFileName); writer.Write("; ");
66          writer.Write(targetVariable); writer.Write("; ");
67          writer.Write(algoName); writer.Write("; ");
68          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
69          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
70          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
71          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
72          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
73          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
74          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
75          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
76          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
77          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
78          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
79          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
80          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
81          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
82          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
83          WriteVariableImpacts(writer, store, model, inputVariables);
84          var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
85          exporter.Export(modelFileName, data);
86        }
87        catch (FormatException ex) {
88          // ignore
89        }
90        finally {
91          writer.WriteLine();
92        }
93      }
94    }
95
96    private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model, List<string> inputVariables) {
97      var inputVariableEntities = store.SelectObjects(model, Ontology.HasInputVariable);
98      Dictionary<string, List<double>> impacts = new Dictionary<string, List<double>>();
99      foreach (Entity inputVariableEntity in inputVariableEntities) {
100        var variableImpacts = new List<double>();
101        var names = store.SelectObjects(inputVariableEntity, Ontology.Name);
102        if (names.Count() == 0) throw new FormatException();
103        impacts[(string)(LiteralValue(names[0]))] = variableImpacts;
104        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.EvaluationImpact)[0])));
105        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.QualityImpact)[0])));
106      }
107
108      foreach (string varName in inputVariables) {
109        if (impacts.ContainsKey(varName)) {
110          writer.Write(impacts[varName][0]); writer.Write("; ");
111        } else {
112          writer.Write(" ; ");
113        }
114      }
115
116      foreach (string varName in inputVariables) {
117        if (impacts.ContainsKey(varName)) {
118          writer.Write(impacts[varName][1]); writer.Write("; ");
119        } else {
120          writer.Write(" ; ");
121        }
122      }
123    }
124
125    private static void WriteColumnHeaders(StreamWriter writer) {
126      writer.Write("Id; Filename; TargetVariable; Algorithm;" +
127        "TrainingMSE; ValidationMSE; TestMSE; " +
128        "TrainingR2; ValidationR2; TestR2; " +
129        "TrainingMAPE; ValidationMAPE; TestMAPE; " +
130        "TrainingMAPRE; ValidationMAPRE; TestMAPRE; " +
131        "TrainingVAF; ValidationVAF; TestVAF; ");
132    }
133
134    private static object LiteralValue(Resource resource) {
135      return ((Literal)resource).ParseValue();
136    }
137  }
138
139  class ModelExporter {
140    private string outputDir;
141    private bool debugging;
142    private Dataset dataset;
143    IFunctionTreeExporter treeExporter;
144
145    public ModelExporter(Dataset ds, string outputDir, bool debugging) {
146      this.dataset = ds;
147      this.outputDir = outputDir;
148      this.debugging = debugging;
149      treeExporter = new SymbolicExpressionExporter(ds);
150    }
151
152    public void Export(string modelFileName, IStorable model) {
153      if (debugging) return;
154      foreach (char c in Path.GetInvalidFileNameChars()) {
155        modelFileName = modelFileName.Replace(c, '_');
156      }
157      if (model is IFunctionTree) {
158        using (StreamWriter writer = File.CreateText(Path.Combine(outputDir, modelFileName + ".gp.txt"))) {
159          writer.Write(treeExporter.Export((IFunctionTree)model));
160        }
161      } else if (model is SVMModel) {
162        SVMModel svmModel = (SVMModel)model;
163        RangeTransform.Write(Path.Combine(outputDir, modelFileName + ".svm.transform.txt"), svmModel.RangeTransform);
164        SVM.Model.Write(Path.Combine(outputDir, modelFileName + ".svm.model.txt"), svmModel.Model);
165      } else throw new NotSupportedException("This type of model is not supported by the CedmaExporter: " + model);
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.