Changeset 15926 for branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs
- Timestamp:
- 05/02/18 13:14:16 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs
r15919 r15926 19 19 namespace HeuristicLab.Problems.Instances.DataAnalysis.Views.Regression.Matlab { 20 20 public partial class RegressionMatlabImportDialog : Form { 21 private const int TAB_INDEX_TIMESERIES = 0; 22 private const int TAB_INDEX_DOUBLE_ARRAYS = 1; 23 21 24 private IMatlabConnector _mlConnector; 25 26 private IMLVariablesLoader _variablesLoader; 27 private IMLVariablesLoader _timeseriesLoader; 28 private IMLVariablesLoader _doubleArraysLoader; 29 30 private CancellationTokenSource _cts = new CancellationTokenSource(); 31 22 32 public event EventHandler<EventArgs<string>> StatusChangedEvent; 23 33 public event EventHandler<EventArgs<string>> ErrorOccuredEvent; 24 25 26 34 27 35 /// <summary> … … 49 57 } 50 58 51 private IList<KeyValuePair<string, MLDatatype>> _selectedVariables; 52 53 public IEnumerable<KeyValuePair<string, MLDatatype>> SelectedVariables { 59 private IList<MLVariableInfo> _selectedTimeseries; 60 private IList<MLVariableInfo> _selectedDoubleArrays; 61 62 public IEnumerable<MLVariableInfo> SelectedVariables { 54 63 get { 55 return _ selectedVariables;64 return _variablesLoader.SelectedVariables; 56 65 } 57 66 } … … 60 69 InitializeComponent(); 61 70 62 _selectedVariables = new List<KeyValuePair<string, MLDatatype>>(); 71 _selectedTimeseries = new List<MLVariableInfo>(); 72 _selectedDoubleArrays = new List<MLVariableInfo>(); 63 73 _values = new Dataset(); 64 74 65 75 this.FormClosed += (s, e) => DisposeMatlabConnector(); 66 76 OpenFileButton.Click += OpenFileButtonClick; 67 VariablesListBox.ItemCheck += (s, e) => BeginInvoke((MethodInvoker)(() => CheckedItemsChanged(s, e))); 77 78 DatatypeSelectorTabControl.SelectedIndexChanged += (s, e) => BeginInvoke((MethodInvoker)(() => DatatypeSelectorTabControlTabIndexChanged(s, e))); 79 68 80 LoadValuesButton.Click += LoadValuesButtonClick; 69 81 TrainingTestTrackBar.ValueChanged += TrainingTestTrackBarValueChanged; 82 70 83 StatusChangedEvent += (s, e) => BeginInvoke((MethodInvoker)(() => StatusLabel.Text = e.Value)); 71 84 ErrorOccuredEvent += (s, e) => BeginInvoke((MethodInvoker)(() => { … … 74 87 ErrorTextBox.Visible = true; 75 88 })); 76 } 77 78 private void CheckedItemsChanged(object sender, ItemCheckEventArgs e) { 79 _selectedVariables.Clear(); 80 foreach (KeyValuePair<string, MLDatatype> item in VariablesListBox.CheckedItems) { 81 if (item.Value == MLDatatype.Timeseries) { 82 _selectedVariables.Add(item); 83 } 84 } 85 LoadValuesButton.Enabled = VariablesListBox.CheckedIndices.Count > 0; 86 } 87 88 #region Events 89 89 90 _timeseriesLoader = new MLTimeseriesLoader(this, TimeseriesListBox); 91 _timeseriesLoader.NumberOfSelectedVariablesChanged += NumberOfSelectedVariablesChanged; 92 _doubleArraysLoader = new MLDoubleArrayLoader(this, DoubleArraysListBox); 93 _doubleArraysLoader.NumberOfSelectedVariablesChanged += NumberOfSelectedVariablesChanged; 94 95 _variablesLoader = _timeseriesLoader; 96 97 } 98 99 #region Methods for Events 100 101 private void NumberOfSelectedVariablesChanged(object s, EventArgs<int> e) { 102 LoadValuesButton.Enabled = e.Value > 0; 103 } 104 105 private void DatatypeSelectorTabControlTabIndexChanged(object s, EventArgs e) { 106 switch (DatatypeSelectorTabControl.SelectedIndex) { 107 case TAB_INDEX_TIMESERIES: 108 _variablesLoader = _timeseriesLoader; 109 break; 110 case TAB_INDEX_DOUBLE_ARRAYS: 111 _variablesLoader = _doubleArraysLoader; 112 break; 113 } 114 115 LoadValuesButton.Enabled = _variablesLoader.SelectedVariables.Count() > 0; 116 } 90 117 91 118 private void LoadValuesButtonClick(object sender, EventArgs e) { 119 _cts.Cancel(); 120 _cts = new CancellationTokenSource(); 92 121 LoadValuesAsync(); 93 122 } … … 106 135 } 107 136 #endregion 108 137 109 138 /// <summary> 110 139 /// Fires the ErrorOccuredEvent with the given text message. … … 127 156 if (_mlConnector == null) { 128 157 _mlConnector = MatlabApiFactory.CreateMatlabConnector(); 158 _mlConnector.ErrorOccuredEvent += ErrorOccured; 129 159 } 130 160 } … … 141 171 142 172 143 144 173 /// <summary> 145 174 /// This method reads all available variables form the selected matlab scirpt and puts them into the list view. … … 147 176 private async void GetVariableNamesAsync() { 148 177 UpdateStatus("Opening Matlab"); 149 150 178 var path = openFileDialog.FileName; 151 179 ProblemTextBox.Text = path; … … 156 184 157 185 UpdateStatus("Loading variable names"); 158 var variablesListBoxItems = VariablesListBox.Items; 159 variablesListBoxItems.Clear(); 160 foreach (var item in variables) { 161 variablesListBoxItems.Add(item, false); 162 } 186 _timeseriesLoader.DisplayVariables(variables); 187 _doubleArraysLoader.DisplayVariables(variables); 163 188 UpdateStatus("Done"); 164 189 } 165 190 166 /// <summary>167 /// Loads the values of the selected timeseries values and displays them at the preview control.168 /// </summary>169 191 private async void LoadValuesAsync() { 192 UpdateStatus("Loading values from Matlab"); 170 193 try { 171 PreviewDatasetMatrix.Content = new Dataset(); 172 173 UpdateStatus("Loading values from Matlab"); 174 var vals = await Task.Run<List<MLTimeseries>>(() => { 175 var values = new List<MLTimeseries>(); 176 foreach (KeyValuePair<string, MLDatatype> item in VariablesListBox.CheckedItems) { 177 if (item.Value == MLDatatype.Timeseries) { 178 var val = _mlConnector.GetTimeseries(item.Key); 179 if (val != null) { 180 values.Add(val); 181 } 182 } 183 } 184 return values; 185 }); 186 187 UpdateStatus("Converting values"); 188 DisplayLoadedValuesAsync(await Task.Run<MLTimeseries>(() => new MLTimeseries(vals))); 189 ErrorTextBox.Text = String.Empty; 190 ErrorTextBox.Visible = false; 191 OkButton.Enabled = true; 194 // As there can be a huge amount of data, it should be freed before reallocating it. 195 OkButton.Enabled = false; 196 _values = new Dataset(); 197 PreviewDatasetMatrix.Content = _values; 198 TargetVariableComboBox.DataSource = new List<string>(); 199 TargetVariableComboBox.SelectedText = ""; 200 GC.Collect(); 201 var token = _cts.Token; 202 var ds = await Task.Run<Dataset>(() => _variablesLoader.GetPreviewDataset(_mlConnector, token), token); 203 GC.Collect(); 204 205 if (ds != null) { 206 _values = ds; 207 PreviewDatasetMatrix.Content = _values; 208 TargetVariableComboBox.DataSource = _values.VariableNames; 209 210 ErrorTextBox.Text = String.Empty; 211 ErrorTextBox.Visible = false; 212 OkButton.Enabled = true; 213 } 192 214 } catch (Exception ex) { 193 215 if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) { 194 216 ErrorOccured(ex.Message); 217 } else if (ex is OutOfMemoryException) { 218 ErrorOccured(ex.Message + " Amount of the given data is too big."); 195 219 } else { 196 220 throw; … … 199 223 UpdateStatus("Done"); 200 224 } 201 202 /// <summary>203 /// Displays the values of a given timeseries at the preview dataset matrix.204 /// </summary>205 /// <param name="timeseries"></param>206 private async void DisplayLoadedValuesAsync(MLTimeseries timeseries) {207 PreviewDatasetMatrix.Content = null;208 IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(timeseries);209 var values = await Task.Run<double[,]>(() => GetValuesForDataset(timeseries));210 _values = new Dataset(variableNamesWithType, values);211 PreviewDatasetMatrix.Content = _values;212 SetPossibleTargetVariables(timeseries);213 }214 215 /// <summary>216 /// Sets the entries of the combobox for the target variable.217 /// </summary>218 /// <param name="timeseries"></param>219 protected void SetPossibleTargetVariables(MLTimeseries timeseries) {220 if (timeseries != null) {221 TargetVariableComboBox.DataSource = timeseries.DataHeader;222 }223 }224 225 /// <summary>226 /// Returns a list with all selected variable names for the preview dataset.227 /// </summary>228 /// <param name="timeseries"></param>229 /// <returns></returns>230 private IEnumerable<string> GetVariableNamesWithType(MLTimeseries timeseries) {231 IList<string> variableNamesWithType = timeseries.DataHeader.ToList();232 variableNamesWithType.Insert(0, "time");233 return variableNamesWithType;234 }235 236 /// <summary>237 /// Returns a two dimensional double array which contains the values including the timestamps of the given timeseries.238 /// Array[value, variable]239 /// If the amount of the data is too big, a OutOfMemoryException exception is being thrown.240 /// This exception is being cought and a message will be shown at the error message box.241 /// </summary>242 /// <param name="timeseries"></param>243 /// <returns></returns>244 private double[,] GetValuesForDataset(MLTimeseries timeseries) {245 try {246 double[,] datasetValues = new double[timeseries.Count, timeseries.DataHeader.Length + 1];247 248 int i = 0;249 foreach (var item in timeseries.Data) {250 datasetValues[i, 0] = item.Key;251 for (int j = 1; j < datasetValues.GetLength(1); j++) {252 datasetValues[i, j] = item.Value[j - 1];253 }254 i++;255 }256 return datasetValues;257 } catch (OutOfMemoryException ex) {258 ErrorOccured(ex.Message + " Amount of the given data is too big.");259 }260 return new double[0, timeseries.DataHeader.Length + 1];261 }262 225 } 263 226 }
Note: See TracChangeset
for help on using the changeset viewer.