Spaces:
Running
Running
completed first version energy prediction pipeline
Browse files- .gitignore +0 -1
- EnergyLSTM/models/lstm_energy_north_01.keras +0 -0
- EnergyLSTM/models/scalerNorth.pkl +3 -0
- src/energy_prediction/EnergyPredictionPipeline.py +77 -9
- src/energy_prediction/models/lstm_energy_north_01.keras +0 -0
- src/energy_prediction/models/scalerNorth.pkl +3 -0
- src/test_main.py +29 -0
.gitignore
CHANGED
@@ -5,4 +5,3 @@ __pycache__/
|
|
5 |
*.tf
|
6 |
data
|
7 |
*.csv
|
8 |
-
src/test_main.py
|
|
|
5 |
*.tf
|
6 |
data
|
7 |
*.csv
|
|
EnergyLSTM/models/lstm_energy_north_01.keras
ADDED
Binary file (434 kB). View file
|
|
EnergyLSTM/models/scalerNorth.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7b6243927a7de9f60f2760a039d9b44c80a8f53662540cdb3a3ac878e671baf6
|
3 |
+
size 689
|
src/energy_prediction/EnergyPredictionPipeline.py
CHANGED
@@ -1,15 +1,83 @@
|
|
1 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
class EnergyPredictionPipeline:
|
4 |
-
|
5 |
-
|
6 |
-
self.model_south = EnergyPredictionSouth(model_path_south)
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
south_prediction = self.model_south.predict(south_data)
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
+
from pickle import load
|
3 |
+
from datetime import datetime, date
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
import joblib
|
6 |
+
import json
|
7 |
+
import numpy as np
|
8 |
|
9 |
class EnergyPredictionPipeline:
|
10 |
+
scalerNorth = None
|
11 |
+
scalerSouth = None
|
|
|
12 |
|
13 |
+
def __init__(self, scaler1_path=None,scaler2_path=None):
|
14 |
+
|
15 |
+
if scaler1_path:
|
16 |
+
self.scalerNorth = self.get_scaler(scaler1_path)
|
17 |
+
if scaler2_path:
|
18 |
+
self.scalerSouth = self.get_scaler(scaler2_path)
|
19 |
+
|
20 |
+
self.input_col_names = self.input_col_names + [
|
21 |
+
"date",
|
22 |
+
"hvac_N"
|
23 |
+
]
|
24 |
+
|
25 |
+
def get_scaler(self, scaler_path):
|
26 |
+
return joblib.load(scaler_path)
|
27 |
+
|
28 |
+
def transform_windows(self, df):
|
29 |
+
return self.scalerNorth.transform(df)
|
30 |
|
31 |
+
def date_encoder(df):
|
|
|
32 |
|
33 |
+
df['day_of_week'] = df.index.dayofweek
|
34 |
+
df['hour_of_day'] = df.index.hour
|
35 |
+
df['month'] = df.index.month
|
36 |
+
|
37 |
+
df['day_encoding'] = np.sin(2*np.pi*df['day_of_week']/7)
|
38 |
+
df['hour_encoding'] = np.sin(2*np.pi*df['hour_of_day']/24)
|
39 |
+
df['month_encoding'] = np.sin(2*np.pi*df['month']/12)
|
40 |
+
|
41 |
+
return df
|
42 |
+
|
43 |
+
def prepare_input(self, df_new):
|
44 |
+
|
45 |
+
df = df_new.copy()
|
46 |
+
df["date"] = pd.to_datetime(df["date"])
|
47 |
+
df.set_index("date", inplace=True)
|
48 |
+
df = df.resample("H").mean()
|
49 |
+
|
50 |
+
df = self.date_encoder(df)
|
51 |
+
|
52 |
+
df.reset_index(inplace=True, drop=True)
|
53 |
+
|
54 |
+
return df
|
55 |
+
|
56 |
+
def extract_data_from_message(self, message):
|
57 |
+
payload = json.loads(message.payload.decode())
|
58 |
+
|
59 |
+
len_df = len(self.df)
|
60 |
+
|
61 |
+
k = {}
|
62 |
+
for col in self.input_col_names:
|
63 |
+
k[col] = payload[col]
|
64 |
+
self.df.loc[len_df] = k
|
65 |
+
return self.df
|
66 |
+
|
67 |
+
def get_window(self, df):
|
68 |
+
len_df = len(df)
|
69 |
+
print(len_df)
|
70 |
+
if len_df > 4*7*24:
|
71 |
+
return df[len_df - 673 : len_df].astype("float32")
|
72 |
+
else:
|
73 |
+
return None
|
74 |
+
|
75 |
+
def fit(self, message):
|
76 |
+
df_new = self.extract_data_from_message(message)
|
77 |
+
df_window = self.get_window(df_new)
|
78 |
+
if df_window is not None:
|
79 |
+
df = self.prepare_input(df_window)
|
80 |
+
df = self.transform_windows(df)
|
81 |
+
else:
|
82 |
+
df = None
|
83 |
+
return df
|
src/energy_prediction/models/lstm_energy_north_01.keras
ADDED
Binary file (434 kB). View file
|
|
src/energy_prediction/models/scalerNorth.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7b6243927a7de9f60f2760a039d9b44c80a8f53662540cdb3a3ac878e671baf6
|
3 |
+
size 689
|
src/test_main.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from energy_prediction.EnergyPredictionNorth import EnergyPredictionNorth
|
2 |
+
from energy_prediction.EnergyPredictionSouth import EnergyPredictionSouth
|
3 |
+
from energy_prediction.EnergyPredictionPipeline import EnergyPredictionPipeline
|
4 |
+
|
5 |
+
def main():
|
6 |
+
# Energy Prediction North wing
|
7 |
+
EnergyPredictionNorth = EnergyPredictionNorth(
|
8 |
+
model_path="src/energy_prediction/models/lstm_energy_north_01.keras"
|
9 |
+
)
|
10 |
+
# Energy Prediction South wing
|
11 |
+
|
12 |
+
def on_message(client, userdata, message):
|
13 |
+
df = EnergyPredictionPipeline.fit(message)
|
14 |
+
|
15 |
+
if not df is None:
|
16 |
+
out_vav = EnergyPredictionNorth.pipeline(df, EnergyPredictionPipeline.scaler)
|
17 |
+
|
18 |
+
broker_address = "localhost"
|
19 |
+
broker_port = 1883
|
20 |
+
topic = "sensor_data"
|
21 |
+
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)
|
22 |
+
print("Connecting to broker")
|
23 |
+
client.on_message = on_message
|
24 |
+
client.connect(broker_address, broker_port)
|
25 |
+
client.subscribe(topic)
|
26 |
+
client.loop_forever()
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
main()
|