Projekt

Obecné

Profil

Stáhnout (5.09 KB) Statistiky
| Větev: | Tag: | Revize:
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
    class FeatureExtractor
17
    {
18
        /// <summary>
19
        /// A DataParser instance used to access info objects.
20
        /// </summary>
21
        private readonly IDataParser DataParser;
22

    
23
        /// <summary>
24
        /// A configuration object of the <c>Predictor</c> package
25
        /// </summary>
26
        private PredictorConfiguration Configuration;
27

    
28
        /// <summary>
29
        /// Instantiates new FeatureExtractor class.
30
        /// </summary>
31
        /// <param name="dataParser">Data parser used to access training data.</param>
32
        public FeatureExtractor(IDataParser dataParser, PredictorConfiguration configuration)
33
        {
34
            this.DataParser = dataParser;
35
            this.Configuration = configuration;
36
        }
37

    
38
        /// <summary>
39
        /// TODO comment
40
        /// </summary>
41
        /// <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
        public List<ModelInput> PrepareTrainingInput(int area, DateTime startDate, DateTime endDate, int interval = 3, bool wholeDay = true)
48
        {
49
            DataParser.Parse(startDate, endDate, interval, wholeDay);
50
            List<string> buildings = new List<string>();
51

    
52
            // find all buildings in area
53
            foreach (KeyValuePair<string, int> kvp in Configuration.BuildingsToAreas)
54
            {
55
                if (kvp.Value == area)
56
                {
57
                    buildings.Add(kvp.Key);
58
                }
59
            }
60

    
61
            var res = new List<ModelInput>();
62
            foreach (WeatherInfo val in DataParser.WeatherList)
63
            {
64
                res.Add(new ModelInput
65
                {
66
                    Time = val.startTime,
67
                    Temp = (float)val.temp,
68
                    Hour = val.startTime.Hour,
69
                    Wind = (float)val.wind,
70
                    Rain = (float)val.rain,
71
                });
72
            }
73

    
74
            List<ActivityInfo> attendance = DataParser.AttendanceList;
75
            foreach (ModelInput input in res)
76
            {
77
                List<int> amounts = new List<int>();
78
                List<int> maxima = new List<int>();
79
                foreach (string building in buildings)
80
                {
81

    
82
                    amounts.Add(attendance.Where(activity => activity.building.Equals(building) && activity.startTime.Equals(input.Time)).Select(activity => activity.amount).First());
83
                    maxima.Add(attendance.Where(activity => activity.building.Equals(building)).Select(activity => activity.amount).Max());
84
                }
85
                double ratio = amounts.Sum() / (double)maxima.Sum();
86
                input.Label = RatioToLabel(ratio);
87
            }
88

    
89
            return res;
90
        }
91

    
92
        private string RatioToLabel(double ratio)
93
        {
94
            if (ratio < 0.1f)
95
            {
96
                return "10%";
97
            }
98
            else if (ratio < 0.2f)
99
            {
100
                return "20%";
101
            }
102
            else if (ratio < 0.3f)
103
            {
104
                return "30%";
105
            }
106
            else if (ratio < 0.4f)
107
            {
108
                return "40%";
109
            }
110
            else if (ratio < 0.5f)
111
            {
112
                return "50%";
113
            }
114
            else if (ratio < 0.6f)
115
            {
116
                return "60%";
117
            }
118
            else if (ratio < 0.7f)
119
            {
120
                return "70%";
121
            }
122
            else if (ratio < 0.8f)
123
            {
124
                return "80%";
125
            }
126
            else if (ratio < 0.9f)
127
            {
128
                return "90%";
129
            }
130
            else
131
            {
132
                return "100%";
133
            }
134
        }
135

    
136
        private double LabelToRatio(string label)
137
        {
138
            if (label.Equals("10%"))
139
            {
140
                return 0.1f;
141
            }
142
            else if (label.Equals("20%"))
143
            {
144
                return 0.2f;
145
            }
146
            else if (label.Equals("30%"))
147
            {
148
                return 0.3f;
149
            }
150
            else if (label.Equals("40%"))
151
            {
152
                return 0.4f;
153
            }
154
            else if (label.Equals("50%"))
155
            {
156
                return 0.5f;
157
            }
158
            else if (label.Equals("60%"))
159
            {
160
                return 0.6f;
161
            }
162
            else if (label.Equals("70%"))
163
            {
164
                return 0.7f;
165
            }
166
            else if (label.Equals("80%"))
167
            {
168
                return 0.8f;
169
            }
170
            else if (label.Equals("90%"))
171
            {
172
                return 0.9f;
173
            }
174
            else
175
            {
176
                return 1.0f;
177
            }
178
        }
179
    }
180
}
(1-1/8)