# How to use equalize\_event\_counts() combined with metadata instead of event\_ids

**URL:** <https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999>\
**Category:** Support & Discussions\
**Tags:** epochs\
**Created:** [April 17, 2021, 6:59am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999 "2021-04-17T06:59:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [April 17, 2021, 6:59am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/1 "2021-04-17T06:59:08Z")

</div>

- MNE-Python version: 0.23.dev
- operating system: linux

Hey! guys,

I’m wondering whether the function `equalize_event_counts (event_ids , method='mintime')` can be used combined with metadata instead of event\_ids.  
In my situation, I have metadata like this:

 ![image](https://global.discourse-cdn.com/free1/uploads/mne/original/1X/da68ea28910c484bf5df706803c47d12b7802315.jpeg)  
I hope that the numbers of epochs of each condition can be equal, is there a way to achieve this based on my metadata?

Thanks a lot!  
Best

---

<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:** [April 17, 2021, 7:26am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/2 "2021-04-17T07:26:03Z")

</div>

Hello @YuZhou, this is an interesting and very valid question that recently came up in discussions with @virvw and @sophie as well. The short answer is, we currently support doing this, but it’s on our todo list. We’ll hopefully have something ready within the next two weeks, but I cannot make any promises here. I’ll open an issue on GitHub with this feature request.

Edit: The issue can be found here:

> <https://github.com/mne-tools/mne-python/issues/9310>
>
> This was brought up in https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/

---

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [April 17, 2021, 7:46am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/3 "2021-04-17T07:46:50Z")

</div>

Hi! Richard,  
Thanks a lot for your reply 😊 Looking forward to the implementation of the new feature. You guys are really really cool to develop such a handy and excellent tool 👍 👍 👍 👍 👍  
By the way, if this new feature is added, will it be updated to version 0.23.dev0 or other versions？

---

<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:** [April 17, 2021, 7:53am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/4 "2021-04-17T07:53:26Z")

</div>

We’ll need to see what will need to be changed to get this to work. Typically, we’ll add new features for new releases only (in this case, this would probably be the upcoming 0.23.0); although sometimes we also backport small and important enhancements to the current “stable” tree. It really depends on how drastic the changes are, and I cannot tell you that with certainty now for this feature. Although my gut feeling is it will only land in 0.23 and not be backported to 0.22, but who knows.

And yes, once this is implemented, it will be merged into the current development tree, i.e., 0.23.dev0.

---

<div class="post-metadata">

**Author:** ![YuZhou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/yuzhou/32/76_2.png) [@YuZhou](https://mne.discourse.group/u/YuZhou)\
**Post date:** [April 17, 2021, 8:32am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/5 "2021-04-17T08:32:12Z")

</div>

Great! 😁Actually, that’s exactly what I’m hoping, because the version I’m using is 0.23.dev0. So if this feature is added, what I need to do is to type this in my terminal in the mne environment, right?

```python
$ pip install --upgrade --no-deps https://github.com/mne-tools/mne-python/archive/main.zip

```

---

<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:** [April 17, 2021, 8:41am UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/6 "2021-04-17T08:41:12Z")

</div>

Yes that’s one way to update to the latest development version.

---

<div class="post-metadata">

**Author:** ![bissera](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/bissera/32/1962_2.png) [@bissera](https://mne.discourse.group/u/bissera)\
**Post date:** [November 20, 2023, 12:39pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/7 "2023-11-20T12:39:53Z")

</div>

Hi @richard ,

i see this issue is supposed to be solved, but i have the same problem. i’m using mne 1.2.3., macOS-13.5.2-x86\_64-i386-64bit and Python 3.9.15.

For me the problem is that SOMEtimes `epochs.equalize_event_counts( [condition1, condition2)]` works, and sometimes it doesn’t. It does this in the same project, so the same version of MNE and everything else. When it doesn’t, the error is `"KeyError: "adj/dist not found in the epoch object's event_id."` (adj/dist is the string in the variable condition1 and corresponds to the event metadata). However, when it gives this error, and just one line below in the notebook i ask it to find epochs[condition1] it finds it ok. Any advice on how i should address this because i have many many event codes associated with condition1 (adj/dist) and it would be annoying to have to use the codes instead of just the annotation keywords…

Thanks (:  
bissera

---

<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:** [November 20, 2023, 1:36pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/8 "2023-11-20T13:36:34Z")

</div>

Hello @bissera, unfortunately it seems that this feature still hasn’t been implemented:

> <https://github.com/mne-tools/mne-python/issues/9310>
>
> This was brought up by @Zhouyu0206 in https://mne.discourse.group/t/how-to-use-e…qualize-event-counts-combined-with-metadata-instead-of-event-ids/

Until this is resolved, I’m afraid you’ll have to work around it somehow…

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![bissera](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/bissera/32/1962_2.png) [@bissera](https://mne.discourse.group/u/bissera)\
**Post date:** [November 20, 2023, 1:40pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/9 "2023-11-20T13:40:53Z")

</div>

Ah sorry, i saw it was marked as completed at some point, my bad. But how come it works sometimes then ?

---

<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:** [November 20, 2023, 1:56pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/10 "2023-11-20T13:56:10Z")

</div>

Possibly because the conditions you’re referring to are sometimes also listed in `epochs.event_id`?

---

<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:** [November 21, 2023, 3:01pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/11 "2023-11-21T15:01:02Z")

</div>

Hello everyone,

I am bit confused about the use case of equalize\_event\_counts on metadata.  
Why not use [equalize\_epoch\_counts()](https://mne.tools/stable/generated/mne.epochs.equalize_epoch_counts.html) on epoch objects, which are based on metadata:

In my current project I investigate differences between two conditions that I create based on metadata, before I equalize the epochs in both conditions:

```python

        if cond == "probability":
            epochs_a = epochs[((epochs.metadata.cue == 0.75)
                              & (epochs.metadata.prevresp == 0)
                              & (epochs.metadata.previsyes == 1))]
            epochs_b = epochs[((epochs.metadata.cue == 0.25)
                              & (epochs.metadata.prevresp == 0)
                              & (epochs.metadata.previsyes == 1))]
            cond_a_name = "high_fa"
            cond_b_name = "low_fa"

            if mirror:
                cond_a_name = f'{cond_a_name}_mirror'
                cond_b_name = f'{cond_b_name}_mirror'

        elif cond == "prev_resp":
            epochs_a = epochs[((epochs.metadata.prevresp == 1) &
                               (epochs.metadata.previsyes == 1) &
                               (epochs.metadata.cue == 0.75))]
            epochs_b = epochs[((epochs.metadata.prevresp == 0) &
                               (epochs.metadata.previsyes == 1) &
                                (epochs.metadata.cue == 0.75))]
            cond_a_name = "prevyesresp_highprob_stim"
            cond_b_name = "prevnoresp_highprob_stim"

            if mirror:
                cond_a_name = f'{cond_a_name}_mirror'
                cond_b_name = f'{cond_b_name}_mirror'

        else:
            raise ValueError("input should be 'probability' or 'prev_resp'")

        # make sure we have an equal amount of trials in both conditions
        mne.epochs.equalize_epoch_counts([epochs_a, epochs_b])

```

Maybe I miss something here?

---

<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:** [November 21, 2023, 3:50pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/12 "2023-11-21T15:50:20Z")

</div>

@CarinaFo The function doesn’t take metadata into consideration (yet), but works based on `epochs.event_id`.

Best wishes,  
Richard

---

<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:** [November 21, 2023, 5:47pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/13 "2023-11-21T17:47:35Z")

</div>

There is nothing related to epochs.event\_id in the source code of [equalize\_epoch\_counts()](https://mne.tools/stable/generated/mne.epochs.equalize_epoch_counts.html). I worked on this function during the SPRINT last week, so I am quite confident 🙂

---

<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:** [November 21, 2023, 5:57pm UTC](https://mne.discourse.group/t/how-to-use-equalize-event-counts-combined-with-metadata-instead-of-event-ids/2999/14 "2023-11-21T17:57:17Z")

</div>

My bad, I was talking about `equalize_event_counts()`, not `equalize_epoch_counts()` 😃
