]> AND Private Git Repository - predictops.git/blob - predictops/source/ephemeris.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Reducing the computation time and adding holidays features
[predictops.git] / predictops / source / ephemeris.py
1 from configparser import ConfigParser
2 from datetime import datetime, timedelta
3 from logging import getLogger
4 from logging.config import fileConfig
5 from pathlib import Path
6
7 import time
8 import calendar
9
10 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
11 logger = getLogger()
12
13 class Ephemeris:
14
15     _start = None
16     _end   = None
17
18     def __init__(self, config_file):
19
20         self._config = ConfigParser()
21         self._config.read(config_file)
22
23         # Collecting ephemeris features
24         self._features = [section for section in self._config
25                               if self._config[section].getboolean('numerical')
26                               or self._config[section].getboolean('categorical')]
27
28         self._dated_features = {}
29
30
31     @property
32     def start(self):
33         return self._start
34
35     @start.setter
36     def start(self, x):
37         self._start = x
38
39
40     @property
41     def end(self):
42         return self._end
43
44     @end.setter
45     def end(self, x):
46         self._end = x
47
48
49     @property
50     def dated_features(self):
51         if self._dated_features == {}:
52             logger.info("Adding ephemeris features")
53             date = self._start
54             while date <= self._end:
55                 dict_hour = {}
56                 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
57                 for feature in self._features:
58                     if feature == 'hour':
59                         dict_hour['hour'] = Date.tm_hour
60                     elif feature == 'dayInWeek':
61                         dict_hour['dayInWeek'] = Date.tm_wday
62                     elif feature == 'dayInMonth':
63                         dict_hour['dayInMonth'] = Date.tm_mday
64                     elif feature == 'month':
65                         dict_hour['month'] = Date.tm_mon
66                     elif feature == 'year':
67                         dict_hour['year'] = Date.tm_year
68                     elif feature == 'dayInYear':
69                         # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
70                         # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
71                         # les 24 décembre... tombent toujours
72                         if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/"+str(Date.tm_year), "%d/%m/%Y"):
73                             dict_hour['dayInYear'] = Date.tm_yday -1
74                         else:
75                             dict_hour['dayInYear'] = Date.tm_yday
76                     elif feature == 'weekInYear':
77                         dict_hour['weekInYear'] = date.isocalendar()[1]
78                 self._dated_features[date] = dict_hour
79                 date += timedelta(hours=1)
80         return self._dated_features