2024-11-29 16:19:05 +01:00

170 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"id": "bb315604-cc62-46b7-8258-61a70f174386",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Load the dataset\n",
"dataset = pd.read_csv(\"./dataset.csv\")\n",
"\n",
"# Group by 'test', 'frame', 'label', and 'type'\n",
"grouped_data = dataset.groupby(['test', 'frame', 'label', 'type'])\n",
"\n",
"# Lists to store data based on type and label\n",
"fetus_baseline = []\n",
"fetus_opcl = []\n",
"fetus_yawn = []\n",
"mother_baseline = []\n",
"mother_opcl = []\n",
"mother_yawn = []\n",
"\n",
"# Process grouped data\n",
"for name, group in grouped_data:\n",
" group = group.drop(group.columns[0], axis=1) # Drop the first column\n",
" group = group.drop([\"leftLip_x\", \"leftLip_y\", \"rightLip_x\", \"rightLip_y\", \n",
" \"topMidInner_x\", \"topMidInner_y\", \"bottomMidInner_x\", \n",
" \"bottomMidInner_y\", \"nose_x\", \"nose_y\"], axis=1)\n",
" \n",
" # Get values from 'top_bottom_distance'\n",
" top_bottom_distance_values = group['top_bottom_distance'].values\n",
" \n",
" # Assign to appropriate lists based on label and type\n",
" label = name[2] # 'label' from groupby\n",
" type_ = name[3] # 'type' from groupby\n",
" \n",
" if type_ == 'fetus':\n",
" if label == 'baseline':\n",
" fetus_baseline.append(top_bottom_distance_values)\n",
" elif label == 'opcl':\n",
" fetus_opcl.append(top_bottom_distance_values)\n",
" elif label == 'yawn':\n",
" fetus_yawn.append(top_bottom_distance_values)\n",
" elif type_ == 'mother':\n",
" if label == 'baseline':\n",
" mother_baseline.append(top_bottom_distance_values)\n",
" elif label == 'opcl':\n",
" mother_opcl.append(top_bottom_distance_values)\n",
" elif label == 'yawn':\n",
" mother_yawn.append(top_bottom_distance_values)\n",
"\n",
"# Function to pad, sample, and smooth each array\n",
"def process_series(series_list, window_size=3):\n",
" max_length = max(len(series) for series in series_list)\n",
" padded_series = np.array([np.pad(series, (0, max_length - len(series)), mode='constant') for series in series_list])\n",
"\n",
" # Sampled series\n",
" sampled_series = []\n",
" sample_n = 1\n",
" for i in range(0, max_length, sample_n):\n",
" segment_values = padded_series[:, i:i + sample_n][padded_series[:, i:i + sample_n] != 0]\n",
" if len(segment_values) > 0:\n",
" sampled_point = np.random.choice(segment_values.flatten(), size=1)\n",
" sampled_series.append(sampled_point[0])\n",
" \n",
" sampled_series = np.array(sampled_series)\n",
"\n",
" # Apply moving average for smoothing\n",
" smooth_sampled_series = np.convolve(sampled_series, np.ones(window_size) / window_size, mode='valid')\n",
" \n",
" return smooth_sampled_series\n",
"\n",
"# Process all series\n",
"smooth_fetus_baseline = process_series(fetus_baseline)\n",
"smooth_fetus_opcl = process_series(fetus_opcl)\n",
"smooth_fetus_yawn = process_series(fetus_yawn)\n",
"smooth_mother_baseline = process_series(mother_baseline)\n",
"smooth_mother_opcl = process_series(mother_opcl)\n",
"smooth_mother_yawn = process_series(mother_yawn)\n",
"\n",
"# Function to plot and save images\n",
"def plot_and_save(series_dict, title, filename):\n",
" plt.figure(figsize=(19.20, 10.80))\n",
" for label, series in series_dict.items():\n",
" plt.plot(series, marker='x', label=label, linestyle='--')\n",
" \n",
" plt.title(title)\n",
" plt.xlabel('Time Points')\n",
" plt.ylabel('Values')\n",
" plt.legend()\n",
" plt.grid()\n",
" \n",
" # Save the plot\n",
" plt.savefig(filename)\n",
" plt.close() # Close the figure to save memory\n",
"\n",
"# Create plots for fetus and mother\n",
"fetus_series_dict = {\n",
" 'Smoothed Fetus Baseline': smooth_fetus_baseline,\n",
" 'Smoothed Fetus OPCL': smooth_fetus_opcl,\n",
" 'Smoothed Fetus Yawn': smooth_fetus_yawn,\n",
"}\n",
"\n",
"mother_series_dict = {\n",
" 'Smoothed Mother Baseline': smooth_mother_baseline,\n",
" 'Smoothed Mother OPCL': smooth_mother_opcl,\n",
" 'Smoothed Mother Yawn': smooth_mother_yawn,\n",
"}\n",
"\n",
"# Plot and save images\n",
"plot_and_save(fetus_series_dict, 'Smoothed Series for Fetus', 'fetus_series.png')\n",
"plot_and_save(mother_series_dict, 'Smoothed Series for Mother', 'mother_series.png')\n",
"\n",
"# Combine the plots into a single figure\n",
"plt.figure(figsize=(19.20, 10.80))\n",
"\n",
"# Plot all series together\n",
"for label, series in fetus_series_dict.items():\n",
" plt.plot(series, marker='x', label=label, linestyle='--')\n",
"for label, series in mother_series_dict.items():\n",
" plt.plot(series, marker='o', label=label, linestyle='--')\n",
"\n",
"plt.title('Combined Smoothed Series for Fetus and Mother')\n",
"plt.xlabel('Time Points')\n",
"plt.ylabel('Values')\n",
"plt.legend()\n",
"plt.grid()\n",
"\n",
"# Save the combined plot\n",
"plt.savefig('combined_fetus_mother_series.png')\n",
"plt.close()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abd865fb-17e1-4608-a98a-32f65c5505bf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}