Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/CedmaExporter/CedmaExporter.cs @ 2552

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

Added CedmaExporter from #715 branch.

File size: 6.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.GP;
6using HeuristicLab.Core;
7using HeuristicLab.GP.StructureIdentification;
8using System.IO;
9using HeuristicLab.Data;
10using SVM;
11using HeuristicLab.DataAnalysis;
12using HeuristicLab.Modeling.Database;
13
14namespace CedmaExporter {
15  class CedmaExporter {
16
17    public static List<string> WriteVariableImpactHeaders(HeuristicLab.Modeling.Database.SQLServerCompact.DatabaseService database, StreamWriter writer) {
18      List<string> inputVarNames = new List<string>();
19      var variables = database.GetAllVariables();
20      foreach (var r in database.GetAllResultsForInputVariables()) {
21        foreach (string varName in variables.Keys) {
22          writer.Write(r.Name); writer.Write(": "); writer.Write(varName); writer.Write("; ");
23        }
24      }
25      writer.WriteLine();
26      return new List<string>(variables.Keys);
27    }
28
29    public static void WriteModel(IModel model, int id, HeuristicLab.Modeling.Database.IModelingDatabase database, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {
30      try {
31        writer.Write(id); writer.Write("; ");
32        string targetVariable = model.TargetVariable.Name;
33        string algoName = model.Algorithm.Name;
34        string modelFileName = "model_" + targetVariable + "_" + id.ToString("000");
35        writer.Write(modelFileName); writer.Write("; ");
36        writer.Write(targetVariable); writer.Write("; ");
37        writer.Write(algoName); writer.Write("; ");
38        var modelResults = database.GetModelResults(model);
39        writer.Write(FindResult("TrainingMeanSquaredError", modelResults)); writer.Write("; ");
40        writer.Write(FindResult("ValidationMeanSquaredError", modelResults)); writer.Write("; ");
41        writer.Write(FindResult("TestMeanSquaredError", modelResults)); writer.Write("; ");
42        writer.Write(FindResult("TrainingCoefficientOfDetermination", modelResults)); writer.Write("; ");
43        writer.Write(FindResult("ValidationCoefficientOfDetermination", modelResults)); writer.Write("; ");
44        writer.Write(FindResult("TestCoefficientOfDetermination", modelResults)); writer.Write("; ");
45        writer.Write(FindResult("TrainingMeanAbsolutePercentageError", modelResults)); writer.Write("; ");
46        writer.Write(FindResult("ValidationMeanAbsolutePercentageError", modelResults)); writer.Write("; ");
47        writer.Write(FindResult("TestMeanAbsolutePercentageError", modelResults)); writer.Write("; ");
48        writer.Write(FindResult("TrainingMeanAbsolutePercentageOfRangeError", modelResults)); writer.Write("; ");
49        writer.Write(FindResult("ValidationMeanAbsolutePercentageOfRangeError", modelResults)); writer.Write("; ");
50        writer.Write(FindResult("TestMeanAbsolutePercentageOfRangeError", modelResults)); writer.Write("; ");
51        writer.Write(FindResult("TrainingVarianceAccountedFor", modelResults)); writer.Write("; ");
52        writer.Write(FindResult("ValidationVarianceAccountedFor", modelResults)); writer.Write("; ");
53        writer.Write(FindResult("TestVarianceAccountedFor", modelResults)); writer.Write("; ");
54        WriteVariableImpacts(writer, database, model, inputVariables);
55        // exporter.Export(modelFileName, data);
56      }
57      catch (FormatException ex) {
58        // ignore
59      }
60      finally {
61        writer.WriteLine();
62      }
63    }
64
65    private static void WriteVariableImpacts(StreamWriter writer, IModelingDatabase database, IModel model, List<string> inputVariables) {
66      Dictionary<string, List<IInputVariableResult>> impacts = new Dictionary<string, List<IInputVariableResult>>();
67      foreach (var inputVariableResult in database.GetInputVariableResults(model)) {
68        if (!impacts.ContainsKey(inputVariableResult.Variable.Name))
69          impacts[inputVariableResult.Variable.Name] = new List<IInputVariableResult>();
70        impacts[inputVariableResult.Variable.Name].Add(inputVariableResult);
71      }
72
73      foreach (string varName in inputVariables) {
74        if (impacts.ContainsKey(varName)) {
75          writer.Write(FindResult("VariableEvaluationImpact", impacts[varName])); writer.Write("; ");
76        } else {
77          writer.Write(" ; ");
78        }
79      }
80
81      foreach (string varName in inputVariables) {
82        if (impacts.ContainsKey(varName)) {
83          writer.Write(FindResult("VariableQualityImpact", impacts[varName])); writer.Write("; ");
84        } else {
85          writer.Write(" ; ");
86        }
87      }
88    }
89
90    private static double FindResult(string p, List<IInputVariableResult> rs) {
91      return rs.First(e => e.Result.Name == p).Value;
92    }
93
94    private static double FindResult(string p, IEnumerable<IModelResult> rs) {
95      return rs.First(e => e.Result.Name == p).Value;
96    }
97
98    public static void WriteColumnHeaders(StreamWriter writer) {
99      writer.Write("Id; Filename; TargetVariable; Algorithm;" +
100        "TrainingMSE; ValidationMSE; TestMSE; " +
101        "TrainingR2; ValidationR2; TestR2; " +
102        "TrainingMAPE; ValidationMAPE; TestMAPE; " +
103        "TrainingMAPRE; ValidationMAPRE; TestMAPRE; " +
104        "TrainingVAF; ValidationVAF; TestVAF; ");
105    }
106  }
107  class ModelExporter {
108    private string outputDir;
109    private bool debugging;
110    private Dataset dataset;
111
112    public ModelExporter(Dataset ds, string outputDir, bool debugging) {
113      this.dataset = ds;
114      this.outputDir = outputDir;
115      this.debugging = debugging;
116    }
117
118    public void Export(string modelFileName, IStorable model) {
119      //if (debugging) return;
120      //foreach (char c in Path.GetInvalidFileNameChars()) {
121      //  modelFileName = modelFileName.Replace(c, '_');
122      //}
123      //if (model is Predictor) {
124      //  using (StreamWriter writer = File.CreateText(Path.Combine(outputDir, modelFileName + ".gp.txt"))) {
125      //    writer.Write(treeExporter.Export(((Predictor)model).FunctionTree);
126      //  }
127      //} else if (model is HeuristicLab.SupportVectorMachines.Predictor) {
128      //  SVMModel svmModel = (SVMModel)model;
129      //  RangeTransform.Write(Path.Combine(outputDir, modelFileName + ".svm.transform.txt"), svmModel.RangeTransform);
130      //  SVM.Model.Write(Path.Combine(outputDir, modelFileName + ".svm.model.txt"), svmModel.Model);
131      //} else throw new NotSupportedException("This type of model is not supported by the CedmaExporter: " + model);
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.