1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types {
|
---|
8 | using TMLTimeseriesValueDT = IMLValueVariable<IList<KeyValuePair<double, double[]>>>;
|
---|
9 | public class MLTimeseries : IMLValueVariable<IList<KeyValuePair<double, double[]>>>, IMLTimeseries {
|
---|
10 | private readonly object _locker = new object();
|
---|
11 |
|
---|
12 | private string[] _dataHeaders;
|
---|
13 |
|
---|
14 | protected MLTimeseries() {
|
---|
15 | Data = new List<KeyValuePair<double, double[]>>();
|
---|
16 | Name = string.Empty;
|
---|
17 | }
|
---|
18 |
|
---|
19 | /// <summary>
|
---|
20 | /// Creates a new timeseries from the given collection of timeseries.
|
---|
21 | /// All values will be merged into one time series.
|
---|
22 | /// </summary>
|
---|
23 | /// <param name="timeseries"></param>
|
---|
24 | public MLTimeseries(IEnumerable<MLTimeseries> timeseries) : this() {
|
---|
25 | var dataHeaders = new List<string>();
|
---|
26 | var times = new SortedSet<double>(); // The time values have to be unique and sorted.
|
---|
27 |
|
---|
28 | foreach (var item in timeseries) {
|
---|
29 | foreach (var dataHeader in item.DataHeader) {
|
---|
30 | dataHeaders.Add(dataHeader);
|
---|
31 | }
|
---|
32 |
|
---|
33 | foreach (var time in item.Times) {
|
---|
34 | times.Add(time);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | var numberOfElements = dataHeaders.Count;
|
---|
39 | Parallel.ForEach(times, (time) => {
|
---|
40 | //foreach (var time in times) {
|
---|
41 | var values = new double[numberOfElements];
|
---|
42 | int idx = 0;
|
---|
43 | foreach (var item in timeseries) {
|
---|
44 | double[] vs = item.GetValuesByTime(time);
|
---|
45 |
|
---|
46 | foreach (var v in vs) {
|
---|
47 | values[idx++] = v;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | lock (_locker) {
|
---|
51 | Data.Add(new KeyValuePair<double, double[]>(time, values));
|
---|
52 | }
|
---|
53 | });
|
---|
54 |
|
---|
55 | _dataHeaders = dataHeaders.ToArray();
|
---|
56 | }
|
---|
57 |
|
---|
58 | public MLTimeseries(string name, object times, object data) : this() {
|
---|
59 | Name = name;
|
---|
60 |
|
---|
61 | if (!(times is double[,]) || !(data is double[,])) {
|
---|
62 | throw new ArgumentException(string.Format("Invalid datatype: times={0}, data={1}", times.GetType(), data.GetType()));
|
---|
63 | }
|
---|
64 |
|
---|
65 | var t = times as double[,];
|
---|
66 | var d = data as double[,];
|
---|
67 |
|
---|
68 | if (t.GetLength(0) != d.GetLength(0)) {
|
---|
69 | throw new ArgumentException(string.Format("Number of elements are not equal: times={0}, data={1}", t.GetLength(0), d.GetLength(0)));
|
---|
70 | }
|
---|
71 |
|
---|
72 | var valueColumns = d.GetLength(1);
|
---|
73 | _dataHeaders = new string[valueColumns];
|
---|
74 |
|
---|
75 | for (int i = 0; i < valueColumns; i++) {
|
---|
76 | _dataHeaders[i] = string.Format("{0}:{1}", Name, i);
|
---|
77 | }
|
---|
78 |
|
---|
79 | for (int i = 0; i < t.GetLength(0); i++) {
|
---|
80 | var time = t[i, 0];
|
---|
81 | var vals = new double[valueColumns];
|
---|
82 | for (int j = 0; j < valueColumns; j++) {
|
---|
83 | vals[j] = d[i, j];
|
---|
84 | }
|
---|
85 | Data.Add(new KeyValuePair<double, double[]>(time, vals));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private MLTimeseries(IMLTimeseries original) : this() {
|
---|
90 | Name = ((TMLTimeseriesValueDT)original).Name;
|
---|
91 |
|
---|
92 | _dataHeaders = (string[])original.DataHeader.Clone();
|
---|
93 |
|
---|
94 | foreach (var entry in ((TMLTimeseriesValueDT)original).Data) {
|
---|
95 | Data.Add(new KeyValuePair<double, double[]>(entry.Key, entry.Value));
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public string Name { get; set; }
|
---|
100 |
|
---|
101 | public IList<KeyValuePair<double, double[]>> Data { get; set; }
|
---|
102 |
|
---|
103 | public MLDatatype Datatype {
|
---|
104 | get { return MLDatatype.Timeseries; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public string[] DataHeader {
|
---|
108 | get { return _dataHeaders; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public double[] this[int idx] {
|
---|
112 | get {
|
---|
113 | var entry = Data[idx];
|
---|
114 | var values = new double[entry.Value.Length + 1];
|
---|
115 | values[0] = Data[idx].Key;
|
---|
116 | for (int i = 1; i < values.Length; i++) {
|
---|
117 | values[i] = entry.Value[i - 1];
|
---|
118 | }
|
---|
119 | return values;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public double[] Times {
|
---|
124 | get {
|
---|
125 | return Data.Select(x => x.Key).ToArray();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public double GetTimeAt(int idx) {
|
---|
130 | if (idx < Data.Count) {
|
---|
131 | return Data[idx].Key;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return double.MaxValue;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public int Count {
|
---|
138 | get {
|
---|
139 | return Data.Count;
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public double[] GetValuesByTime(double time) {
|
---|
144 | double[] value;
|
---|
145 | if (Data.Count < 0) {
|
---|
146 | value = new double[] { 0.0 };
|
---|
147 | }
|
---|
148 | value = Data.Where(x => x.Key <= time).LastOrDefault().Value;
|
---|
149 | if (value == null) {
|
---|
150 | return new double[Data[0].Value.Length];
|
---|
151 | }
|
---|
152 | return value;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public IMLTimeseries ToTimeseries() {
|
---|
156 | return new MLTimeseries(this);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|