Changeset 15926


Ignore:
Timestamp:
05/02/18 13:14:16 (3 years ago)
Author:
rhanghof
Message:

#2913:

  • Added the support for importing different Matlab datatypes.
  • Added some classes and changed the import dialog for importing double arrays.
Location:
branches/2913_MatlabScriptProblemInstanceProvider
Files:
5 added
10 edited

Legend:

Unmodified
Added
Removed
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/HeuristicLab.Problems.Instances.DataAnalysis.Views-3.3.csproj

    r15912 r15926  
    123123      <DependentUpon>RegressionImportDialog.cs</DependentUpon>
    124124    </Compile>
     125    <Compile Include="Regression\Matlab\IMLVariablesLoader.cs">
     126      <SubType>Form</SubType>
     127    </Compile>
     128    <Compile Include="Regression\Matlab\MLDoubleArrayLoader.cs">
     129      <SubType>Form</SubType>
     130    </Compile>
     131    <Compile Include="Regression\Matlab\MLTimeseriesLoader.cs">
     132      <SubType>Form</SubType>
     133    </Compile>
     134    <Compile Include="Regression\Matlab\MLVariablesLoader.cs">
     135      <SubType>Form</SubType>
     136    </Compile>
    125137    <Compile Include="Regression\Matlab\RegressionMatlabImportDialog.cs">
    126138      <SubType>Form</SubType>
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.Designer.cs

    r15919 r15926  
    3232            this.ToolTip = new System.Windows.Forms.ToolTip(this.components);
    3333            this.ProblemDataSettingsGroupBox = new System.Windows.Forms.GroupBox();
     34            this.ErrorTextBox = new System.Windows.Forms.TextBox();
     35            this.label1 = new System.Windows.Forms.Label();
    3436            this.TargetVariableComboBox = new System.Windows.Forms.ComboBox();
    35             this.ErrorTextBox = new System.Windows.Forms.TextBox();
    3637            this.PreviewLabel = new System.Windows.Forms.Label();
    3738            this.TestLabel = new System.Windows.Forms.Label();
     
    4243            this.OkButton = new System.Windows.Forms.Button();
    4344            this.MatlabVariablesGroupBox = new System.Windows.Forms.GroupBox();
     45            this.DatatypeSelectorTabControl = new System.Windows.Forms.TabControl();
     46            this.Timeseries = new System.Windows.Forms.TabPage();
     47            this.TimeseriesListBox = new System.Windows.Forms.CheckedListBox();
     48            this.DoubleArrays = new System.Windows.Forms.TabPage();
     49            this.DoubleArraysListBox = new System.Windows.Forms.CheckedListBox();
    4450            this.LoadValuesButton = new System.Windows.Forms.Button();
    45             this.VariablesListBox = new System.Windows.Forms.CheckedListBox();
    46             this.label1 = new System.Windows.Forms.Label();
    4751            this.StatusLabel = new System.Windows.Forms.Label();
    4852            this.ProblemDataSettingsGroupBox.SuspendLayout();
    4953            ((System.ComponentModel.ISupportInitialize)(this.TrainingTestTrackBar)).BeginInit();
    5054            this.MatlabVariablesGroupBox.SuspendLayout();
     55            this.DatatypeSelectorTabControl.SuspendLayout();
     56            this.Timeseries.SuspendLayout();
     57            this.DoubleArrays.SuspendLayout();
    5158            this.SuspendLayout();
    5259            //
     
    97104            this.ProblemDataSettingsGroupBox.Controls.Add(this.TrainingLabel);
    98105            this.ProblemDataSettingsGroupBox.Controls.Add(this.TrainingTestTrackBar);
    99             this.ProblemDataSettingsGroupBox.Location = new System.Drawing.Point(12, 191);
     106            this.ProblemDataSettingsGroupBox.Location = new System.Drawing.Point(12, 210);
    100107            this.ProblemDataSettingsGroupBox.Name = "ProblemDataSettingsGroupBox";
    101             this.ProblemDataSettingsGroupBox.Size = new System.Drawing.Size(447, 225);
     108            this.ProblemDataSettingsGroupBox.Size = new System.Drawing.Size(447, 258);
    102109            this.ProblemDataSettingsGroupBox.TabIndex = 20;
    103110            this.ProblemDataSettingsGroupBox.TabStop = false;
    104111            this.ProblemDataSettingsGroupBox.Text = "ProblemData Settings";
    105112            //
    106             // TargetVariableComboBox
    107             //
    108             this.TargetVariableComboBox.BackColor = System.Drawing.SystemColors.Control;
    109             this.TargetVariableComboBox.FormattingEnabled = true;
    110             this.TargetVariableComboBox.Location = new System.Drawing.Point(256, 19);
    111             this.TargetVariableComboBox.Name = "TargetVariableComboBox";
    112             this.TargetVariableComboBox.Size = new System.Drawing.Size(185, 21);
    113             this.TargetVariableComboBox.TabIndex = 8;
    114             //
    115113            // ErrorTextBox
    116114            //
    117             this.ErrorTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     115            this.ErrorTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     116            | System.Windows.Forms.AnchorStyles.Left)
    118117            | System.Windows.Forms.AnchorStyles.Right)));
    119118            this.ErrorTextBox.Location = new System.Drawing.Point(6, 15);
     
    121120            this.ErrorTextBox.Name = "ErrorTextBox";
    122121            this.ErrorTextBox.ReadOnly = true;
    123             this.ErrorTextBox.Size = new System.Drawing.Size(435, 204);
     122            this.ErrorTextBox.Size = new System.Drawing.Size(435, 237);
    124123            this.ErrorTextBox.TabIndex = 0;
    125124            this.ErrorTextBox.Text = "Open a Matlab File";
     125            //
     126            // label1
     127            //
     128            this.label1.AutoSize = true;
     129            this.label1.Location = new System.Drawing.Point(9, 22);
     130            this.label1.Name = "label1";
     131            this.label1.Size = new System.Drawing.Size(82, 13);
     132            this.label1.TabIndex = 9;
     133            this.label1.Text = "Target Variable:";
     134            //
     135            // TargetVariableComboBox
     136            //
     137            this.TargetVariableComboBox.BackColor = System.Drawing.SystemColors.Control;
     138            this.TargetVariableComboBox.FormattingEnabled = true;
     139            this.TargetVariableComboBox.Location = new System.Drawing.Point(97, 19);
     140            this.TargetVariableComboBox.Name = "TargetVariableComboBox";
     141            this.TargetVariableComboBox.Size = new System.Drawing.Size(185, 21);
     142            this.TargetVariableComboBox.TabIndex = 8;
    126143            //
    127144            // PreviewLabel
     
    156173            this.PreviewDatasetMatrix.ShowRowsAndColumnsTextBox = false;
    157174            this.PreviewDatasetMatrix.ShowStatisticalInformation = false;
    158             this.PreviewDatasetMatrix.Size = new System.Drawing.Size(435, 111);
     175            this.PreviewDatasetMatrix.Size = new System.Drawing.Size(435, 144);
    159176            this.PreviewDatasetMatrix.TabIndex = 0;
    160177            //
     
    185202            this.CancellationButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
    186203            this.CancellationButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    187             this.CancellationButton.Location = new System.Drawing.Point(384, 422);
     204            this.CancellationButton.Location = new System.Drawing.Point(384, 474);
    188205            this.CancellationButton.Name = "CancellationButton";
    189206            this.CancellationButton.Size = new System.Drawing.Size(75, 23);
     
    197214            this.OkButton.DialogResult = System.Windows.Forms.DialogResult.OK;
    198215            this.OkButton.Enabled = false;
    199             this.OkButton.Location = new System.Drawing.Point(303, 422);
     216            this.OkButton.Location = new System.Drawing.Point(303, 474);
    200217            this.OkButton.Name = "OkButton";
    201218            this.OkButton.Size = new System.Drawing.Size(75, 23);
     
    206223            // MatlabVariablesGroupBox
    207224            //
     225            this.MatlabVariablesGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     226            | System.Windows.Forms.AnchorStyles.Right)));
     227            this.MatlabVariablesGroupBox.Controls.Add(this.DatatypeSelectorTabControl);
    208228            this.MatlabVariablesGroupBox.Controls.Add(this.LoadValuesButton);
    209             this.MatlabVariablesGroupBox.Controls.Add(this.VariablesListBox);
    210229            this.MatlabVariablesGroupBox.Location = new System.Drawing.Point(12, 32);
    211230            this.MatlabVariablesGroupBox.Name = "MatlabVariablesGroupBox";
    212             this.MatlabVariablesGroupBox.Size = new System.Drawing.Size(447, 153);
     231            this.MatlabVariablesGroupBox.Size = new System.Drawing.Size(447, 172);
    213232            this.MatlabVariablesGroupBox.TabIndex = 22;
    214233            this.MatlabVariablesGroupBox.TabStop = false;
    215234            this.MatlabVariablesGroupBox.Text = "Matlab variables";
    216235            //
     236            // DatatypeSelectorTabControl
     237            //
     238            this.DatatypeSelectorTabControl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     239            | System.Windows.Forms.AnchorStyles.Right)));
     240            this.DatatypeSelectorTabControl.Controls.Add(this.Timeseries);
     241            this.DatatypeSelectorTabControl.Controls.Add(this.DoubleArrays);
     242            this.DatatypeSelectorTabControl.Location = new System.Drawing.Point(6, 19);
     243            this.DatatypeSelectorTabControl.Name = "DatatypeSelectorTabControl";
     244            this.DatatypeSelectorTabControl.SelectedIndex = 0;
     245            this.DatatypeSelectorTabControl.Size = new System.Drawing.Size(435, 124);
     246            this.DatatypeSelectorTabControl.TabIndex = 24;
     247            //
     248            // Timeseries
     249            //
     250            this.Timeseries.Controls.Add(this.TimeseriesListBox);
     251            this.Timeseries.Location = new System.Drawing.Point(4, 22);
     252            this.Timeseries.Name = "Timeseries";
     253            this.Timeseries.Padding = new System.Windows.Forms.Padding(3);
     254            this.Timeseries.Size = new System.Drawing.Size(427, 98);
     255            this.Timeseries.TabIndex = 0;
     256            this.Timeseries.Text = "Timeseries";
     257            this.Timeseries.UseVisualStyleBackColor = true;
     258            //
     259            // TimeseriesListBox
     260            //
     261            this.TimeseriesListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     262            | System.Windows.Forms.AnchorStyles.Right)));
     263            this.TimeseriesListBox.FormattingEnabled = true;
     264            this.TimeseriesListBox.Location = new System.Drawing.Point(0, 2);
     265            this.TimeseriesListBox.Name = "TimeseriesListBox";
     266            this.TimeseriesListBox.Size = new System.Drawing.Size(424, 94);
     267            this.TimeseriesListBox.TabIndex = 23;
     268            //
     269            // DoubleArrays
     270            //
     271            this.DoubleArrays.Controls.Add(this.DoubleArraysListBox);
     272            this.DoubleArrays.Location = new System.Drawing.Point(4, 22);
     273            this.DoubleArrays.Name = "DoubleArrays";
     274            this.DoubleArrays.Padding = new System.Windows.Forms.Padding(3);
     275            this.DoubleArrays.Size = new System.Drawing.Size(427, 98);
     276            this.DoubleArrays.TabIndex = 1;
     277            this.DoubleArrays.Text = "Double Arrays";
     278            this.DoubleArrays.UseVisualStyleBackColor = true;
     279            //
     280            // DoubleArraysListBox
     281            //
     282            this.DoubleArraysListBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     283            | System.Windows.Forms.AnchorStyles.Right)));
     284            this.DoubleArraysListBox.FormattingEnabled = true;
     285            this.DoubleArraysListBox.Location = new System.Drawing.Point(0, 4);
     286            this.DoubleArraysListBox.Name = "DoubleArraysListBox";
     287            this.DoubleArraysListBox.Size = new System.Drawing.Size(425, 94);
     288            this.DoubleArraysListBox.TabIndex = 24;
     289            //
    217290            // LoadValuesButton
    218291            //
    219292            this.LoadValuesButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
    220293            this.LoadValuesButton.Enabled = false;
    221             this.LoadValuesButton.Location = new System.Drawing.Point(366, 124);
     294            this.LoadValuesButton.Location = new System.Drawing.Point(366, 143);
    222295            this.LoadValuesButton.Name = "LoadValuesButton";
    223296            this.LoadValuesButton.Size = new System.Drawing.Size(75, 23);
     
    226299            this.LoadValuesButton.UseVisualStyleBackColor = true;
    227300            //
    228             // VariablesListBox
    229             //
    230             this.VariablesListBox.FormattingEnabled = true;
    231             this.VariablesListBox.Location = new System.Drawing.Point(6, 18);
    232             this.VariablesListBox.Name = "VariablesListBox";
    233             this.VariablesListBox.Size = new System.Drawing.Size(435, 94);
    234             this.VariablesListBox.TabIndex = 22;
    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             //
    245301            // StatusLabel
    246302            //
    247303            this.StatusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
    248304            this.StatusLabel.AutoSize = true;
    249             this.StatusLabel.Location = new System.Drawing.Point(9, 427);
     305            this.StatusLabel.Location = new System.Drawing.Point(9, 479);
    250306            this.StatusLabel.Name = "StatusLabel";
    251307            this.StatusLabel.Size = new System.Drawing.Size(37, 13);
     
    257313            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    258314            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    259             this.ClientSize = new System.Drawing.Size(471, 457);
     315            this.ClientSize = new System.Drawing.Size(471, 509);
    260316            this.Controls.Add(this.StatusLabel);
    261317            this.Controls.Add(this.MatlabVariablesGroupBox);
     
    273329            ((System.ComponentModel.ISupportInitialize)(this.TrainingTestTrackBar)).EndInit();
    274330            this.MatlabVariablesGroupBox.ResumeLayout(false);
     331            this.DatatypeSelectorTabControl.ResumeLayout(false);
     332            this.Timeseries.ResumeLayout(false);
     333            this.DoubleArrays.ResumeLayout(false);
    275334            this.ResumeLayout(false);
    276335            this.PerformLayout();
     
    296355    private System.Windows.Forms.GroupBox MatlabVariablesGroupBox;
    297356    protected System.Windows.Forms.Button LoadValuesButton;
    298     private System.Windows.Forms.CheckedListBox VariablesListBox;
    299357    private System.Windows.Forms.ComboBox TargetVariableComboBox;
    300358    protected System.Windows.Forms.Label label1;
    301359    protected System.Windows.Forms.Label StatusLabel;
     360    private System.Windows.Forms.TabControl DatatypeSelectorTabControl;
     361    private System.Windows.Forms.TabPage Timeseries;
     362    private System.Windows.Forms.CheckedListBox TimeseriesListBox;
     363    private System.Windows.Forms.TabPage DoubleArrays;
     364    private System.Windows.Forms.CheckedListBox DoubleArraysListBox;
    302365  }
    303366}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/RegressionMatlabImportDialog.cs

    r15919 r15926  
    1919namespace HeuristicLab.Problems.Instances.DataAnalysis.Views.Regression.Matlab {
    2020  public partial class RegressionMatlabImportDialog : Form {
     21    private const int TAB_INDEX_TIMESERIES = 0;
     22    private const int TAB_INDEX_DOUBLE_ARRAYS = 1;
     23
    2124    private IMatlabConnector _mlConnector;
     25
     26    private IMLVariablesLoader _variablesLoader;
     27    private IMLVariablesLoader _timeseriesLoader;
     28    private IMLVariablesLoader _doubleArraysLoader;
     29
     30    private CancellationTokenSource _cts = new CancellationTokenSource();
     31
    2232    public event EventHandler<EventArgs<string>> StatusChangedEvent;
    2333    public event EventHandler<EventArgs<string>> ErrorOccuredEvent;
    24 
    25 
    2634
    2735    /// <summary>
     
    4957    }
    5058
    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 {
    5463      get {
    55         return _selectedVariables;
     64        return _variablesLoader.SelectedVariables;
    5665      }
    5766    }
     
    6069      InitializeComponent();
    6170
    62       _selectedVariables = new List<KeyValuePair<string, MLDatatype>>();
     71      _selectedTimeseries = new List<MLVariableInfo>();
     72      _selectedDoubleArrays = new List<MLVariableInfo>();
    6373      _values = new Dataset();
    6474
    6575      this.FormClosed += (s, e) => DisposeMatlabConnector();
    6676      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
    6880      LoadValuesButton.Click += LoadValuesButtonClick;
    6981      TrainingTestTrackBar.ValueChanged += TrainingTestTrackBarValueChanged;
     82
    7083      StatusChangedEvent += (s, e) => BeginInvoke((MethodInvoker)(() => StatusLabel.Text = e.Value));
    7184      ErrorOccuredEvent += (s, e) => BeginInvoke((MethodInvoker)(() => {
     
    7487        ErrorTextBox.Visible = true;
    7588      }));
    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    }
    90117
    91118    private void LoadValuesButtonClick(object sender, EventArgs e) {
     119      _cts.Cancel();
     120      _cts = new CancellationTokenSource();
    92121      LoadValuesAsync();
    93122    }
     
    106135    }
    107136    #endregion
    108    
     137
    109138    /// <summary>
    110139    /// Fires the ErrorOccuredEvent with the given text message.
     
    127156      if (_mlConnector == null) {
    128157        _mlConnector = MatlabApiFactory.CreateMatlabConnector();
     158        _mlConnector.ErrorOccuredEvent += ErrorOccured;
    129159      }
    130160    }
     
    141171
    142172
    143 
    144173    /// <summary>
    145174    /// This method reads all available variables form the selected matlab scirpt and puts them into the list view.
     
    147176    private async void GetVariableNamesAsync() {
    148177      UpdateStatus("Opening Matlab");
    149 
    150178      var path = openFileDialog.FileName;
    151179      ProblemTextBox.Text = path;
     
    156184
    157185      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);
    163188      UpdateStatus("Done");
    164189    }
    165190
    166     /// <summary>
    167     /// Loads the values of the selected timeseries values and displays them at the preview control.
    168     /// </summary>
    169191    private async void LoadValuesAsync() {
     192      UpdateStatus("Loading values from Matlab");
    170193      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        }       
    192214      } catch (Exception ex) {
    193215        if (ex is IOException || ex is InvalidOperationException || ex is ArgumentException) {
    194216          ErrorOccured(ex.Message);
     217        } else if (ex is OutOfMemoryException) {
     218          ErrorOccured(ex.Message + " Amount of the given data is too big.");
    195219        } else {
    196220          throw;
     
    199223      UpdateStatus("Done");
    200224    }
    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     }
    262225  }
    263226}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/HeuristicLab.Problems.Instances.DataAnalysis-3.3.csproj

    r15912 r15926  
    165165    <Compile Include="Regression\Matlab\Api\MatlabApiFactory.cs" />
    166166    <Compile Include="Regression\Matlab\Api\MatlabConnector.cs" />
     167    <Compile Include="Regression\Matlab\Api\Types\MLVariableInfo.cs" />
    167168    <Compile Include="Regression\Matlab\RegressionMatlabImportType.cs" />
    168169    <Compile Include="Regression\Matlab\RegressionMatlabInstanceProvider.cs" />
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/IMatlabConnector.cs

    r15912 r15926  
    99namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api {
    1010  public interface IMatlabConnector : IDisposable {
     11    event Action<string> ErrorOccuredEvent;
     12
    1113    /// <summary>
    1214    /// Executes a given matlab script.
     
    2527    /// </summary>
    2628    /// <returns></returns>
    27     IDictionary<string, MLDatatype> GetVariablesFromScript(string script);
     29    IEnumerable<MLVariableInfo> GetVariablesFromScript(string script);
    2830
    2931    /// <summary>
     
    4749    /// <returns></returns>
    4850    MLDouble GetDouble(string varName);
     51
     52    /// <summary>
     53    ///
     54    /// </summary>
     55    /// <param name="varName"></param>
     56    /// <returns></returns>
     57    MLDoubleArray GetDoubleArray(string varName);
    4958  }
    5059}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/MatlabConnector.cs

    r15919 r15926  
    3131    private MLApp.MLApp matLabApi;
    3232    private string workspace;
     33    private object locker = new object();
     34
     35    public event Action<string> ErrorOccuredEvent;
    3336
    3437    public MatlabConnector(bool showcommandWindow = false, string workspace = "base") {
     
    6871
    6972
    70     public IDictionary<string, MLDatatype> GetVariablesFromScript(string script) {
    71       var variables = new Dictionary<string, MLDatatype>();
    72       ExecuteScript(script);
     73    public IEnumerable<MLVariableInfo> GetVariablesFromScript(string script) {
     74      var variables = new List<MLVariableInfo>();
     75      try {
     76        ExecuteScript(script);
     77      } catch(InvalidOperationException ex) {
     78        if (ErrorOccuredEvent != null) {
     79          ErrorOccuredEvent.Invoke(ex.Message);
     80        }
     81      }
    7382
    7483      Execute("variables=whos();nrOfVariables=size(variables)");
     
    7786      for (int i = 1; i <= len; i++) {
    7887        var varName = GetVariable(string.Format("variables({0}).name", i)) as string;
    79         var varSize = GetVariable(string.Format("variables({0}).size", i));
     88        var varSize = GetVariable(string.Format("variables({0}).size", i)) as double[,];
    8089        var varClass = GetVariable(string.Format("variables({0}).class", i)) as string;
    81         // At the moment only timeseries are supported.
    82         if (varClass == "timeseries") {
    83           variables.Add(varName, MLDatatype.Timeseries);
    84         }
     90       
     91        GetVariablesFromScriptRec(variables, varName, varSize, varClass);
    8592      }
    8693
    8794      return variables;
    8895    }
    89    
     96
     97    private void GetVariablesFromScriptRec(IList<MLVariableInfo> variables, string varName, double[,] varSize, string varClass) {
     98      var variable = new MLVariableInfo() {
     99        VarName = varName,
     100        Row = varSize[0, 0],
     101        Column = varSize[0, 1]
     102      };
     103      if (varClass == "timeseries") {
     104        variable.Datatype = MLDatatype.Timeseries;
     105        variables.Add(variable);
     106      } else if (varClass == "double") {
     107        if (variable.Row == 1 && variable.Column == 1) {
     108          variable.Datatype = MLDatatype.Double;
     109        } else {
     110          variable.Datatype = MLDatatype.DoubleArray;
     111        }
     112        variables.Add(variable);
     113      } else if (varClass == "struct") {
     114        Execute(string.Format("mlFieldNames = fieldnames({0})", varName));
     115        var fields = GetVariable("mlFieldNames") as object[,];
     116
     117        for (int i = 0; i < fields.GetLength(0); i++) {
     118          var newVarName = string.Format("{0}.{1}", varName, fields[i, 0] as string);
     119          Execute(string.Format("mlClass = class({0}); mlSize = size({0});", newVarName));
     120          var newVarClass = GetVariable("mlClass") as string;
     121          var newVarSize = GetVariable("mlSize") as double[,];
     122          GetVariablesFromScriptRec(variables, newVarName, newVarSize, newVarClass);
     123        }
     124      }
     125    }
    90126
    91127    public MLTimeseries GetTimeseries(string varName) {
    92       object times = GetVariable(varName + ".Time");
    93       object data = GetVariable(varName + ".Data");
    94       if (times == null || data == null) {
    95         return null;
     128      lock (locker) {
     129        object times = GetVariable(varName + ".Time");
     130        object data = GetVariable(varName + ".Data");
     131
     132        if (times == null || data == null) {
     133          return null;
     134        }
     135        return new MLTimeseries(varName, times, data);
    96136      }
    97 
    98       return new MLTimeseries(varName, times, data);
    99137    }
    100138
    101139    public MLDouble GetDouble(string varName) {
    102140      return new MLDouble(varName, GetVariable(varName));
     141    }
     142
     143
     144    public MLDoubleArray GetDoubleArray(string varName) {
     145      return new MLDoubleArray(varName, GetVariable(varName));
    103146    }
    104147
     
    142185      }
    143186    }
    144 
    145187  }
    146188}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDatatype.cs

    r15912 r15926  
    99        Double,
    1010        DoubleArray,
    11         Timeseries
     11        Timeseries,
     12        Struct,
     13        Undefinded
    1214    }
    1315}
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLDoubleArray.cs

    r15919 r15926  
    2727      Name = string.Format("{0};{1}", a1.Name, a2.Name);
    2828
    29       if (!ConcatFeasible(a1, a2)) {
     29      if (!ConcatIsPossible(a1, a2)) {
    3030        throw new ArgumentException(string.Format("Cannot concat {0} and {1}. Different number of rows.", a1.Name, a2.Name));
    3131      }
    3232
    33       var numberOfColumns = a1.Columns + a2.Columns;
    34       _dataHeaders = new string[numberOfColumns];
    35       for (int i = 0; i < numberOfColumns; i++) {
     33      var columns = a1.Columns + a2.Columns;
     34      _dataHeaders = new string[columns];
     35      for (int i = 0; i < columns; i++) {
    3636        if (i < a1.Columns) {
    3737          _dataHeaders[i] = a1.DataHeader[i];
     
    4141      }
    4242
    43       Data = new double[a1.Rows, numberOfColumns];
     43      Data = new double[a1.Rows, columns];
    4444      for (int i = 0; i < a1.Rows; i++) {
    45         for (int j = 0; j < numberOfColumns; j++) {
     45        for (int j = 0; j < columns; j++) {
    4646          if (j < a1.Columns) {
    4747            Data[i, j] = a1.Data[i, j];
    4848          } else {
    4949            Data[i, j] = a1.Data[i, j - a1.Columns];
     50          }
     51        }
     52      }
     53    }
     54
     55    private MLDoubleArray(IEnumerable<MLDoubleArray> arrays) {
     56      StringBuilder sb = new StringBuilder();
     57      foreach (var array in arrays) {
     58        sb.Append(array.Name + ";");
     59      }
     60      Name = sb.ToString();
     61
     62      var columns = 0;
     63      foreach (var array in arrays) {
     64        columns += array.Columns;
     65      }
     66
     67      var dataHeaders = new List<string>();
     68      foreach (var array in arrays) {
     69        foreach (var header in array.DataHeader) {
     70          dataHeaders.Add(header);
     71        }
     72      }
     73      _dataHeaders = dataHeaders.ToArray();
     74
     75      var rows = arrays.First().Rows;
     76      Data = new double[rows, columns];
     77      for (int i = 0; i < rows; i++) {
     78        var j = 0;
     79        foreach (var array in arrays) {
     80          for (int k = 0; k < array.Columns; k++) {
     81            Data[i, j] = array.Data[i, k];
     82            j++;
    5083          }
    5184        }
     
    5790    /// <summary>
    5891    /// Concats two given MLDoubleArrays if their number of rows equals.
    59     /// If they can't be concated, this method returns null.
     92    /// If throws an InvalidOperationException if they can't be concatenated
    6093    /// </summary>
    6194    /// <param name="a1"></param>
     
    6396    /// <returns></returns>
    6497    public static MLDoubleArray Concat(MLDoubleArray a1, MLDoubleArray a2) {
    65       if (ConcatFeasible(a1, a2)) {
    66         return null;
     98      if (ConcatIsPossible(a1, a2)) {
     99        throw new InvalidOperationException("Concatination is not possible because of different different number of their rows.");
    67100      }
    68101      return new MLDoubleArray(a1, a2);
    69102    }
    70103
    71     public static bool ConcatFeasible(MLDoubleArray a1, MLDoubleArray a2) {
     104    public static MLDoubleArray Concat(IEnumerable<MLDoubleArray> arrays) {
     105      if (arrays.Count() < 1) {
     106        throw new ArgumentException("Given number of MLDoubleArrays is 0");
     107      }
     108      if (!ConcatIsPossible(arrays)) {
     109        throw new InvalidOperationException("Concatenation is not possible because of different different number of their rows.");
     110      }
     111      return new MLDoubleArray(arrays);
     112    }
     113
     114    public static bool ConcatIsPossible(MLDoubleArray a1, MLDoubleArray a2) {
    72115      return a1.Rows == a2.Rows;
     116    }
     117
     118    public static bool ConcatIsPossible(IEnumerable<MLDoubleArray> arrays) {
     119      var first = arrays.First();
     120      foreach (var array in arrays) {
     121        if (!ConcatIsPossible(first, array)) {
     122          return false;
     123        }
     124      }
     125      return true;
    73126    }
    74127
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/Api/Types/MLTimeseries.cs

    r15919 r15926  
    33using System.Linq;
    44using System.Text;
     5using System.Threading;
    56using System.Threading.Tasks;
    67
     
    2425    /// </summary>
    2526    /// <param name="timeseries"></param>
    26     public MLTimeseries(IEnumerable<MLTimeseries> timeseries) : this() {
     27    public MLTimeseries(IEnumerable<MLTimeseries> timeseries, CancellationToken token) : this() {
    2728      var dataHeaders = new List<string>();
    2829      var times = new SortedSet<double>(); // The time values have to be unique and sorted.
     
    6465
    6566          foreach (var v in vs) {
     67            if (token.IsCancellationRequested) {
     68              return;
     69            }
    6670            values[idx++] = v;
    6771          }
  • branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/Matlab/RegressionMatlabInstanceProvider.cs

    r15912 r15926  
    6767
    6868
    69     public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable<KeyValuePair<string, MLDatatype>> variableNames) {
     69    public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable<MLVariableInfo> variableNames) {
    7070      var dataset = type.Values;
    7171      var targetVar = type.TargetVariable;
Note: See TracChangeset for help on using the changeset viewer.