refactor(notebooks): clean up imports, adjust damage case processing, and improve model training structure

- Removed unnecessary imports (os, pandas, numpy) from the STFT notebook.
- Adjusted the number of damage cases in the multiprocessing pool to correctly reflect the range.
- Updated model training code for Sensor B to ensure consistent naming and structure.
- Cleaned up commented-out code for clarity and maintainability.
This commit is contained in:
nuluh
2025-08-17 23:39:57 +07:00
parent 4a1c0ed83e
commit 855114d633

View File

@@ -217,9 +217,6 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import os\n",
"import pandas as pd\n",
"import numpy as np\n",
"from scipy.signal import hann\n", "from scipy.signal import hann\n",
"import multiprocessing" "import multiprocessing"
] ]
@@ -244,16 +241,6 @@
"Fs = 1024" "Fs = 1024"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define the base directory where DAMAGE_X folders are located\n",
"damage_base_path = 'D:/thesis/data/converted/raw'"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -304,11 +291,11 @@
"# Import custom in-house functions\n", "# Import custom in-house functions\n",
"from src.process_stft import process_damage_case\n", "from src.process_stft import process_damage_case\n",
"\n", "\n",
"num_damage_cases = 7 # DAMAGE_0 to DAMAGE_6\n", "num_damage_cases = 6 # DAMAGE_0 to DAMAGE_6\n",
"\n", "\n",
"with multiprocessing.Pool() as pool:\n", "with multiprocessing.Pool() as pool:\n",
" # Process each DAMAGE_X case in parallel\n", " # Process each DAMAGE_X case in parallel\n",
" pool.map(process_damage_case, range(num_damage_cases), Fs, window_size, hop_size, output_dirs)" " pool.map(process_damage_case, range(num_damage_cases + 1))"
] ]
}, },
{ {
@@ -665,24 +652,33 @@
" # \"Decision Tree\": DecisionTreeClassifier(),\n", " # \"Decision Tree\": DecisionTreeClassifier(),\n",
" # \"KNN\": KNeighborsClassifier(),\n", " # \"KNN\": KNeighborsClassifier(),\n",
" # \"LDA\": LinearDiscriminantAnalysis(),\n", " # \"LDA\": LinearDiscriminantAnalysis(),\n",
" # \"SVM\": make_pipeline(\n", " \"SVM\": make_pipeline(\n",
" # StandardScaler(),\n", " StandardScaler(),\n",
" # SVC(kernel='rbf', probability=True)\n", " SVC(kernel='rbf')\n",
" # ),\n", " ),\n",
" # \"SVM with StandardScaler and PCA\": make_pipeline(\n", " \"SVM with StandardScaler and PCA\": make_pipeline(\n",
" # StandardScaler(),\n", " StandardScaler(),\n",
" # PCA(n_components=10),\n", " PCA(n_components=10),\n",
" # SVC(kernel='rbf')\n", " SVC(kernel='rbf')\n",
" # ),\n", " ),\n",
"\n", "\n",
" # \"XGBoost\": XGBClassifier()\n", " # \"XGBoost\": XGBClassifier()\n",
" \"MLPClassifier\": make_pipeline(\n", " # \"MLPClassifier\": make_pipeline(\n",
" StandardScaler(),\n", " # StandardScaler(),\n",
" MLPClassifier(hidden_layer_sizes=(1, 10), max_iter=500, random_state=42)\n", " # MLPClassifier(hidden_layer_sizes=(1, 10), max_iter=500, random_state=42)\n",
" )\n", " # )\n",
"}" "}"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_train1"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -712,9 +708,15 @@
" # \"Decision Tree\": DecisionTreeClassifier(),\n", " # \"Decision Tree\": DecisionTreeClassifier(),\n",
" # \"KNN\": KNeighborsClassifier(),\n", " # \"KNN\": KNeighborsClassifier(),\n",
" # \"LDA\": LinearDiscriminantAnalysis(),\n", " # \"LDA\": LinearDiscriminantAnalysis(),\n",
" \"SVM\": SVC(),\n", " \"SVM\": make_pipeline(\n",
" # \"SVM with StandardScaler and PCA\": make_pipeline(\n", " StandardScaler(),\n",
" # StandardScaler(),\n", " SVC(kernel='rbf')\n",
" ),\n",
" \"SVM with StandardScaler and PCA\": make_pipeline(\n",
" StandardScaler(),\n",
" PCA(n_components=10),\n",
" SVC(kernel='rbf')\n",
" ),\n",
" # PCA(n_components=10),\n", " # PCA(n_components=10),\n",
" # SVC(kernel='rbf')\n", " # SVC(kernel='rbf')\n",
" # ),\n", " # ),\n",
@@ -730,8 +732,8 @@
"source": [ "source": [
"results_sensor2 = []\n", "results_sensor2 = []\n",
"for name, model in models_sensor2.items():\n", "for name, model in models_sensor2.items():\n",
" res = train_and_evaluate_model(model, name, \"sensor2\", x_train2, y_train2, x_test2, y_test2, \n", " res = train_and_evaluate_model(model, name, \"Sensor B\", x_train2, y_train2, x_test2, y_test2, \n",
" export='D:/thesis/models/sensor2')\n", " export='D:/thesis/models/Sensor B')\n",
" results_sensor2.append(res)\n", " results_sensor2.append(res)\n",
" print(f\"{name} on sensor2: Accuracy = {res['accuracy']:.2f}%\")\n", " print(f\"{name} on sensor2: Accuracy = {res['accuracy']:.2f}%\")\n",
"\n", "\n",