[2228] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using SemWeb;
|
---|
| 6 | using HeuristicLab.GP;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 9 | using System.IO;
|
---|
| 10 | using HeuristicLab.Data;
|
---|
| 11 | using SVM;
|
---|
[2232] | 12 | using HeuristicLab.DataAnalysis;
|
---|
[2228] | 13 |
|
---|
| 14 | namespace CedmaExporter {
|
---|
| 15 | class CedmaExporter {
|
---|
| 16 |
|
---|
[2286] | 17 | public static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
|
---|
[2232] | 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 |
|
---|
[2286] | 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);
|
---|
[2228] | 67 | }
|
---|
[2286] | 68 | catch (FormatException ex) {
|
---|
| 69 | // ignore
|
---|
| 70 | }
|
---|
| 71 | finally {
|
---|
| 72 | writer.WriteLine();
|
---|
| 73 | }
|
---|
[2228] | 74 | }
|
---|
| 75 |
|
---|
[2232] | 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) {
|
---|
[2228] | 80 | var variableImpacts = new List<double>();
|
---|
[2232] | 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])));
|
---|
[2228] | 86 | }
|
---|
| 87 |
|
---|
[2232] | 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 | }
|
---|
[2228] | 94 | }
|
---|
[2232] | 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 | }
|
---|
[2228] | 103 | }
|
---|
| 104 |
|
---|
[2286] | 105 | public static void WriteColumnHeaders(StreamWriter writer) {
|
---|
[2232] | 106 | writer.Write("Id; Filename; TargetVariable; Algorithm;" +
|
---|
[2228] | 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;
|
---|
[2232] | 122 | private Dataset dataset;
|
---|
| 123 | IFunctionTreeExporter treeExporter;
|
---|
[2228] | 124 |
|
---|
[2232] | 125 | public ModelExporter(Dataset ds, string outputDir, bool debugging) {
|
---|
| 126 | this.dataset = ds;
|
---|
[2228] | 127 | this.outputDir = outputDir;
|
---|
| 128 | this.debugging = debugging;
|
---|
[2232] | 129 | treeExporter = new SymbolicExpressionExporter(ds);
|
---|
[2228] | 130 | }
|
---|
| 131 |
|
---|
| 132 | public void Export(string modelFileName, IStorable model) {
|
---|
| 133 | if (debugging) return;
|
---|
[2232] | 134 | foreach (char c in Path.GetInvalidFileNameChars()) {
|
---|
| 135 | modelFileName = modelFileName.Replace(c, '_');
|
---|
| 136 | }
|
---|
[2228] | 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 | }
|
---|