Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15919


Ignore:
Timestamp:
04/25/18 09:20:54 (6 years ago)
Author:
rhanghof
Message:

#2913: The import does now work with Matlab timeseries datatypes.

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  
    4444            this.LoadValuesButton = new System.Windows.Forms.Button();
    4545            this.VariablesListBox = new System.Windows.Forms.CheckedListBox();
     46            this.label1 = new System.Windows.Forms.Label();
     47            this.StatusLabel = new System.Windows.Forms.Label();
    4648            this.ProblemDataSettingsGroupBox.SuspendLayout();
    4749            ((System.ComponentModel.ISupportInitialize)(this.TrainingTestTrackBar)).BeginInit();
     
    8789            | System.Windows.Forms.AnchorStyles.Left)
    8890            | System.Windows.Forms.AnchorStyles.Right)));
     91            this.ProblemDataSettingsGroupBox.Controls.Add(this.ErrorTextBox);
     92            this.ProblemDataSettingsGroupBox.Controls.Add(this.label1);
    8993            this.ProblemDataSettingsGroupBox.Controls.Add(this.TargetVariableComboBox);
    90             this.ProblemDataSettingsGroupBox.Controls.Add(this.ErrorTextBox);
    9194            this.ProblemDataSettingsGroupBox.Controls.Add(this.PreviewLabel);
    9295            this.ProblemDataSettingsGroupBox.Controls.Add(this.TestLabel);
     
    105108            this.TargetVariableComboBox.BackColor = System.Drawing.SystemColors.Control;
    106109            this.TargetVariableComboBox.FormattingEnabled = true;
    107             this.TargetVariableComboBox.Location = new System.Drawing.Point(12, 20);
     110            this.TargetVariableComboBox.Location = new System.Drawing.Point(256, 19);
    108111            this.TargetVariableComboBox.Name = "TargetVariableComboBox";
    109112            this.TargetVariableComboBox.Size = new System.Drawing.Size(185, 21);
     
    114117            this.ErrorTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    115118            | 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);
    117120            this.ErrorTextBox.Multiline = true;
    118121            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);
    120124            this.ErrorTextBox.TabIndex = 0;
    121125            this.ErrorTextBox.Text = "Open a Matlab File";
    122             this.ErrorTextBox.Visible = false;
    123126            //
    124127            // PreviewLabel
     
    231234            this.VariablesListBox.TabIndex = 22;
    232235            //
     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            //
    233255            // RegressionMatlabImportDialog
    234256            //
     
    236258            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    237259            this.ClientSize = new System.Drawing.Size(471, 457);
     260            this.Controls.Add(this.StatusLabel);
    238261            this.Controls.Add(this.MatlabVariablesGroupBox);
    239262            this.Controls.Add(this.ProblemDataSettingsGroupBox);
     
    275298    private System.Windows.Forms.CheckedListBox VariablesListBox;
    276299    private System.Windows.Forms.ComboBox TargetVariableComboBox;
     300    protected System.Windows.Forms.Label label1;
     301    protected System.Windows.Forms.Label StatusLabel;
    277302  }
    278303}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs

    r15912 r15919  
    99using System.Text;
    1010using System.Threading.Tasks;
     11using System.Threading;
    1112using System.Windows.Forms;
    1213using HeuristicLab.Common;
     
    1920  public partial class RegressionMatlabImportDialog : Form {
    2021    private IMatlabConnector _mlConnector;
     22    public event EventHandler<EventArgs<string>> StatusChangedEvent;
     23    public event EventHandler<EventArgs<string>> ErrorOccuredEvent;
     24
     25
    2126
    2227    /// <summary>
     
    3237    /// </summary>
    3338    private Dataset _values;
     39
    3440    public RegressionMatlabImportType ImportType {
    3541      get {
     
    5157    }
    5258
    53 
    5459    public RegressionMatlabImportDialog() {
    5560      InitializeComponent();
     
    6368      LoadValuesButton.Click += LoadValuesButtonClick;
    6469      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      }));
    6676    }
    6777
     
    7888    #region Events
    7989
     90
    8091    private void LoadValuesButtonClick(object sender, EventArgs e) {
    8192      LoadValuesAsync();
     
    8798      }
    8899      GetVariableNamesAsync();
    89     }   
     100      ErrorTextBox.Visible = false;
     101    }
    90102
    91103    private void TrainingTestTrackBarValueChanged(object sender, System.EventArgs e) {
     
    95107    #endregion
    96108   
     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
    97125    #region Opening and disposing of the Matlab connector
    98126    private void OpenMatlabConnector() {
     
    102130    }
    103131
    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      });
    108139    }
    109140    #endregion
    110141
    111    
     142
    112143
    113144    /// <summary>
     
    115146    /// </summary>
    116147    private async void GetVariableNamesAsync() {
     148      UpdateStatus("Opening Matlab");
     149
    117150      var path = openFileDialog.FileName;
    118151      ProblemTextBox.Text = path;
    119      
    120152      var variables = await Task.Run(() => {
    121153        OpenMatlabConnector();
     
    123155      });
    124156
     157      UpdateStatus("Loading variable names");
    125158      var variablesListBoxItems = VariablesListBox.Items;
    126159      variablesListBoxItems.Clear();
     
    128161        variablesListBoxItems.Add(item, false);
    129162      }
     163      UpdateStatus("Done");
    130164    }
    131165
     
    134168    /// </summary>
    135169    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            }
    142183          }
    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)));
    159189        ErrorTextBox.Text = String.Empty;
    160190        ErrorTextBox.Visible = false;
     
    162192      } catch (Exception ex) {
    163193        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);
    167195        } else {
    168196          throw;
    169197        }
    170198      }
    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>
    173219    protected void SetPossibleTargetVariables(MLTimeseries timeseries) {
    174220      if (timeseries != null) {
    175 
    176221        TargetVariableComboBox.DataSource = timeseries.DataHeader;
    177222      }
    178223    }
    179224
     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>
    180230    private IEnumerable<string> GetVariableNamesWithType(MLTimeseries timeseries) {
    181231      IList<string> variableNamesWithType = timeseries.DataHeader.ToList();
    182232      variableNamesWithType.Insert(0, "time");
    183       /*for (int i = 0; i < variableNamesWithType.Count; i++) {
    184         variableNamesWithType[i] += " (Double)";
    185       }*/
    186      
    187233      return variableNamesWithType;
    188234    }
    189 
    190     private object locker = new object();
    191235
    192236    /// <summary>
    193237    /// Returns a two dimensional double array which contains the values including the timestamps of the given timeseries.
    194238    /// 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.
    195241    /// </summary>
    196242    /// <param name="timeseries"></param>
    197243    /// <returns></returns>
    198244    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    }
    223262  }
    224263}
     264
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/MatlabConnector.cs

    r15912 r15919  
    112112          matLabApi.GetWorkspaceData(varName, workspace, out value);
    113113        }
    114       } catch (COMException e) {
     114      } catch (COMException) {
    115115        throw new ArgumentException(string.Format("The variable {0} does not exist in the current workspace {1}.", varName, workspace));
    116116      }
     
    137137          matLabApi.Execute("exit");
    138138        }
    139       } catch (COMException e) {
     139      } catch (COMException) {
    140140      } finally {
    141141        matLabApi = null;
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/IMLTimeseries.cs

    r15912 r15919  
    66
    77namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types {
    8     public interface IMLTimeseries {
     8    public interface IMLTimeseries : IMLVariable {
    99        /// <summary>
    1010        /// Data headers of the timeseries.
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/IMLValueVariable.cs

    r15912 r15919  
    66
    77namespace 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  }
    1514}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/IMLVariable.cs

    r15912 r15919  
    66
    77namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types {
    8     public interface IMLVariable<TData> {
    9         /// <summary>
    10         /// The name of the Matlab variable.
    11         /// </summary>
    12         string Name { get; set; }
     8  public interface IMLVariable {
     9    /// <summary>
     10    /// The name of the Matlab variable.
     11    /// </summary>
     12    string Name { get; set; }
    1313
    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  }
    2419}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDouble.cs

    r15912 r15919  
    2222    public MLDatatype Datatype {
    2323      get { return MLDatatype.Double; }
    24     }
    25 
    26     public IMLTimeseries ToTimeseries() {
    27       return new MLTimeseries(Name, new double[,] { { 0 } }, new double[,] { { Data } });
    28     }
     24    }   
    2925  }
    3026}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDoubleArray.cs

    r15912 r15919  
    77namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types {
    88  public class MLDoubleArray : IMLValueVariable<double[,]> {
     9    private string[] _dataHeaders;
     10
     11    #region Constructors
    912
    1013    public MLDoubleArray(string name, object values) {
     
    1417      }
    1518      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      }
    1624    }
    1725
     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
    1878    public string Name { get; set; }
     79
    1980    public double[,] Data { get; set; }
    2081
     
    2384    }
    2485
    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);
    2993      }
     94    }
    3095
    31       return new MLTimeseries(Name, times, Data);
     96    public int Columns {
     97      get {
     98        return Data.GetLength(1);
     99      }
    32100    }
    33101  }
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLTimeseries.cs

    r15912 r15919  
    1111
    1212    private string[] _dataHeaders;
     13
     14    #region Constructors
    1315
    1416    protected MLTimeseries() {
     
    3537        }
    3638      }
     39     
    3740
     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];
    3849      var numberOfElements = dataHeaders.Count;
    39       Parallel.ForEach(times, (time) => {
    40         //foreach (var time in times) {
     50      foreach (var time in times) {
    4151        var values = new double[numberOfElements];
    4252        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          }         
    4564
    4665          foreach (var v in vs) {
     
    4867          }
    4968        }
     69
    5070        lock (_locker) {
    5171          Data.Add(new KeyValuePair<double, double[]>(time, values));
    5272        }
    53       });
    54 
     73      }
    5574      _dataHeaders = dataHeaders.ToArray();
    5675    }
     
    96115      }
    97116    }
     117    #endregion
    98118
    99119    public string Name { get; set; }
Note: See TracChangeset for help on using the changeset viewer.