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

Private GIT Repository
Adding a source module to check for redundancy in feature names.
[predictops.git] / predictops / learn / preprocessing.py
index b58ffac00588fc22d7f7f3d37edcf63b791d16f4..a878a8215d83e8cd504ff7f345cbd1c15165a7e7 100644 (file)
@@ -1,6 +1,10 @@
+from configparser import ConfigParser
+from csv import DictReader
+from datetime import datetime, timedelta
 from itertools import chain
 from logging import getLogger
 from logging.config import fileConfig
 from itertools import chain
 from logging import getLogger
 from logging.config import fileConfig
+from os import listdir
 from pathlib import Path
 
 import numpy as np
 from pathlib import Path
 
 import numpy as np
@@ -10,50 +14,152 @@ fileConfig((Path.cwd() / 'config') / 'logging.cfg')
 logger = getLogger()
 
 class Preprocessing:
 logger = getLogger()
 
 class Preprocessing:
-    def __init__(self, dict_features,
-                 start, end, timestep,
-                 features = None):
+    '''
+    Generate a pandas dataframe from a dictionary of features per datetime, which
+    respects the starting and ending dates of the study, and its precision (the
+    time step) as passed to the constructor. Missing feature values are completed.
+
+     - Missing datetimes are added first with np.NaN feature values,
+     - The dataframe is then constructed based on the filled feature dictionary,
+     - NaN values are then filled with last known values.
+    '''
+
+    def __init__(self, config_file = None, dict_features = None, features = None):
+        '''
+        Constructor that defines all needed attributes and collects features.
+        '''
+        self._config = ConfigParser()
+        self._config.read(config_file)
+
+        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')
+        self._timestep = timedelta(hours =
+                                   self._config['DATETIME'].getfloat('hourStep'))
         self._dict_features = dict_features
         self._dict_features = dict_features
-        self._start = start
-        self._end = end
-        self._timestep = timestep
+        self._full_dict = None
         self._dataframe = None
         self._dataframe = None
-
+        self._datetimes = []
+        # If features are not provided to the constructor, then we collect
+        # any existing feature in the dictionary
         if features != None:
             self._features = features
         else:
             self._features = set(chain.from_iterable([tuple(u.keys())
                                                       for u in [*dict_features.values()]]))
         if features != None:
             self._features = features
         else:
             self._features = set(chain.from_iterable([tuple(u.keys())
                                                       for u in [*dict_features.values()]]))
+        for csv_file in listdir():
+            with open(csv_file, "r") as f:
+                reader = DictReader(f, delimiter=',')
+                dico_features = {{row['name']: row['type']  # qualitative (2) or quantitative (1)
+                                    }
+                                for row in reader if row['name'] in self._features}
+
+        self._features = {feat : None for feat in self._features}
+        print(self._features)
+        exit()
+
+
+    @property
+    def start(self):
+        return self._start
+
+    @start.setter
+    def start(self, x):
+        self._start = x
+
+
+    @property
+    def end(self):
+        return self._end
+
+    @end.setter
+    def end(self, x):
+        self._end = x
+
+
+    @property
+    def timestep(self):
+        return self._timestep
+
+    @timestep.setter
+    def timestep(self, x):
+        self._timestep = x
 
 
     def _fill_dict(self):
 
 
     def _fill_dict(self):
+        '''
+        Add datetime keys in the dated feature dictionary that are missing. The
+        features are then set to np.NaN. Add missing features in existing datetimes
+        too.
+        '''
+        logger.info("Adding missing dates and filling missing features with NaN values")
         current = self._start
         while current <= self._end:
         current = self._start
         while current <= self._end:
+            self._datetimes.append(current)
             if current not in self._dict_features:
             if current not in self._dict_features:
-                self._dict_features[current] = {feature:np.NaN for feature in self._features}
+                self._dict_features[current] = {feature:np.NaN
+                                                for feature in self._features}
             else:
             else:
-                null_dict = {feature:np.NaN for feature in self._features}
+                null_dict = {feature:np.NaN
+                             for feature in self._features}
                 null_dict.update(self._dict_features[current])
                 self._dict_features[current] = null_dict
             current += self._timestep
                 null_dict.update(self._dict_features[current])
                 self._dict_features[current] = null_dict
             current += self._timestep
+        for k in self._dict_features:
+            null_dict = {feature:np.NaN
+                         for feature in self._features}
+            null_dict.update(self._dict_features[k])
+            self._dict_features[k] = null_dict
+
+        self._full_dict = {k: self._dict_features[k]
+                           for k in sorted(self._dict_features.keys())}
+
 
 
     @property
     def full_dict(self):
 
 
     @property
     def full_dict(self):
-        self._fill_dict()
-        return {k: self._dict_features[k] for k in sorted(self._dict_features.keys())}
+        '''
+        Returns the fully filled dated feature dictionary, ordered by datetimes
+        '''
+        if self._full_dict is None:
+            self._fill_dict()
+        return self._full_dict
+
 
 
     @property
     def dataframe(self):
 
 
     @property
     def dataframe(self):
+        '''
+        Returns the feature dataframe, after creating it if needed.
+        '''
         if self._dataframe is None:
         if self._dataframe is None:
-            self._dataframe = pd.DataFrame.from_dict(self.full_dict, orient='index')
+            logger.info("Creating feature dataframe from feature dictionary")
+            self._dataframe = pd.DataFrame.from_dict(self.full_dict,
+                                                     orient='index')
+            logger.info("Filling NaN values in the feature dataframe")
+
+            if self._config['PREPROCESSING']['fill_method'] == 'propagate':
+                self._dataframe = self._dataframe.fillna(method='ffill')
+            elif self._config['PREPROCESSING']['fill_method'] == 'linear':
+                self._dataframe = self._dataframe.interpolate()
+            elif self._config['PREPROCESSING']['fill_method'] == 'spline':
+                self._dataframe = self._dataframe.interpolate(method='spline',
+                                                              order=self._config['PREPROCESSING'].getint('order'))
+
+            # Uncomment this line to fill NaN values at the beginning of the
+            # dataframe. This may not be a good idea, especially for features
+            # that are available only for recent years, e.g., air quality
+            #self._dataframe = self._dataframe.fillna(method='bfill')
+
+            self._dataframe = self._dataframe.drop([k.to_pydatetime()
+                                                   for k in self._dataframe.T
+                                                   if k not in self._datetimes])
         return self._dataframe
 
         return self._dataframe
 
+
     @dataframe.setter
     def dataframe(self, df):
         self._dataframe = df
 
 
     @dataframe.setter
     def dataframe(self, df):
         self._dataframe = df
 
 
-    def fill_na(self):
-        self.dataframe = self.dataframe.fillna(method='ffill')
\ No newline at end of file