File size: 2,309 Bytes
9627b36
6a62ce9
 
 
 
 
 
9627b36
 
6a62ce9
 
9627b36
6a62ce9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9627b36
6a62ce9
9627b36
6a62ce9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pandas as pd
from pickle import load
from datetime import datetime, date
from sklearn.preprocessing import StandardScaler
import joblib
import json
import numpy as np

class EnergyPredictionPipeline:
    scalerNorth = None 
    scalerSouth = None 

    def __init__(self, scaler1_path=None,scaler2_path=None):
        
        if scaler1_path:
            self.scalerNorth = self.get_scaler(scaler1_path)
        if scaler2_path:
            self.scalerSouth = self.get_scaler(scaler2_path)
        
        self.input_col_names = self.input_col_names + [
            "date",
            "hvac_N"
            ]
     
    def get_scaler(self, scaler_path):
        return joblib.load(scaler_path)
    
    def transform_windows(self, df):
        return self.scalerNorth.transform(df)

    def date_encoder(df):

        df['day_of_week']  = df.index.dayofweek
        df['hour_of_day']  = df.index.hour
        df['month']        = df.index.month

        df['day_encoding'] = np.sin(2*np.pi*df['day_of_week']/7)
        df['hour_encoding'] = np.sin(2*np.pi*df['hour_of_day']/24)
        df['month_encoding'] = np.sin(2*np.pi*df['month']/12)

        return df
    
    def prepare_input(self, df_new):
        
        df = df_new.copy()
        df["date"] = pd.to_datetime(df["date"])
        df.set_index("date", inplace=True)
        df = df.resample("H").mean()
        
        df = self.date_encoder(df)

        df.reset_index(inplace=True, drop=True)

        return df
    
    def extract_data_from_message(self, message):
        payload = json.loads(message.payload.decode())

        len_df = len(self.df)

        k = {}
        for col in self.input_col_names:
            k[col] = payload[col]
        self.df.loc[len_df] = k
        return self.df
      
    def get_window(self, df):
        len_df = len(df)
        print(len_df)
        if len_df > 4*7*24:
            return df[len_df - 673 : len_df].astype("float32")
        else:
            return None
        
    def fit(self, message):
        df_new      = self.extract_data_from_message(message)
        df_window   = self.get_window(df_new)
        if df_window is not None:
            df = self.prepare_input(df_window)
            df = self.transform_windows(df)
        else:
            df = None
        return df