Changeset 15919
- Timestamp:
- 04/25/18 09:20:54 (7 years ago)
- Location:
- branches/2913_MatlabScriptProblemInstanceProvider
- Files:
-
- 9 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 -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/MatlabConnector.cs
r15912 r15919 112 112 matLabApi.GetWorkspaceData(varName, workspace, out value); 113 113 } 114 } catch (COMException e) {114 } catch (COMException) { 115 115 throw new ArgumentException(string.Format("The variable {0} does not exist in the current workspace {1}.", varName, workspace)); 116 116 } … … 137 137 matLabApi.Execute("exit"); 138 138 } 139 } catch (COMException e) {139 } catch (COMException) { 140 140 } finally { 141 141 matLabApi = null; -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/IMLTimeseries.cs
r15912 r15919 6 6 7 7 namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types { 8 public interface IMLTimeseries {8 public interface IMLTimeseries : IMLVariable { 9 9 /// <summary> 10 10 /// Data headers of the timeseries. -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/IMLValueVariable.cs
r15912 r15919 6 6 7 7 namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types { 8 public interface IMLValueVariable<TData> : IMLVariable<TData> { 9 /// <summary> 10 /// Converts a Matlab variable into a timeseries. 11 /// </summary> 12 /// <returns></returns> 13 IMLTimeseries ToTimeseries(); 14 } 8 public interface IMLValueVariable<TData> : IMLVariable { 9 /// <summary> 10 /// The data of the Matlab variable. 11 /// </summary> 12 TData Data { get; set; } 13 } 15 14 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/IMLVariable.cs
r15912 r15919 6 6 7 7 namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types { 8 public interface IMLVariable<TData>{9 10 11 12 8 public interface IMLVariable { 9 /// <summary> 10 /// The name of the Matlab variable. 11 /// </summary> 12 string Name { get; set; } 13 13 14 /// <summary> 15 /// The data of the Matlab variable. 16 /// </summary> 17 TData Data { get; set; } 18 19 /// <summary> 20 /// The Matlab datatype. 21 /// </summary> 22 MLDatatype Datatype { get; } 23 } 14 /// <summary> 15 /// The Matlab datatype. 16 /// </summary> 17 MLDatatype Datatype { get; } 18 } 24 19 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDouble.cs
r15912 r15919 22 22 public MLDatatype Datatype { 23 23 get { return MLDatatype.Double; } 24 } 25 26 public IMLTimeseries ToTimeseries() { 27 return new MLTimeseries(Name, new double[,] { { 0 } }, new double[,] { { Data } }); 28 } 24 } 29 25 } 30 26 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDoubleArray.cs
r15912 r15919 7 7 namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types { 8 8 public class MLDoubleArray : IMLValueVariable<double[,]> { 9 private string[] _dataHeaders; 10 11 #region Constructors 9 12 10 13 public MLDoubleArray(string name, object values) { … … 14 17 } 15 18 Data = values as double[,]; 19 20 _dataHeaders = new string[Data.GetLength(1)]; 21 for (int i = 0; i < _dataHeaders.Length; i++) { 22 _dataHeaders[i] = string.Format("{0}:{1}", name, i); 23 } 16 24 } 17 25 26 private MLDoubleArray(MLDoubleArray a1, MLDoubleArray a2) { 27 Name = string.Format("{0};{1}", a1.Name, a2.Name); 28 29 if (!ConcatFeasible(a1, a2)) { 30 throw new ArgumentException(string.Format("Cannot concat {0} and {1}. Different number of rows.", a1.Name, a2.Name)); 31 } 32 33 var numberOfColumns = a1.Columns + a2.Columns; 34 _dataHeaders = new string[numberOfColumns]; 35 for (int i = 0; i < numberOfColumns; i++) { 36 if (i < a1.Columns) { 37 _dataHeaders[i] = a1.DataHeader[i]; 38 } else { 39 _dataHeaders[i] = a2.DataHeader[i - a1.Columns]; 40 } 41 } 42 43 Data = new double[a1.Rows, numberOfColumns]; 44 for (int i = 0; i < a1.Rows; i++) { 45 for (int j = 0; j < numberOfColumns; j++) { 46 if (j < a1.Columns) { 47 Data[i, j] = a1.Data[i, j]; 48 } else { 49 Data[i, j] = a1.Data[i, j - a1.Columns]; 50 } 51 } 52 } 53 } 54 #endregion 55 56 57 /// <summary> 58 /// Concats two given MLDoubleArrays if their number of rows equals. 59 /// If they can't be concated, this method returns null. 60 /// </summary> 61 /// <param name="a1"></param> 62 /// <param name="a2"></param> 63 /// <returns></returns> 64 public static MLDoubleArray Concat(MLDoubleArray a1, MLDoubleArray a2) { 65 if (ConcatFeasible(a1, a2)) { 66 return null; 67 } 68 return new MLDoubleArray(a1, a2); 69 } 70 71 public static bool ConcatFeasible(MLDoubleArray a1, MLDoubleArray a2) { 72 return a1.Rows == a2.Rows; 73 } 74 75 76 #region Properties from the interface 77 18 78 public string Name { get; set; } 79 19 80 public double[,] Data { get; set; } 20 81 … … 23 84 } 24 85 25 public IMLTimeseries ToTimeseries() { 26 var times = new double[Data.GetLength(0), 1]; 27 for (int i = 0; i < times.GetLength(0); i++) { 28 times[i, 1] = i; 86 public string[] DataHeader { 87 get { return _dataHeaders; } 88 } 89 #endregion 90 91 public int Rows { get { 92 return Data.GetLength(0); 29 93 } 94 } 30 95 31 return new MLTimeseries(Name, times, Data); 96 public int Columns { 97 get { 98 return Data.GetLength(1); 99 } 32 100 } 33 101 } -
branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLTimeseries.cs
r15912 r15919 11 11 12 12 private string[] _dataHeaders; 13 14 #region Constructors 13 15 14 16 protected MLTimeseries() { … … 35 37 } 36 38 } 39 37 40 41 IList<MLTimeseries> tsCollection; 42 if (!(timeseries is IList<MLTimeseries>)) { 43 tsCollection = timeseries.ToList(); 44 } else { 45 tsCollection = timeseries as IList<MLTimeseries>; 46 } 47 48 int[] indices = new int[tsCollection.Count]; 38 49 var numberOfElements = dataHeaders.Count; 39 Parallel.ForEach(times, (time) => { 40 //foreach (var time in times) { 50 foreach (var time in times) { 41 51 var values = new double[numberOfElements]; 42 52 int idx = 0; 43 foreach (var item in timeseries) { 44 double[] vs = item.GetValuesByTime(time); 53 for (int i = 0; i < tsCollection.Count; i++) { 54 var item = tsCollection[i]; 55 double[] vs; 56 if (indices[i] == 0 && item.Data[indices[i]].Key > time) { 57 vs = new double[item.Data[0].Value.Length]; 58 } else { 59 if (indices[i] < item.Count - 1 && item.Data[indices[i] + 1].Key == time) { 60 indices[i]++; 61 } 62 vs = item.Data[indices[i]].Value; 63 } 45 64 46 65 foreach (var v in vs) { … … 48 67 } 49 68 } 69 50 70 lock (_locker) { 51 71 Data.Add(new KeyValuePair<double, double[]>(time, values)); 52 72 } 53 }); 54 73 } 55 74 _dataHeaders = dataHeaders.ToArray(); 56 75 } … … 96 115 } 97 116 } 117 #endregion 98 118 99 119 public string Name { get; set; }
Note: See TracChangeset
for help on using the changeset viewer.