Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added first crude implementation of CEDMA exporter. #715

File size: 6.9 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;
12
13namespace CedmaExporter {
14  class CedmaExporter {
15
16    public static void Export(string inputFileName, string outputFileName) {
17
18      string rdfConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";
19      using (StreamWriter writer = File.CreateText(outputFileName)) {
20        using (Store store = Store.Create(rdfConnectionString)) {
21          WriteColumnHeaders(store, writer);
22          WriteModels(store, writer, new ModelExporter(Path.GetDirectoryName(outputFileName), true));
23        }
24      }
25    }
26
27    private static void WriteModels(Store store, StreamWriter writer, ModelExporter exporter) {
28      var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri));
29      int i = 0;
30      foreach (var model in subjects) {
31        writer.Write(i++); writer.Write("; ");
32        string modelFileName = "model_" + i.ToString();
33        writer.Write(modelFileName); writer.Write("; ");
34        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString()); writer.Write("; ");
35        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
36        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
37        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
38        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
39        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
40        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
41        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
42        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
43        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
44        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
45        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
46        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
47        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
48        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
49        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
50        WriteVariableImpacts(writer, store, model);
51        writer.WriteLine();
52        var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
53        exporter.Export(modelFileName, data);
54      }
55    }
56
57    private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model) {
58      var inputVariables = store.SelectObjects(model, Ontology.HasInputVariable);
59      Dictionary<string, List<double>> impacts = new Dictionary<string,List<double>>();
60      foreach (Entity inputVariable in inputVariables) {
61        var variableImpacts = new List<double>();
62        impacts[inputVariable.ToString()] = variableImpacts;
63        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariable, Ontology.EvaluationImpact)[0])));
64        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariable, Ontology.QualityImpact)[0])));
65      }
66
67      foreach (string varName in impacts.Keys.OrderBy(x => x)) {
68        writer.Write(impacts[varName][0]); writer.Write("; ");
69        writer.Write(impacts[varName][1]); writer.Write("; ");
70      }
71    }
72
73    private static void WriteColumnHeaders(Store store, StreamWriter writer) {
74      writer.Write("Id; Filename; TargetVariable; " +
75        "TrainingMSE; ValidationMSE; TestMSE; " +
76        "TrainingR2; ValidationR2; TestR2; " +
77        "TrainingMAPE; ValidationMAPE; TestMAPE; " +
78        "TrainingMAPRE; ValidationMAPRE; TestMAPRE; " +
79        "TrainingVAF; ValidationVAF; TestVAF; ");
80
81      List<string> inputVarNames = new List<string>();
82      Statement template = new Statement();
83      template.Predicate = Ontology.HasInputVariable;
84      var inputVars = store.Select(template).Select(x => x.Object);
85      foreach (Entity inputVar in inputVars) {
86        var inputVarName = ((Literal)store.SelectObjects(inputVar, Ontology.Name)[0]).Value;
87        if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
88      }
89      inputVarNames.Sort();
90      foreach (string inputVarName in inputVarNames) {
91        writer.Write(inputVarName); writer.Write(": EvaluationImpact; ");
92        writer.Write(inputVarName); writer.Write(": QualityImpact; ");
93      }
94      writer.WriteLine();
95    }
96
97    private static object LiteralValue(Resource resource) {
98      return ((Literal)resource).ParseValue();
99    }
100  }
101
102  class ModelExporter {
103    private string outputDir;
104    private bool debugging;
105    IFunctionTreeExporter treeExporter = new SymbolicExpressionExporter();
106
107    public ModelExporter(string outputDir, bool debugging) {
108      this.outputDir = outputDir;
109      this.debugging = debugging;
110    }
111
112    public void Export(string modelFileName, IStorable model) {
113      if (debugging) return;
114      if (model is IFunctionTree) {
115        using (StreamWriter writer = File.CreateText(Path.Combine(outputDir, modelFileName + ".gp.txt"))) {
116          writer.Write(treeExporter.Export((IFunctionTree)model));
117        }
118      } else if (model is SVMModel) {
119        SVMModel svmModel = (SVMModel)model;
120        RangeTransform.Write(Path.Combine(outputDir, modelFileName + ".svm.transform.txt"), svmModel.RangeTransform);
121        SVM.Model.Write(Path.Combine(outputDir, modelFileName + ".svm.model.txt"), svmModel.Model);
122      } else throw new NotSupportedException("This type of model is not supported by the CedmaExporter: " + model);
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.