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
10 from .learn.learning import Learning
11 from .learn.preprocessing import Preprocessing
12 from .source.ephemeris import Ephemeris
13 from .source.holidays import Holidays
14 from .source.ramadan import Ramadan
15 from .source.meteofrance import MeteoFrance
16 from .target.target import Target
18 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
27 def __exit__(self, type, value, traceback):
28 with open(str(self._file_name / os.path.basename(self._file_name)) + '.cfg', 'w') as f:
29 f.write(self._config_text)
31 def __init__(self, config_file=(Path.cwd() / 'config') / 'learn.cfg'):
32 self._config = ConfigParser()
33 self._config.read(config_file)
34 launching_time = datetime.strftime(datetime.now(), '%Y_%m_%d_%H_%M')
35 self._name = os.path.splitext(os.path.basename(eval(self._config['TARGET']['config'])))[0]
36 self._file_name = f"{self._name}-{launching_time}"
37 p = Path.cwd() / 'results' / self._name
38 p.mkdir(exist_ok=True, parents=True)
39 self._file_name = p / self._file_name
41 self._config_text = ''
42 with open(config_file) as f:
43 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
44 self._config_text += f.read() + '\n\n'
46 self._start = datetime.strptime(self._config['DATETIME']['start'],
48 self._end = datetime.strptime(self._config['DATETIME']['end'],
51 self._timestep = timedelta(hours=self._config['DATETIME'].getfloat('hourStep'))
56 # Cleaning the data directory
57 logger.info("Cleaning and restoring data directory")
58 directory = Path.cwd() / 'data'
59 if directory.is_dir():
61 p = Path(Path.cwd() / 'data')
64 def add_features(self):
65 if self._config['FEATURES'].getboolean('ephemeris'):
66 config_file = eval(self._config['FEATURE_CONFIG']['ephemeris'])
67 with open(config_file) as f:
68 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
69 self._config_text += f.read() + '\n\n'
71 ephemerides = Ephemeris(config_file=config_file)
73 ephemerides.start = self._start
74 ephemerides.end = self._end
76 dated_features = ephemerides.dated_features
77 for date in dated_features:
78 self._X.setdefault(date, {}).update(dated_features[date])
80 if self._config['FEATURES'].getboolean('holidays'):
81 config_file = eval(self._config['FEATURE_CONFIG']['holidays'])
82 with open(config_file) as f:
83 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
84 self._config_text += f.read() + '\n\n'
86 holidays = Holidays(config_file=config_file)
88 holidays.start = self._start
89 holidays.end = self._end
91 dated_features = holidays.dated_features
92 for date in dated_features:
93 self._X.setdefault(date, {}).update(dated_features[date])
95 if self._config['FEATURES'].getboolean('meteofrance'):
96 config_file = eval(self._config['FEATURE_CONFIG']['meteofrance'])
97 with open(config_file) as f:
98 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
99 self._config_text += f.read() + '\n\n'
101 meteofeature = MeteoFrance(config_file=config_file)
103 meteofeature.start = self._start
104 meteofeature.end = self._end
106 meteofeature.update()
107 dated_features = meteofeature.dated_features
108 for date in dated_features:
109 self._X.setdefault(date, {}).update(dated_features[date])
111 if self._config['FEATURES'].getboolean('ramadan'):
112 config_file = eval(self._config['FEATURE_CONFIG']['ramadan'])
113 with open(config_file) as f:
114 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
115 self._config_text += f.read() + '\n\n'
117 ramadan = Ramadan(config_file=config_file)
119 ramadan.start = self._start
120 ramadan.end = self._end
122 dated_features = ramadan.dated_features
123 for date in dated_features:
124 self._X.setdefault(date, {}).update(dated_features[date])
126 def add_target(self):
127 config_file = eval(self._config['TARGET']['config'])
128 cumulative = self._config['TARGET'].getboolean('cumulative')
129 with open(config_file) as f:
130 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
131 self._config_text += f.read() + '\n\n'
133 self._target = Target(config_file=config_file,
134 start=self._start, end=self._end,
135 timestep=self._timestep, cumulative=cumulative)
137 def add_preprocessing(self):
138 self._preproc = Preprocessing(config_file=self._config,
139 dict_features=self.X,
143 config_file = eval(self._config['LEARNER']['config'])
144 with open(config_file) as f:
145 self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
146 self._config_text += f.read() + '\n\n'
148 history = eval(self._config['HISTORY_KNOWLEDGE']['nb_lines'])
149 self._learner = Learning(config_file=config_file, file_name=self._file_name,
150 X=self._preproc.dataframe, y=list(self.y.values())[history:],
151 horizon=self._config['TARGET'].getint('horizon'))
163 return self._target.y