feat(notebooks): Add type hints for data lists, create preview function for STFT visualization, and save plot for Sensor B. Introduce test section with AU data processing.

This commit is contained in:
nuluh
2025-08-28 07:10:41 +07:00
parent 7a7b2a41af
commit b2bf1b0e31

View File

@@ -342,7 +342,7 @@
"import os\n", "import os\n",
"import pandas as pd\n", "import pandas as pd\n",
"\n", "\n",
"ready_data1a = []\n", "ready_data1a: list[pd.DataFrame] = []\n",
"for file in os.listdir('D:/thesis/data/converted/raw/sensor1'):\n", "for file in os.listdir('D:/thesis/data/converted/raw/sensor1'):\n",
" ready_data1a.append(pd.read_csv(os.path.join('D:/thesis/data/converted/raw/sensor1', file), skiprows=1))" " ready_data1a.append(pd.read_csv(os.path.join('D:/thesis/data/converted/raw/sensor1', file), skiprows=1))"
] ]
@@ -354,7 +354,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Load the processed data for Sensor 2\n", "# Load the processed data for Sensor 2\n",
"ready_data2a = []\n", "ready_data2a: list[pd.DataFrame] = []\n",
"for file in os.listdir('D:/thesis/data/converted/raw/sensor2'):\n", "for file in os.listdir('D:/thesis/data/converted/raw/sensor2'):\n",
" ready_data2a.append(pd.read_csv(os.path.join('D:/thesis/data/converted/raw/sensor2', file), skiprows=1))" " ready_data2a.append(pd.read_csv(os.path.join('D:/thesis/data/converted/raw/sensor2', file), skiprows=1))"
] ]
@@ -414,6 +414,33 @@
"import matplotlib.pyplot as plt" "import matplotlib.pyplot as plt"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def preview_stft(data: pd.DataFrame, x_num_ticks: int = 6, y_num_ticks: int = 5):\n",
" \"\"\"\n",
" Preview the Short-Time Fourier Transform (STFT) of the given data.\n",
"\n",
" Parameters:\n",
" -------\n",
" data (pd.DataFrame): The STFT data to be visualized.\n",
" x_num_ticks (int): Number of ticks on the x-axis (time frames). Defaults to 6.\n",
" y_num_ticks (int): Number of ticks on the y-axis (frequency bins). Defaults to 5.\n",
" \"\"\"\n",
" plt.figure(dpi=300) # Set figure size and DPI\n",
" plt.pcolormesh(data.transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n",
" # plt.title('STFT Preview')\n",
" plt.colorbar(label='Magnitude')\n",
" plt.xlabel('Segmen Waktu')\n",
" plt.ylabel('Sampel Frekuensi (Hz)')\n",
" plt.xticks(np.linspace(0, len(data)-1, x_num_ticks)) # Set x-ticks at regular intervals\n",
" plt.yticks(np.linspace(0, len(data.columns)-1, y_num_ticks)) # Set y-ticks at regular intervals\n",
" plt.show()"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -422,10 +449,7 @@
"source": [ "source": [
"# colormesh give title x is frequency and y is time and rotate/transpose the data\n", "# colormesh give title x is frequency and y is time and rotate/transpose the data\n",
"# Plotting the STFT Data\n", "# Plotting the STFT Data\n",
"plt.figure(dpi=300) # Set figure size and DPI\n", "preview_stft(ready_data1a[0]) # Preview for Sensor 1"
"plt.pcolormesh(ready_data1a[0].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n",
"plt.title('STFT of Sensor A Dataset A Label 0 Undamaged')\n",
"plt.show()"
] ]
}, },
{ {
@@ -437,6 +461,7 @@
"plt.figure(dpi=300) # Set figure size and DPI\n", "plt.figure(dpi=300) # Set figure size and DPI\n",
"plt.pcolormesh(ready_data2a[0].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n", "plt.pcolormesh(ready_data2a[0].transpose(), cmap='jet', vmax=0.03, vmin=0.0)\n",
"plt.title('STFT of Sensor B Dataset A Label 0 Undamaged')\n", "plt.title('STFT of Sensor B Dataset A Label 0 Undamaged')\n",
"plt.savefig(\"stft-sensor-b-dataset-a-undamaged.png\", dpi=300)\n",
"plt.show()" "plt.show()"
] ]
}, },
@@ -1005,6 +1030,51 @@
"print(\"Accuracy on Dataset B:\", accuracy_score(y, y_pred_svm))\n", "print(\"Accuracy on Dataset B:\", accuracy_score(y, y_pred_svm))\n",
"print(classification_report(y, y_pred_svm))" "print(classification_report(y, y_pred_svm))"
] ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Test with AU"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"file_path = 'D:/thesis/data/dataset_B/zzzBU.TXT'\n",
"df = pd.read_csv(file_path, delim_whitespace=True, skiprows=10, header=0, memory_map=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df1= df.iloc[:, [1]]\n",
"df2 = df.iloc[:, [26]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from scipy.signal.windows import hann\n",
"from scipy.signal import stft\n",
"\n",
"window = 1024\n",
"hop = 512\n",
"\n",
"stft1 = stft(df1.values.flatten(), window=hann(window), nperseg=window, noverlap=hop, fs=window)\n",
"stft2 = stft(df2.values.flatten(), window=hann(window), nperseg=window, noverlap=hop, fs=window)\n"
]
} }
], ],
"metadata": { "metadata": {