Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/CedmaDatabaseMerger/MergerForm.cs @ 2281

Last change on this file since 2281 was 2277, checked in by gkronber, 15 years ago

Worked on merger tool to combine to CEDMA result sets. #719.

File size: 4.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.Modeling.Database.SQLServerCompact;
10using HeuristicLab.Modeling;
11using HeuristicLab.GP;
12using HeuristicLab.Core;
13using HeuristicLab.Modeling.Database;
14using System.Reflection;
15
16namespace CedmaDatabaseMerger {
17  public partial class MergerForm : Form {
18    string destinationFile;
19    public MergerForm() {
20      InitializeComponent();
21    }
22
23    private void setOutputButton_Click(object sender, EventArgs e) {
24      using (OpenFileDialog dialog = new OpenFileDialog()) {
25        DialogResult result = dialog.ShowDialog();
26        if (result == DialogResult.OK) {
27          outputTextBox.Text = dialog.FileName;
28          destinationFile = dialog.FileName;
29          importButton.Enabled = true;
30        } else {
31          outputTextBox.Text = string.Empty;
32          destinationFile = string.Empty;
33          importButton.Enabled = false;
34        }
35      }
36    }
37
38    private void importButton_Click(object sender, EventArgs e) {
39      string importFileName;
40      using (OpenFileDialog dialog = new OpenFileDialog()) {
41        DialogResult result = dialog.ShowDialog();
42        if (result == DialogResult.OK) {
43          importFileName = dialog.FileName;
44        } else {
45          importFileName = string.Empty;
46        }
47      }
48      if (!string.IsNullOrEmpty(importFileName)) {
49        importButton.Enabled = false;
50        BackgroundWorker worker = new BackgroundWorker();
51        worker.DoWork += CreateDoWorkDelegate(worker, importFileName);
52        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
53        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
54        worker.RunWorkerAsync();
55      }
56    }
57
58    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
59      importButton.Enabled = true;
60      importProgressBar.Value = 0;
61    }
62
63    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
64      importProgressBar.Value = e.ProgressPercentage;
65    }
66
67    private DoWorkEventHandler CreateDoWorkDelegate(BackgroundWorker worker, string importFileName) {
68      return (sender, args) => {
69        DatabaseService destiationDatabase = new DatabaseService("Data Source=" + destinationFile);
70        DatabaseService sourceDatabase = new DatabaseService("Data Source=" + importFileName);
71
72        var models = sourceDatabase.GetAllModels();
73        int importCount = 0;
74        foreach (HeuristicLab.Modeling.Database.IModel m in models) {
75          HeuristicLab.Modeling.Model model = new HeuristicLab.Modeling.Model();
76          model.TargetVariable = m.TargetVariable.Name;
77          model.Data = (IItem)PersistenceManager.RestoreFromGZip(sourceDatabase.GetModelData(m));
78          model.Dataset = sourceDatabase.GetDataset();
79          model.TrainingSamplesStart = m.TrainingSamplesStart;
80          model.TrainingSamplesEnd = m.TrainingSamplesEnd;
81          model.ValidationSamplesStart = m.ValidationSamplesStart;
82          model.ValidationSamplesEnd = m.ValidationSamplesEnd;
83          model.TestSamplesStart = m.TestSamplesStart;
84          model.TestSamplesEnd = m.TestSamplesEnd;
85          //get all double properties to save as modelResult
86          IEnumerable<PropertyInfo> modelResultInfos = model.GetType().GetProperties().Where(
87            info => info.PropertyType == typeof(double));
88          var modelResults = sourceDatabase.GetModelResults(m);
89          foreach (IModelResult result in modelResults) {
90            PropertyInfo matchingPropInfo = modelResultInfos.First(x=>x.Name == result.Result.Name);
91            if(matchingPropInfo!=null) matchingPropInfo.SetValue(model, result.Value, null);
92          }
93          var inputVariableResults = sourceDatabase.GetInputVariableResults(m);
94          foreach (IInputVariableResult result in inputVariableResults) {
95            model.AddInputVariables(result.Variable.Name);
96            if (result.Result.Name == "VariableEvaluationImpact") {
97              model.SetVariableEvaluationImpact(result.Variable.Name, result.Value);
98            } else if (result.Result.Name == "VariableQualityImpact") {
99              model.SetVariableQualityImpact(result.Variable.Name, result.Value);
100            } else throw new FormatException();
101          }
102         
103          destiationDatabase.Persist(model, m.Algorithm.Name, m.Algorithm.Description);
104          worker.ReportProgress((++importCount * 100) / models.Count());
105        }
106      };
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.