{ "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 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 }