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

Private GIT Repository
Adding ramadan features, and binary category of feat.
[predictops.git] / predictops / source / ramadan.py
1 from configparser import ConfigParser
2 from convertdate import islamic
3 from datetime import datetime, timedelta
4 from logging import getLogger
5 from logging.config import fileConfig
6 from pathlib import Path
7
8
9 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
10 logger = getLogger()
11
12
13 class Ramadan:
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 holidays 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     @property
31     def start(self):
32         return self._start
33
34     @start.setter
35     def start(self, x):
36         self._start = x
37
38     @property
39     def end(self):
40         return self._end
41
42     @end.setter
43     def end(self, x):
44         self._end = x
45
46     @property
47     def dated_features(self):
48         if self._dated_features == {}:
49             logger.info("Adding Ramadan features")
50             date = self._start
51             while date <= self._end:
52                 year, month, day = date.year, date.month, date.day
53                 eve = datetime(year, month, day) - timedelta(days=1)
54                 tomorrow = datetime(year, month, day) + timedelta(days=1)
55                 Hegirian_month = islamic.from_gregorian(year, month, day)[1]
56                 dict_hour = {
57                     'ramadanEve': False,
58                     'ramadan': False,
59                     'ramadanDayAfter': False
60                 }
61                 if Hegirian_month == 8 and\
62                    islamic.from_gregorian(tomorrow.year, tomorrow.month, tomorrow.day)[1] == 9:
63                     dict_hour['ramadanEve'] = True
64                 elif Hegirian_month == 9:
65                     dict_hour['ramadan'] = True
66                 elif Hegirian_month == 10 and\
67                         islamic.from_gregorian(eve.year, eve.month, eve.day)[1] == 9:
68                     dict_hour['ramadanDayAfter'] = True
69                 self._dated_features[date] = dict_hour
70                 date += timedelta(hours=1)
71         return self._dated_features