# How to get events numbers from raw

**URL:** <https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446>\
**Category:** Support & Discussions\
**Tags:** preprocessing\
**Created:** [August 28, 2023, 4:34am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446 "2023-08-28T04:34:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 28, 2023, 4:34am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/1 "2023-08-28T04:34:00Z")

</div>

Mne automatically finds events in raw data, where the actual event is different from the recorded file, so i’m going to compare it. Is there a way to do it efficiently?

---

<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:** [August 29, 2023, 10:21am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/2 "2023-08-29T10:21:50Z")

</div>

MNE should normally preserve the event codes from your original file. Could you provide more context as to what you’re doing and which kind of data you’re loading?

Richard

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 29, 2023, 10:33am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/3 "2023-08-29T10:33:23Z")

</div>

Aha, what I’m talking about is that the event is calculated when the rawedf function is used, but I wonder if there is a code that can bring only that separately.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 10:45am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/4 "2023-08-29T10:45:40Z")

</div>

This depends on how events are originally stored in the file. If they are available as an analog trigger channel, you need to load the file and parse that channel to get the actual events. If they are stored as discrete events (e.g. annotations in EDF), then you can read these annotations without touching (i.e., loading) the raw (continuous) EEG data.

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 29, 2023, 10:58am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/5 "2023-08-29T10:58:45Z")

</div>

The files I’m using sage have separate raw EDF files and annotation files. Therefore, when using the function, we want to compare event detection in EDF with the number of events in the annotation file.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 11:31am UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/6 "2023-08-29T11:31:29Z")

</div>

OK. But do you know how events are stored in your EDFs? In an analog stim channel? Or as discrete annotations? You can find out by taking a look at the channel types after loading, e.g. by printing `raw.info` (look for channels of type `stim`) and checking if `raw.annotations` contains entries.

I still don’t understand what exactly you would like to do though. Can you show your code so we can see what you have tried?

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 29, 2023, 12:09pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/7 "2023-08-29T12:09:29Z")

</div>

Well, there are two files, x.edf and x\_annotation.edf.

```python
raw = mne.io.read_raw_edf('x.edf', preload=True)

```

Once run the above code and see several outputs, telling how many events there are. But not all, but some of them were detected incorrectly, so I asked because I needed a process to find out.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 12:25pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/8 "2023-08-29T12:25:37Z")

</div>

This line already prints how many events were detected? I think you have some more code that produces the output you are talking about. Can you also show it?

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 29, 2023, 12:39pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/9 "2023-08-29T12:39:13Z")

</div>

So, since there are many edf files, I am trying to compare the original number of events and the event detected by mne from code.

I need to remove the wrongly detected files, but I don’t know where they are hiding in order to detect which ones have the wrong number of events.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 1:07pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/10 "2023-08-29T13:07:05Z")

</div>

I understand that. But I have no idea how you detect events in both types of files (`x.edf` and `x_annotation.edf`). You haven’t shown how you do that yet.

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 29, 2023, 1:21pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/11 "2023-08-29T13:21:32Z")

</div>

The annotation file is also in edf and txt format, so it is labeled for each epoch so that it can be counted. Rather than reading two EDFs with the mne raw function and comparing them, only need to know the number of events in code. That’s the point. don’t need to know anything else.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 1:25pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/12 "2023-08-29T13:25:03Z")

</div>

I’m sorry, I cannot help you with the information you’re sharing here. I understand what you’re trying to do, but I need more details. Maybe you can share two example files?

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 29, 2023, 1:39pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/13 "2023-08-29T13:39:55Z")

</div>

