Acta Polytechnica CTU Proceedings https://doi.org/10.14311/APP.2024.51.0075 Acta Polytechnica CTU Proceedings 51:75–80, 2024 © 2024 The Author(s). Licensed under a CC-BY 4.0 licence Published by the Czech Technical University in Prague AUTOMATIC DETECTION OF SLEEP SPINDLES BY NEURAL NETWORKS ALGORITHMS Jan Rychlík∗, Roman Mouček University of West Bohemia, Faculty of Applied Sciences, Department of Computer Science, Univerzitní 8, 301 00 Plzeň, Czech Republic ∗ corresponding author: rychlikj@kiv.zcu.cz Abstract. Sleep constitutes an essential aspect of human existence, with the average individual dedicating approximately one-third of their life to this physiological activity. Consequently, comprehending and accurately analyzing sleep patterns is of paramount importance. This research aims to introduce, formulate, execute, and assess diverse machine/deep learning methodologies tailored for the processing of EEG signals geared explicitly towards identifying sleep spindles. The learning algorithms underwent training using meticulously annotated data from the Montreal Archive of Sleep Studies (MASS) data center. The convolutional neural network emerged as the most effective classification model, achieving an accuracy surpassing 67%. Keywords: Deep learning, EEG data standards, EEG workflows, EEG pipelines, electroencephalog- raphy, event-related potentials, human brain, machine learning. 1. Introduction Sleep, comprising approximately one-third of human life, is crucial for bodily rejuvenation and mental relax- ation. However, the prevalence of sleeping disorders in modern society underscores the significance of research in this domain to enhance sleep quality. The brain’s activity undergoes distinct changes dur- ing sleep, categorized into REM and non-REM phases; a specific phenomenon within the non-REM phase is known as a sleep spindle. Sleep spindles are brief bursts of neural oscillatory activity during non-REM sleep and play a crucial role in memory consolidation. These spindle events are essential indicators of sleep quality and cognitive functions. Electroencephalography (EEG), the fundamental method and technique for measuring and collecting electrical activity of the human brain, is also used in sleep data collection; their abundance and complexity necessitate further computer processing. This miti- gates human error and promotes more efficient and accurate data analysis. Machine and deep learning, particularly neural net- works such as Convolutional Neural Networks (CNN), Long Short-Term Memory Networks (LSTM), and Dense Networks, offer a promising approach to EEG signal and sleep data processing. This paper focuses on identifying sleep spindles. This is accomplished by designing, implementing, and assessing deep learning methods (neural networks). The general aim is to improve understanding and inter- pretation of EEG data, contribute to advancements in automated sleep analysis, and enhance our knowledge of sleep-related phenomena. The paper is organized in the following way. The state-of-the-art section provides insight into EEG and sleep stages classification, sleep spindle characteristics, and sleep data platforms and archives. It is followed by the sections describing the dataset and neural net- work architectures used. Then, the dataset processing is presented, and the results are provided. The out- comes and future perspectives are summarized in the conclusion section. 2. State of the art The typical use case in EEG signal classification is to compare methods for categorizing preictal, postic- tal, and interictal classes when epileptic seizures are detected. The model proposed in [1] integrated a two- layer LSTM and a four-layer enhanced neural net- work (NN) deep learning architecture using improved one-dimensional gradient descent activation functions. The study used the pre-processed Bonn University database; statistical features were extracted. Con- ventional methods used for the classification included Support Vector machines (SVM) with different ker- nels, logistic regression, and NNs. The results showed that the improved NN algorithm achieved the highest accuracy (nearly 79%), surpassing LSTM (71%), NN neural network (61%), and SVM (70%), while logistic regression had the lowest accuracy (about 53%) [1]. In collaboration with the Department of Medi- cal Engineering and Technomathematics in Germany, Aachen University of Applied Sciences used a machine learning approach [2] to score pre-REM sleep stages in mice automatically. Using a dataset of polysomno- graphic recordings from 18 mice over 52 days, the study investigated the impact of dietary variation on sleep. The mice, chronically implanted with EEG and EMG electrodes, underwent data pre-processing, 75 https://doi.org/10.14311/APP.2024.51.0075 https://creativecommons.org/licenses/by/4.0/ https://www.cvut.cz/en Jan Rychlík, Roman Mouček Acta Polytechnica CTU Proceedings including low-pass filtering and synthetic data aug- mentation for a balanced training set. The neural network architecture, consisting of eight convolutional layers and a classifier with two fully connected layers, demonstrated high accuracy in classifying Wake, REM, and NREM segments, achieving over 98%, more than 94%, and close to 92% accuracy, respectively. The pre-REM stage was correctly classified in 58% of the segments. The classifier showed robust performance, correctly predicting human-expert assigned stages for segments predicted to be Wake, REM, and NREM, demonstrating the effectiveness of the network in the automated classification of sleep stages in mice [2]. A 1D CNN-LSTM algorithm for automatic sleep staging (Wake, REM, Non-REM) on the Sleep-EDF dataset, achieving 93.47% accuracy with the Fpz-Cz EEG channel, is described in [3]. The dataset com- prises 197 PolySomnoGraphic sleep recordings with manually scored hypnograms. With seven layers (four 1D CNN and three LSTM layers), the algorithm ex- hibited robust performance across various physiologi- cal signals. Evaluations on single-channel EEG and EOG classifications, using five test sets for each group, confirmed the model’s reliability. Achieving 94.15% accuracy when incorporating Fpz-Cz EEG and EOG signals, the algorithm demonstrates promise for auto- mated sleep staging in future sleep-related research. It offers an efficient alternative to manual expert in- spection in large-scale Polysomnography (PSG) signal analysis, showcasing the effectiveness of deep learning in discerning similar sleep periods [3]. Figure 1 displays a characteristic sleep spindle ev- ident in the EEG waveform during non-rapid eye movement (non-REM) sleep. This spindle, observed in frontal and central brain regions, manifests as a brief burst of rhythmic oscillations lasting 0.5 to 2 seconds, with frequencies ranging from 11 to 16Hz. The figure provides a visual representation of the spatial and temporal dynamics of the sleep spindle, contributing to the understanding of its electrographic features. Recognized for its role in memory consolidation, the accurate identification and analysis of sleep spindles, as illustrated in Figure 1, are crucial for investigating sleep disorders and neurological conditions, enhancing comprehension of neural activities during sleep. Due to its characteristic properties, it is suitable for test- ing processing techniques like machine learning and neural networks further to refine the detection and interpretation of sleep spindles. The Massive online data annotation (MODA) plat- form is pivotal in generating standardized datasets for training and validating automated detectors of biological signals, including EEG [5]. A Canadian study using MODA compared the results of expert, researcher, and non-expert scorers with seven spin- dle detection algorithms, revealing that only two al- gorithms performed comparably to human experts, showcasing MODA’s significance in benchmarking au- tomated sleep analysis methods. Figure 1. Example of EEG sleep spindles; adopted from [4]. The Montreal Archive of Sleep Studies (MASS) also serves as an open-access repository, providing PSG data for benchmarking automated sleep analysis sys- tems [6]. Established as part of the MODA project [5], MASS is a comprehensive and openly accessible repos- itory of PSG recordings with annotated EEG signals, supporting large-scale collaborations in sleep stud- ies [6]. The MAAS database is structured into cohorts, with subsets categorizing recordings based on specific characteristics [6]. 3. Sleep Spindles Dataset This study used EEG data from the MASS archive’s stage SS2 (described in Table 1). The data preparation of EEG recordings involves addressing challenges such as the unique occurrence of sleep spindles in specific sleep segments, constituting about two percent of the overall recording. Given this imbalance, set balancing is crucial for effective neural network training. The measured voltage values in the EEG signal range from 10−4 to 10−6 Volts, necessitating normalization. The EEG records from only the Pz-CLE channel were further processed, as this channel provides optimal visibility for sleep spindles. The EEG records were subdivided into smaller segments for neural network processing; each segment was further divided into sets using annotations. The dataset’s imbalance, where segments without spindles make up about ninety-eight percent of the dataset, is reduced using a Python random number generator for unbiased set balancing, avoiding pattern-based distortions during training. 4. Neural network architectures This section briefly overviews the methods (neural network architectures) employed for the automated detection of sleep spindles in the dataset described above. Furthermore, these architectures were comple- mented by their combinations, with the anticipation of preserving the advantages and properties inherent in each architecture. 4.1. Dense Neural Network The Dense Neural Network (DNN) depicted in Fig- ure 2, also known as a fully connected neural network, exhibits a structure where each node in one layer connects to every node in the next. This dense con- nectivity allows comprehensive EEG signal processing 76 vol. 51/2024 Sleep spindle detection Stage 2 (SS2) Memory size 7.26GB Total number of measurements 19 Total number of spindles 11204 Average number of spindles in a measurement 217 Time length 151 h Maximum number of spindles in a measurement 569 Minimum number of spindles in a measurement 86 Maximum duration of spindle 2.218605 s Minimum duration of spindle 0.335915 s Average length of an EEG measurement 7 h 59min Table 1. Characteristics of stage SS2. Figure 2. An example of a standard dense neural network. The network has one input, two hidden, and one output layer, adopted from [7]. and feature extraction, enabling the network to cap- ture intricate data relationships. DNNs are widely applied in domains such as image recognition, natu- ral language processing, and signal processing due to their ability to learn complex patterns from extensive datasets. 4.2. CNN The Convolutional Neural Network (CNN) illustrated in Figure 3 is a specialized class of artificial neural networks designed for processing structured grid data, particularly effective for tasks involving image analysis and recognition. With distinctive architectural ele- ments, including convolutional layers, pooling layers, and fully connected layers, CNNs excel at captur- ing hierarchical features and spatial hierarchies in input data. Convolutional layers use filters to con- volve across input data, extracting local features and patterns. Pooling layers reduce spatial dimensions, preserving crucial information while minimizing com- putational complexity. Fully connected layers enable high-level abstraction and decision-making. CNNs demonstrate notable efficacy in applications like image classification, object detection, and facial recognition, underscoring their significance in computer vision and pattern recognition research. Figure 3. An example of an application of filter on an image inside a CNN network, adopted from [8]. 4.3. LSTM The Long Short-Term Memory (LSTM) neural net- work, a specialized recurrent neural network (RNN) architecture, addresses the challenge of capturing long- range dependencies in sequential data by incorpo- rating memory cells with input, forget, and output gates, making these networks particularly suitable for tasks involving memory and temporal patterns. The gates selectively control information flow, allowing the network to retain relevant context over extended sequences and mitigating the vanishing gradient prob- lem. LSTMs excel in natural language processing, speech recognition, and time series analysis, effec- tively capturing temporal dependencies. Their role in mitigating the vanishing gradient problem has posi- tioned LSTMs as a cornerstone in developing advanced deep-learning models for sequential data processing, contributing to diverse areas of artificial intelligence research. The network architecture is depicted in Fig- ure 4, showcasing its capability to detect specific EEG signals. 5. Dataset Processing The current research incorporates three prominent neural network architectures discussed above: DNN, LSTM, and CNN. The strategic use of these archi- tectures underscores a sophisticated approach to ad- 77 Jan Rychlík, Roman Mouček Acta Polytechnica CTU Proceedings Figure 4. LSTM neural network scheme, adopted from [9]. dressing the complexities associated with sleep spindle detection in research studies. The DNN architecture was the primary choice for sleep spindle dataset processing; it provides a basic model for initial exploration and understanding of the dataset. The LSTM architecture has been incorpo- rated to process the data further. The inclusion of LSTMs is motivated by their ability to detect subtle signals that precede the occurrence of sleep spindles. To enhance the data processing capabilities, the CNN was introduced into the architecture. In the con- text of sleep spindle detection, treating EEG data as an image allows CNNs to capture hierarchical features and spatial hierarchies efficiently. The convolutional layers in CNNs facilitate extracting local features and patterns, contributing to a comprehensive understand- ing of the dataset. Various combinations of DNN, LSTM, and CNN architectures provide a diverse and comprehensive ap- proach to the data processing to improve sleep spindle detection algorithms; we chose the following simple architectures: LSTM, Dense, CNN and CNN and their combinations of architecture: CNN-LSTM im- plemented in Keras python package and CNN-LSTM implemented in Torch python package. The proposed CNN-LSTM implemented in Keras package architecture is shown in Figure 5. It consists of seven layers, with the initial four layers character- ized by convolutional structures, each having 32 neu- rons. Following the non-convolutional layer is a linear layer with 384 neurons, introducing a deeper level of abstraction. Subsequently, the penultimate layer is also linear, incorporating 64 neurons. Ultimately, the final layer consists of two neurons. 5.1. Validation dataset The validation dataset encompasses nearly 200,000 instances, with 51.2% attributed to non-sleep spin- dles, and the remaining instances dedicated to sleep spindles. This meticulously constructed dataset is a subset derived from the extensive data repository provided by the Montreal Archive of Sleep Studies (MASS) center. The deliberate balance between the two classes ensures a representative and unbiased sam- ple for thorough evaluation, fostering the reliability and generalizability of the neural network’s perfor- mance assessment. The training and testing of the neural network archi- tectures has been performed on a normalized data set. The parameters for the neural networks, such as the size of the input layer and the number of epochs, were determined using genetic algorithms. The number of epochs varies for each architecture. The last method used to process the sleep spindle dataset, the value-based method, is a simple analytic method. Its principle is that it sums the measured values on a specific interval and compares the resulting values with each other. For this method, it is crucial to set the border correctly. At the outset of the algorithm, the border was initialized to zero. Subsequently, the EEG signal un- derwent segmentation, with each segment consistently comprising 64 measured values. The absolute sum of all values within the segment was computed and compared against the border. If the computed value exceeded the border threshold, the segment was anno- tated as a sleep spindle. The algorithm then tallied the number of correctly and incorrectly classified seg- ments, following which the border was incrementally adjusted by a small increment. The optimal thresh- old value for classification was determined as the one yielding the highest number of correctly identified spindles. 6. Results Table 2 summarizes the results obtained from testing various classifiers and their combinations. Also the Figure 6 shows peak accuracies achieved by the used classifiers. Network architecture Accuracy Dense 64.63% LSTM 52.81% CNN 60.87% CNN-LSTM in Keras 52.81% CNN-LSTM in Torch 67.15% Value-based method 64.12% Table 2. Best results provided by the used classifiers. Figures 7 and 8 present the accuracy and loss func- tion values corresponding to the optimal neural net- work configuration evaluated on the validation dataset. Both figures are structured with the number of the validation set epochs delineated on the x-axis, visually representing the network’s performance metrics across multiple epochs. 78 vol. 51/2024 Sleep spindle detection Figure 5. The CNN-LSTM architecture implemented in Torch package. The dropout layer prevents getting stuck in the local minimum; Flatten links CNN and the Linear layer. Figure 6. The best classification results (peak accu- racies) achieved by the used classifiers. 7. Conclusion The paper examines the processing of EEG signals dur- ing sleep to identify sleep spindles using mostly various simple and hybrid neural network architectures. The study used data from the recognized MASS archive. The used dataset was balanced, divided into training and testing sets, and used to train neural networks. The classification accuracy of LSTM and LSTM-CNN networks was the lowest at 52.81%. Keras’ Dense and CNN networks showed slightly better results with an accuracy of 60–65%. The most successful network was the CNN network implemented in the Torch package, achieving accuracy exceeding 67%. Figure 7. The accuracy on the validation dataset; X- axis = number of epochs over the validation dataset; Y -axis = the accuracy value. The achieved classification results are consistent with those reported in the literature, where the best results for EEG sleeep artifact classification reached 70–75%. The value-based method achieved an accu- racy of 64.12%, slightly below the state-of-the-art. It may be worth refining the neural network parame- ters by extending the state area of input parameters to improve performance. Also, the potential benefits of the next alternative hybrid deep learning architectures in identifying sleep spindles can be considered. Acknowledgements 79 Jan Rychlík, Roman Mouček Acta Polytechnica CTU Proceedings Figure 8. The loss function on the validation dataset; X-axis = number of epochs over the validation dataset; Y -axis = the loss value. This work was supported by the university-specific re- search project SGS-2022-016 Advanced Methods of Data Processing and Analysis (project SGS-2022-016). References [1] P. Nagabushanam, S. T. George, S. Radha. EEG signal classification using LSTM and improved neural network algorithms. Soft Computing 7:9981–10003, 2020. https://doi.org/10.1007/s00500-019-04515-0 [2] N. Grieger, J. T. C. Schwabedal, S. Wendel, et al. Automated scoring of pre-REM sleep in mice with deep learning. Scientific Reports 11:12245, 2021. https://doi.org/10.1038/s41598-021-91286-0 [3] D. Zhao, R. Jiang, M. Feng, et al. A deep learning algorithm based on 1D CNN-LSTM for automatic sleep staging. Technology and Health Care 30(2):323–336, 2022. https://doi.org/10.3233/THC-212847 [4] I. B. Iotchev, E. Kubinyi. Shared and unique features of mammalian sleep spindles – insights from new and old animal modelss. Biological Reviews 96(3):1021–1034, 2021. https://doi.org/10.1111/brv.12688 [5] K. Lacourse, B. Yetton, S. Mednick, S. C. Warby. Massive online data annotation, crowdsourcing to generate high quality sleep spindle annotations from EEG data. Scientific Data 7:190, 2020. https://doi.org/10.1038/s41597-020-0533-4 [6] T. Paquette. Montreal Archive of Sleep Studies (MASS), 2015. [2022-01-21]. http://ceams-carsm.ca/mass/ [7] G. Ognjanovski. Everything you need to know about Neural Networks and Backpropagation — Machine Learning Easy and Fun, 2019. [2022-01-04]. https://towardsdatascience.com/everything-you- need-to-know-about-neural-networks-and- backpropagation-machine-learning-made-easy- /e5285bc2be3a [8] IBM Cloud Education. Convolutional neural networks, 2020. [2022-05-03]. https://www.ibm.com/cloud/learn/convolutional- neural-networks [9] S. Dobilas. LSTM Recurrent Neural Networks — How to teach a network to remember the past, 2021. [2022-05-03]. https://towardsdatascience.com/lstm- recurrent-neural-networks-how-to-teach-a- network-to-remember-the-past-55e54c2ff22e 80 https://doi.org/10.1007/s00500-019-04515-0 https://doi.org/10.1038/s41598-021-91286-0 https://doi.org/10.3233/THC-212847 https://doi.org/10.1111/brv.12688 https://doi.org/10.1038/s41597-020-0533-4 http://ceams-carsm.ca/mass/ https://towardsdatascience.com/everything-you-need-to-know-about-neural-networks-and-backpropagation-machine-learning-made-easy-/e5285bc2be3a https://towardsdatascience.com/everything-you-need-to-know-about-neural-networks-and-backpropagation-machine-learning-made-easy-/e5285bc2be3a https://towardsdatascience.com/everything-you-need-to-know-about-neural-networks-and-backpropagation-machine-learning-made-easy-/e5285bc2be3a https://towardsdatascience.com/everything-you-need-to-know-about-neural-networks-and-backpropagation-machine-learning-made-easy-/e5285bc2be3a https://www.ibm.com/cloud/learn/convolutional-neural-networks https://www.ibm.com/cloud/learn/convolutional-neural-networks https://towardsdatascience.com/lstm-recurrent-neural-networks-how-to-teach-a-network-to-remember-the-past-55e54c2ff22e https://towardsdatascience.com/lstm-recurrent-neural-networks-how-to-teach-a-network-to-remember-the-past-55e54c2ff22e https://towardsdatascience.com/lstm-recurrent-neural-networks-how-to-teach-a-network-to-remember-the-past-55e54c2ff22e Acta Polytechnica CTU Proceedings 51:75–80, 2024 1 Introduction 2 State of the art 3 Sleep Spindles Dataset 4 Neural network architectures 4.1 Dense Neural Network 4.2 CNN 4.3 LSTM 5 Dataset Processing 5.1 Validation dataset 6 Results 7 Conclusion Acknowledgements References