Development of an epileptic seizure prediction algorithm using R–R intervals with self-attentive autoencoder

Epilepsy is a neurological disorder that may affect the autonomic nervous system (ANS) from 15 to 20 min before seizure onset, and disturbances of ANS affect R–R intervals (RRI) on an electrocardiogram (ECG). This study aims to develop a machine learning algorithm for predicting focal epileptic seizures by monitoring R–R interval (RRI) data in real time. The developed algorithm adopts a self-attentive autoencoder (SA-AE), which is a neural network for time-series data. The results of applying the developed seizure prediction algorithm to clinical data demonstrated that it functioned well in most patients; however, false positives (FPs) occurred in specific participants. In a future work, we will investigate the causes of FPs and optimize the developing seizure prediction algorithm to further improve performance using newly added clinical data.


Introduction
Epilepsy is a neurological disorder that causes recurrent seizures and affects about 1 % of the global population [1]. Although 70-80% of epilepsy patients can control seizures by taking appropriate medications, the remaining 20-30% of patients have intractable epilepsy that cannot be controlled with current therapies. In addition, women of childbearing potential may not be able to take antiepileptic drugs due to teratogenicity [2].
Epileptic seizures are caused by excessive electric discharge in the cerebrum, which also may affect the autonomic nervous system (ANS) from 15 to 20 min before seizure onset [3]. The alteration in ANS affects heart rate variability (HRV), which is defined as fluctuations of R-R intervals (RRI) on an electrocardiogram (ECG). RRI is the temporal distance derived from the interval between two adjacent R waves, as shown in Fig. 1.
Taking this effect into account, Fujiwara et al. have developed an algorithm for predicting epileptic seizures by means of multivariate statistical process control (MSPC) using HRV features. MSPC is a well-known anomaly detection method used mainly in the process industry. Fujiwara et al reported that their algorithm could predict seizures with high 1 3 sensitivity and a low false positive (FP) rate [4]. According to a guideline on HRV analysis [5], RRI data for at least two to three minutes are needed to extract HRV features, which means that patients must wait for a few minutes before Fujiwara's algorithm can be utilized. This fact motivated us to develop a new epileptic seizure prediction algorithm with a short downtime before algorithm start, preferably less than one minute.
It has been reported that RRIs become shorter around the time of epileptic seizures, a fact that could be used as a simple method for assessing ANS of epileptic patients [6]. It may be possible to shorten the downtime before the algorithm starts using raw RRI data for epileptic seizure prediction, as compared with using HRV features, by means of an anomaly detection framework. Thus, this study aims to develop a new machine learning algorithm for predicting focal epileptic seizures using RRI directly.

Self-attentive autoencoder
Autoencoder (AE) is a type of neural network model that is often used for unsupervised learning and in which outputs become close to inputs due to a latent function of hidden layers [7]. Figure 2 shows a network diagram of one example of AE. An objective function of the training is to minimize the following reconstruction error RE, RE as follows: where x i is input features, N is the number of features, and x ′ i is features restored by an AE model. Self-attentive autoencoder (SA-AE) is an extension of the AE model with a self-attention (SA) mechanism in the hidden layer, which is usually used for time-series analysis.
An SA mechanism is a method for learning important dependence in the input time-series data for determining the output. Figure 3 illustrates an SA-AE model with inputs of RRI data. The RRI data are designated as queries, keys, and values, each of which are divided by time windows. The attention weight of RRI is obtained by calculating dot products of the queries and the keys. Finally, the SA mechanism reconstructs the RRI data by multiplying the values by the attention weights, which is expected to suitably take into account the time-dependency of the RRI data.
The output of the SA mechanism is calculated in accordance with Eq. (2). Q, K, V are the matrixes of the queries, the keys, and the values, respectively, d k is the dimension of the query, and softmax indicates attention weight of the SA mechanism, which will take a larger value if the similarity between the Q and K components is high, and a smaller value if the similarity is low. By divide Q, K by √ d k , we can

