1 from configparser import ConfigParser
2 from datetime import datetime, timedelta
3 from jours_feries_france.compute import JoursFeries
4 from logging import getLogger
5 from logging.config import fileConfig
6 from pathlib import Path
7 from vacances_scolaires_france import SchoolHolidayDates
11 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
20 def __init__(self, config_file):
22 self._config = ConfigParser()
23 self._config.read(config_file)
25 # Collecting holidays features
26 self._features = [section for section in self._config
27 if self._config[section].getboolean('binary')
28 or self._config[section].getboolean('categorical')
29 or self._config[section].getboolean('numerical')]
31 self._dated_features = {}
49 def _get_academic_zone(self, name, date):
52 'Clermont-Ferrand': ('A', 'A'),
53 'Grenoble': ('A', 'A'),
55 'Montpellier': ('A', 'C'),
56 'Nancy-Metz': ('A', 'B'),
59 'Toulouse': ('A', 'C'),
60 'Aix-Marseille': ('B', 'B'),
62 'Besançon': ('B', 'A'),
65 'Limoges': ('B', 'A'),
67 'Orléans-Tours': ('B', 'B'),
68 'Poitiers': ('B', 'A'),
71 'Strasbourg': ('B', 'B'),
72 'Bordeaux': ('C', 'A'),
73 'Créteil': ('C', 'C'),
75 'Versailles': ('C', 'C')
77 if date < datetime(2016, 1, 1):
78 return dict_zones[name][0]
80 return dict_zones[name][1]
83 def dated_features(self):
84 if self._dated_features == {}:
85 logger.info("Adding holidays features")
86 bankHolidays = tuple(itertools.chain.from_iterable(list(JoursFeries.for_year(k).values())
87 for k in range(self.start.year, self.end.year + 1)))
88 bankHolidaysEve = tuple(u - timedelta(days=1) for u in bankHolidays)
89 name = self._config['ZONE']['name']
91 Date = datetime.date(date)
92 tomorrow = date + timedelta(days=1)
93 Tomorrow = datetime.date(tomorrow)
94 d = SchoolHolidayDates()
96 'bankHolidays': Date in bankHolidays,
97 'bankHolidaysEve': Date in bankHolidaysEve,
98 'holidays': d.is_holiday_for_zone(Date, self._get_academic_zone(name, date)),
99 'holidaysEve': d.is_holiday_for_zone(Tomorrow, self._get_academic_zone(name, tomorrow))
101 while date <= self._end:
102 self._dated_features[date] = dict_hour
104 date += timedelta(hours=1)
105 if date.day != current.day:
106 Date = datetime.date(date)
107 tomorrow = date + timedelta(days=1)
108 Tomorrow = datetime.date(tomorrow)
110 'bankHolidays': Date in bankHolidays,
111 'bankHolidaysEve': Date in bankHolidaysEve,
112 'holidays': d.is_holiday_for_zone(Date, self._get_academic_zone(name, date)),
113 'holidaysEve': d.is_holiday_for_zone(Tomorrow, self._get_academic_zone(name, tomorrow))
115 return self._dated_features