Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed a bug in the s-expression exporter. Improved exporter UI. #715 & #719

File size: 7.6 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 List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
18      List<string> inputVarNames = new List<string>();
19      Statement template = new Statement();
20      template.Predicate = Ontology.HasInputVariable;
21      var inputVars = store.Select(template).Select(x => x.Object);
22      foreach (Entity inputVar in inputVars) {
23        var names = store.SelectObjects(inputVar, Ontology.Name);
24        if (names.Count() > 0) {
25          var inputVarName = ((Literal)names[0]).Value;
26          if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
27        }
28      }
29      inputVarNames.Sort();
30      foreach (string inputVarName in inputVarNames) {
31        writer.Write("EvaluationImpact ("); writer.Write(inputVarName); writer.Write("); ");
32      }
33      foreach (string inputVarName in inputVarNames) {
34        writer.Write("QualityImpact ("); writer.Write(inputVarName); writer.Write("); ");
35      }
36      writer.WriteLine();
37      return inputVarNames;
38    }
39
40    public static void WriteModel(Entity model, int id, Store store, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {
41      try {
42        writer.Write(id); writer.Write("; ");
43        string targetVariable = LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString();
44        string algoName = LiteralValue(store.SelectObjects(model, Ontology.Name)[0]).ToString();
45        string modelFileName = "model_" + targetVariable + "_" + id.ToString("000");
46        writer.Write(modelFileName); writer.Write("; ");
47        writer.Write(targetVariable); writer.Write("; ");
48        writer.Write(algoName); writer.Write("; ");
49        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
50        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
51        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
52        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
53        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
54        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
55        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
56        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
57        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
58        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
59        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
60        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
61        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
62        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
63        writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
64        WriteVariableImpacts(writer, store, model, inputVariables);
65        var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
66        exporter.Export(modelFileName, data);
67      }
68      catch (FormatException ex) {
69        // ignore
70      }
71      finally {
72        writer.WriteLine();
73      }
74    }
75
76    private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model, List<string> inputVariables) {
77      var inputVariableEntities = store.SelectObjects(model, Ontology.HasInputVariable);
78      Dictionary<string, List<double>> impacts = new Dictionary<string, List<double>>();
79      foreach (Entity inputVariableEntity in inputVariableEntities) {
80        var variableImpacts = new List<double>();
81        var names = store.SelectObjects(inputVariableEntity, Ontology.Name);
82        if (names.Count() == 0) throw new FormatException();
83        impacts[(string)(LiteralValue(names[0]))] = variableImpacts;
84        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.EvaluationImpact)[0])));
85        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.QualityImpact)[0])));
86      }
87
88      foreach (string varName in inputVariables) {
89        if (impacts.ContainsKey(varName)) {
90          writer.Write(impacts[varName][0]); writer.Write("; ");
91        } else {
92          writer.Write(" ; ");
93        }
94      }
95
96      foreach (string varName in inputVariables) {
97        if (impacts.ContainsKey(varName)) {
98          writer.Write(impacts[varName][1]); writer.Write("; ");
99        } else {
100          writer.Write(" ; ");
101        }
102      }
103    }
104
105    public static void WriteColumnHeaders(StreamWriter writer) {
106      writer.Write("Id; Filename; TargetVariable; Algorithm;" +
107        "TrainingMSE; ValidationMSE; TestMSE; " +
108        "TrainingR2; ValidationR2; TestR2; " +
109        "TrainingMAPE; ValidationMAPE; TestMAPE; " +
110        "TrainingMAPRE; ValidationMAPRE; TestMAPRE; " +
111        "TrainingVAF; ValidationVAF; TestVAF; ");
112    }
113
114    private static object LiteralValue(Resource resource) {
115      return ((Literal)resource).ParseValue();
116    }
117  }
118
119  class ModelExporter {
120    private string outputDir;
121    private bool debugging;
122    private Dataset dataset;
123    IFunctionTreeExporter treeExporter;
124
125    public ModelExporter(Dataset ds, string outputDir, bool debugging) {
126      this.dataset = ds;
127      this.outputDir = outputDir;
128      this.debugging = debugging;
129      treeExporter = new SymbolicExpressionExporter(ds);
130    }
131
132    public void Export(string modelFileName, IStorable model) {
133      if (debugging) return;
134      foreach (char c in Path.GetInvalidFileNameChars()) {
135        modelFileName = modelFileName.Replace(c, '_');
136      }
137      if (model is IFunctionTree) {
138        using (StreamWriter writer = File.CreateText(Path.Combine(outputDir, modelFileName + ".gp.txt"))) {
139          writer.Write(treeExporter.Export((IFunctionTree)model));
140        }
141      } else if (model is SVMModel) {
142        SVMModel svmModel = (SVMModel)model;
143        RangeTransform.Write(Path.Combine(outputDir, modelFileName + ".svm.transform.txt"), svmModel.RangeTransform);
144        SVM.Model.Write(Path.Combine(outputDir, modelFileName + ".svm.model.txt"), svmModel.Model);
145      } else throw new NotSupportedException("This type of model is not supported by the CedmaExporter: " + model);
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.