Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2286


Ignore:
Timestamp:
08/14/09 11:13:52 (15 years ago)
Author:
gkronber
Message:

Fixed a bug in the s-expression exporter. Improved exporter UI. #715 & #719

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

Legend:

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

    r2232 r2286  
    1515  class CedmaExporter {
    1616
    17     public static void Export(string inputFileName, string outputFileName) {
    18 
    19       string rdfConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";
    20       using (StreamWriter writer = File.CreateText(outputFileName)) {
    21         using (Store store = Store.Create(rdfConnectionString)) {
    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));
    29         }
    30       }
    31     }
    32 
    33     private static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
     17    public static List<string> WriteVariableImpactHeaders(Store store, StreamWriter writer) {
    3418      List<string> inputVarNames = new List<string>();
    3519      Statement template = new Statement();
     
    5438    }
    5539
    56     private static void WriteModels(Store store, StreamWriter writer, List<string> inputVariables, ModelExporter exporter) {
    57       var subjects = store.SelectSubjects(new Entity(Ontology.InstanceOf.Uri), new Entity(Ontology.TypeModel.Uri));
    58       int i = 0;
    59       foreach (var model in subjects) {
    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         }
     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);
     67      }
     68      catch (FormatException ex) {
     69        // ignore
     70      }
     71      finally {
     72        writer.WriteLine();
    9373      }
    9474    }
     
    123103    }
    124104
    125     private static void WriteColumnHeaders(StreamWriter writer) {
     105    public static void WriteColumnHeaders(StreamWriter writer) {
    126106      writer.Write("Id; Filename; TargetVariable; Algorithm;" +
    127107        "TrainingMSE; ValidationMSE; TestMSE; " +
  • branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporter.csproj

    r2262 r2286  
    119119    <Compile Include="Program.cs" />
    120120    <Compile Include="Properties\AssemblyInfo.cs" />
     121    <EmbeddedResource Include="CedmaExporterForm.resx">
     122      <DependentUpon>CedmaExporterForm.cs</DependentUpon>
     123    </EmbeddedResource>
    121124    <EmbeddedResource Include="Properties\Resources.resx">
    122125      <Generator>ResXFileCodeGenerator</Generator>
  • branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporterForm.Designer.cs

    r2228 r2286  
    2424    /// </summary>
    2525    private void InitializeComponent() {
    26       this.components = new System.ComponentModel.Container();
     26      this.exportButton = new System.Windows.Forms.Button();
     27      this.progressBar = new System.Windows.Forms.ProgressBar();
     28      this.cancelButton = new System.Windows.Forms.Button();
     29      this.SuspendLayout();
     30      //
     31      // exportButton
     32      //
     33      this.exportButton.Location = new System.Drawing.Point(12, 12);
     34      this.exportButton.Name = "exportButton";
     35      this.exportButton.Size = new System.Drawing.Size(75, 23);
     36      this.exportButton.TabIndex = 0;
     37      this.exportButton.Text = "Export...";
     38      this.exportButton.UseVisualStyleBackColor = true;
     39      this.exportButton.Click += new System.EventHandler(this.exportButton_Click);
     40      //
     41      // progressBar
     42      //
     43      this.progressBar.Location = new System.Drawing.Point(12, 41);
     44      this.progressBar.Name = "progressBar";
     45      this.progressBar.Size = new System.Drawing.Size(260, 23);
     46      this.progressBar.TabIndex = 1;
     47      //
     48      // cancelButton
     49      //
     50      this.cancelButton.Enabled = false;
     51      this.cancelButton.Location = new System.Drawing.Point(94, 11);
     52      this.cancelButton.Name = "cancelButton";
     53      this.cancelButton.Size = new System.Drawing.Size(75, 23);
     54      this.cancelButton.TabIndex = 2;
     55      this.cancelButton.Text = "Cancel";
     56      this.cancelButton.UseVisualStyleBackColor = true;
     57      this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
     58      //
     59      // CedmaExporterForm
     60      //
     61      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    2762      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    28       this.Text = "Form1";
     63      this.ClientSize = new System.Drawing.Size(284, 81);
     64      this.Controls.Add(this.cancelButton);
     65      this.Controls.Add(this.progressBar);
     66      this.Controls.Add(this.exportButton);
     67      this.Name = "CedmaExporterForm";
     68      this.Text = "CEDMA Exporter";
     69      this.ResumeLayout(false);
     70
    2971    }
    3072
    3173    #endregion
     74
     75    private System.Windows.Forms.Button exportButton;
     76    private System.Windows.Forms.ProgressBar progressBar;
     77    private System.Windows.Forms.Button cancelButton;
    3278  }
    3379}
  • branches/CEDMA-Exporter-715/tools/CedmaExporter/CedmaExporterForm.cs

    r2228 r2286  
    77using System.Text;
    88using System.Windows.Forms;
     9using System.IO;
     10using SemWeb;
     11using HeuristicLab.Core;
     12using HeuristicLab.DataAnalysis;
    913
    1014namespace CedmaExporter {
    1115  public partial class CedmaExporterForm : Form {
    12 
     16    private BackgroundWorker worker;
    1317    public CedmaExporterForm() {
    1418      InitializeComponent();
     19    }
    1520
     21    private void exportButton_Click(object sender, EventArgs e) {
    1622      var dialog = new OpenFileDialog();
    1723      DialogResult result;
    18       do {
    19         result = dialog.ShowDialog();
    20         if (result == DialogResult.OK) {
    21           string fileName = dialog.FileName;
    22           CedmaExporter.Export(fileName, fileName.Replace(".db3", "") + ".txt");
     24      result = dialog.ShowDialog();
     25      if (result == DialogResult.OK) {
     26        string fileName = dialog.FileName;
     27        exportButton.Enabled = false;
     28        cancelButton.Enabled = true;
     29        worker = new BackgroundWorker();
     30        worker.WorkerReportsProgress = true;
     31        worker.WorkerSupportsCancellation = true;
     32        worker.DoWork += CreateWorkerDelegate(worker, fileName);
     33        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
     34        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
     35        worker.RunWorkerAsync();
     36      }
     37    }
     38
     39    private DoWorkEventHandler CreateWorkerDelegate(BackgroundWorker worker, string fileName) {
     40      return (sender, args) => {
     41        string inputFileName = fileName;
     42        string outputFileName = fileName.Replace(".db3", "") + ".txt";
     43        string rdfConnectionString = "sqlite:rdf:Data Source=\"" + inputFileName + "\"";
     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            }
     60          }
    2361        }
    24       } while (result != DialogResult.Cancel);
    25       Application.Exit();
     62      };
     63    }
     64
     65    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
     66      exportButton.Enabled = true;
     67      cancelButton.Enabled = false;
     68      progressBar.Value = 0;
     69    }
     70
     71    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
     72      progressBar.Value = e.ProgressPercentage;
     73    }
     74
     75    private void cancelButton_Click(object sender, EventArgs e) {
     76      worker.CancelAsync();
    2677    }
    2778  }
  • branches/CEDMA-Exporter-715/tools/CedmaExporter/SymbolicExpressionExporter.cs

    r2262 r2286  
    5757        exported = Export(tree);
    5858        return true;
    59       } catch(UnknownFunctionException) {
     59      }
     60      catch (UnknownFunctionException) {
    6061        exported = "";
    6162        return false;
     
    6566    private void BuildExportString(IFunctionTree tree) {
    6667      builder.Append(currentIndent);
    67       builder.Append("(" + ExportFunction(tree.Function, tree) + " ");
    68       currentIndent += "  ";
    69       foreach(IFunctionTree subTree in tree.SubTrees) {
    70         builder.Append("\n");
    71         BuildExportString(subTree);
    72       }
    73       builder.Append(")");
    74       currentIndent = currentIndent.Remove(0, 2);
     68      if (tree.SubTrees.Count == 0) {
     69        builder.Append(ExportFunction(tree.Function, tree));
     70      } else {
     71        builder.Append("(" + ExportFunction(tree.Function, tree) + " ");
     72        currentIndent += "  ";
     73        foreach (IFunctionTree subTree in tree.SubTrees) {
     74          builder.Append("\n");
     75          BuildExportString(subTree);
     76        }
     77        builder.Append(")");
     78        currentIndent = currentIndent.Remove(0, 2);
     79      }
    7580    }
    7681
     
    7883      // this is smelly, if there is a cleaner way to have a 'dynamic' visitor
    7984      // please let me know! (gkronber 14.10.2008)
    80       if(function is Addition) return ((Addition)function).ExportToScheme();
    81       if(function is And) return ((And)function).ExportToScheme();
    82       if(function is Average) return ((Average)function).ExportToScheme();
    83       if(function is Constant) return ((Constant)function).ExportToScheme(tree);
    84       if(function is Cosinus) return ((Cosinus)function).ExportToScheme();
    85       if(function is Differential) return ((Differential)function).ExportToScheme(tree, dataset);
    86       if(function is Division) return ((Division)function).ExportToScheme();
    87       if(function is Equal) return ((Equal)function).ExportToScheme();
    88       if(function is Exponential) return ((Exponential)function).ExportToScheme();
    89       if(function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
    90       if(function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
    91       if(function is LessThan) return ((LessThan)function).ExportToScheme();
    92       if(function is Logarithm) return ((Logarithm)function).ExportToScheme();
    93       if(function is Multiplication) return ((Multiplication)function).ExportToScheme();
    94       if(function is Not) return ((Not)function).ExportToScheme();
    95       if(function is Or) return ((Or)function).ExportToScheme();
    96       if(function is Power) return ((Power)function).ExportToScheme();
    97       if(function is Signum) return ((Signum)function).ExportToScheme();
    98       if(function is Sinus) return ((Sinus)function).ExportToScheme();
    99       if(function is Sqrt) return ((Sqrt)function).ExportToScheme();
    100       if(function is Subtraction) return ((Subtraction)function).ExportToScheme();
    101       if(function is Tangens) return ((Tangens)function).ExportToScheme();
    102       if(function is Variable) return ((Variable)function).ExportToScheme(tree, dataset);
    103       if(function is Xor) return ((Xor)function).ExportToScheme();
     85      if (function is Addition) return ((Addition)function).ExportToScheme();
     86      if (function is And) return ((And)function).ExportToScheme();
     87      if (function is Average) return ((Average)function).ExportToScheme();
     88      if (function is Constant) return ((Constant)function).ExportToScheme(tree);
     89      if (function is Cosinus) return ((Cosinus)function).ExportToScheme();
     90      if (function is Differential) return ((Differential)function).ExportToScheme(tree, dataset);
     91      if (function is Division) return ((Division)function).ExportToScheme();
     92      if (function is Equal) return ((Equal)function).ExportToScheme();
     93      if (function is Exponential) return ((Exponential)function).ExportToScheme();
     94      if (function is GreaterThan) return ((GreaterThan)function).ExportToScheme();
     95      if (function is IfThenElse) return ((IfThenElse)function).ExportToScheme();
     96      if (function is LessThan) return ((LessThan)function).ExportToScheme();
     97      if (function is Logarithm) return ((Logarithm)function).ExportToScheme();
     98      if (function is Multiplication) return ((Multiplication)function).ExportToScheme();
     99      if (function is Not) return ((Not)function).ExportToScheme();
     100      if (function is Or) return ((Or)function).ExportToScheme();
     101      if (function is Power) return ((Power)function).ExportToScheme();
     102      if (function is Signum) return ((Signum)function).ExportToScheme();
     103      if (function is Sinus) return ((Sinus)function).ExportToScheme();
     104      if (function is Sqrt) return ((Sqrt)function).ExportToScheme();
     105      if (function is Subtraction) return ((Subtraction)function).ExportToScheme();
     106      if (function is Tangens) return ((Tangens)function).ExportToScheme();
     107      if (function is Variable) return ((Variable)function).ExportToScheme(tree, dataset);
     108      if (function is Xor) return ((Xor)function).ExportToScheme();
    104109      throw new UnknownFunctionException(function.Name);
    105110    }
     
    118123      try {
    119124        name = ExportFunction(tree.Function, tree);
    120       } catch(UnknownFunctionException) {
     125      }
     126      catch (UnknownFunctionException) {
    121127        name = "N/A";
    122128      }
     
    181187
    182188    public static string ExportToScheme(this Variable variable, IFunctionTree tree, Dataset ds) {
    183       return "variable " + tree.GetLocalVariable(Variable.WEIGHT).Value + " " +
    184         ds.GetVariableName(int.Parse(tree.GetLocalVariable(Variable.INDEX).Value.ToString())) + " " + tree.GetLocalVariable(Variable.OFFSET).Value;
     189      return "(variable " + tree.GetLocalVariable(Variable.WEIGHT).Value + " " +
     190        ds.GetVariableName(int.Parse(tree.GetLocalVariable(Variable.INDEX).Value.ToString())) + " " + tree.GetLocalVariable(Variable.OFFSET).Value + ")";
    185191    }
    186192    public static string ExportToScheme(this Differential differential, IFunctionTree tree, Dataset ds) {
    187       return "differential " + tree.GetLocalVariable(Differential.WEIGHT).Value + " " +
    188         ds.GetVariableName(int.Parse(tree.GetLocalVariable(Differential.INDEX).Value.ToString())) + " " + tree.GetLocalVariable(Differential.OFFSET).Value;
     193      return "(differential " + tree.GetLocalVariable(Differential.WEIGHT).Value + " " +
     194        ds.GetVariableName(int.Parse(tree.GetLocalVariable(Differential.INDEX).Value.ToString())) + " " + tree.GetLocalVariable(Differential.OFFSET).Value + ")";
    189195    }
    190196
Note: See TracChangeset for help on using the changeset viewer.