1 from configparser import ConfigParser
2 from datetime import datetime, timedelta
3 from logging import getLogger
4 from logging.config import fileConfig
5 from pathlib import Path
6 from shutil import rmtree
11 from .learn.learning import Learning
12 from .learn.preprocessing import Preprocessing
13 from .source.ephemeris import Ephemeris
14 from .source.holidays import Holidays
15 from .source.meteofrance import MeteoFrance
16 from .source.ramadan import Ramadan
17 from .source.sentinelles import Sentinelles
18 from .target.target import Target
20 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
29 def __exit__(self, type, value, traceback):
30 with open(str(self._file_name / os.path.basename(self._file_name)) + '.cfg', 'w') as f:
31 f.write(self._config_text)
33 def __init__(self, config_file=(Path.cwd() / 'config') / 'learn.cfg'):
34 self._config = ConfigParser()
35 self._config.read(config_file)
36 launching_time = datetime.strftime(datetime.now(), '%Y_%m_%d_%H_%M')
37 self._name = os.path.splitext(os.path.basename(eval(self._config['TARGET']['config'])))[0]
38 self._file_name = f"{self._name}-{launching_time}"
39 p = Path.cwd() / 'results' / self._name
40 p.mkdir(exist_ok=True, parents=True)
41 self._file_name = p / self._file_name
43 self._config_text = ''
44 with open(config_file) as f:
45 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
46 self._config_text += f.read() + '\n\n'
48 self._start = datetime.strptime(self._config['DATETIME']['start'],
50 self._end = datetime.strptime(self._config['DATETIME']['end'],
53 self._timestep = timedelta(hours=self._config['DATETIME'].getfloat('hourStep'))
58 # Cleaning the data directory
59 logger.info("Cleaning and restoring data directory")
60 directory = Path.cwd() / 'data'
61 if directory.is_dir():
63 p = Path(Path.cwd() / 'data')
66 def add_features(self):
67 if self._config['FEATURES'].getboolean('ephemeris'):
68 config_file = eval(self._config['FEATURE_CONFIG']['ephemeris'])
69 with open(config_file) as f:
70 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
71 self._config_text += f.read() + '\n\n'
73 ephemerides = Ephemeris(config_file=config_file, start=self._start, end=self._end)
75 # ephemerides.start = self._start
76 # ephemerides.end = self._end
78 dated_features = ephemerides.dated_features
79 for date in dated_features:
80 self._X.setdefault(date, {}).update(dated_features[date])
82 if self._config['FEATURES'].getboolean('holidays'):
83 config_file = eval(self._config['FEATURE_CONFIG']['holidays'])
84 with open(config_file) as f:
85 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
86 self._config_text += f.read() + '\n\n'
88 holidays = Holidays(config_file=config_file)
90 holidays.start = self._start
91 holidays.end = self._end
93 dated_features = holidays.dated_features
94 for date in dated_features:
95 self._X.setdefault(date, {}).update(dated_features[date])
97 if self._config['FEATURES'].getboolean('meteofrance'):
98 config_file = eval(self._config['FEATURE_CONFIG']['meteofrance'])
99 with open(config_file) as f:
100 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
101 self._config_text += f.read() + '\n\n'
103 meteofeature = MeteoFrance(config_file=config_file)
105 meteofeature.start = self._start
106 meteofeature.end = self._end
108 meteofeature.update()
109 dated_features = meteofeature.dated_features
110 for date in dated_features:
111 self._X.setdefault(date, {}).update(dated_features[date])
113 if self._config['FEATURES'].getboolean('ramadan'):
114 config_file = eval(self._config['FEATURE_CONFIG']['ramadan'])
115 with open(config_file) as f:
116 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
117 self._config_text += f.read() + '\n\n'
119 ramadan = Ramadan(config_file=config_file)
121 ramadan.start = self._start
122 ramadan.end = self._end
124 dated_features = ramadan.dated_features
125 for date in dated_features:
126 self._X.setdefault(date, {}).update(dated_features[date])
128 def add_target(self):
129 config_file = eval(self._config['TARGET']['config'])
130 cumulative = self._config['TARGET'].getboolean('cumulative')
131 with open(config_file) as f:
132 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
133 self._config_text += f.read() + '\n\n'
135 self._target = Target(config_file=config_file,
136 start=self._start, end=self._end,
137 timestep=self._timestep, cumulative=cumulative)
139 def add_preprocessing(self):
140 self._preproc = Preprocessing(config_file=self._config,
141 start=self._start, end=self._end,
142 timestep=self._timestep,
143 dict_features=self.X,
147 config_file = eval(self._config['LEARNER']['config'])
148 with open(config_file) as f:
149 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
150 self._config_text += f.read() + '\n\n'
152 history = eval(self._config['HISTORY_KNOWLEDGE']['nb_lines'])
153 self._learner = Learning(config_file=config_file, file_name=self._file_name,
154 X=self._preproc.dataframe, y=list(self.y.values())[history:],
155 horizon=self._config['TARGET'].getint('horizon'))
167 return self._target.y