Changeset 2312 for trunk/tools/CedmaExporter
- Timestamp:
- 08/26/09 17:40:03 (15 years ago)
- Location:
- trunk/tools/CedmaExporter
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/CedmaExporter/CedmaExporter.cs
r2286 r2312 3 3 using System.Linq; 4 4 using System.Text; 5 using SemWeb;6 5 using HeuristicLab.GP; 7 6 using HeuristicLab.Core; … … 11 10 using SVM; 12 11 using HeuristicLab.DataAnalysis; 12 using HeuristicLab.Modeling.Database; 13 13 14 14 namespace CedmaExporter { 15 15 class CedmaExporter { 16 16 17 public static List<string> WriteVariableImpactHeaders( Store store, StreamWriter writer) {17 public static List<string> WriteVariableImpactHeaders(HeuristicLab.Modeling.Database.SQLServerCompact.DatabaseService database, StreamWriter writer) { 18 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); 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("; "); 27 23 } 28 24 } 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 25 writer.WriteLine(); 37 return inputVarNames;26 return new List<string>(variables.Keys); 38 27 } 39 28 40 public static void WriteModel( Entity model, int id, Store store, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {29 public static void WriteModel(IModel model, int id, HeuristicLab.Modeling.Database.IModelingDatabase database, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) { 41 30 try { 42 31 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();32 string targetVariable = model.TargetVariable.Name; 33 string algoName = model.Algorithm.Name; 45 34 string modelFileName = "model_" + targetVariable + "_" + id.ToString("000"); 46 35 writer.Write(modelFileName); writer.Write("; "); 47 36 writer.Write(targetVariable); writer.Write("; "); 48 37 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);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); 67 56 } 68 57 catch (FormatException ex) { … … 74 63 } 75 64 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]))); 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); 86 71 } 87 72 88 73 foreach (string varName in inputVariables) { 89 74 if (impacts.ContainsKey(varName)) { 90 writer.Write( impacts[varName][0]); writer.Write("; ");75 writer.Write(FindResult("VariableEvaluationImpact", impacts[varName])); writer.Write("; "); 91 76 } else { 92 77 writer.Write(" ; "); … … 96 81 foreach (string varName in inputVariables) { 97 82 if (impacts.ContainsKey(varName)) { 98 writer.Write( impacts[varName][1]); writer.Write("; ");83 writer.Write(FindResult("VariableQualityImpact", impacts[varName])); writer.Write("; "); 99 84 } else { 100 85 writer.Write(" ; "); 101 86 } 102 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; 103 96 } 104 97 … … 111 104 "TrainingVAF; ValidationVAF; TestVAF; "); 112 105 } 113 114 private static object LiteralValue(Resource resource) {115 return ((Literal)resource).ParseValue();116 }117 106 } 118 119 107 class ModelExporter { 120 108 private string outputDir; 121 109 private bool debugging; 122 110 private Dataset dataset; 123 IFunctionTreeExporter treeExporter;124 111 125 112 public ModelExporter(Dataset ds, string outputDir, bool debugging) { … … 127 114 this.outputDir = outputDir; 128 115 this.debugging = debugging; 129 treeExporter = new SymbolicExpressionExporter(ds);130 116 } 131 117 132 118 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);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); 146 132 } 147 133 } -
trunk/tools/CedmaExporter/CedmaExporter.csproj
r2286 r2312 56 56 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Core-3.2.dll</HintPath> 57 57 </Reference> 58 <Reference Include="HeuristicLab.Data-3.2, Version=3.2.0.1609, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 59 <SpecificVersion>False</SpecificVersion> 60 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Data-3.2.dll</HintPath> 61 </Reference> 58 62 <Reference Include="HeuristicLab.DataAnalysis-3.2, Version=3.2.0.2162, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 59 63 <SpecificVersion>False</SpecificVersion> … … 72 76 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Modeling-3.2.dll</HintPath> 73 77 </Reference> 78 <Reference Include="HeuristicLab.Modeling.Database-3.2, Version=3.2.0.2304, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 79 <SpecificVersion>False</SpecificVersion> 80 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Modeling.Database-3.2.dll</HintPath> 81 </Reference> 82 <Reference Include="HeuristicLab.Modeling.Database.SQLServerCompact-3.2, Version=3.2.0.2306, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 83 <SpecificVersion>False</SpecificVersion> 84 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Modeling.Database.SQLServerCompact-3.2.dll</HintPath> 85 </Reference> 74 86 <Reference Include="HeuristicLab.PluginInfrastructure, Version=3.2.0.1990, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 75 87 <SpecificVersion>False</SpecificVersion> … … 83 95 <SpecificVersion>False</SpecificVersion> 84 96 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\LibSVM.dll</HintPath> 85 </Reference>86 <Reference Include="SemWeb, Version=0.0.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">87 <SpecificVersion>False</SpecificVersion>88 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\SemWeb.dll</HintPath>89 </Reference>90 <Reference Include="SemWeb.SqliteStore, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">91 <SpecificVersion>False</SpecificVersion>92 <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\SemWeb.SqliteStore.dll</HintPath>93 97 </Reference> 94 98 <Reference Include="System" /> … … 116 120 <DependentUpon>CedmaExporterForm.cs</DependentUpon> 117 121 </Compile> 118 <Compile Include="Ontology.cs" />119 122 <Compile Include="Program.cs" /> 120 123 <Compile Include="Properties\AssemblyInfo.cs" /> … … 140 143 <DesignTimeSharedInput>True</DesignTimeSharedInput> 141 144 </Compile> 142 <Compile Include="SymbolicExpressionExporter.cs" />143 145 </ItemGroup> 144 146 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
trunk/tools/CedmaExporter/CedmaExporterForm.cs
r2286 r2312 8 8 using System.Windows.Forms; 9 9 using System.IO; 10 using SemWeb;11 10 using HeuristicLab.Core; 12 11 using HeuristicLab.DataAnalysis; 12 using HeuristicLab.Modeling.Database.SQLServerCompact; 13 13 14 14 namespace CedmaExporter { … … 40 40 return (sender, args) => { 41 41 string inputFileName = fileName; 42 string outputFileName = fileName.Replace(". db3", "") + ".txt";43 string rdfConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";42 string outputFileName = fileName.Replace(".sdf", "") + ".txt"; 43 string sqlCompactConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\""; 44 44 using (StreamWriter writer = File.CreateText(outputFileName)) { 45 using (Store store = Store.Create(rdfConnectionString)) { 46 var dsEntities = store.SelectSubjects(Ontology.InstanceOf, Ontology.TypeDataSet); 47 HeuristicLab.CEDMA.Core.Problem prob = (HeuristicLab.CEDMA.Core.Problem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(((Literal)store.SelectObjects(dsEntities[0], Ontology.SerializedData)[0]).Value)); 48 Dataset ds = prob.Dataset; 49 CedmaExporter.WriteColumnHeaders(writer); 50 List<string> inputVariables = CedmaExporter.WriteVariableImpactHeaders(store, writer); 51 writer.WriteLine(); 52 var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri)); 53 int i = 0; 54 var exporter = new ModelExporter(ds, Path.GetDirectoryName(outputFileName), false); 55 foreach (var model in subjects) { 56 CedmaExporter.WriteModel(model, ++i, store, writer, inputVariables, exporter); 57 worker.ReportProgress((i * 100) / subjects.Count()); 58 if (worker.CancellationPending) return; 59 } 45 DatabaseService database = new DatabaseService("Data Source=" + fileName); 46 database.Connect(); 47 var models = database.GetAllModels(); 48 var dataset = database.GetDataset(); 49 CedmaExporter.WriteColumnHeaders(writer); 50 List<string> inputVariables = CedmaExporter.WriteVariableImpactHeaders(database, writer); 51 writer.WriteLine(); 52 int i = 0; 53 var exporter = new ModelExporter(dataset, Path.GetDirectoryName(outputFileName), true); 54 var allModels = database.GetAllModels(); 55 foreach (HeuristicLab.Modeling.Database.IModel m in allModels) { 56 CedmaExporter.WriteModel(m, ++i, database, writer, inputVariables, exporter); 57 worker.ReportProgress((i * 100) / allModels.Count()); 58 if (worker.CancellationPending) return; 60 59 } 61 60 }
Note: See TracChangeset
for help on using the changeset viewer.