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

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