MYTER

How the seizure detector works

Data: CHB-MIT · PhysioNet

The question in one sentence

A standard EEG uses about 21 electrodes. We ask: if we use only 8, can a computer still spot seizures about as well? Fewer electrodes means a simpler, cheaper headset for home use.

Below is the whole pipeline, start to finish, in plain language. No step is a black box — you can see exactly what the program measures and how it decides.

1

Start with brain-wave recordings

EEG electrodes sit on the scalp and record tiny electrical voltages from the brain, hundreds of times per second (256 times/second here). Each electrode pair is a channel. During a seizure, the brain's electrical activity changes in ways that often show up in the signal — sharper, more rhythmic, higher-energy waves.

We use the CHB-MIT dataset: real recordings from pediatric epilepsy patients, where doctors have already marked the exact start and end of every seizure. Those marks are our "answer key."

2

Cut the recording into 4-second windows

A one-hour recording is far too long to judge all at once, so we slice it into short 4-second windows (overlapping by half, so we don't miss anything that falls on a boundary). The model looks at one window at a time and answers a yes/no question: does this 4 seconds look like a seizure?

Each window gets a label from the doctors' answer key: seizure if it overlaps a marked seizure, otherwise background.

3

Turn each window into numbers (the features)

A computer can't learn from a raw squiggle directly, so for every window we measure a set of meaningful numbers. These are chosen to be understandable, not mysterious:

Shape of the wave (time-domain)

  • Line length — how wiggly / jagged it is (spikes raise this)
  • Energy & variance — how big and how spread-out the swings are
  • Peak amplitude — the single largest spike
  • Skew & kurtosis — how lopsided or spiky the distribution is

Rhythm of the wave (frequency-domain)

  • Band power — how much energy sits in each brain-wave band: delta, theta, alpha, beta, gamma
  • Spectral entropy — whether the rhythm is focused or chaotic
  • Peak frequency — the dominant rhythm in that window

We compute these for each channel, then summarize across channels (average, strongest, and spread). The result is one tidy list of numbers per window — the "fingerprint" of those 4 seconds.

4

Teach a model to tell seizure from background

We use a Random Forest — think of it as a committee of hundreds of simple yes/no decision trees. Each tree learns rules like "if theta-band power is high AND the line length spikes, lean toward seizure." They vote, and the majority wins.

Seizures are rare (most of an hour is normal brain activity), so we tell the model to weight the rare seizure windows more heavily — otherwise it could score well by just guessing "background" every time.

We train on part of the data and keep the rest hidden for testing, so the model is always judged on windows it has never seen.

5

Make a prediction, then group flags into alarms

For each test window the model outputs a probability. Above 50% we flag it as a seizure. But a single real seizure spans many windows, and we don't want to count each flagged window as a separate alert. So we merge nearby flags (within 20 seconds) into one alarm event — the way a real monitor would raise one alert, not dozens.

6

Check the guesses: two honest scores

Seizure sensitivity

Of the real seizures, how many did we raise an alarm for?
detected ÷ total seizures

This is the safety number. Missing a seizure is the worst outcome, so we want this high.

False alarms per hour

How many alarms fired when there was no seizure, per hour of recording?
false alarms ÷ hours

This is the cost number. Every false alarm wastes a clinician's review time, so we want this low.

We run this twice — once with all 21 channels, once with just 8 — on the exact same recording, and compare. If the 8-channel scores stay close, the cheaper setup is justified.

The math, exactly as the code computes it

Every formula below maps to a real line in our feature or scoring code. For a window we write the EEG samples of one channel as \(x_1, x_2, \dots, x_N\) (here \(N = 4\text{s} \times 256\,\text{Hz} = 1024\) samples).

Time-domain features (the shape of the wave)

Line length — total up-and-down travel. Spiky seizure activity makes this large.
$$ \text{LL} = \sum_{i=1}^{N-1} \lvert x_{i+1} - x_i \rvert $$
Energy — total squared signal. Bigger swings raise it.
$$ E = \sum_{i=1}^{N} x_i^{2} $$
Variance — how much the signal jumps around its average. Bigger, wilder swings mean higher variance.
$$ \sigma^2 = \frac{1}{N}\sum_{i=1}^{N} (x_i - \mu)^2 $$
Skew & kurtosis — whether the wave is lopsided (skew) and how sharp/spiky its peaks are (kurtosis). Seizures tend to be spiky.
$$ \text{Skew} = \frac{\tfrac1N\sum (x_i-\mu)^3}{\sigma^{3}}, \qquad \text{Kurt} = \frac{\tfrac1N\sum (x_i-\mu)^4}{\sigma^{4}} - 3 $$

Frequency-domain features (the rhythm of the wave)

We estimate how the signal's power is spread across frequencies using the Welch power spectral density \(S(f)\) (the signal is split into overlapping pieces, each Fourier-transformed, and the results averaged to reduce noise). From \(S(f)\) we read off:

Band power — how much of the brain's activity falls in each rhythm range (e.g. theta = 4–8 waves per second). Theta surges during seizures.
$$ P_{\text{band}} = \int_{f_{lo}}^{f_{hi}} S(f)\,df $$
Spectral entropy — whether the brain's rhythm is scattered across many frequencies (chaotic) or concentrated in a few (focused).
$$ H = -\sum_{f} p_f \log_2 p_f $$
Peak frequency — the single dominant rhythm in the window.
$$ f_{\text{peak}} = \arg\max_{f} \; S(f) $$

The model: Random Forest

The \(T\) features above (per channel, summarized across channels) form a vector \(\mathbf{z}\). A Random Forest of \(B\) decision trees each outputs a class, and the forest's seizure probability is simply the fraction of trees that vote "seizure":

$$ \hat{p}(\text{seizure}\mid \mathbf{z}) = \frac{1}{B}\sum_{b=1}^{B} \mathbb{1}\big[\,t_b(\mathbf{z}) = \text{seizure}\,\big] $$

Each tree \(t_b\) splits the data to reduce Gini impurity \(\;G = 1 - \sum_k p_k^2\;\) (lower = purer, more one-class groups). Because seizure windows are rare, we weight each class inversely to how often it appears, so the rare seizure class isn't ignored: \(\; w_k = \frac{\text{total samples}}{(\#\text{classes}) \cdot (\text{samples in class } k)} \).

Turning a probability into a decision

Flag a window as seizure when its probability clears the threshold (we use \(\tau = 0.5\)).
$$ \hat{y} = \begin{cases} 1 & \hat{p} \ge \tau \\ 0 & \hat{p} < \tau \end{cases} $$

The two scores

Seizure sensitivity (safety) — share of real seizures we alarmed on.
$$ \text{Sensitivity} = \frac{\#\,\text{seizures detected}}{\#\,\text{seizures total}} $$
False alarms per hour (cost) — alarms with no real seizure, per recording hour.
$$ \text{FA/hr} = \frac{\#\,\text{false alarms}}{\text{hours recorded}} $$

We also report the window-level F1 score, the harmonic mean of precision and recall: \(\; F_1 = 2\cdot\dfrac{\text{precision}\cdot\text{recall}}{\text{precision}+\text{recall}} \), which balances "don't miss seizures" against "don't over-flag."

▶ See these equations run on a real seizure window

Every number below was computed by our actual pipeline on one real 4-second window from patient chb06 (starting 1,722 seconds into the recording), on a single EEG channel. Nothing here is made up.

Input

  • 4 seconds × 256 samples/sec → N = 1024 samples
  • First voltage readings: 125.2, 120.9, 117.0, 111.2 … µV

Time-domain equations

Variance \( \sigma^2 = \tfrac1N\sum(x_i-\mu)^2 \)= 4,418.8
Line length \( \sum\lvert x_{i+1}-x_i\rvert \)= 6,786 µV
Energy \( \sum x_i^2 \)= 4,525,327
Skew= −0.242
Kurtosis= −0.411

Frequency-domain equations (band power \( \int S(f)\,df \))

Delta (0.5–4 Hz)39.4% of total power
Theta (4–8 Hz)20.8%
Alpha (8–13 Hz)4.1%
Beta (13–30 Hz)7.8%
Gamma (30–80 Hz)0.6%
Spectral entropy \( -\sum p_f\log_2 p_f \)= 3.502
Peak frequency \( \arg\max S(f) \)= 1.0 Hz

These ~17 numbers (per channel) form the feature vector the Random Forest votes on. Important: on this single window, theta was only ~1.6× background and line length wasn't elevated — no one feature or channel decides it. Only when all features across all channels are combined (where theta averages 6–13× higher) does the seizure clearly separate out. That's exactly why we use a trained model, not a single hard threshold.

How we make the comparison fair

From a detector to a product: the alert tiers

A raw seizure flag is not an action. The goal is a wearable home monitor that loops in the right person at the right urgency. So the detector's two signals — how confident it is and how long the abnormal activity lasts — decide one of three actions:

Dismiss

A brief or low-confidence blip. No action — this absorbs most false alarms so the patient and clinician aren't bothered.

→

Notify clinician

Moderate confidence, or confident but short. Ask a human: "is this a false positive, or are you OK?"

→

Escalate

High confidence and sustained. After a patient non-response window, a real device would contact emergency services.

The two gates we use: notify at probability ≥ 0.50; escalate only when probability ≥ 0.80 and the episode lasts ≥ 20 seconds. Requiring both confidence and persistence is what prevents a single twitchy window from triggering an emergency.

Why this answers the "false positives" problem

On our data, the one false alarm the 8-channel model produced did not become a false emergency — it was downgraded to a "notify the clinician" event, because it wasn't confident-and-sustained enough to escalate. Every real seizure still reached the emergency tier. In other words, we don't chase an impossible zero-false-positive model; we design the system so that imperfect detections fail safe.

This is a demonstration of the decision logic on a small dataset. The prototype never contacts a clinician or emergency services, and an auto-escalating medical alert device would require clinical validation and regulatory clearance before any real use.

What this does and doesn't prove

↑ Back to the Overview