Projekt

Obecné

Profil

Stáhnout (2.92 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

    
9
namespace ServerApp.Predictor
10
{
11
    /// <summary>
12
    /// A class responsible for preparation of features for classifiers.
13
    /// </summary>
14
    public class FeatureExtractor
15
    {
16
        /// <summary>
17
        /// A DataParser instance used to access info objects.
18
        /// </summary>
19
        private readonly IDataParser dataParser;
20

    
21
        private Dictionary<string, int> buildingsToAreas;
22

    
23
        /// <summary>
24
        /// Instantiates new FeatureExtractor class.
25
        /// </summary>
26
        /// <param name="dataParser">Data parser used to access training data.</param>
27
        public FeatureExtractor(IDataParser dataParser, Dictionary<string, int> buildingsToAreas)
28
        {
29
            this.dataParser = dataParser;
30
            this.buildingsToAreas = buildingsToAreas;
31
        }
32

    
33

    
34
        /// <summary>
35
        /// Extracts list of feature vectors from parsed info objects.
36
        /// </summary>
37
        /// <param name="weatherInfos">List of weather info objects.</param>
38
        /// <param name="activityInfos">List of info objects about activities at the site.</param>
39
        /// <returns>A list of feature vectors for model training.</returns>
40
        public IEnumerable<ModelInput> PrepareModelInput(int area, DateTime startDate, DateTime endDate, int interval = 1, bool wholeDay = true)
41
        {
42
            dataParser.Parse(startDate, endDate, interval, wholeDay);
43
            List<string> buildings = new List<string>();
44

    
45
            // find all buildings in area
46
            foreach (KeyValuePair<string, int> kvp in buildingsToAreas)
47
            {
48
                if (kvp.Value == area)
49
                {
50
                    buildings.Add(kvp.Key);
51
                }
52
            }
53

    
54
            var res =  new List<ModelInput>();
55
            // use enumerator to access records about attendance and weather together
56
            IEnumerator<Parser.OutputInfo.WeatherInfo> weatherEnumerator = dataParser.WeatherList.GetEnumerator();
57
            weatherEnumerator.MoveNext();
58
            foreach (Parser.OutputInfo.ActivityInfo val in dataParser.AttendanceList)
59
            {
60
                if (!weatherEnumerator.Current.startTime.Equals(val.startTime))
61
                {
62
                    weatherEnumerator.MoveNext();
63
                }
64
                if (buildings.Contains(val.building))
65
                {
66
                    Parser.OutputInfo.WeatherInfo weatherInfo = weatherEnumerator.Current;
67
                    res.Add(new ModelInput
68
                    {
69
                        Temp = (float)weatherInfo.temp,
70
                        Time = val.startTime,
71
                        Wind = (float)weatherInfo.wind,
72
                        Rain = (float)weatherInfo.rain,
73
                        // TODO convert val.amount to label
74
                    });
75
                }
76
            }
77

    
78
            return res;
79
        }
80
    }
81
}
(1-1/7)