Changeset 15919 for branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression
- Timestamp:
- 04/25/18 09:20:54 (7 years ago)
- Location:
- branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.Designer.cs
r15912 r15919 44 44 this.LoadValuesButton = new System.Windows.Forms.Button(); 45 45 this.VariablesListBox = new System.Windows.Forms.CheckedListBox(); 46 this.label1 = new System.Windows.Forms.Label(); 47 this.StatusLabel = new System.Windows.Forms.Label(); 46 48 this.ProblemDataSettingsGroupBox.SuspendLayout(); 47 49 ((System.ComponentModel.ISupportInitialize)(this.TrainingTestTrackBar)).BeginInit(); … … 87 89 | System.Windows.Forms.AnchorStyles.Left) 88 90 | System.Windows.Forms.AnchorStyles.Right))); 91 this.ProblemDataSettingsGroupBox.Controls.Add(this.ErrorTextBox); 92 this.ProblemDataSettingsGroupBox.Controls.Add(this.label1); 89 93 this.ProblemDataSettingsGroupBox.Controls.Add(this.TargetVariableComboBox); 90 this.ProblemDataSettingsGroupBox.Controls.Add(this.ErrorTextBox);91 94 this.ProblemDataSettingsGroupBox.Controls.Add(this.PreviewLabel); 92 95 this.ProblemDataSettingsGroupBox.Controls.Add(this.TestLabel); … … 105 108 this.TargetVariableComboBox.BackColor = System.Drawing.SystemColors.Control; 106 109 this.TargetVariableComboBox.FormattingEnabled = true; 107 this.TargetVariableComboBox.Location = new System.Drawing.Point( 12, 20);110 this.TargetVariableComboBox.Location = new System.Drawing.Point(256, 19); 108 111 this.TargetVariableComboBox.Name = "TargetVariableComboBox"; 109 112 this.TargetVariableComboBox.Size = new System.Drawing.Size(185, 21); … … 114 117 this.ErrorTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 115 118 | System.Windows.Forms.AnchorStyles.Right))); 116 this.ErrorTextBox.Location = new System.Drawing.Point( 384, 15);119 this.ErrorTextBox.Location = new System.Drawing.Point(6, 15); 117 120 this.ErrorTextBox.Multiline = true; 118 121 this.ErrorTextBox.Name = "ErrorTextBox"; 119 this.ErrorTextBox.Size = new System.Drawing.Size(57, 73); 122 this.ErrorTextBox.ReadOnly = true; 123 this.ErrorTextBox.Size = new System.Drawing.Size(435, 204); 120 124 this.ErrorTextBox.TabIndex = 0; 121 125 this.ErrorTextBox.Text = "Open a Matlab File"; 122 this.ErrorTextBox.Visible = false;123 126 // 124 127 // PreviewLabel … … 231 234 this.VariablesListBox.TabIndex = 22; 232 235 // 236 // label1 237 // 238 this.label1.AutoSize = true; 239 this.label1.Location = new System.Drawing.Point(168, 22); 240 this.label1.Name = "label1"; 241 this.label1.Size = new System.Drawing.Size(82, 13); 242 this.label1.TabIndex = 9; 243 this.label1.Text = "Target Variable:"; 244 // 245 // StatusLabel 246 // 247 this.StatusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 248 this.StatusLabel.AutoSize = true; 249 this.StatusLabel.Location = new System.Drawing.Point(9, 427); 250 this.StatusLabel.Name = "StatusLabel"; 251 this.StatusLabel.Size = new System.Drawing.Size(37, 13); 252 this.StatusLabel.TabIndex = 23; 253 this.StatusLabel.Text = "Status"; 254 // 233 255 // RegressionMatlabImportDialog 234 256 // … … 236 258 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 237 259 this.ClientSize = new System.Drawing.Size(471, 457); 260 this.Controls.Add(this.StatusLabel); 238 261 this.Controls.Add(this.MatlabVariablesGroupBox); 239 262 this.Controls.Add(this.ProblemDataSettingsGroupBox); … … 275 298 private System.Windows.Forms.CheckedListBox VariablesListBox; 276 299 private System.Windows.Forms.ComboBox TargetVariableComboBox; 300 protected System.Windows.Forms.Label label1; 301 protected System.Windows.Forms.Label StatusLabel; 277 302 } 278 303 } -
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.