1 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.

Fig. 1
figure 1

ECG trace and RRI

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 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.

2 Methods

2.1 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:

$$\begin{aligned} RE = \sum _{i = 1}^N\Vert {\varvec{x}}_{i} - {\varvec{x'}}_{i}\Vert , \end{aligned}$$
(1)

where \({\varvec{x}}_{i}\) is input features, N is the number of features, and \({\varvec{x}}'_{i}\) is features restored by an AE model.

Fig. 2
figure 2

Schematic diagram of AE

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.

$$\begin{aligned} \textrm{Attention}({\varvec{Q}},{\varvec{K}},{\varvec{V}}) = \textrm{softmax}\left( \frac{{\varvec{QK}}^{T}}{\sqrt{d_{k}}}\right) {\varvec{V}}, \end{aligned}$$
(2)

The output of the SA mechanism is calculated in accordance with Eq. (2). \({\varvec{Q}}, {\varvec{K}}, {\varvec{V}}\) are the matrixes of the queries, the keys, and the values, respectively, \(d_{k}\) is the dimension of the query, and \(\textrm{softmax}\left( \frac{{\varvec{QK}}^{T}}{\sqrt{d_{k}}}\right)\) indicates attention weight of the SA mechanism, which will take a larger value if the similarity between the \({\varvec{Q}}\) and \({\varvec{K}}\) components is high, and a smaller value if the similarity is low. By divide \({\varvec{Q}}, {\varvec{K}}\) by \(\sqrt{d_{k}}\), we can prevent the dot products becoming large and pushing the softmax function into regions where it has extremely small gradients [8].

Fig. 3
figure 3

Schematic diagram of SA-AE

2.2 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, \({\varvec{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 \(\tilde{{\varvec{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 \({\varvec{X}}\) in step 6. Finally, in steps 7 and 8, the control limit of RE is determined for each patient and denoted as \(\overline{RE}^{[i]}\).

After training the SA-AE model and setting an appropriate control limit \(\overline{RE}\), epileptic seizure prediction is performed based on Algorithm 2. C represents the patient’s condition (\(\mathcal {N}\): Normal, \(\mathcal {A}\): Abnormal), so that \(\lnot \mathcal {N} = \mathcal {A}\) and vice versa. \(\tau\) represents the time exceeding \(\overline{RE}\) if \(C = \mathcal {N}\), and the time below the control limit if \(C = \mathcal {A}\). \(\overline{\tau }\) 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 \(\overline{\tau }\) 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 \(\overline{RE}\) for more than \(\overline{\tau }\) when the patient’s status is \(\mathcal {N}\), the status of the patient is determined to be \(\mathcal {A}\), since persistence of a large RE means that there is some abnormality. On the other hand, if RE continuously stays below \(\overline{RE}\) for more than \(\overline{\tau }\) when the patient’s status is \(\mathcal {A}\), the status of the patient is determined to be \(\mathcal {N}\).

In steps 5–13, the patient’s status is determined to be normal \(\mathcal {N}\) or abnormal \(\mathcal {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.

figure a
figure b

In this study, the seizure prediction algorithm is stopped for 30 min to restore ANS to its normal state once a seizure is predicted.

2.3 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.

3 Results

3.1 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.

3.2 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.

Fig. 4
figure 4

Seizure prediction results of patient A for preictal data (top) and interictal period data (bottom)

4 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. Figure 8 shows a correlation matrix of the attention weights in the attention layer of the trained SA-AE model. The size of the correlation matrix was 45\(\times\) 45 because the length of the time window of the input RRI data for the trained SA-AE model was 45 seconds.

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.

Table 1 The result of seizure prediction of each model
Fig. 5
figure 5

Histograms of FP rates per patient for seizure prediction

Fig. 6
figure 6

Seizure prediction results of patient B for preictal data (upper) and interictal period data (lower)

Fig. 7
figure 7

Seizure prediction results of patient C for preictal data (upper) and interictal period data (lower)

Fig. 8
figure 8

Heatmaps of the correlation matrix of attention weights of Patient A (top) and Patient C (bottom); left and right figures are in inter- and peri-ictal periods, respectively

5 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.