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