Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs @ 15958

Last change on this file since 15958 was 15958, checked in by rhanghof, 6 years ago

#2913:

  • Enhancements on the RegressionMatlabImportDialog
  • Added a new text formater SymbolicDataAnalysisExpressionMATLABFunctionFormatter
File size: 7.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.IO;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.Threading;
11using System.Windows.Forms;
12using HeuristicLab.Common;
13using HeuristicLab.Problems.DataAnalysis;
14using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab;
15using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api;
16using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types;
17
18namespace HeuristicLab.Problems.Instances.DataAnalysis.Views.Regression.Matlab {
19  public partial class RegressionMatlabImportDialog : Form {
20    private const int TAB_INDEX_TIMESERIES = 0;
21    private const int TAB_INDEX_DOUBLE_ARRAYS = 1;
22
23    private IMatlabConnector _mlConnector;
24
25    private IMLVariablesLoader _variablesLoader;
26    private IMLVariablesLoader _timeseriesLoader;
27    private IMLVariablesLoader _doubleArraysLoader;
28
29    private CancellationTokenSource _cts = new CancellationTokenSource();
30
31    public event EventHandler<EventArgs<string>> StatusChangedEvent;
32    public event EventHandler<EventArgs<string>> ErrorOccuredEvent;
33
34    /// <summary>
35    /// Path to the Matlab file
36    /// </summary>
37    public string Path {
38      get { return ProblemTextBox.Text; }
39    }
40
41    /// <summary>
42    /// As loading the data can take much time, loaded values will be stored.
43    /// So they have not have to be loaded a secound time.
44    /// </summary>
45    private Dataset _values;
46
47    public RegressionMatlabImportType ImportType {
48      get {
49        return new RegressionMatlabImportType() {
50          Shuffle = ShuffleDataCheckbox.Checked,
51          TrainingPercentage = TrainingTestTrackBar.Value,
52          TargetVariable = (String)TargetVariableComboBox.SelectedValue,
53          Values = _values
54        };
55      }
56    }
57
58    private IList<MLVariableInfo> _selectedTimeseries;
59    private IList<MLVariableInfo> _selectedDoubleArrays;
60
61    public IEnumerable<MLVariableInfo> SelectedVariables {
62      get {
63        return _variablesLoader.SelectedVariables;
64      }
65    }
66
67    public RegressionMatlabImportDialog() {
68      InitializeComponent();
69
70      _selectedTimeseries = new List<MLVariableInfo>();
71      _selectedDoubleArrays = new List<MLVariableInfo>();
72      _values = new Dataset();
73
74      this.FormClosed += (s, e) => DisposeMatlabConnector();
75      OpenFileButton.Click += OpenFileButtonClick;
76
77      DatatypeSelectorTabControl.SelectedIndexChanged += (s, e) => BeginInvoke((MethodInvoker)(() => DatatypeSelectorTabControlTabIndexChanged(s, e)));
78
79      LoadValuesButton.Click += LoadValuesButtonClick;
80      TrainingTestTrackBar.ValueChanged += TrainingTestTrackBarValueChanged;
81
82      StatusChangedEvent += (s, e) => BeginInvoke((MethodInvoker)(() => StatusLabel.Text = e.Value));
83      ErrorOccuredEvent += (s, e) => BeginInvoke((MethodInvoker)(() => {
84        OkButton.Enabled = false;
85        ErrorTextBox.Text = e.Value;
86        ErrorTextBox.Visible = true;
87      }));
88
89      _timeseriesLoader = new MLTimeseriesLoader(this, TimeseriesListBox);
90      _timeseriesLoader.NumberOfSelectedVariablesChanged += NumberOfSelectedVariablesChanged;
91      _doubleArraysLoader = new MLDoubleArrayLoader(this, DoubleArraysListBox);
92      _doubleArraysLoader.NumberOfSelectedVariablesChanged += NumberOfSelectedVariablesChanged;
93
94      _variablesLoader = _timeseriesLoader;
95
96    }
97
98    #region Methods for Events
99
100    private void NumberOfSelectedVariablesChanged(object s, EventArgs<int> e) {
101      LoadValuesButton.Enabled = e.Value > 0;
102    }
103
104    private void DatatypeSelectorTabControlTabIndexChanged(object s, EventArgs e) {
105      switch (DatatypeSelectorTabControl.SelectedIndex) {
106        case TAB_INDEX_TIMESERIES:
107          _variablesLoader = _timeseriesLoader;
108          break;
109        case TAB_INDEX_DOUBLE_ARRAYS:
110          _variablesLoader = _doubleArraysLoader;
111          break;
112      }
113
114      LoadValuesButton.Enabled = _variablesLoader.SelectedVariables.Count() > 0;
115    }
116
117    private void LoadValuesButtonClick(object sender, EventArgs e) {
118      _cts.Cancel();
119      _cts = new CancellationTokenSource();
120      LoadValuesAsync();
121    }
122
123    private void OpenFileButtonClick(object sender, EventArgs e) {
124      if (openFileDialog.ShowDialog(this) != DialogResult.OK) {
125        return;
126      }
127      GetVariableNamesAsync();
128      ErrorTextBox.Visible = false;
129    }
130
131    private void TrainingTestTrackBarValueChanged(object sender, System.EventArgs e) {
132      TrainingLabel.Text = "Training: " + TrainingTestTrackBar.Value + " %";
133      TestLabel.Text = "Test: " + (TrainingTestTrackBar.Maximum - TrainingTestTrackBar.Value) + " %";
134    }
135    #endregion
136
137    /// <summary>
138    /// Fires the ErrorOccuredEvent with the given text message.
139    /// </summary>
140    /// <param name="message"></param>
141    private void ErrorOccured(string message) {
142      ErrorOccuredEvent?.Invoke(this, new EventArgs<string>(message));
143    }
144
145    /// <summary>
146    /// Fires the StatusChangedEvent.
147    /// </summary>
148    /// <param name="text"></param>
149    private void UpdateStatus(string text) {
150      StatusChangedEvent?.Invoke(this, new EventArgs<string>(text));
151    }
152
153    #region Opening and disposing of the Matlab connector
154    private void OpenMatlabConnector() {
155      if (_mlConnector == null) {
156        _mlConnector = MatlabApiFactory.CreateMatlabConnector();
157        _mlConnector.ErrorOccuredEvent += ErrorOccured;
158      }
159    }
160
161    private async void DisposeMatlabConnector() {
162      UpdateStatus("Closing Matlab");
163      await Task.Run(() => {
164        if (_mlConnector != null) {
165          _mlConnector.Dispose();
166        }
167      });
168    }
169    #endregion
170
171
172    /// <summary>
173    /// This method reads all available variables form the selected matlab scirpt and puts them into the list view.
174    /// </summary>
175    private async void GetVariableNamesAsync() {
176      UpdateStatus("Opening Matlab");
177      var path = openFileDialog.FileName;
178      ProblemTextBox.Text = path;
179      var variables = await Task.Run(() => {
180        OpenMatlabConnector();
181        return _mlConnector.GetVariablesFromScript(path);
182      });
183
184      UpdateStatus("Loading variable names");
185      _timeseriesLoader.DisplayVariables(variables);
186      _doubleArraysLoader.DisplayVariables(variables);
187      UpdateStatus("Done");
188    }
189
190    private async void LoadValuesAsync() {
191      UpdateStatus("Loading values from Matlab");
192      try {
193        // As there can be a huge amount of data, it should be freed before reallocating it.
194        OkButton.Enabled = false;
195        _values = new Dataset();
196        PreviewDatasetMatrix.Content = _values;
197        TargetVariableComboBox.DataSource = new List<string>();
198        TargetVariableComboBox.SelectedText = "";
199        GC.Collect();
200        var token = _cts.Token;
201        var values = await Task.Run<Dataset>(() => _variablesLoader.GetPreviewDataset(_mlConnector, token), token);
202        GC.Collect();
203
204        if (values != null) {
205          _values = values;
206          PreviewDatasetMatrix.Content = _values;
207          TargetVariableComboBox.DataSource = _values.VariableNames;
208
209          ErrorTextBox.Text = String.Empty;
210          ErrorTextBox.Visible = false;
211          OkButton.Enabled = true;
212        }       
213      } catch (Exception ex) {
214        if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) {
215          ErrorOccured(ex.Message);
216        } else if (ex is OutOfMemoryException) {
217          ErrorOccured(ex.Message + " Amount of the given data is too big.");
218        } else {
219          throw;
220        }
221      }
222      UpdateStatus("Done");
223    }
224  }
225}
226
Note: See TracBrowser for help on using the repository browser.