Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/Regression/Matlab/MLTimeseriesLoader.cs @ 18242

Last change on this file since 18242 was 15926, checked in by rhanghof, 7 years ago

#2913:

  • Added the support for importing different Matlab datatypes.
  • Added some classes and changed the import dialog for importing double arrays.
File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using System.Threading.Tasks;
7using System.Windows.Forms;
8using HeuristicLab.Problems.DataAnalysis;
9using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api;
10using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types;
11
12namespace HeuristicLab.Problems.Instances.DataAnalysis.Views.Regression.Matlab {
13  public partial class RegressionMatlabImportDialog {
14    private class MLTimeseriesLoader : MLVariablesLoader {
15      public MLTimeseriesLoader(RegressionMatlabImportDialog importDialog, CheckedListBox variablesListBox)
16        : base(importDialog, variablesListBox, MLDatatype.Timeseries) {
17      }     
18
19      public override Dataset GetPreviewDataset(IMatlabConnector mlConnector, CancellationToken token) {
20        var timeseries = new List<MLTimeseries>();
21        foreach (MLVariableInfo item in _variablesListBox.CheckedItems.Cast<MLVariableInfo>().ToList()) {
22          if (token.IsCancellationRequested) {
23            return null;
24          }
25
26          if (item.Datatype == _mlDatatype) {
27            var val = mlConnector.GetTimeseries(item.VarName);
28            if (val != null) {
29              timeseries.Add(val);
30            }
31          }
32        }
33
34        var merged = new MLTimeseries(timeseries, token);
35        if (token.IsCancellationRequested) {
36          return null;
37        }
38        return new Dataset(GetVariableNames(merged), GetValuesForDataset(merged));
39      }     
40
41      /// <summary>
42      /// Returns a list with all selected variable names for the preview dataset.
43      /// </summary>
44      /// <param name="timeseries"></param>
45      /// <returns></returns>
46      private IEnumerable<string> GetVariableNames(MLTimeseries timeseries) {
47        IList<string> variableNamesWithType = timeseries.DataHeader.ToList();
48        variableNamesWithType.Insert(0, "time");
49        return variableNamesWithType;
50      }
51
52      /// <summary>
53      /// Returns a two dimensional double array which contains the values including the timestamps of the given timeseries.
54      /// Array[value, variable]
55      /// If the amount of the data is too big, a OutOfMemoryException exception is being thrown.
56      /// This exception is being cought and a message will be shown at the error message box.
57      /// </summary>
58      /// <param name="timeseries"></param>
59      /// <returns></returns>
60      private double[,] GetValuesForDataset(MLTimeseries timeseries) {
61        double[,] datasetValues = new double[timeseries.Count, timeseries.DataHeader.Length + 1];
62
63        int i = 0;
64        foreach (var item in timeseries.Data) {
65          datasetValues[i, 0] = item.Key;
66          for (int j = 1; j < datasetValues.GetLength(1); j++) {
67            datasetValues[i, j] = item.Value[j - 1];
68          }
69          i++;
70        }
71        return datasetValues;
72      }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.