1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using HeuristicLab.Problems.DataAnalysis;
|
---|
9 | using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api;
|
---|
10 | using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|