# How to recognize organism state via EEG?

**URL:** https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898
**Category:** Support & Discussions
**Tags:** preprocessing, eeg, visualization
**Created:** [June 3, 2024, 6:49pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898 "2024-06-03T18:49:24Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 3, 2024, 6:49pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/1 "2024-06-03T18:49:24Z")

</div>

I’m still new in EEG, and I need advice about the strategy of working with available data. The main idea is to recognize whether a person is tired or not. My way of thinking was started from setting max and min frequency filters 8-15Hz because as I read the best for drowsiness detection is alpha waves. Then I applied ICA filters, which as personally I understood required for removing artifacts like eye blinking and so on (because we don’t have any other channels for the file only EEG). And now I’m not sure what to do then.

In the end I’m trying to understand whether a person is tired or now via EEG, so as I read from some papers I will need to classify my features of EEG, or what? Below you can see script which shows some of my results:

```python
data_path = './'
vhdr_file_base_1 = os.path.join(data_path, 'S01_exp.vhdr')
raw = mne.io.read_raw_brainvision(vhdr_file_base_1, preload=True) # read raw data with mne
info = raw.info

raw.plot(clipping=None, scalings='auto')

raw.load_data()

montage = mne.channels.make_standard_montage("biosemi32")
for ch in range(0, 32):
    mne.rename_channels(info, {info["ch_names"][ch]: montage.ch_names[ch]})

raw.set_montage('biosemi32', on_missing='ignore')

raw.filter(l_freq=1, h_freq=None) # High-pass filter above 1 Hz
raw.filter(l_freq=None, h_freq=40) # Low-pass filter below 40 Hz
raw.filter(l_freq=8, h_freq=30) # Band-pass filter between 8 and 30 Hz
raw.notch_filter(freqs=50) # Notch filter at 50 Hz (power line noise)

# The regression technique works regardless of chosen reference. However, it is
# important to choose a reference before proceeding with the analysis.
raw.set_eeg_reference("average")

# Removing slow drifts makes for more stable regression coefficients. Make sure
# to apply the same filter to both EEG and EOG channels!
raw.filter(0.3, 40)

# # set up and fit the ICA
ica = mne.preprocessing.ICA(n_components=20, random_state=97, max_iter=800)
ica.fit(raw)
ica.exclude = [1, 2] # details on how we picked these are omitted here

raw.plot()
raw.compute_psd(fmax=50).plot(picks="eeg", exclude="bads", amplitude=False)
raw.plot_psd()
raw.plot(duration=5, n_channels=30)
ica.plot_properties(raw, picks=ica.exclude)

method = 'fastica'

# Choose other parameters
n_components = 25 # if float, select n_components by explained variance of PCA
decim = 3 # we need sufficient statistics, not all time points -> saves time
random_state = 23
ica = ICA(n_components=n_components, method=method, random_state=random_state)
print(ica)
reject = dict(mag=5e-12, grad=4000e-13)

picks_meg = mne.pick_types(raw.info, meg=False, eeg=True, eog=False,
                           stim=False, exclude='bads')
ica.fit(raw, picks=picks_meg, decim=decim, reject=reject)
print(ica)

ica.plot_components()
ica.plot_sources(raw)

```

But maybe I’m wrong. In general the another idea was to convert the raw EEG data to numpy and compare with other available EEG files after the experiments, with possible analysis of alpha waves, but I’m not sure that this is very promissing direction.

It will be good when I will just have some understanding what to do next and the strategy in general, because now I’m little bit confused 😃 and also not sure whether all what I had done and think now are correct 🙂

---

<div class="post-metadata">

### Author: ![CarinaFo](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/carinafo/32/746_2.png) [@CarinaFo](https://mne.discourse.group/u/CarinaFo)
#### Post date: [June 4, 2024, 1:38pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/2 "2024-06-04T13:38:30Z")

</div>

Hello, how long is your recording? Do you have events or did you record resting-state EEG? If you have no events, you could look at alpha power over time and based on the [power](https://mne.tools/1.6/auto_tutorials/time-freq/20_sensors_time_frequency.html#sphx-glr-auto-tutorials-time-freq-20-sensors-time-frequency-py) value (higher alpha power =\> participants more tired) classify sleepiness in individual participants over time. Maybe tutorials dealing with [sleep staging](https://mne.tools/1.6/auto_tutorials/clinical/60_sleep.html) might also be helpful.

Best,

Carina

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 4, 2024, 2:01pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/3 "2024-06-04T14:01:33Z")

</div>

Hi, thank you for your response. The length of recording is about 2-2.5 minutes. Yes, I have resting-state EEG, but no events. Just EEG and that is all. I checked the power tutorial, so it’s possible as I see to handle only alpha power as I see what will the possible solution for me, right?

Best,  
Andrii

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 9:55am UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/4 "2024-06-05T09:55:30Z")

</div>

also when I’m trying to get events like below:  
`events = mne.events_from_annotations(raw)`

and then from mne tutorial:

