]> AND Private Git Repository - predictops.git/blob - predictops/engine.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
e7bbf1c5aa58221da7a8aaa71788cf0339258cbc
[predictops.git] / predictops / engine.py
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
7
8 from .source.ephemeris import Ephemeris
9 from .source.holidays import Holidays
10 from .source.meteofrance import MeteoFrance
11 from .learn.learning import Learning
12 from .learn.preprocessing import Preprocessing
13 from .target.target import Target
14
15 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
16 logger = getLogger()
17
18
19 class Engine:
20
21     def __init__(self, config_file = (Path.cwd() / 'config') / 'learn.cfg'):
22         self._config = ConfigParser()
23         self._config.read(config_file)
24         self._start = datetime.strptime(self._config['DATETIME']['start'],
25                                         '%m/%d/%Y %H:%M:%S')
26         self._end = datetime.strptime(self._config['DATETIME']['end'],
27                                         '%m/%d/%Y %H:%M:%S')
28
29         self._timestep = timedelta(hours =
30                                    self._config['DATETIME'].getfloat('hourStep'))
31
32         self._X = {}
33
34
35
36     def clean(self):
37         # Cleaning the data directory
38         logger.info("Cleaning and restoring data directory")
39         directory  = Path.cwd() / 'data'
40         if directory.is_dir():
41             rmtree(directory)
42         p = Path(Path.cwd() / 'data')
43         p.mkdir()
44
45
46     def add_features(self):
47         if self._config['FEATURES'].getboolean('meteofrance'):
48             meteofeature = MeteoFrance(config_file =
49                                        eval(self._config['FEATURE_CONFIG']['meteofrance']))
50
51             meteofeature.start = self._start
52             meteofeature.end = self._end
53
54             meteofeature.update()
55             dated_features = meteofeature.dated_features
56             for date in dated_features:
57                 self._X.setdefault(date,{}).update(dated_features[date])
58
59         if self._config['FEATURES'].getboolean('ephemeris'):
60             ephemerides = Ephemeris(config_file =
61                                     eval(self._config['FEATURE_CONFIG']['ephemeris']))
62
63             ephemerides.start = self._start
64             ephemerides.end = self._end
65
66             dated_features = ephemerides.dated_features
67             for date in dated_features:
68                 self._X.setdefault(date,{}).update(dated_features[date])
69
70         if self._config['FEATURES'].getboolean('holidays'):
71             holidays = Holidays(config_file =
72                                 eval(self._config['FEATURE_CONFIG']['holidays']))
73
74             holidays.start = self._start
75             holidays.end = self._end
76
77             dated_features = holidays.dated_features
78             for date in dated_features:
79                 self._X.setdefault(date,{}).update(dated_features[date])
80
81
82     def add_target(self):
83         self._target = Target(config_file = eval(self._config['TARGET']['config']),
84                               start = self._start, end = self._end,
85                               timestep = self._timestep)
86
87
88     def add_preprocessing(self):
89         self._preproc = Preprocessing(config_file = self._config,
90                                       dict_features = self.X,
91                                       dict_target = self.y)
92
93
94     def learn(self):
95         history = self._config['HISTORY_KNOWLEDGE'].getint('nb_lines')
96         self._learner = Learning(config_file = eval(self._config['LEARNER']['config']),
97                                  X = self._preproc.dataframe, y = list(self.y.values())[history:])
98
99
100     @property
101     def X(self):
102         return self._X
103
104     @X.setter
105     def X(self, x):
106         self._X = x
107
108
109     @property
110     def y(self):
111         return self._target.y
112
113     @y.setter
114     def end(self, y):
115         self._target.y = y