Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.