1
|
//
|
2
|
// Author: Roman Kalivoda
|
3
|
//
|
4
|
|
5
|
using System.Collections.Generic;
|
6
|
using System;
|
7
|
using ServerApp.Parser.Parsers;
|
8
|
using ServerApp.Parser.OutputInfo;
|
9
|
using System.Linq;
|
10
|
|
11
|
namespace ServerApp.Predictor
|
12
|
{
|
13
|
/// <summary>
|
14
|
/// A class responsible for preparation of features for classifiers.
|
15
|
/// </summary>
|
16
|
public class FeatureExtractor
|
17
|
{
|
18
|
/// <summary>
|
19
|
/// A DataParser instance used to access info objects.
|
20
|
/// </summary>
|
21
|
private readonly IDataParser dataParser;
|
22
|
|
23
|
private Dictionary<string, int> buildingsToAreas;
|
24
|
|
25
|
/// <summary>
|
26
|
/// Instantiates new FeatureExtractor class.
|
27
|
/// </summary>
|
28
|
/// <param name="dataParser">Data parser used to access training data.</param>
|
29
|
public FeatureExtractor(IDataParser dataParser, Dictionary<string, int> buildingsToAreas)
|
30
|
{
|
31
|
this.dataParser = dataParser;
|
32
|
this.buildingsToAreas = buildingsToAreas;
|
33
|
}
|
34
|
|
35
|
/// <summary>
|
36
|
/// TODO comment
|
37
|
/// </summary>
|
38
|
/// <param name="area"></param>
|
39
|
/// <param name="startDate"></param>
|
40
|
/// <param name="endDate"></param>
|
41
|
/// <param name="interval"></param>
|
42
|
/// <param name="wholeDay"></param>
|
43
|
/// <returns></returns>
|
44
|
public List<ModelInput> PrepareTrainingInput(int area, DateTime startDate, DateTime endDate, int interval = 1, bool wholeDay = true)
|
45
|
{
|
46
|
dataParser.Parse(startDate, endDate, interval, wholeDay);
|
47
|
List<string> buildings = new List<string>();
|
48
|
|
49
|
// find all buildings in area
|
50
|
foreach (KeyValuePair<string, int> kvp in buildingsToAreas)
|
51
|
{
|
52
|
if (kvp.Value == area)
|
53
|
{
|
54
|
buildings.Add(kvp.Key);
|
55
|
}
|
56
|
}
|
57
|
|
58
|
var res = new List<ModelInput>();
|
59
|
foreach (WeatherInfo val in dataParser.WeatherList)
|
60
|
{
|
61
|
res.Add(new ModelInput
|
62
|
{
|
63
|
Temp = (float)val.temp,
|
64
|
Time = val.startTime,
|
65
|
Wind = (float)val.wind,
|
66
|
Rain = (float)val.rain,
|
67
|
});
|
68
|
}
|
69
|
|
70
|
List<ActivityInfo> attendance = dataParser.AttendanceList;
|
71
|
foreach (ModelInput input in res)
|
72
|
{
|
73
|
List<int> amounts = new List<int>();
|
74
|
List<int> maxima = new List<int>();
|
75
|
foreach (string building in buildings)
|
76
|
{
|
77
|
|
78
|
amounts.Add(attendance.Where(activity => activity.building.Equals(building) && activity.startTime.Equals(input.Time)).Select(activity => activity.amount).First());
|
79
|
maxima.Add(attendance.Where(activity => activity.building.Equals(building)).Select(activity => activity.amount).Max());
|
80
|
}
|
81
|
double ratio = amounts.Sum() / (double)maxima.Sum();
|
82
|
input.Label = RatioToLabel(ratio);
|
83
|
}
|
84
|
|
85
|
return res;
|
86
|
}
|
87
|
|
88
|
private string RatioToLabel(double ratio)
|
89
|
{
|
90
|
if (ratio < 0.1f)
|
91
|
{
|
92
|
return "10%";
|
93
|
} else if (ratio < 0.2f)
|
94
|
{
|
95
|
return "20%";
|
96
|
} else if (ratio < 0.3f)
|
97
|
{
|
98
|
return "30%";
|
99
|
} else if (ratio < 0.4f)
|
100
|
{
|
101
|
return "40%";
|
102
|
} else if (ratio < 0.5f)
|
103
|
{
|
104
|
return "50%";
|
105
|
} else if (ratio < 0.6f)
|
106
|
{
|
107
|
return "60%";
|
108
|
} else if(ratio < 0.7f) {
|
109
|
return "70%";
|
110
|
} else if (ratio < 0.8f)
|
111
|
{
|
112
|
return "80%";
|
113
|
} else if (ratio < 0.9f)
|
114
|
{
|
115
|
return "90%";
|
116
|
} else
|
117
|
{
|
118
|
return "100%";
|
119
|
}
|
120
|
}
|
121
|
|
122
|
private double LabelToRatio(string label)
|
123
|
{
|
124
|
if (label.Equals("10%")) {
|
125
|
return 0.1f;
|
126
|
}
|
127
|
else if (label.Equals("20%")) {
|
128
|
return 0.2f;
|
129
|
}
|
130
|
else if (label.Equals("30%")) {
|
131
|
return 0.3f;
|
132
|
}
|
133
|
else if (label.Equals("40%")) {
|
134
|
return 0.4f;
|
135
|
}
|
136
|
else if (label.Equals("50%")) {
|
137
|
return 0.5f;
|
138
|
}
|
139
|
else if (label.Equals("60%")) {
|
140
|
return 0.6f;
|
141
|
}
|
142
|
else if (label.Equals("70%")) {
|
143
|
return 0.7f;
|
144
|
}
|
145
|
else if (label.Equals("80%")) {
|
146
|
return 0.8f;
|
147
|
}
|
148
|
else if (label.Equals("90%")) {
|
149
|
return 0.9f;
|
150
|
}
|
151
|
else
|
152
|
{
|
153
|
return 1.0f;
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
}
|