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

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