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

Private GIT Repository
d0e4ca06cb14a5a9ec18e17ce626a83498434647
[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         # Check for the integrity of feature names
19         super(Source, self).__init__()
20
21         self._config = ConfigParser()
22         self._config.read(config_file)
23
24         # Collecting ephemeris features
25         with open(CSV_FILE, "r") as f:
26             reader = DictReader(f, delimiter=',')
27             self._features = [row['name'] for row in reader
28                               if self._config['FEATURES'].getboolean(row['name'])]
29
30         self._dated_features = {}
31
32
33     @property
34     def start(self):
35         return self._start
36
37     @start.setter
38     def start(self, x):
39         self._start = x
40
41
42     @property
43     def end(self):
44         return self._end
45
46     @end.setter
47     def end(self, x):
48         self._end = x
49
50
51
52     @property
53     def dated_features(self):
54         if self._dated_features == {}:
55             date = self._start
56             while date <= self._end:
57                 dict_hour = {}
58                 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
59                 for feature in self._features:
60                     if feature == 'hour':
61                         dict_hour['hour'] = Date.tm_hour
62                     elif feature == 'dayInWeek':
63                         dict_hour['dayInWeek'] = Date.tm_wday
64                     elif feature == 'dayInMonth':
65                         dict_hour['dayInMonth'] = Date.tm_mday
66                     elif feature == 'month':
67                         dict_hour['month'] = Date.tm_mon
68                     elif feature == 'year':
69                         dict_hour['year'] = Date.tm_year
70                     elif feature == 'dayInYear':
71                         # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
72                         # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
73                         # les 24 décembre... tombent toujours
74                         if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/"+str(Date.tm_year), "%d/%m/%Y"):
75                             dict_hour['dayInYear'] = Date.tm_yday -1
76                         else:
77                             dict_hour['dayInYear'] = Date.tm_yday
78                     elif feature == 'weekInYear':
79                         dict_hour['weekInYear'] = date.isocalendar()[1]
80                 self._dated_features[date] = dict_hour
81                 date += timedelta(hours=1)
82         return self._dated_features