# can't read annotations

**URL:** <https://mne.discourse.group/t/cant-read-annotations/4131>\
**Category:** Support & Discussions\
**Tags:** preprocessing, eeg, epochs, annotations\
**Created:** [December 14, 2021, 8:22am UTC](https://mne.discourse.group/t/cant-read-annotations/4131 "2021-12-14T08:22:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![6aghayeq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/6aghayeq/32/1056_2.png) [@6aghayeq](https://mne.discourse.group/u/6aghayeq)\
**Post date:** [December 14, 2021, 8:22am UTC](https://mne.discourse.group/t/cant-read-annotations/4131/1 "2021-12-14T08:22:14Z")

</div>

Hi everybody. I have annotations for EEG signal in .txt file. The signals annotate every 30-s with a sampling rate of 250. I extracted sample and Aux columns. Then I extracted the starting and ending sample of each event (‘H’, 'HA) to segment the signal. I used pandas to do that and then converted the dataframe to .txt file. Now I’m trying to read it by mne.read\_annotations which can read .txt files, but it doesn’t work. What is the problem? Is there any other way to find events and epoch the signal?  
I hope that I provide all the required informations. If not, please tell me to add more. Thank you.

By the way, I’m working on MIT-BIH Polysomnographic Database. If someone worked on it before, I’d appreciate it if help me to preprocess the signal.

- MNE version: 0.24.1
- operating system: Google Colab

---

<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:** [December 14, 2021, 8:29am UTC](https://mne.discourse.group/t/cant-read-annotations/4131/2 "2021-12-14T08:29:50Z")

</div>

> [@6aghayeq](#):
>
> By the way, I’m working on MIT-BIH Polysomnographic Database. If someone worked on it before, I’d appreciate it if help me to preprocess the signal.

You might want to check out [SleepECG](https://github.com/cbrnr/sleepecg) – we’re currently working on providing readers for various sleep database records, and it could be that the record you want to load is already implemented.

If this does not work, then the format of your .txt file is probably not what `mne.read_annotations` expects. You’d have to provide more details on how your file is structured, and what the exact error message is.

---

<div class="post-metadata">

**Author:** ![6aghayeq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/6aghayeq/32/1056_2.png) [@6aghayeq](https://mne.discourse.group/u/6aghayeq)\
**Post date:** [December 15, 2021, 8:21am UTC](https://mne.discourse.group/t/cant-read-annotations/4131/3 "2021-12-15T08:21:38Z")

</div>

Thank you Clemens. I checked the link but I didn’t find the records I was looking for. The data I’m working on is: [slpdb](https://archive.physionet.org/physiobank/database/slpdb/)

The .txt file I extracted is like this:

```
index Sample # Aux                  
 12 90000 3 HA
 13 97500 3 H LA
 14 105000         
 36 270000 4 LA HA
 37 277500         
 46 345000 2 H
 47 352500         
 49 367500 2 H
 50 375000         
 163 1222500 2 HA
 164 1230000         
 167 1252500 2 H LA
 168 1260000         
 174 1305000 2 H
 175 1312500         
 179 1342500 2 HA
 180 1350000 2 LA HA
 181 1357500 2 H LA
 182 1365000         
 185 1387500 2 LA H
 186 1395000         
 187 1402500 2 H
 188 1410000 3 H
 189 1417500 3 HA
 ... ... ...

```

And the error is:

ValueError: Invalid value for the ‘text header’ parameter. Allowed values are 3, and 4, but got 58 instead.

---

<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:** [December 15, 2021, 8:53am UTC](https://mne.discourse.group/t/cant-read-annotations/4131/4 "2021-12-15T08:53:40Z")

</div>

Actually, there is [`sleepecg.io.read_slpdb()`](https://github.com/cbrnr/sleepecg/blob/main/sleepecg/io/sleep_readers.py#L261), which I think reads the data you want.

---

<div class="post-metadata">

**Author:** ![6aghayeq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/6aghayeq/32/1056_2.png) [@6aghayeq](https://mne.discourse.group/u/6aghayeq)\
**Post date:** [December 15, 2021, 4:27pm UTC](https://mne.discourse.group/t/cant-read-annotations/4131/5 "2021-12-15T16:27:45Z")

</div>

Can I use it in Google Colab? I tried to use it but I couldn’t.

---

<div class="post-metadata">

**Author:** ![hofaflo](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/hofaflo/32/1061_2.png) [@hofaflo](https://mne.discourse.group/u/hofaflo)\
**Post date:** [December 15, 2021, 9:59pm UTC](https://mne.discourse.group/t/cant-read-annotations/4131/6 "2021-12-15T21:59:13Z")

</div>

Hi!

The [wfdb](https://pypi.org/project/wfdb/) package allows to read record and annotation files from PhysioNet. This minimal example shows how you could access annotations and annotation times. It requires the `.hea`,`.dat` and `.st` files for the desired record.

```python
import wfdb
import mne

# change the path if necessary
# note that this path does _not_ include any file extension, as it's added automatically by `wfdb.rdrecord`
record_path = '../../datasets/slpdb/slp01a'

record = wfdb.rdrecord(record_path)
annotation = wfdb.rdann(record_path, 'st')

# annotation.sample contains the annotation's sample indices
# annotation.aux_note contains sleep stage, apnea and movement information
# slpdb doesn't provide annotation durations, so let's assume 30 seconds
annotation_mne = mne.Annotations(
    onset=annotation.sample / record.fs,
    duration=30,
    description=annotation.aux_note
)

```

---

<div class="post-metadata">

**Author:** ![6aghayeq](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/6aghayeq/32/1056_2.png) [@6aghayeq](https://mne.discourse.group/u/6aghayeq)\
**Post date:** [December 17, 2021, 7:53pm UTC](https://mne.discourse.group/t/cant-read-annotations/4131/7 "2021-12-17T19:53:19Z")

</div>

I read the annotations successfully. Thank you so much Florian!

---

<div class="post-metadata">

**Author:** ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)\
**Post date:** [August 23, 2023, 3:34pm UTC](https://mne.discourse.group/t/cant-read-annotations/4131/8 "2023-08-23T15:34:16Z")

</div>

Split this topic on August 23, 2023, 3:34pm UTC

A post was split to a new topic: [help with sleepecg package](https://mne.discourse.group/t/help-with-sleepecg-package/7424)
