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

Private GIT Repository
lightgbm is now working
[predictops.git] / predictops / engine.py
index 8ba5043ebfdec4ef386aef2cf223b7cb52407d0a..a63ef2932aee9086fb23ede8902ec4ea0820adb8 100644 (file)
@@ -5,45 +5,100 @@ from logging.config import fileConfig
 from pathlib import Path
 from shutil import rmtree
 
 from pathlib import Path
 from shutil import rmtree
 
+import os
+
+from .learn.learning import Learning
+from .learn.preprocessing import Preprocessing
 from .source.ephemeris import Ephemeris
 from .source.ephemeris import Ephemeris
+from .source.holidays import Holidays
+from .source.ramadan import Ramadan
 from .source.meteofrance import MeteoFrance
 from .source.meteofrance import MeteoFrance
+from .target.target import Target
 
 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
 logger = getLogger()
 
 
 
 fileConfig((Path.cwd() / 'config') / 'logging.cfg')
 logger = getLogger()
 
 
-class Engine:
+class Engine(object):
+
+    def __enter__(self):
+        return self
 
 
-    def __init__(self, config_file = (Path.cwd() / 'config') / 'learn.cfg'):
+    def __exit__(self, type, value, traceback):
+        with open(str(self._file_name / os.path.basename(self._file_name)) + '.cfg', 'w') as f:
+            f.write(self._config_text)
+
+    def __init__(self, config_file=(Path.cwd() / 'config') / 'learn.cfg'):
         self._config = ConfigParser()
         self._config.read(config_file)
         self._config = ConfigParser()
         self._config.read(config_file)
+        launching_time = datetime.strftime(datetime.now(), '%Y_%m_%d_%H_%M')
+        self._name = os.path.splitext(os.path.basename(eval(self._config['TARGET']['config'])))[0]
+        self._file_name = f"{self._name}-{launching_time}"
+        p = Path.cwd() / 'results' / self._name
+        p.mkdir(exist_ok=True, parents=True)
+        self._file_name = p / self._file_name
+
+        self._config_text = ''
+        with open(config_file) as f:
+            self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+            self._config_text += f.read() + '\n\n'
+
         self._start = datetime.strptime(self._config['DATETIME']['start'],
                                         '%m/%d/%Y %H:%M:%S')
         self._end = datetime.strptime(self._config['DATETIME']['end'],
         self._start = datetime.strptime(self._config['DATETIME']['start'],
                                         '%m/%d/%Y %H:%M:%S')
         self._end = datetime.strptime(self._config['DATETIME']['end'],
-                                        '%m/%d/%Y %H:%M:%S')
+                                      '%m/%d/%Y %H:%M:%S')
 
 
-        self._timestep = timedelta(hours =
-                                   self._config['DATETIME'].getfloat('hourStep'))
+        self._timestep = timedelta(hours=self._config['DATETIME'].getfloat('hourStep'))
 
         self._X = {}
 
         self._X = {}
-        self._Y = {}
-
-
 
     def clean(self):
         # Cleaning the data directory
         logger.info("Cleaning and restoring data directory")
 
     def clean(self):
         # Cleaning the data directory
         logger.info("Cleaning and restoring data directory")
-        directory  = Path.cwd() / 'data'
+        directory = Path.cwd() / 'data'
         if directory.is_dir():
             rmtree(directory)
         p = Path(Path.cwd() / 'data')
         p.mkdir()
 
         if directory.is_dir():
             rmtree(directory)
         p = Path(Path.cwd() / 'data')
         p.mkdir()
 
-
     def add_features(self):
     def add_features(self):
