File size: 11,723 Bytes
2bc71f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Import necessary libraries and modules.\n",
    "\"\"\"\n",
    "import pandas as pd \n",
    "from datetime import date\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from keras.models import Sequential\n",
    "from keras.layers import LSTM, Dense\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import MinMaxScaler,StandardScaler\n",
    "from keras.callbacks import ModelCheckpoint\n",
    "from sklearn.cluster import KMeans\n",
    "from sklearn.decomposition import PCA\n",
    "from tensorflow.keras.models import load_model\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Load the dataset\n",
    "\"\"\"\n",
    "\n",
    "merged = pd.read_csv('long_merge.csv')\n",
    "\n",
    "zone = \"47\"\n",
    "\n",
    "if zone in [\"36\", \"37\", \"38\", \"39\", \"40\", \"41\", \"42\", \"64\", \"65\", \"66\", \"67\", \"68\", \"69\", \"70\"]:\n",
    "    rtu = \"rtu_001\"\n",
    "    wing = \"hvac_N\"\n",
    "elif zone in [\"18\", \"25\", \"26\", \"45\", \"48\", \"55\", \"56\", \"61\"]:\n",
    "    rtu = \"rtu_003\"\n",
    "    wing = \"hvac_S\"\n",
    "elif zone in [\"16\", \"17\", \"21\", \"22\", \"23\", \"24\", \"46\", \"47\", \"51\", \"52\", \"53\", \"54\"]:\n",
    "    rtu = \"rtu_004\"\n",
    "    wing = \"hvac_S\"\n",
    "else:\n",
    "    rtu = \"rtu_002\"\n",
    "    wing = \"hvac_N\"\n",
    "sorted = merged[[\"date\"]+[col for col in merged.columns if zone in col or rtu in col or wing in col]+[\"hp_hws_temp\", \"aru_001_cwr_temp\" , \"aru_001_cws_fr_gpm\" ,\"aru_001_cws_temp\",\"aru_001_hwr_temp\" ,\"aru_001_hws_fr_gpm\" ,\"aru_001_hws_temp\"]]\n",
    "sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Load the dataset\n",
    "\"\"\"\n",
    "\n",
    "rtu = [\"rtu_003\",\"rtu_004\",\"rtu_001\",\"rtu_002\"]\n",
    "env = [\"air_temp_set_1\",\"air_temp_set_2\",\"dew_point_temperature_set_1d\",\"relative_humidity_set_1\",\"solar_radiation_set_1\"]\n",
    "energy_data = merged[[\"date\",\"hp_hws_temp\"]+[col for col in merged.columns if \n",
    "                               any(sub in col for sub in rtu)]+env]\n",
    "df_filtered = energy_data[[col for col in energy_data.columns if 'Unnamed' not in col]]\n",
    "df_filtered = df_filtered[[col for col in df_filtered.columns if 'co2' not in col]]\n",
    "df_filtered = df_filtered[[col for col in df_filtered.columns if 'templogger' not in col]]\n",
    "df_filtered['date'] = pd.to_datetime(df_filtered['date'], format = \"%Y-%m-%d %H:%M:%S\")\n",
    "df_filtered = df_filtered[ (df_filtered.date.dt.date >date(2018, 5, 1)) & (df_filtered.date.dt.date< date(2020, 5, 1))] #(2018, 5, 1)\n",
    "if df_filtered.isna().any().any():\n",
    "    print(\"There are NA values\",df_filtered.isna().sum().tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Extract the relevant parameters\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "df_filtered = df_filtered.loc[:,['date','hp_hws_temp',\n",
    " 'rtu_003_sa_temp',\n",
    " 'rtu_003_oadmpr_pct',\n",
    " 'rtu_003_ra_temp',\n",
    " 'rtu_003_oa_temp',\n",
    " 'rtu_003_ma_temp',\n",
    " 'rtu_003_sf_vfd_spd_fbk_tn',\n",
    " 'rtu_003_rf_vfd_spd_fbk_tn',\n",
    " 'rtu_004_sa_temp',\n",
    " 'rtu_004_oadmpr_pct',\n",
    " 'rtu_004_ra_temp',\n",
    " 'rtu_004_oa_temp',\n",
    " 'rtu_004_ma_temp',\n",
    " 'rtu_004_sf_vfd_spd_fbk_tn',\n",
    " 'rtu_004_rf_vfd_spd_fbk_tn',\n",
    " 'rtu_003_sat_sp_tn',\n",
    " 'rtu_004_sat_sp_tn',\n",
    " \n",
    "#  'rtu_001_sa_temp',\n",
    "#  'rtu_001_oadmpr_pct',\n",
    "#  'rtu_001_ra_temp',\n",
    "#  'rtu_001_oa_temp',\n",
    "#  'rtu_001_ma_temp',\n",
    "#  'rtu_001_sf_vfd_spd_fbk_tn',\n",
    "#  'rtu_001_rf_vfd_spd_fbk_tn',\n",
    "#  'rtu_002_sa_temp',\n",
    "#  'rtu_002_oadmpr_pct',\n",
    "#  'rtu_002_ra_temp',\n",
    "#  'rtu_002_oa_temp',\n",
    "#  'rtu_002_ma_temp',\n",
    "#  'rtu_002_sf_vfd_spd_fbk_tn',\n",
    "#  'rtu_002_rf_vfd_spd_fbk_tn',\n",
    "#  'rtu_001_sat_sp_tn',\n",
    "#  'rtu_002_sat_sp_tn',\n",
    "\n",
    " 'air_temp_set_1',\n",
    " 'air_temp_set_2',\n",
    " 'dew_point_temperature_set_1d',\n",
    " 'relative_humidity_set_1',\n",
    " 'solar_radiation_set_1']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "splitting into training and testing sets.\n",
    "\"\"\"\n",
    "\n",
    "df_filtered = df_filtered.dropna()\n",
    "\n",
    "testdataset_df = df_filtered[(df_filtered.date.dt.date >date(2019, 7, 21))]\n",
    "\n",
    "traindataset_df = df_filtered[(df_filtered.date.dt.date <date(2019, 7, 21))]\n",
    "\n",
    "testdataset = testdataset_df.drop(columns=[\"date\"]).rolling(window=30,min_periods=1).mean().values\n",
    "\n",
    "traindataset = traindataset_df.drop(columns=[\"date\"]).rolling(window=30,min_periods=1).mean().values\n",
    "\n",
    "columns_with_na_train = traindataset_df.columns[traindataset_df.isna().any()].tolist()\n",
    "columns_with_na_test = testdataset_df.columns[testdataset_df.isna().any()].tolist()\n",
    "print(columns_with_na_train)\n",
    "print(columns_with_na_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\"\"\"\n",
    "Preprocess the data by scaling\n",
    "\"\"\"\n",
    "\n",
    "traindataset = traindataset.astype('float32')\n",
    "testdataset = testdataset.astype('float32')\n",
    "\n",
    "scaler = StandardScaler()\n",
    "traindataset = scaler.fit_transform(traindataset)\n",
    "testdataset = scaler.transform(testdataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Training the model\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "train,test = traindataset,testdataset\n",
    "\n",
    "def create_dataset(dataset,time_step):\n",
    "    x = [[] for _ in range(22)] \n",
    "    Y = []\n",
    "    for i in range(len(dataset) - time_step - 1):\n",
    "        for j in range(22):\n",
    "            x[j].append(dataset[i:(i + time_step), j])\n",
    "        Y.append([dataset[i + time_step, 0],dataset[i + time_step, 1],dataset[i + time_step, 2],dataset[i + time_step, 3],\n",
    "                  dataset[i + time_step, 4],dataset[i + time_step, 5],\n",
    "                  dataset[i + time_step, 6],dataset[i + time_step, 7],\n",
    "                  dataset[i + time_step, 8],dataset[i + time_step, 9],dataset[i + time_step, 10],\n",
    "                  dataset[i + time_step, 11],dataset[i + time_step, 12],\n",
    "                  dataset[i + time_step, 13],dataset[i + time_step, 14]])\n",
    "    x= [np.array(feature_list) for feature_list in x]\n",
    "    Y = np.reshape(Y,(len(Y),15))\n",
    "    return np.stack(x,axis=2),Y\n",
    "\n",
    "time_step = 30\n",
    "X_train, y_train = create_dataset(train, time_step)\n",
    "X_test, y_test = create_dataset(test, time_step)\n",
    "\n",
    "\n",
    "model = Sequential()\n",
    "model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))\n",
    "model.add(LSTM(units=50, return_sequences=True))\n",
    "model.add(LSTM(units=30))\n",
    "model.add(Dense(units=15))\n",
    "\n",
    "model.compile(optimizer='adam', loss='mean_squared_error')\n",
    "\n",
    "checkpoint_path = \"lstm_2rtu_smooth_04.keras\" #           \"lstm_2rtu_smooth_03.keras\"--> 3,4 rtu\n",
    "checkpoint_callback = ModelCheckpoint(filepath=checkpoint_path, monitor='val_loss', verbose=1, save_best_only=True, mode='min')\n",
    "model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5, batch_size=64, verbose=1, callbacks=[checkpoint_callback])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "load the model for prediction\n",
    "\"\"\"\n",
    "\n",
    "checkpoint_path = \"lstm_2rtu_smooth_03.keras\"\n",
    "model = load_model(checkpoint_path)\n",
    "test_predict1 = model.predict(X_test)\n",
    "train_predict1 = model.predict(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib qt\n",
    "var = 6\n",
    "plt.plot(testdataset_df['date'][31:], y_test[:,var], label='Original Testing Data')\n",
    "plt.plot(testdataset_df['date'][31:] ,test_predict1[:,var], label='Predicted Data')\n",
    "\n",
    "# anomalies = np.where(abs(test_predict1[:,var] - y_test[:,var]) > 0.38)\n",
    "# plt.scatter(anomalies,test_predict1[anomalies,var], color='black',marker =\"o\",s=100 )\n",
    "\n",
    "\n",
    "plt.title('Testing Data - Predicted vs Actual')\n",
    "plt.xlabel('Time')\n",
    "plt.ylabel('Value')\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    Perform clustering and dimensionality reduction using KMeans and PCA\n",
    "\"\"\"\n",
    "\n",
    "np.random.seed(0)\n",
    "X1 = train_predict1[:,1:8] - y_train[:,1:8]\n",
    "X2 = train_predict1[:,8:15] - y_train[:,8:15]\n",
    "\n",
    "\n",
    "k = 1\n",
    "\n",
    "pca1 = PCA(n_components=2)\n",
    "X1 = pca1.fit_transform(X1)\n",
    "pca2 = PCA(n_components=2)\n",
    "X2 = pca2.fit_transform(X2)\n",
    "\n",
    "kmeans1 = KMeans(n_clusters=k, random_state=10) \n",
    "kmeans1.fit(X1)\n",
    "kmeans2 = KMeans(n_clusters=k, random_state=10) \n",
    "kmeans2.fit(X2)\n",
    "\n",
    "\n",
    "labels = kmeans1.labels_\n",
    "plt.scatter(X1[:, 0], X1[:, 1],c=labels, cmap='rainbow')\n",
    "plt.scatter(kmeans1.cluster_centers_[:, 0], kmeans1.cluster_centers_[:, 1], marker='x', c='red', s=200, linewidths=2)\n",
    "plt.title('KMeans Clustering')\n",
    "plt.xlabel('Feature 1')\n",
    "plt.ylabel('Feature 2')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    Plot the faults after windowing\n",
    "\"\"\"\n",
    "\n",
    "%matplotlib qt\n",
    "\n",
    "distance1 = np.linalg.norm((pca1.transform(test_predict1[:,1:8]-y_test[:,1:8]))-kmeans1.cluster_centers_[0], ord=2, axis = 1)\n",
    "distance2 = np.linalg.norm((pca2.transform(test_predict1[:,8:15]-y_test[:,8:15]))-kmeans2.cluster_centers_[0], ord=2, axis = 1)\n",
    "\n",
    "plt.plot(testdataset_df['date'][31:] ,y_test[:,7],label='Return air fan speed')\n",
    "plt.plot(testdataset_df['date'][31:] ,abs(distance1)>1,linewidth=2.5,label='Faults')\n",
    "plt.plot(testdataset_df['date'][31:] ,pd.Series((distance1)>3.5).rolling(window=60,min_periods=1).mean()==1,linewidth=2.5,label='Faults')\n",
    "\n",
    "plt.title('RTU-2 Faults')\n",
    "plt.xlabel('Time')\n",
    "plt.ylabel('Value')\n",
    "plt.legend()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "smartbuilding",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}