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

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