> **[Haaglanden Medisch Centrum sleep staging database v1.1](https://physionet.org/content/hmc-sleep-staging/1.1/)**
>
> A collection of 151 whole-night PolySomnoGraphic (PSG) sleep recordings from the Haaglanden Medisch Centrum (HMC, The Netherlands) sleep center containing different traces of ExG activity and expert's scorings of sleep stages

Hmm… Actually, I don’t think I asked a question that was difficult to understand.

---

<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:** [August 29, 2023, 1:49pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/14 "2023-08-29T13:49:15Z")

</div>

> [@PhD-GOAT](#):
>
> Actually, I don’t think I asked a question that was difficult to understand.

You’re asking questions about very specific problems without providing enough information for anyone else to fully grasp what it is that you want to achieve or what exactly the problem is you’re running into – as indicated by the sheer endless back-and-forth required here. It’s not nice to anyone involved.

Richard

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 2:03pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/15 "2023-08-29T14:03:50Z")

</div>

> [@PhD-GOAT](#):
>
> Hmm… Actually, I don’t think I asked a question that was difficult to understand.

Yes, it is difficult to understand.

You provided the data in question in your seventh post, so now we know what the data actually looks like. Your question would have been easier to understand if you had done that in your first post. Now please share your code that demonstrates the issue you are seeing using `SN001`, namely a discrepancy in the number of events/annotations between the EDF file and the TXT file. Again, you should have done that in your first post if you wanted your question to be easy to understand.

---

<div class="post-metadata">

**Author:** ![cbrnr](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/cbrnr/32/1409_2.png) [@cbrnr](https://mne.discourse.group/u/cbrnr)\
**Post date:** [August 29, 2023, 2:19pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/16 "2023-08-29T14:19:21Z")

</div>

Still not sure what your question is, but could it be that you want to load an EDF file that contains only annotations? When loading `SN001_sleepscoring.edf` with `mne.io.read_raw("SN001_sleepscoring.edf")`, you get the following information as part of the output:

> It is possible that this file only contains annotations and no signals. In that case, please use mne.read\_annotations() to load these annotations.

Therefore, you can load the file as suggested:

```python
import mne
annotations = mne.read_annotations("SN001_sleepscoring.edf")

```

You can use pandas to read the text file:

```python
import pandas as pd
df = pd.read_csv("SN001_sleepscoring.txt")

```

They both contain 856 annotations:

```python
>>> len(annotations)
856
>>> df.shape
(856, 6)

```

---

<div class="post-metadata">

**Author:** ![PhD-GOAT](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/phd-goat/32/1326_2.png) [@PhD-GOAT](https://mne.discourse.group/u/PhD-GOAT)\
**Post date:** [August 30, 2023, 1:09pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/17 "2023-08-30T13:09:32Z")

</div>

You still don’t understand the question. If the question is a, the answer to a’ must be provided, but you are asking for information about the situation about b or c.

I was clearly asking how to get the event number that appears when running rawedf, and you answer by getting the event number from another annotation file. This is the answer to b or c. Still not sure what the intention is?

I also compared the numbers of all edf files and all annotation edf or txt files in a similar way, and several files did not match. So, since it takes time to compare them one by one, I asked a question using code to find records with different event numbers.

---

<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:** [August 30, 2023, 1:27pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/18 "2023-08-30T13:27:38Z")

</div>

> [@PhD-GOAT](#):
>
> You still don’t understand the question. If the question is a, the answer to a’ must be provided, but you are asking for information about the situation about b or c.

We are simply not understanding the question because it is poorly described, _without a single code snippet_ that we can use to try to figure out your specific problem. Several post ago, code snippets of what you were doing were requested.

> This line already prints how many events were detected? I think you have some more code that produces the output you are talking about. Can you also show it?

We are not magician, we can’t read your mind, so please give us something that we can use to figure out the issue. Ideally, a minimally working example (MWE), i.e., a code snippet that we can directly copy/paste and execute and that will clearly exhibit the error/problem encounter.

* * *

> get the event number that appears when running rawedf

Again, how are the events stored in that particular file? Trigger channel or annotations?

Mathieu

---

<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:** [August 30, 2023, 7:47pm UTC](https://mne.discourse.group/t/how-to-get-events-numbers-from-raw/7446/19 "2023-08-30T19:47:55Z")

</div>

> [@PhD-GOAT](#):
>
> You still don’t understand the question. If the question is a, the answer to a’ must be provided, but you are asking for information about the situation about b or c.

I’ll just have you know that this behavior of yours is downright outrageous and disrespectful, and I won’t be engaging in any conversation with you any longer unless your demeanor changes.

Richard
