# HTM and automatic seizure detection using EEG data

**URL:** <https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917>\
**Category:** NuPIC\
**Created:** [February 16, 2017, 10:31am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917 "2017-02-16T10:31:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![WoongWonLee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/woongwonlee/32/1106_2.png) [@WoongWonLee](https://discourse.numenta.org/u/WoongWonLee)\
**Post date:** [February 16, 2017, 10:31am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/1 "2017-02-16T10:31:09Z")

</div>

Hi, Matt taylor

First of all, I am really appreciated for your lecture (HTM School). It helped me a lot.  
I want to use HTM for automatic seizure detection using EEG data.  
and there are some projects dealing with EEG data.

But I want to use not a single channel. To detect seizure, I have to use  
many channels and I am going to use not a raw data but features include Power spectral density.

I don’t know how to use HTM for these. Do I have to use HTM regions for each features?  
Then I need hundreds of regions…  
Is there any encoder to dear with these input datas?

Thank you!

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [February 16, 2017, 4:03pm UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/2 "2017-02-16T16:03:49Z")

</div>



---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [February 16, 2017, 4:04pm UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/3 "2017-02-16T16:04:36Z")

</div>

I highly suggest you chat with @marion about her work with signal analysis and sequence classification with NuPIC. Feel free to have this conversation on the #nupic forum for others to join.

---

<div class="post-metadata">

**Author:** ![WoongWonLee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/woongwonlee/32/1106_2.png) [@WoongWonLee](https://discourse.numenta.org/u/WoongWonLee)\
**Post date:** [February 17, 2017, 2:36am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/5 "2017-02-17T02:36:29Z")

</div>

Hi @marion! It is very nice to talk with you!

I have already looked your presentation wich using EEG data to control quadrotors.  
And also I have looked your github.

> **[marionleborgne/nupic.eeg](https://github.com/marionleborgne/nupic.eeg)**
>
> EEG data Classification & Anomaly Detection with NuPIC - marionleborgne/nupic.eeg

I am using EEG data but the objective of using HTM is different.  
My objective is predicting seizure in advance and finding the location of seizure which is represented as channels. To detect seizure accurately, I have to use multi-channels and the features of multi-channels make contexts about brain activity.

The features I will use is power spectral density(PSD) of each channels with window. The relative power between channels and frequency bands. Because before seizure, the energy of low frequency bands moves to high frequency bands, so these information looks important to predict seizure.

The problem is that I don’t know how to use HTM for these. Do I have to use HTM regions for each features? or aggregate these features into one encoder(maybe scalar encoder)? Is there any encoder to dear with these input data?

I am still thinking about how to detect the location of seizure… and how to make robust detector that can be used to many patient.

Thank you!!

---

<div class="post-metadata">

**Author:** ![marion](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/marion/32/491_2.png) [@marion](https://discourse.numenta.org/u/marion)\
**Post date:** [February 17, 2017, 6:46am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/6 "2017-02-17T06:46:19Z")

</div>

Hi @WoongWonLee,

My recommendation would be to create an HTM model (region) per channel and then use a multi-encoder that is setup in the following manner:

- Chunk up your FFT or PSD in a couple of of frequency buckets
- Feed each frequency bucket value to a [scalar encoder](https://github.com/numenta/nupic/wiki/Encoders)
- Feed the output of all these scalar encoders to a [MultiEncoder](http://numenta.github.io/nupic/classnupic_1_1encoders_1_1multi_1_1_multi_encoder.html)

Then you can feed the output of the multi-encoder to the SP + TM. Repeat the process for each channel.

The setup of the multi-encoder is similar to what is described in the project [ECG+HTM](https://devpost.com/software/ecg-htm) project. I recommend using the multi-encoder since the vector encoder described in the project is now deprecated in NuPIC.

 ![](https://canada1.discourse-cdn.com/flex030/uploads/numenta/original/2X/c/ceb50b1365218ad8c3bdcad89bc660194b68a106.png)

Once you have a model (encoders + SP + TM) setup for for the output of each channel, you can look at various outputs like the raw anomaly score, anomaly likelihood or classification of Temporal Memory cell states (the [SDRClassifier](https://github.com/numenta/nupic/blob/master/src/nupic/algorithms/sdr_classifier.py) does that for example).

I am not sure if this will actually work for your use case, but one idea could be to look at the anomaly likelihood of each model (one model per channel) and see which ones appear anomalous. This might help you locating the channel position of the anomaly (seizure). I am not very knowledgeable about seizure detection in EEG so I am not sure if this part will work or if it is even relevant to your use case.

More info about the project that I was talking about:

- Description: [https://devpost.com/software/ecg-htm](https://devpost.com/software/ecg-htm)
- There is an overview video and the different encoding strategies are described at [3:17](https://youtu.be/VOczYasynUU?t=197).

I hope this information is useful.

Marion

---

<div class="post-metadata">

**Author:** ![WoongWonLee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/woongwonlee/32/1106_2.png) [@WoongWonLee](https://discourse.numenta.org/u/WoongWonLee)\
**Post date:** [February 17, 2017, 9:19am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/7 "2017-02-17T09:19:10Z")

</div>

Thank you @marion

I am planning to use wavelet data first. As you said, I will use scalar encoders for each frequency and aggregate the outputs with a single multi-encoder.

I know this is hard problem to solve… there is no specific pattern for seizure and I think HTM is most accurate predictor for the seizure.

I will share the results to HTM forum!

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [February 17, 2017, 2:37pm UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/8 "2017-02-17T14:37:49Z")

</div>

Do you guys mind if I make this private message into a public forum post? I think others will be interested.

---

<div class="post-metadata">

**Author:** ![WoongWonLee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/woongwonlee/32/1106_2.png) [@WoongWonLee](https://discourse.numenta.org/u/WoongWonLee)\
**Post date:** [February 17, 2017, 3:34pm UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/9 "2017-02-17T15:34:49Z")

</div>

Oh I will be pleased if you make this public!

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [February 17, 2017, 3:59pm UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/10 "2017-02-17T15:59:05Z")

</div>



---

<div class="post-metadata">

**Author:** ![WoongWonLee](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/woongwonlee/32/1106_2.png) [@WoongWonLee](https://discourse.numenta.org/u/WoongWonLee)\
**Post date:** [February 22, 2017, 5:19am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/11 "2017-02-22T05:19:20Z")

</div>

I have two questions

**1.**  
I have a question about multi-encoder.  
Can you show me some examples using multi-encoder?  
I don’t know how to use it.  
I have set my multi-encoder in modelParams.py like this way.

> “encoders”: {  
> “EEG”: {  
> “fieldname”: “EEG”,  
> “type”: “MultiEncoder”,  
> “encoderDescriptions”: {  
> “d3”: {  
> “resolution”: 1,  
> “fieldname”: “d3”,  
> “name”: “d3”,  
> “w”: 21,  
> “type”: “RandomDistributedScalarEncoder”  
> },  
> “d4”: {  
> “resolution”: 1,  
> “fieldname”: “d4”,  
> “name”: “d4”,  
> “w”: 21,  
> “type”: “RandomDistributedScalarEncoder”  
> }

And I run model like this

> result = model.run({“EEG” : {“d3”: d3, “d4”:d4}})

But I have an error like this…

> self.\_classifierInputEncoder = encoderList[self.\_predictedFieldIdx]  
> TypeError: list indices must be integers, not NoneType

I don’t know how to fix this

**2.**  
What I want to do is predicting seizure.  
There is pattern right before seizure. In the picture below, the red rectangular represents the preceding pattern of seizure. It is raw EEG data. I want to detect this.

 ![](https://canada1.discourse-cdn.com/flex030/uploads/numenta/original/2X/2/255fd49ea3ccdab434c5b1958319e3da2f04d72f.png)

So I exported short data which include the seizure and the preceding pattern and trained the HTM model with this data. Next, I turned the training off and adjust the HTM model to original data(full length).

I expected the anomaly score is high in the normal state in the data and low in seizure state. But the result was mass… It was not working like that… Below is anomaly score of the EEG data(one channel) and red rectangular is the preceding pattern of seizure

 ![](https://canada1.discourse-cdn.com/flex030/uploads/numenta/original/2X/8/8da0d69a89acc7023c4c855d25c8f3cb5edb5691.png)

I can achieve my goal? Training the model for the preceding pattern before seizure and recognizing the pattern in any eeg data is wrong way to do this?

---

<div class="post-metadata">

**Author:** ![sheiser1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/sheiser1/32/766_2.png) [@sheiser1](https://discourse.numenta.org/u/sheiser1)\
**Post date:** [February 23, 2017, 12:17am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/12 "2017-02-23T00:17:42Z")

</div>

Hi @WoongWonLee,

First of all here is an example of a params file with multiple inputs:  
[http://pastebin.com/NuAFN24c](http://pastebin.com/NuAFN24c)  
Each of the lines starting with **u’** is another field, so just add another section like that for each input field you want to include (starting with the clipInput = True down until w = 21).

Here is the [run.py](http://run.py) file I use to run it:  
[http://pastebin.com/GT7SpLkG](http://pastebin.com/GT7SpLkG)

From the raw EEG data you showed that looks like a single metric. It’s always ideal to keep it to a single metric if possible, and at least worth trying to begin with. From the look of that data, if you feed that metric right into NuPIC it would find that area you boxed in red as anomalous.

If you have enough data from the subject you’re trying to detect seizures in I don’t think you’d have to turn off learning. The algorithm learns continuously, so when the EEG values are near 0 as in the beginning it’ll learn that, then when it spikes up it will adjust to that and stop finding it as anomalous, and then when it returns to calm near 0 after it will adjust and get used to that again.

I would guess that the majority of the time the data will look calm like it does in the beginning, since people aren’t having seizures most of the time. If that’s the case NuPIC will be used to calmness and surprised by the seizure spikes. I’m just saying that you may have success with a simpler approach of one metric and learning continuously (without turning off training). If you don’t have enough data of the calmer non-seizure times that’s when I’d recommend turning off training after the normal times, to ensure it’ll be surprised by the seizure spikey times.

These are just my basic intuitions and could not be correct, hope it helps.

– Sam

---

<div class="post-metadata">

**Author:** ![sheiser1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/sheiser1/32/766_2.png) [@sheiser1](https://discourse.numenta.org/u/sheiser1)\
**Post date:** [February 23, 2017, 12:18am UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/13 "2017-02-23T00:18:28Z")

</div>

Also I’d recommend look at the anomaly likelihood, which normalizes the anomaly score for noisier, less predictable data sets.

---

<div class="post-metadata">

**Author:** ![SimoneM](https://avatars.discourse-cdn.com/v4/letter/s/8edcca/32.png) [@SimoneM](https://discourse.numenta.org/u/SimoneM)\
**Post date:** [October 14, 2018, 5:03pm UTC](https://discourse.numenta.org/t/htm-and-automatic-seizure-detection-using-eeg-data/1917/14 "2018-10-14T17:03:01Z")

</div>

Hi, I want to try the some idea, do you have any good or bad results?
