1 from astral import LocationInfo
2 from astral.sun import sun
3 from configparser import ConfigParser
4 from datetime import datetime, timedelta
5 from logging import getLogger
6 from logging.config import fileConfig
7 from pathlib import Path
13 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
22 def __init__(self, config_file, start, end):
24 self._config = ConfigParser()
25 self._config.read(config_file)
27 self._city = LocationInfo("Besançon", "France", "Europe/Paris", 47.237829, -6.0240539)
30 # Collecting ephemeris features
31 self._features = [section for section in self._config
32 if self._config[section].getboolean('binary')
33 or self._config[section].getboolean('categorical')
34 or self._config[section].getboolean('numerical')]
36 self._dated_features = {}
55 def dated_features(self):
56 if self._dated_features == {}:
57 logger.info("Adding ephemeris features")
58 paris = pytz.timezone('Europe/Paris')
60 while date <= self._end:
61 datel = paris.localize(date)
63 Date = time.strptime(datetime.strftime(date, '%m/%d/%Y %H:%M:%S'), '%m/%d/%Y %H:%M:%S')
64 s = sun(self._city.observer, date=date,
65 tzinfo=pytz.timezone('Europe/Paris'))
66 for feature in self._features:
68 dict_hour['hour'] = Date.tm_hour
69 elif feature == 'dayInWeek':
70 dict_hour['dayInWeek'] = Date.tm_wday
71 elif feature == 'dayInMonth':
72 dict_hour['dayInMonth'] = Date.tm_mday
73 elif feature == 'month':
74 dict_hour['month'] = Date.tm_mon
75 elif feature == 'year':
76 dict_hour['year'] = Date.tm_year
77 elif feature == 'dayInYear':
78 # Si c'est une année bissextile et qu'on est après le 29 février, on compte une journée
79 # dans l'année de moins, car on va supprimer les 29 févriers, de sorte que les 14 juillets,
80 # les 24 décembre... tombent toujours
81 if calendar.isleap(Date.tm_year) and Date >= time.strptime("29/02/" + str(Date.tm_year), "%d/%m/%Y"):
82 dict_hour['dayInYear'] = Date.tm_yday - 1
84 dict_hour['dayInYear'] = Date.tm_yday
85 elif feature == 'weekInYear':
86 dict_hour['weekInYear'] = date.isocalendar()[1]
87 elif feature == 'sunRised':
88 dict_hour['sunRised'] = (datel >= s["sunrise"] - timedelta(minutes=30)
89 and datel <= s["sunset"] - timedelta(minutes=30))
90 elif feature == 'noon':
91 dict_hour['noon'] = (datel.hour == s["noon"].hour)
92 elif feature == 'night':
93 dict_hour['night'] = (datel <= s["dawn"] - timedelta(minutes=30)
94 or datel >= s["dusk"] - timedelta(minutes=30))
95 elif feature == 'daylightSavingTime':
96 dict_hour['daylightSavingTime'] = (datel.dst() == timedelta(0))
98 self._dated_features[date] = dict_hour
99 date += timedelta(hours=1)
100 return self._dated_features