Changeset 15919 for branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs
- Timestamp:
- 04/25/18 09:20:54 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs
r15912 r15919 9 9 using System.Text; 10 10 using System.Threading.Tasks; 11 using System.Threading; 11 12 using System.Windows.Forms; 12 13 using HeuristicLab.Common; … … 19 20 public partial class RegressionMatlabImportDialog : Form { 20 21 private IMatlabConnector _mlConnector; 22 public event EventHandler<EventArgs<string>> StatusChangedEvent; 23 public event EventHandler<EventArgs<string>> ErrorOccuredEvent; 24 25 21 26 22 27 /// <summary> … … 32 37 /// </summary> 33 38 private Dataset _values; 39 34 40 public RegressionMatlabImportType ImportType { 35 41 get { … … 51 57 } 52 58 53 54 59 public RegressionMatlabImportDialog() { 55 60 InitializeComponent(); … … 63 68 LoadValuesButton.Click += LoadValuesButtonClick; 64 69 TrainingTestTrackBar.ValueChanged += TrainingTestTrackBarValueChanged; 65 70 StatusChangedEvent += (s, e) => BeginInvoke((MethodInvoker)(() => StatusLabel.Text = e.Value)); 71 ErrorOccuredEvent += (s, e) => BeginInvoke((MethodInvoker)(() => { 72 OkButton.Enabled = false; 73 ErrorTextBox.Text = e.Value; 74 ErrorTextBox.Visible = true; 75 })); 66 76 } 67 77 … … 78 88 #region Events 79 89 90 80 91 private void LoadValuesButtonClick(object sender, EventArgs e) { 81 92 LoadValuesAsync(); … … 87 98 } 88 99 GetVariableNamesAsync(); 89 } 100 ErrorTextBox.Visible = false; 101 } 90 102 91 103 private void TrainingTestTrackBarValueChanged(object sender, System.EventArgs e) { … … 95 107 #endregion 96 108 109 /// <summary> 110 /// Fires the ErrorOccuredEvent with the given text message. 111 /// </summary> 112 /// <param name="message"></param> 113 private void ErrorOccured(string message) { 114 ErrorOccuredEvent?.Invoke(this, new EventArgs<string>(message)); 115 } 116 117 /// <summary> 118 /// Fires the StatusChangedEvent. 119 /// </summary> 120 /// <param name="text"></param> 121 private void UpdateStatus(string text) { 122 StatusChangedEvent?.Invoke(this, new EventArgs<string>(text)); 123 } 124 97 125 #region Opening and disposing of the Matlab connector 98 126 private void OpenMatlabConnector() { … … 102 130 } 103 131 104 private void DisposeMatlabConnector() { 105 if (_mlConnector != null) { 106 _mlConnector.Dispose(); 107 } 132 private async void DisposeMatlabConnector() { 133 UpdateStatus("Closing Matlab"); 134 await Task.Run(() => { 135 if (_mlConnector != null) { 136 _mlConnector.Dispose(); 137 } 138 }); 108 139 } 109 140 #endregion 110 141 111 142 112 143 113 144 /// <summary> … … 115 146 /// </summary> 116 147 private async void GetVariableNamesAsync() { 148 UpdateStatus("Opening Matlab"); 149 117 150 var path = openFileDialog.FileName; 118 151 ProblemTextBox.Text = path; 119 120 152 var variables = await Task.Run(() => { 121 153 OpenMatlabConnector(); … … 123 155 }); 124 156 157 UpdateStatus("Loading variable names"); 125 158 var variablesListBoxItems = VariablesListBox.Items; 126 159 variablesListBoxItems.Clear(); … … 128 161 variablesListBoxItems.Add(item, false); 129 162 } 163 UpdateStatus("Done"); 130 164 } 131 165 … … 134 168 /// </summary> 135 169 private async void LoadValuesAsync() { 136 var values = new List<MLTimeseries>(); 137 foreach (KeyValuePair<string, MLDatatype> item in VariablesListBox.CheckedItems) { 138 if (item.Value == MLDatatype.Timeseries) { 139 var val = _mlConnector.GetTimeseries(item.Key); 140 if (val != null) { 141 values.Add(val); 170 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 } 142 183 } 143 } 144 } 145 146 DisplayLoadedValuesAsync(await Task.Run<MLTimeseries>(() => new MLTimeseries(values))); 147 } 148 149 private async void DisplayLoadedValuesAsync(MLTimeseries timeseries) { 150 PreviewDatasetMatrix.Content = null; 151 try { 152 153 IEnumerable<string> variableNamesWithType = GetVariableNamesWithType(timeseries); 154 var values = await Task.Run<double[,]>(() => GetValuesForDataset(timeseries)); 155 _values = new Dataset(variableNamesWithType, values); 156 PreviewDatasetMatrix.Content = _values; 157 SetPossibleTargetVariables(timeseries); 158 184 return values; 185 }); 186 187 UpdateStatus("Converting values"); 188 DisplayLoadedValuesAsync(await Task.Run<MLTimeseries>(() => new MLTimeseries(vals))); 159 189 ErrorTextBox.Text = String.Empty; 160 190 ErrorTextBox.Visible = false; … … 162 192 } catch (Exception ex) { 163 193 if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) { 164 OkButton.Enabled = false; 165 ErrorTextBox.Text = ex.Message; 166 ErrorTextBox.Visible = true; 194 ErrorOccured(ex.Message); 167 195 } else { 168 196 throw; 169 197 } 170 198 } 171 } 172 199 UpdateStatus("Done"); 200 } 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> 173 219 protected void SetPossibleTargetVariables(MLTimeseries timeseries) { 174 220 if (timeseries != null) { 175 176 221 TargetVariableComboBox.DataSource = timeseries.DataHeader; 177 222 } 178 223 } 179 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> 180 230 private IEnumerable<string> GetVariableNamesWithType(MLTimeseries timeseries) { 181 231 IList<string> variableNamesWithType = timeseries.DataHeader.ToList(); 182 232 variableNamesWithType.Insert(0, "time"); 183 /*for (int i = 0; i < variableNamesWithType.Count; i++) {184 variableNamesWithType[i] += " (Double)";185 }*/186 187 233 return variableNamesWithType; 188 234 } 189 190 private object locker = new object();191 235 192 236 /// <summary> 193 237 /// Returns a two dimensional double array which contains the values including the timestamps of the given timeseries. 194 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. 195 241 /// </summary> 196 242 /// <param name="timeseries"></param> 197 243 /// <returns></returns> 198 244 private double[,] GetValuesForDataset(MLTimeseries timeseries) { 199 double[,] datasetValues = new double[timeseries.Count, timeseries.DataHeader.Length + 1]; 200 var sortedValues = new SortedList<double, double[]>(); 201 var times = timeseries.Times; 202 Parallel.For(0, datasetValues.GetLength(0), (idx) => { 203 var time = times[idx]; 204 var values = timeseries.GetValuesByTime(time); 205 lock(locker) { 206 sortedValues.Add(time, values); 207 } 208 }); 209 210 int i = 0; 211 foreach (var item in sortedValues) { 212 datasetValues[i, 0] = item.Key; 213 for (int j = 1; j < datasetValues.GetLength(1); j++) { 214 datasetValues[i, j] = item.Value[j - 1]; 215 } 216 i++; 217 } 218 return datasetValues; 219 } 220 221 222 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 } 223 262 } 224 263 } 264
Note: See TracChangeset
for help on using the changeset viewer.