Anomaly detection
Anomaly detection refers to a framework for finding patterns in data that do not conform to expected patterns. This framework uses only normal data as training data to learn patterns in said data. This learning method can determine whether the input data are normal or have abnormal patterns even if it is difficult to collect data containing anomalies.
In the proposed epileptic seizure prediction algorithm, the input values of SA-AE are the original RRI data, and the output values are the RRI data reconstructed by SA-AE. Algorithm 1 was adopted to train the SA-AE model before seizure prediction. In this algorithm, y [i] indicates the ith RRI segment clipped from the original RRI data with a window having a size of W, T represents the measured time length, P i represents ith patients, and I train is the number of patients. The interictal RRI segments of each patient are merged into one matrix X in steps 1-4 and are standardized into zero-means and a standard deviation of one in step 5. The SA-AE model is trained with X in step 6. Finally, in steps 7 and 8, the control limit of RE is determined for each patient and denoted as RE [i] .
After training the SA-AE model and setting an appropriate control limit RE , epileptic seizure prediction is performed based on Algorithm 2. C represents the patient's condition ( N : Normal, A : Abnormal), so that ¬N = A and vice versa. represents the time exceeding RE if C = N , and the time below the control limit if C = A . is the time threshold, which is intended to reduce FPs, since even routine activities such as eating, walking, and turning can cause ECG artifacts and easily disrupt the RRI. Setting larger than one is useful to suppress the influence of ECG artifacts and reduce FPs. In steps 3-5, the newly input RRI is standardized, and the RE is calculated using the trained SA-AE model. If RE continuously exceeds RE for more than when the patient's status is N , the status of the patient is determined to be A , since persistence of a large RE means that there is some abnormality. On the other hand, if RE continuously stays below RE for more than when the patient's status is A , the status of the patient is determined to be N .
In steps 5-13, the patient's status is determined to be normal N or abnormal A . If Algorithm 2 can successfully detect abnormal changes in RRI before a seizure, the patient would be able know in advance that a seizure could occur and take appropriate actions to prevent injury or accidents caused by epileptic seizures.
In this study, the seizure prediction algorithm is stopped for 30 min to restore ANS to its normal state once a seizure is predicted.

Data description
We collected clinical RRI data from 39 patients with focal epilepsy who were admitted to the Medical Hospital of Tokyo Medical and Dental University (TMDU), the National Hospital Organization Nara Medical Center (NMC), National Center of Neurology and Psychiatry (NCNP) Hospital, Tohoku University Hospital (TUH), Osaka University Hospital Epilepsy Center (OUHEC), and University of Tokyo Hospital (UTH) to monitor clinical video-EEG. Seizure onsets were identified by clinical epilepsy specialists, certified by the Japan Epilepsy Society, based on the video-EEG monitoring data. Collection and analysis of the clinical data were approved by the Ethics Committee of TMDU, NMC, NCNP, TUH, OUHEC, and UTH. In addition to our collected data, we also retrieved data of 27 European partial epileptic patients from the European epilepsy database [11] to evaluate ethnic differences.
Training data included 131 interictal episodes for 77.4 h and validation data consisted of 264 interictal and 85 preictal episodes for a total of 195 h. The type of partial epilepsy varied for each patient, including temporal lobe epilepsy (TLE), frontal lobe epilepsy (FLE), and parietal lobe epilepsy (PLE). The mean age of patients was 36.4 ± 13.7. 45 patients were male, and 21 were female.

Condition for seizure prediction
We trained the SA-AE model in accordance with Algorithm 1 with the training data and evaluated its performance in accordance with Algorithm 2 using the validation data. The hyperparameters such as the number of units in the hidden layer and the internal function of the SA mechanism were determined by Optune™, which is a type of a Bayesian optimization library implemented in Python [10].
In this study, the control limit of RE was determined based on the 99% confidence limit, and the time threshold was set to eight seconds. They were determined by means of grid search.

Seizure prediction performance
The overall results showed that sensitivity, precision, FP rate, and Area Under Curve (AUC) of the trained SA-AE model were 74%, 0.35, 0.85 times/h, and 0.97, respectively. A sensitivity of 100% was achieved in 29 patients, eight of whom had no FP. Figure 4 shows an example of a seizure prediction result for patient A (male, 41 years old, right TLE). The top and bottom figures show the preictal and interictal episodes, respectively. The green area is the period that the algorithm determined as abnormal, and the blue area is the period during which seizure prediction was stopped.
The abnormal condition was detected before seizure onset in the preictal episode, while RE was monotonic and stayed below the control limits in the interictal episode. It indicates that seizure prediction with the proposed algorithm functioned well for patient A.