```python
# picks MEG gradiometers
picks = mne.pick_types(raw.info, meg="grad", eeg=False, eog=True, stim=False)

# Construct Epochs
event_id, tmin, tmax = 1, -1.0, 3.0
baseline = (None, 0)
epochs = mne.Epochs(raw, events, tmin=-0.3, tmax=0.7)

epochs.compute_psd(fmin=2.0, fmax=40.0).plot(
    average=True, amplitude=False, picks="data", exclude="bads"
)

epochs.compute_psd().plot_topomap(ch_type="grad", normalize=False, contours=0)

```

it gives error:  
`TypeError: events should be a NumPy array of integers, got <class 'tuple'>`

but I don’t have any events, so maybe it’s possible to work with epochs without events?

---

<div class="post-metadata">

### Author: ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)
#### Post date: [June 5, 2024, 10:45am UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/5 "2024-06-05T10:45:29Z")

</div>

> [@AndrewG0R](#):
>
> it gives error:  
> `TypeError: events should be a NumPy array of integers, got <class 'tuple'>`

According to the [documentation](https://mne.tools/stable/generated/mne.events_from_annotations.html#mne.events_from_annotations), `events_from_annotations()` returns a tuple `(events, event_id)`. So you will want to change your call of the function to:

```python
events, event_id = mne.events_from_annotations(raw)

```

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 11:01am UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/6 "2024-06-05T11:01:42Z")

</div>

> [@richard](#):
>
> `events, event_id = mne.events_from_annotations(raw)`

thank you for you help, but it still does not have ability to compute psd because of error:

> ValueError: zero-size array to reduction operation minimum which has no identity

but as I see I have some values in epochs, or I’m wrong with it:

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/2/23e7a0c3764c2eecfede4f27552ae6ff7136b08d.png)

---

<div class="post-metadata">

### Author: ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)
#### Post date: [June 5, 2024, 11:08am UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/7 "2024-06-05T11:08:07Z")

</div>

You only have one epoch… i don’t know if this is actually what you want. And I would assume that this one even gets dropped when you call `epochs.load_data()` as it’s just the marker at the very start of the recording, so MNE will probably not be able to find a baseline signal.

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 11:29am UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/8 "2024-06-05T11:29:01Z")

</div>

in general I don’t have any stimuli inside the eeg, just plain recording during some time and that is all. Because of so small amount of features I asked here what to do and how to recognize states of the body. And now after some more context from @CarinaFo and you, I’m trying to build some plots or analyze values of alpha power during the record.

---

<div class="post-metadata">

### Author: ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)
#### Post date: [June 5, 2024, 12:37pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/9 "2024-06-05T12:37:25Z")

</div>

You could create rolling window epochs with `mne.make_fixed_length_epochs`.

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 12:41pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/10 "2024-06-05T12:41:37Z")

</div>

it should be done before plotting or at which place?

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 1:02pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/11 "2024-06-05T13:02:49Z")

</div>

> [@mscheltienne](#):
>
> `make_fixed_length_epochs`

I did the current window, but now when I use such code:

```python
epochs = mne.make_fixed_length_epochs(raw)

epochs.compute_psd(fmin=2.0, fmax=40.0).plot(
    average=True, amplitude=False, picks="data", exclude="bads"
)

epochs.compute_psd().plot_topomap(normalize=False, contours=0)

```

with several different EEG files from the experiments, it usually gives the similar results:

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/1/1f771b25316ed2d64cbb1dd26e1c3e735f8b64fa.png)

even when between two files was 4-5 hours between measurements, or it’s ok?

---

<div class="post-metadata">

### Author: ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)
#### Post date: [June 5, 2024, 2:28pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/12 "2024-06-05T14:28:24Z")

</div>

This looks fine to me, what’s your concern here?

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 3:36pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/13 "2024-06-05T15:36:10Z")

</div>

I’m trying to understand where to see difference between two similar plots and for example to say that we have wrong body state ☹

---

<div class="post-metadata">

### Author: ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)
#### Post date: [June 5, 2024, 3:44pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/14 "2024-06-05T15:44:48Z")

</div>

I don’t think you’ll be able to see that with your eyes. You’ll need to run some statistics or machine learning model. I’d suggest checking the literature to learn how others have done it.

Good luck,  
Richard

---

<div class="post-metadata">

### Author: ![AndrewG0R](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/andrewg0r/32/3697_2.png) [@AndrewG0R](https://mne.discourse.group/u/AndrewG0R)
#### Post date: [June 5, 2024, 5:13pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/15 "2024-06-05T17:13:57Z")

</div>

thank you for your response, in general I thought it would be visually visible via plot, but now I understand that better run some pipeline for further classification. I will try to continue, in any case I will usually able to ask here for better search direction for such newbie people like me 😃 Thank you and have a nice day!

Andrii

---

<div class="post-metadata">

### Author: ![system](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/85cc6bd2b69cb698a166dc6d880fb550510d0144.jpeg) [@system](https://mne.discourse.group/u/system)
#### Post date: [June 12, 2024, 5:14pm UTC](https://mne.discourse.group/t/how-to-recognize-organism-state-via-eeg/8898/16 "2024-06-12T17:14:39Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
