-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimeseries.cs
100 lines (84 loc) · 2.7 KB
/
timeseries.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
namespace DesktopApp
{
//TimeSeries class
public class TimeSeries
{
private readonly Dictionary<string, List<float>> _csvMap; //the dictionary
private readonly int _columnsSize;
private readonly int _rowsSize;
private readonly List<string> _featuresList;
//Constructor. Processes the CSV file into a dictionary.
public TimeSeries(string file)
{
_featuresList = new List<string>();
_csvMap = new Dictionary<string, List<float>>();
var lines = file.Split('\n');
for (var index = 0; index < lines.Length; index++)
{
lines[index] = lines[index].Split('\r')[0];
}
_rowsSize = lines.Length - 2;
var lineIndex = 0;
var line = lines[lineIndex];
_columnsSize = 0;
var temp = "";
foreach (var ch in line)
{
if (ch == ',')
{
_featuresList.Add(temp);
_columnsSize++;
temp = "";
}
else
{
temp += ch;
}
}
_featuresList.Add(temp);
_columnsSize++;
lineIndex++; // features
for (var j = 0; j < _columnsSize; j++)
{
var key = _featuresList[j];
_csvMap[key] = new List<float>();
}
for (var i = 0; i < _rowsSize; i++)
{
line = lines[lineIndex++];
var stringValues = line.Split(',');
var floatValues = stringValues.Select(float.Parse).ToList();
for (var j = 0; j < _columnsSize; j++)
{
var key = _featuresList[j];
_csvMap[key].Add(floatValues[j]);
}
floatValues.Clear();
}
Console.WriteLine("");
}
//returns column by its name.
public List<float> GetColumn(string key)
{
return _csvMap.ContainsKey(key) ? _csvMap[key] : new List<float>();
}
//returns the column size.
public int GetColumnSize()
{
return _columnsSize;
}
//returns row size.
public int GetRowSize()
{
return _rowsSize;
}
//returns all the Features
public List<string> GetFeatures()
{
return _featuresList;
}
}
}