+        if self._config['FEATURES'].getboolean('ephemeris'):
+            config_file = eval(self._config['FEATURE_CONFIG']['ephemeris'])
+            with open(config_file) as f:
+                self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+                self._config_text += f.read() + '\n\n'
+
+            ephemerides = Ephemeris(config_file=config_file)
+
+            ephemerides.start = self._start
+            ephemerides.end = self._end
+
+            dated_features = ephemerides.dated_features
+            for date in dated_features:
+                self._X.setdefault(date, {}).update(dated_features[date])
+
+        if self._config['FEATURES'].getboolean('holidays'):
+            config_file = eval(self._config['FEATURE_CONFIG']['holidays'])
+            with open(config_file) as f:
+                self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+                self._config_text += f.read() + '\n\n'
+
+            holidays = Holidays(config_file=config_file)
+
+            holidays.start = self._start
+            holidays.end = self._end
+
+            dated_features = holidays.dated_features
+            for date in dated_features:
+                self._X.setdefault(date, {}).update(dated_features[date])
+
         if self._config['FEATURES'].getboolean('meteofrance'):
         if self._config['FEATURES'].getboolean('meteofrance'):
-            meteofeature = MeteoFrance(config_file =
-                                       eval(self._config['FEATURE_CONFIG']['meteofrance']))
+            config_file = eval(self._config['FEATURE_CONFIG']['meteofrance'])
+            with open(config_file) as f:
+                self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+                self._config_text += f.read() + '\n\n'
+
+            meteofeature = MeteoFrance(config_file=config_file)
 
             meteofeature.start = self._start
             meteofeature.end = self._end
 
             meteofeature.start = self._start
             meteofeature.end = self._end
@@ -51,19 +106,49 @@ class Engine:
             meteofeature.update()
             dated_features = meteofeature.dated_features
             for date in dated_features:
             meteofeature.update()
             dated_features = meteofeature.dated_features
             for date in dated_features:
-                self._X.setdefault(date,{}).update(dated_features[date])
+                self._X.setdefault(date, {}).update(dated_features[date])
 
 
-        if self._config['FEATURES'].getboolean('ephemeris'):
-            ephemerides = Ephemeris(config_file =
-                                    eval(self._config['FEATURE_CONFIG']['ephemeris']))
+        if self._config['FEATURES'].getboolean('ramadan'):
+            config_file = eval(self._config['FEATURE_CONFIG']['ramadan'])
+            with open(config_file) as f:
+                self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+                self._config_text += f.read() + '\n\n'
 
 
-            ephemerides.start = self._start
-            ephemerides.end = self._end
+            ramadan = Ramadan(config_file=config_file)
 
 
-            dated_features = ephemerides.dated_features
-            for date in dated_features:
-                self._X.setdefault(date,{}).update(dated_features[date])
+            ramadan.start = self._start
+            ramadan.end = self._end
 
 
+            dated_features = ramadan.dated_features
+            for date in dated_features:
+                self._X.setdefault(date, {}).update(dated_features[date])
+
+    def add_target(self):
+        config_file = eval(self._config['TARGET']['config'])
+        cumulative = self._config['TARGET'].getboolean('cumulative')
+        with open(config_file) as f:
+            self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+            self._config_text += f.read() + '\n\n'
+
+        self._target = Target(config_file=config_file,
+                              start=self._start, end=self._end,
+                              timestep=self._timestep, cumulative=cumulative)
+
+    def add_preprocessing(self):
+        self._preproc = Preprocessing(config_file=self._config,
+                                      dict_features=self.X,
+                                      dict_target=self.y)
+
+    def learn(self):
+        config_file = eval(self._config['LEARNER']['config'])
+        with open(config_file) as f:
+            self._config_text += f"{'='*10} {os.path.basename(config_file)} {'='*10}\n\n"
+            self._config_text += f.read() + '\n\n'
+
+        history = eval(self._config['HISTORY_KNOWLEDGE']['nb_lines'])
+        self._learner = Learning(config_file=config_file, file_name=self._file_name,
+                                 X=self._preproc.dataframe, y=list(self.y.values())[history:],
+                                 horizon=self._config['TARGET'].getint('horizon'))
 
     @property
     def X(self):
 
     @property
     def X(self):
@@ -71,4 +156,12 @@ class Engine:
 
     @X.setter
     def X(self, x):
 
     @X.setter
     def X(self, x):
-        self._X = x
\ No newline at end of file
+        self._X = x
+
+    @property
+    def y(self):
+        return self._target.y
+
+    @y.setter
+    def end(self, y):
+        self._target.y = y