X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/predictops.git/blobdiff_plain/a2faba3f0797b7be72d0c8fa9cb9db67456136d6..refs/heads/master:/predictops/source/holidays.py?ds=sidebyside diff --git a/predictops/source/holidays.py b/predictops/source/holidays.py index 1a536fe..db61f01 100644 --- a/predictops/source/holidays.py +++ b/predictops/source/holidays.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from jours_feries_france.compute import JoursFeries from logging import getLogger from logging.config import fileConfig +from pathlib import Path from vacances_scolaires_france import SchoolHolidayDates import itertools @@ -10,10 +11,11 @@ import itertools fileConfig((Path.cwd() / 'config') / 'logging.cfg') logger = getLogger() + class Holidays: _start = None - _end = None + _end = None def __init__(self, config_file): @@ -22,12 +24,12 @@ class Holidays: # Collecting holidays features self._features = [section for section in self._config - if self._config[section].getboolean('numerical') - or self._config[section].getboolean('categorical')] + if self._config[section].getboolean('binary') + or self._config[section].getboolean('categorical') + or self._config[section].getboolean('numerical')] self._dated_features = {} - @property def start(self): return self._start @@ -36,7 +38,6 @@ class Holidays: def start(self, x): self._start = x - @property def end(self): return self._end @@ -45,62 +46,70 @@ class Holidays: def end(self, x): self._end = x - - def _get_academic_zone(self, name, date): dict_zones = { - 'Caen' : ('A', 'B'), - 'Clermont-Ferrand' : ('A', 'A'), - 'Grenoble' : ('A', 'A'), - 'Lyon' : ('A', 'A'), - 'Montpellier' : ('A', 'C'), - 'Nancy-Metz' : ('A', 'B'), - 'Nantes' : ('A', 'B'), - 'Rennes' : ('A', 'B'), - 'Toulouse' : ('A', 'C'), - 'Aix-Marseille' : ('B', 'B'), - 'Amiens' : ('B', 'B'), - 'Besançon' : ('B', 'A'), - 'Dijon' : ('B', 'A'), - 'Lille' : ('B', 'B'), - 'Limoges' : ('B', 'A'), - 'Nice' : ('B', 'B'), - 'Orléans-Tours' : ('B', 'B'), - 'Poitiers' : ('B', 'A'), - 'Reims' : ('B', 'B'), - 'Rouen ' : ('B', 'B'), - 'Strasbourg' : ('B', 'B'), - 'Bordeaux' : ('C', 'A'), - 'Créteil' : ('C', 'C'), - 'Paris' : ('C', 'C'), - 'Versailles' : ('C', 'C') + 'Caen': ('A', 'B'), + 'Clermont-Ferrand': ('A', 'A'), + 'Grenoble': ('A', 'A'), + 'Lyon': ('A', 'A'), + 'Montpellier': ('A', 'C'), + 'Nancy-Metz': ('A', 'B'), + 'Nantes': ('A', 'B'), + 'Rennes': ('A', 'B'), + 'Toulouse': ('A', 'C'), + 'Aix-Marseille': ('B', 'B'), + 'Amiens': ('B', 'B'), + 'Besançon': ('B', 'A'), + 'Dijon': ('B', 'A'), + 'Lille': ('B', 'B'), + 'Limoges': ('B', 'A'), + 'Nice': ('B', 'B'), + 'Orléans-Tours': ('B', 'B'), + 'Poitiers': ('B', 'A'), + 'Reims': ('B', 'B'), + 'Rouen ': ('B', 'B'), + 'Strasbourg': ('B', 'B'), + 'Bordeaux': ('C', 'A'), + 'Créteil': ('C', 'C'), + 'Paris': ('C', 'C'), + 'Versailles': ('C', 'C') } if date < datetime(2016, 1, 1): return dict_zones[name][0] else: return dict_zones[name][1] - @property def dated_features(self): if self._dated_features == {}: logger.info("Adding holidays features") bankHolidays = tuple(itertools.chain.from_iterable(list(JoursFeries.for_year(k).values()) - for k in range(self.start.year, self.end.year+1))) - bankHolidaysEve = tuple(u-timedelta(days=1) for u in bankHolidays) + for k in range(self.start.year, self.end.year + 1))) + bankHolidaysEve = tuple(u - timedelta(days=1) for u in bankHolidays) name = self._config['ZONE']['name'] date = self._start + Date = datetime.date(date) + tomorrow = date + timedelta(days=1) + Tomorrow = datetime.date(tomorrow) d = SchoolHolidayDates() + dict_hour = { + 'bankHolidays': Date in bankHolidays, + 'bankHolidaysEve': Date in bankHolidaysEve, + 'holidays': d.is_holiday_for_zone(Date, self._get_academic_zone(name, date)), + 'holidaysEve': d.is_holiday_for_zone(Tomorrow, self._get_academic_zone(name, tomorrow)) + } while date <= self._end: - Date = datetime.date(date) - tomorrow = date + timedelta(days=1) - Tomorrow = datetime.date(tomorrow) - dict_hour = { - 'bankHolidays' : Date in bankHolidays, - 'bankHolidaysEve': Date in bankHolidaysEve, - 'holidays': d.is_holiday_for_zone(Date, self._get_academic_zone(name, date)), - 'holidaysEve': d.is_holiday_for_zone(Tomorrow, self._get_academic_zone(name, tomorrow)) - } self._dated_features[date] = dict_hour + current = date date += timedelta(hours=1) - return self._dated_features \ No newline at end of file + if date.day != current.day: + Date = datetime.date(date) + tomorrow = date + timedelta(days=1) + Tomorrow = datetime.date(tomorrow) + dict_hour = { + 'bankHolidays': Date in bankHolidays, + 'bankHolidaysEve': Date in bankHolidaysEve, + 'holidays': d.is_holiday_for_zone(Date, self._get_academic_zone(name, date)), + 'holidaysEve': d.is_holiday_for_zone(Tomorrow, self._get_academic_zone(name, tomorrow)) + } + return self._dated_features