# Annotations to dataframe

**URL:** <https://mne.discourse.group/t/annotations-to-dataframe/6551>\
**Category:** Support & Discussions\
**Created:** [March 16, 2023, 10:30pm UTC](https://mne.discourse.group/t/annotations-to-dataframe/6551 "2023-03-16T22:30:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hanna](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/hanna/32/1835_2.png) [@Hanna](https://mne.discourse.group/u/Hanna)\
**Post date:** [March 16, 2023, 10:30pm UTC](https://mne.discourse.group/t/annotations-to-dataframe/6551/1 "2023-03-16T22:30:05Z")

</div>

Hello guys, I’m annotating my data like the code shows:

```python
my_annot = mne.Annotations(onset=2670, # in seconds
                           duration=170, # in seconds, too
                           description='seizure')
print(my_annot)
raw.set_annotations(my_annot)

```

After a series os preprocessing I transform the data to a pandas dataframe, however the column condition only shows the number one. The annotations indicate seizure and I need them to be correctly labeled for classification purposes.

```python
raw.set_eeg_reference()

raw.filter(1., None)

raw.apply_function (lambda x: x * 1e-6)

raw.pick_channels(['F3-C3','C3-P3', 'F4-C4','C4-P4'])

raw.filter(l_freq=0.5,h_freq=40.0, method='iir', iir_params=iir_params, verbose=True)

epochs=mne.make_fixed_length_epochs(raw, duration=4, overlap=0.5)

epochs=epochs.to_data_frame()

```

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

---

<div class="post-metadata">

**Author:** ![agramfort](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@agramfort](https://mne.discourse.group/u/agramfort)\
**Post date:** [March 17, 2023, 7:32am UTC](https://mne.discourse.group/t/annotations-to-dataframe/6551/2 "2023-03-17T07:32:34Z")

</div>

you don’t pass any event\_id so your condition is the default so 1.

did you mean to use events\_from\_annotations to cut epochs in seizure intervals?

Alex

---

<div class="post-metadata">

**Author:** ![Hanna](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/hanna/32/1835_2.png) [@Hanna](https://mne.discourse.group/u/Hanna)\
**Post date:** [March 17, 2023, 3:27pm UTC](https://mne.discourse.group/t/annotations-to-dataframe/6551/3 "2023-03-17T15:27:34Z")

</div>

So if I pass the event\_id only the epochs “inside” the seizure will be marked as 1?  
I’m using this calculation to know the epochs that seizures are so I manually mark in the column.

```python
epochs['condition'] =0

start_time = 2670 # example seizure start time in seconds
duration = 171 # example seizure duration in seconds
sampling_rate = 256 # EEG data sampling rate in Hz
epoch_len = 4 # epoch length in seconds
overlap = 0.5 # epoch overlap in seconds
epoch_len_samples = epoch_len * sampling_rate # epoch length in data points
overlap_samples = overlap * sampling_rate # epoch overlap in data points
start_sample = int((start_time - overlap) * sampling_rate) # calculate starting sample index
end_sample = int((start_time + duration - overlap) * sampling_rate) # calculate ending sample index
start_epoch = int(start_sample // epoch_len_samples) # calculate starting epoch index
end_epoch = int(end_sample // epoch_len_samples) # calculate ending epoch index
seizure_epochs = list(range(start_epoch, end_epoch + 1)) # create list of epochs that correspond to seizure

seizure_epochs 
[667,
 668,
 669,
 670,
 671,
 672,
 673,
 674,
 675,
 676,
 677,
 678,
 679,
 680,
 681,
 682,
 683,
 684,
 685,
 686,
 687,
 688,
 689,
 690,
 691,
 692,
 693,
 694,
 695,
 696,
 697,
 698,
 699,
 700,
 701,
 702,
 703,
 704,
 705,
 706,
 707,
 708,
 709,
 710]

epochs.loc[(epochs['epoch']>=667) & (epochs['epoch']<=710), 'condition']=1

epochs['condition'].value_counts()

```

---

<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:** [March 22, 2023, 4:28pm UTC](https://mne.discourse.group/t/annotations-to-dataframe/6551/4 "2023-03-22T16:28:34Z")

</div>

Here’s an example that re-codes the equally spaced event codes based on whether they’re not in the seizure annotation, partially overlapping the seizure, or fully within the seizure:

```python
import numpy as np
import mne

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = (sample_data_folder / 'MEG' / 'sample' /
                        'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False, preload=True)
annot = mne.Annotations(onset=[3, 55], duration=[15, 27],
                        description=['seizure'] * 2)
raw.set_annotations(annot)
annot_events, annot_event_id = mne.events_from_annotations(raw)
# get onset and duration in samples
onset_samp = annot_events[:, 0]
duration_samp = np.rint(annot.duration * raw.info['sfreq']).astype(int)
offset_samp = onset_samp + duration_samp

# make equally spaced events
epoch_dur = 4
events = mne.make_fixed_length_events(raw, duration=epoch_dur, overlap=0.5, first_samp=False)
event_samp = events[:, [0]]
epoch_duration_samp = epoch_dur * raw.info['sfreq']

# compute overlap / containment
overlap_with_seizure = np.logical_and(
    (event_samp + epoch_duration_samp) >= onset_samp,
    event_samp <= offset_samp
).any(axis=1)

fully_within_seizure = np.logical_and(
    event_samp >= onset_samp,
    (event_samp + epoch_duration_samp) <= offset_samp
).any(axis=1)

events[overlap_with_seizure, 2] = 2
events[fully_within_seizure, 2] = 3

```

Now you can use that `events` to do your epoching. Condition `1` is “not in seizure”, `2` is partial overlap, and `3` is fully within seizure. Of course you can skip the partial overlap step if you don’t care about those, or skip the “fully within” step if you want to treat partial and full overlap the same.