Discussion
In this study, the RRI-based epileptic seizure prediction model was trained utilizing SA-AE, which achieved a sensitivity of 74% and an FP rate of 0.85 times/h. To confirm the validity of the trained model, we compared its performance with other models using only the RRI as input. The compared models were an AE model without the SA mechanism and a long short-term memory (LSTM) network with a hidden layer of AE. Table 1 shows the comparison results. The SA-AE model was able to predict seizures with the highest precision among the three models. In particular, the result suggests that the SA mechanism helps reduce FPs since the sensitivity of SA-AE was the same as that of AE while the FP rate was the lowest.
The seizure prediction performance varied for each patient. Figure 5 shows histograms and a box plot of FP rates per patient, which indicate that only six patients had an FP rate of more than 2.5 times/h.
We checked the profile of patient B (male, 46 years old, localization-related epilepsy, Rt temporal lobe epilepsy s/o), whose FP rate was 2.6 times/h. As shown in Fig. 6, the change in RE in patient B was more intense than in patient A. Although there were no significant findings in the video monitoring data or complications, the brain structure in localization-related epilepsy is problematic, and its epileptic focus is difficult to identify. Thus, the effect on ANS before seizures may be different from other types of epilepsy. Lehtimäki et al. suggested that a single seizure triggers inflammatory signals in patients with chronic  localization-related epilepsy, which may lead to structural changes in neural tissue [12]. The AUC exceeded 0.9 in almost all patients; however, the AUC of patient C [male, 50 years old, bitemporal lobe epilepsy (BTLE)] was 0.56. Figure 7 shows his seizure prediction results, in which changes in RE were different from those of patients A and B for whom no significant changes were observed even in the preictal period. Patient C may have problems other than epilepsy since his seizures could not be predicted with any model, but a detailed profile of him is unknown. In addition, his control limit was much larger than those of other patients.
Page et al. reported that bitemporal ictal activity detected by intracranial depth EEG monitoring induced more significant autonomic dysregulation compared to nontemporal ictal activity [13]. Repetitive stimulation of the central autonomic network by epileptic discharges may result in interictal dysfunction of the autonomic nervous system [14]. It is possible that the interictal RRI of patient C deviated from those of other patients included in the training data, due to pathological cardiac autonomic activity caused by chronic BTLE. This could be the rationale for setting a large control limit for patient C.
Poor seizure prediction performance of other patients may have been caused by motion artifacts contaminating the RRI data. In addition, alteration of ANS may induce FPs. It is well known that there is a close relationship between the activities of ANS and epileptic seizures [3]. In addition, it has been reported that ANS activities tend to be impaired in patients with anxiety disorders. Thus, poor mental conditions like anxiety may affect ANS, and such alterations of ANS may be detected by the SA-AE model as FPs. It is also possible that the SA-AE model could not capture changes in RRI well, because the characteristics of the RRI data used as training data were different from those of such patients. Thus, it is necessary to individually tune hyperparameters of the seizure prediction algorithm to cover typical causes of FP.
The attention mechanism adopted in SA-AE is a method for identifying which parts of the input data contribute to output calculation. In natural language processing, for instance, the attention mechanism identifies important words for sentence understanding, which is capable of interpretation even for humans [15]. However, in the proposed epileptic seizure prediction model, it is difficult to understand the meaning of important RRIs. Instead of interpretations about each RRI, we focus on co-occurrence information about important RRIs for seizure prediction in the SA-AE model.  The correlation matrixes of the attention weights in interictal and preictal periods of Patient A who had a high sensitivity and a low FP rate as shown in Fig. 8a, b. These figures indicate that RRIs close to each other contributed to the prediction in Patient A. On the other hand, in Patient C, as shown in Fig. 8c, d, RRIs wider than those in Patient A were used. That is, the SA-AE model might not find important information for seizure prediction in the input RRI data. This characteristic in the attention weights was common among some patients with poor seizure prediction performance. Therefore, it may be possible to identify participants for whom seizure prediction is effective by checking the attention weights.

Conclusion
In this study, we proposed an anomaly detection algorithm for epileptic seizure prediction based on an SA-AE model. The results of applying the proposed algorithm to the clinical data demonstrated that it functioned well in some patients; however, further improvements in prediction performance are needed to clinically apply it to more epilepsy patients. In future works, we aim to further improve the seizure prediction performance by optimizing the parameters of the model or developing a new seizure prediction model. In addition, we will collect more clinical data to investigate typical causes of FPs in epileptic seizure prediction.