{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example: Sleep Stage Classifier Evaluation using Accel & Heart Rate\n", "\n", "This notebook demonstrates the evaluation of a classifier for detecting sleep stages: Wakefulness, REM (Rapid Eye Movement), and NREM (Non-Rapid Eye Movement).\n", "- We'll generate synthetic data based on accelerometer and heart rate features.\n", "- We'll then create a simple classifier, and evaluate its performance using confusion matrix, precision, recall, and F1 score.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "from scipy.signal import welch\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.metrics import confusion_matrix, precision_recall_fscore_support, accuracy_score, balanced_accuracy_score\n", "\n", "# Set random seed for reproducibility\n", "np.random.seed(42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Synthetic Data Generation\n", "\n", "We'll create synthetic data to simulate accelerometer, heart rate, and heart rate variability (HRV) measurements for different sleep stages. This function generates data with the following characteristics:\n", "\n", "- Wakefulness: Higher accelerometer activity, higher heart rate, lower HRV\n", "- REM: Low accelerometer activity, slightly elevated heart rate, moderate HRV\n", "- NREM: Very low accelerometer activity, lower heart rate, higher HRV\n", "\n", "We then extract a bunch of features from the accelerometer and heart rate signals (e.g. min, max, mean, etc); we also extract some specialized features from heart rate corresponding to heart rate variability (e.g. rmssd, sdnn); and obtain the dominant frequency using the `welch` function from SciPy." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | accel_mean | \n", "accel_std | \n", "accel_max | \n", "accel_min | \n", "accel_median | \n", "accel_dominant_freq | \n", "hr_mean | \n", "hr_std | \n", "hr_max | \n", "hr_min | \n", "hrv_rmssd | \n", "hrv_sdnn | \n", "hrv_pnn50 | \n", "Stage | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "3.160171 | \n", "1.369994 | \n", "5.736376 | \n", "0.770211 | \n", "3.218683 | \n", "33.333333 | \n", "71.754275 | \n", "12.820519 | \n", "103.847061 | \n", "54.711142 | \n", "18.543974 | \n", "12.820519 | \n", "0.000000 | \n", "Wakefulness | \n", "
1 | \n", "2.757489 | \n", "1.187661 | \n", "5.774216 | \n", "0.810158 | \n", "2.668205 | \n", "6.666667 | \n", "70.222542 | \n", "11.530130 | \n", "93.633252 | \n", "43.950718 | \n", "14.828998 | \n", "11.530130 | \n", "0.000000 | \n", "Wakefulness | \n", "
2 | \n", "2.583652 | \n", "0.960001 | \n", "4.712651 | \n", "0.857776 | \n", "2.600193 | \n", "30.000000 | \n", "57.935638 | \n", "7.986526 | \n", "77.874840 | \n", "36.572344 | \n", "10.991775 | \n", "7.986526 | \n", "0.000000 | \n", "NREM | \n", "
3 | \n", "2.832752 | \n", "0.983541 | \n", "4.633715 | \n", "0.836868 | \n", "2.717458 | \n", "23.333333 | \n", "66.557528 | \n", "14.364074 | \n", "91.209212 | \n", "36.122140 | \n", "20.304962 | \n", "14.364074 | \n", "0.000000 | \n", "REM | \n", "
4 | \n", "2.812697 | \n", "1.054019 | \n", "5.032076 | \n", "0.949556 | \n", "2.848692 | \n", "36.666667 | \n", "70.133988 | \n", "12.177066 | \n", "113.358585 | \n", "43.797419 | \n", "19.262584 | \n", "12.177066 | \n", "6.896552 | \n", "Wakefulness | \n", "