Changeset 2312


Ignore:
Timestamp:
08/26/09 17:40:03 (11 years ago)
Author:
gkronber
Message:

Added CedmaExporter from #715 branch.

Location:
trunk/tools
Files:
5 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/tools/CedmaExporter/CedmaExporter.cs

    r2286 r2312  
    33using System.Linq;
    44using System.Text;
    5 using SemWeb;
    65using HeuristicLab.GP;
    76using HeuristicLab.Core;
     
    1110using SVM;
    1211using HeuristicLab.DataAnalysis;
     12using HeuristicLab.Modeling.Database;
    1313
    1414namespace CedmaExporter {
    1515  class CedmaExporter {
    1616
    17     public static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
     17    public static List<string> WriteVariableImpactHeaders(HeuristicLab.Modeling.Database.SQLServerCompact.DatabaseService database, StreamWriter writer) {
    1818      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("; ");
    2723        }
    2824      }
    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       }
    3625      writer.WriteLine();
    37       return inputVarNames;
     26      return new List<string>(variables.Keys);
    3827    }
    3928
    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) {
    4130      try {
    4231        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;
    4534        string modelFileName = "model_" + targetVariable + "_" + id.ToString("000");
    4635        writer.Write(modelFileName); writer.Write("; ");
    4736        writer.Write(targetVariable); writer.Write("; ");
    4837        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);
    6756      }
    6857      catch (FormatException ex) {
     
    7463    }
    7564
    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);
    8671      }
    8772
    8873      foreach (string varName in inputVariables) {
    8974        if (impacts.ContainsKey(varName)) {
    90           writer.Write(impacts[varName][0]); writer.Write("; ");
     75          writer.Write(FindResult("VariableEvaluationImpact", impacts[varName])); writer.Write("; ");
    9176        } else {
    9277          writer.Write(" ; ");
     
    9681      foreach (string varName in inputVariables) {
    9782        if (impacts.ContainsKey(varName)) {
    98           writer.Write(impacts[varName][1]); writer.Write("; ");
     83          writer.Write(FindResult("VariableQualityImpact", impacts[varName])); writer.Write("; ");
    9984        } else {
    10085          writer.Write(" ; ");
    10186        }
    10287      }
     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;
    10396    }
    10497
     
    111104        "TrainingVAF; ValidationVAF; TestVAF; ");
    112105    }
    113 
    114     private static object LiteralValue(Resource resource) {
    115       return ((Literal)resource).ParseValue();
    116     }
    117106  }
    118 
    119107  class ModelExporter {
    120108    private string outputDir;
    121109    private bool debugging;
    122110    private Dataset dataset;
    123     IFunctionTreeExporter treeExporter;
    124111
    125112    public ModelExporter(Dataset ds, string outputDir, bool debugging) {
     
    127114      this.outputDir = outputDir;
    128115      this.debugging = debugging;
    129       treeExporter = new SymbolicExpressionExporter(ds);
    130116    }
    131117
    132118    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);
    146132    }
    147133  }
  • trunk/tools/CedmaExporter/CedmaExporter.csproj

    r2286 r2312  
    5656      <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Core-3.2.dll</HintPath>
    5757    </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>
    5862    <Reference Include="HeuristicLab.DataAnalysis-3.2, Version=3.2.0.2162, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
    5963      <SpecificVersion>False</SpecificVersion>
     
    7276      <HintPath>..\..\sources\HeuristicLab\bin\x86\Release\plugins\HeuristicLab.Modeling-3.2.dll</HintPath>
    7377    </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>
    7486    <Reference Include="HeuristicLab.PluginInfrastructure, Version=3.2.0.1990, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
    7587      <SpecificVersion>False</SpecificVersion>
     
    8395      <SpecificVersion>False</SpecificVersion>
    8496      <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>
    9397    </Reference>
    9498    <Reference Include="System" />
     
    116120      <DependentUpon>CedmaExporterForm.cs</DependentUpon>
    117121    </Compile>
    118     <Compile Include="Ontology.cs" />
    119122    <Compile Include="Program.cs" />
    120123    <Compile Include="Properties\AssemblyInfo.cs" />
     
    140143      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    141144    </Compile>
    142     <Compile Include="SymbolicExpressionExporter.cs" />
    143145  </ItemGroup>
    144146  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  • trunk/tools/CedmaExporter/CedmaExporterForm.cs

    r2286 r2312  
    88using System.Windows.Forms;
    99using System.IO;
    10 using SemWeb;
    1110using HeuristicLab.Core;
    1211using HeuristicLab.DataAnalysis;
     12using HeuristicLab.Modeling.Database.SQLServerCompact;
    1313
    1414namespace CedmaExporter {
     
    4040      return (sender, args) => {
    4141        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 + "\"";
    4444        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;
    6059          }
    6160        }
  • trunk/tools/CedmaImporter/Importer.cs

    r2285 r2312  
    153153      }
    154154      if (algoName == "SupportVectorRegression") {
    155         HeuristicLab.SupportVectorMachines.SVMModel model = new HeuristicLab.SupportVectorMachines.SVMModel();
    156         model.Model = SVM.Model.Read(Path.Combine(dirName, modelFileName) + ".svm.model.txt");
    157         model.RangeTransform = SVM.RangeTransform.Read(Path.Combine(dirName, modelFileName) + ".svm.transform.txt");
    158         return new HeuristicLab.SupportVectorMachines.Predictor(model, targetVariable);
     155        //HeuristicLab.SupportVectorMachines.SVMModel model = new HeuristicLab.SupportVectorMachines.SVMModel();
     156        //model.Model = SVM.Model.Read(Path.Combine(dirName, modelFileName) + ".svm.model.txt");
     157        //model.RangeTransform = SVM.RangeTransform.Read(Path.Combine(dirName, modelFileName) + ".svm.transform.txt");
     158        //return new HeuristicLab.SupportVectorMachines.Predictor(model, targetVariable);
     159        throw new FormatException();
    159160      } else {
    160161        SymbolicExpressionImporter sexpImporter = new SymbolicExpressionImporter();
  • trunk/tools/Tools.sln

    r2285 r2312  
    1515EndProject
    1616Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CedmaDatabaseMerger", "CedmaDatabaseMerger\CedmaDatabaseMerger.csproj", "{D1D66C9C-5B65-4348-B8B5-528A513695E4}"
     17EndProject
     18Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CedmaExporter", "CedmaExporter\CedmaExporter.csproj", "{59334706-3386-4823-AF9B-B33CD793BA71}"
    1719EndProject
    1820Global
     
    5658    {D1D66C9C-5B65-4348-B8B5-528A513695E4}.Release|x86.ActiveCfg = Release|Any CPU
    5759    {D1D66C9C-5B65-4348-B8B5-528A513695E4}.Release|x86.Build.0 = Release|Any CPU
     60    {59334706-3386-4823-AF9B-B33CD793BA71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
     61    {59334706-3386-4823-AF9B-B33CD793BA71}.Debug|Any CPU.Build.0 = Debug|Any CPU
     62    {59334706-3386-4823-AF9B-B33CD793BA71}.Debug|x86.ActiveCfg = Debug|x86
     63    {59334706-3386-4823-AF9B-B33CD793BA71}.Debug|x86.Build.0 = Debug|x86
     64    {59334706-3386-4823-AF9B-B33CD793BA71}.Release|Any CPU.ActiveCfg = Release|Any CPU
     65    {59334706-3386-4823-AF9B-B33CD793BA71}.Release|Any CPU.Build.0 = Release|Any CPU
     66    {59334706-3386-4823-AF9B-B33CD793BA71}.Release|x86.ActiveCfg = Release|x86
     67    {59334706-3386-4823-AF9B-B33CD793BA71}.Release|x86.Build.0 = Release|x86
    5868  EndGlobalSection
    5969  GlobalSection(SolutionProperties) = preSolution
Note: See TracChangeset for help on using the changeset viewer.