Changeset 2232


Ignore:
Timestamp:
08/04/09 12:26:53 (12 years ago)
Author:
gkronber
Message:

Implemented export of variable impacts and changed GP Tree exporter to use variable names instead of indexes. #715

Location:
branches/CEDMA-Exporter-715/tools/CedmaExporter
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • branches/CEDMA-Exporter-715/tools/CedmaExporter

    • Property svn:ignore
      •  

        old new  
        11bin
        22obj
         3*.user
  • branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.cs

    r2228 r2232  
    1010using HeuristicLab.Data;
    1111using SVM;
     12using HeuristicLab.DataAnalysis;
    1213
    1314namespace CedmaExporter {
     
    1920      using (StreamWriter writer = File.CreateText(outputFileName)) {
    2021        using (Store store = Store.Create(rdfConnectionString)) {
    21           WriteColumnHeaders(store, writer);
    22           WriteModels(store, writer, new ModelExporter(Path.GetDirectoryName(outputFileName), true));
     22          var dsEntities = store.SelectSubjects(Ontology.InstanceOf, Ontology.TypeDataSet);
     23          HeuristicLab.CEDMA.Core.Problem prob = (HeuristicLab.CEDMA.Core.Problem)PersistenceManager.RestoreFromGZip(Convert.FromBase64String(((Literal)store.SelectObjects(dsEntities[0], Ontology.SerializedData)[0]).Value));
     24          Dataset ds = prob.Dataset;
     25          WriteColumnHeaders(writer);
     26          List<string> inputVariables = WriteVariableImpactHeaders(store, writer);
     27          writer.WriteLine();
     28          WriteModels(store, writer, inputVariables, new ModelExporter(ds, Path.GetDirectoryName(outputFileName), false));
    2329        }
    2430      }
    2531    }
    2632
    27     private static void WriteModels(Store store, StreamWriter writer, ModelExporter exporter) {
     33    private static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
     34      List<string> inputVarNames = new List<string>();
     35      Statement template = new Statement();
     36      template.Predicate = Ontology.HasInputVariable;
     37      var inputVars = store.Select(template).Select(x => x.Object);
     38      foreach (Entity inputVar in inputVars) {
     39        var names = store.SelectObjects(inputVar, Ontology.Name);
     40        if (names.Count() > 0) {
     41          var inputVarName = ((Literal)names[0]).Value;
     42          if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
     43        }
     44      }
     45      inputVarNames.Sort();
     46      foreach (string inputVarName in inputVarNames) {
     47        writer.Write("EvaluationImpact ("); writer.Write(inputVarName); writer.Write("); ");
     48      }
     49      foreach (string inputVarName in inputVarNames) {
     50        writer.Write("QualityImpact ("); writer.Write(inputVarName); writer.Write("); ");
     51      }
     52      writer.WriteLine();
     53      return inputVarNames;
     54    }
     55
     56    private static void WriteModels(Store store, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {
    2857      var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri));
    2958      int i = 0;
    3059      foreach (var model in subjects) {
    31         writer.Write(i++); writer.Write("; ");
    32         string modelFileName = "model_" + i.ToString();
    33         writer.Write(modelFileName); writer.Write("; ");
    34         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString()); writer.Write("; ");
    35         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
    36         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
    37         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
    38         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
    39         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
    40         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
    41         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
    42         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
    43         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
    44         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
    45         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
    46         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
    47         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
    48         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
    49         writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
    50         WriteVariableImpacts(writer, store, model);
    51         writer.WriteLine();
    52         var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
    53         exporter.Export(modelFileName, data);
     60        try {
     61          writer.Write(i++); writer.Write("; ");
     62          string targetVariable = LiteralValue(store.SelectObjects(model, Ontology.TargetVariable)[0]).ToString();
     63          string algoName = LiteralValue(store.SelectObjects(model, Ontology.Name)[0]).ToString();
     64          string modelFileName = "model_" + targetVariable + "_" + i.ToString("000");
     65          writer.Write(modelFileName); writer.Write("; ");
     66          writer.Write(targetVariable); writer.Write("; ");
     67          writer.Write(algoName); writer.Write("; ");
     68          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanSquaredError)[0]).ToString()); writer.Write("; ");
     69          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanSquaredError)[0]).ToString()); writer.Write("; ");
     70          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanSquaredError)[0]).ToString()); writer.Write("; ");
     71          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
     72          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
     73          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestCoefficientOfDetermination)[0]).ToString()); writer.Write("; ");
     74          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
     75          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
     76          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageError)[0]).ToString()); writer.Write("; ");
     77          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
     78          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
     79          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestMeanAbsolutePercentageOfRangeError)[0]).ToString()); writer.Write("; ");
     80          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TrainingVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
     81          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.ValidationVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
     82          writer.Write(LiteralValue(store.SelectObjects(model, Ontology.TestVarianceAccountedFor)[0]).ToString()); writer.Write("; ");
     83          WriteVariableImpacts(writer, store, model, inputVariables);
     84          var data = PersistenceManager.RestoreFromGZip(Convert.FromBase64String((string)LiteralValue(store.SelectObjects(model, Ontology.SerializedData)[0])));
     85          exporter.Export(modelFileName, data);
     86        }
     87        catch (FormatException ex) {
     88          // ignore
     89        }
     90        finally {
     91          writer.WriteLine();
     92        }
    5493      }
    5594    }
    5695
    57     private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model) {
    58       var inputVariables = store.SelectObjects(model, Ontology.HasInputVariable);
    59       Dictionary<string, List<double>> impacts = new Dictionary<string,List<double>>();
    60       foreach (Entity inputVariable in inputVariables) {
     96    private static void WriteVariableImpacts(StreamWriter writer, Store store, Entity model, List<string> inputVariables) {
     97      var inputVariableEntities = store.SelectObjects(model, Ontology.HasInputVariable);
     98      Dictionary<string, List<double>> impacts = new Dictionary<string, List<double>>();
     99      foreach (Entity inputVariableEntity in inputVariableEntities) {
    61100        var variableImpacts = new List<double>();
    62         impacts[inputVariable.ToString()] = variableImpacts;
    63         variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariable, Ontology.EvaluationImpact)[0])));
    64         variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariable, Ontology.QualityImpact)[0])));
     101        var names = store.SelectObjects(inputVariableEntity, Ontology.Name);
     102        if (names.Count() == 0) throw new FormatException();
     103        impacts[(string)(LiteralValue(names[0]))] = variableImpacts;
     104        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.EvaluationImpact)[0])));
     105        variableImpacts.Add((double)(LiteralValue(store.SelectObjects(inputVariableEntity, Ontology.QualityImpact)[0])));
    65106      }
    66107
    67       foreach (string varName in impacts.Keys.OrderBy(x => x)) {
    68         writer.Write(impacts[varName][0]); writer.Write("; ");
    69         writer.Write(impacts[varName][1]); writer.Write("; ");
     108      foreach (string varName in inputVariables) {
     109        if (impacts.ContainsKey(varName)) {
     110          writer.Write(impacts[varName][0]); writer.Write("; ");
     111        } else {
     112          writer.Write(" ; ");
     113        }
     114      }
     115
     116      foreach (string varName in inputVariables) {
     117        if (impacts.ContainsKey(varName)) {
     118          writer.Write(impacts[varName][1]); writer.Write("; ");
     119        } else {
     120          writer.Write(" ; ");
     121        }
    70122      }
    71123    }
    72124
    73     private static void WriteColumnHeaders(Store store, StreamWriter writer) {
    74       writer.Write("Id; Filename; TargetVariable; " +
     125    private static void WriteColumnHeaders(StreamWriter writer) {
     126      writer.Write("Id; Filename; TargetVariable; Algorithm;" +
    75127        "TrainingMSE; ValidationMSE; TestMSE; " +
    76128        "TrainingR2; ValidationR2; TestR2; " +
     
    78130        "TrainingMAPRE; ValidationMAPRE; TestMAPRE; " +
    79131        "TrainingVAF; ValidationVAF; TestVAF; ");
    80 
    81       List<string> inputVarNames = new List<string>();
    82       Statement template = new Statement();
    83       template.Predicate = Ontology.HasInputVariable;
    84       var inputVars = store.Select(template).Select(x => x.Object);
    85       foreach (Entity inputVar in inputVars) {
    86         var inputVarName = ((Literal)store.SelectObjects(inputVar, Ontology.Name)[0]).Value;
    87         if (!inputVarNames.Contains(inputVarName)) inputVarNames.Add(inputVarName);
    88       }
    89       inputVarNames.Sort();
    90       foreach (string inputVarName in inputVarNames) {
    91         writer.Write(inputVarName); writer.Write(": EvaluationImpact; ");
    92         writer.Write(inputVarName); writer.Write(": QualityImpact; ");
    93       }
    94       writer.WriteLine();
    95132    }
    96133
     
    103140    private string outputDir;
    104141    private bool debugging;
    105     IFunctionTreeExporter treeExporter = new SymbolicExpressionExporter();
     142    private Dataset dataset;
     143    IFunctionTreeExporter treeExporter;
    106144
    107     public ModelExporter(string outputDir, bool debugging) {
     145    public ModelExporter(Dataset ds, string outputDir, bool debugging) {
     146      this.dataset = ds;
    108147      this.outputDir = outputDir;
    109148      this.debugging = debugging;
     149      treeExporter = new SymbolicExpressionExporter(ds);
    110150    }
    111151
    112152    public void Export(string modelFileName, IStorable model) {
    113153      if (debugging) return;
     154      foreach (char c in Path.GetInvalidFileNameChars()) {
     155        modelFileName = modelFileName.Replace(c, '_');
     156      }
    114157      if (model is IFunctionTree) {
    115158        using (StreamWriter writer = File.CreateText(Path.Combine(outputDir, modelFileName + ".gp.txt"))) {
  • branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.csproj

    r2228 r2232  
    3232  </PropertyGroup>
    3333  <ItemGroup>
     34    <Reference Include="HeuristicLab.CEDMA.Core-3.3, Version=3.3.0.2146, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     35      <SpecificVersion>False</SpecificVersion>
     36      <HintPath>..\..\sources\HeuristicLab\bin\Debug\plugins\HeuristicLab.CEDMA.Core-3.3.dll</HintPath>
     37    </Reference>
    3438    <Reference Include="HeuristicLab.Core-3.2, Version=3.2.0.2143, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    3539      <SpecificVersion>False</SpecificVersion>
    3640      <HintPath>..\..\..\..\..\Program Files\HeuristicLab 3.0\plugins\HeuristicLab.Core-3.2.dll</HintPath>
     41    </Reference>
     42    <Reference Include="HeuristicLab.Data-3.2, Version=3.2.0.1609, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     43      <SpecificVersion>False</SpecificVersion>
     44      <HintPath>..\..\sources\HeuristicLab\bin\Debug\plugins\HeuristicLab.Data-3.2.dll</HintPath>
     45    </Reference>
     46    <Reference Include="HeuristicLab.DataAnalysis-3.2, Version=3.2.0.2162, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     47      <SpecificVersion>False</SpecificVersion>
     48      <HintPath>..\..\sources\HeuristicLab.DataAnalysis\3.2\bin\Debug\HeuristicLab.DataAnalysis-3.2.dll</HintPath>
    3749    </Reference>
    3850    <Reference Include="HeuristicLab.GP-3.3, Version=3.3.0.1534, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     
    105117      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    106118    </Compile>
     119    <Compile Include="SymbolicExpressionExporter.cs" />
    107120  </ItemGroup>
    108121  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Note: See TracChangeset for help on using the changeset viewer.