question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DOC: metadata must have the same number of rows as events

See original GitHub issue

I am trying to create epochs with metadata from raw, but run into an error with a piece of code that worked fine about 2 months ago (some previous version, likely 0.20.dev). I updated to the latest master.

I want to use a subset of events of interest, while my raw data contains more events. Creating epochs without metadata works fine.

(I tried to comment out the size check in _check_metadata, but it breaks later then.)

File “/home/sh254795/anaconda3/lib/python3.7/site-packages/mne/utils/mixin.py”, line 358, in _check_metadata % (len(metadata), len(self.events)))

ValueError: metadata must have the same number of rows (38) as events (86)

Have there been any changes to this code that could explain the error?

Example:

import os
import numpy as np
import mne
import pandas as pd

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample',
                                    'sample_audvis_raw.fif')
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False)
raw.crop(tmax=60).load_data()

# find all events
events = mne.find_events(raw, stim_channel='STI 014')

# define events of interest 
event_id = dict(A=4, B=3)

tmin = -0.1
tmax = 0.5

#%% create metadata for the events of interest

# count events of interest 
n_events = sum(events[:,2] == 3) + sum(events[:,2] == 4)

# create arbitrary metadata
METADATA = np.arange(0,n_events)
 
META_DF = pd.DataFrame(data=METADATA, columns = ['trial'])
   
# Epoch the data without metadata - FINE
print('creating epochs...')
epochs = mne.Epochs(raw, events, event_id, tmin, tmax)
      

# Epoch the data with metadata - BREAKS
print('creating epochs...')
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, metadata=META_DF)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
SophieHerbstcommented, Oct 13, 2020

Yes, thanks! My point is more that metadata is a super useful feature, which we use more and more, but it is quite difficult to implement for people less proficient with python. Hence, one nice complete tutorial where the above points are made explicit would be great.

1reaction
larsonercommented, Oct 13, 2020

So if the metadata has to match the length of selected events, this would require to create metadata for events for which it is not needed. Especially tricky if metadata are loaded from a separate file which contains behavioral responses. I am afraid that this would increase the difficulty of working with metadata.

I would suggest you trim your events to the ones you plan on using. This could just be a quick mask line like:

events = events[np.in1d(events[:, 2], list(event_id.values())]
Read more comments on GitHub >

github_iconTop Results From Across the Web

mne-python/epochs.py at main - GitHub
If duplicate time samples have same event val, "merge" == "drop". # and no new event_id key ... 'The number of epochs and...
Read more >
Recording with too little events - MNE Forum - Discourse
I'm now getting this error message → ValueError: metadata must have the same number of rows (176) as events (170). I had one...
Read more >
Use content types to manage content consistently on a site
Applying consistent metadata across multiple document libraries is only one benefit of using content types to manage your content.
Read more >
Metadata Vocabulary for Tabular Data - W3C
Tabular data MUST conform to the description from [ tabular-data-model ]. In particular note that each row MUST contain the same number of ......
Read more >
Events and streams | Datastream | Google Cloud
Streams, objects, and events have data and metadata associated with them. ... This number is used with rs_id and uniquely identifies a row...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found