# Epochs with variable durations... again

**URL:** <https://mne.discourse.group/t/epochs-with-variable-durations-again/2449>\
**Category:** Support & Discussions\
**Tags:** preprocessing, meg, feature-request\
**Created:** [January 6, 2021, 4:06pm UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449 "2021-01-06T16:06:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sslaats](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/sslaats/32/42_2.png) [@sslaats](https://mne.discourse.group/u/sslaats)\
**Post date:** [January 6, 2021, 4:06pm UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449/1 "2021-01-06T16:06:09Z")

</div>

- MNE-Python version: 0.21.0
- operating system: Linux

Dear MNE developers,

I am working with MEG data of sentence listening. I need to epoch the data on the basis of sentence duration - and they are all different. This means I want epochs with variable durations - but in the Epochs class, it’s not possible to specify a variable tmax.

An idea I had to get this done, was to epoch using the longest possible sentence duration as tmax, and then trim each epoch based on the duration it’s supposed to have… But is there a better (faster, cleaner) way to do this? I have offset triggers, as well as a dataframe with samples and times for both onsets and offsets to work with.

I have found the following related issues:  
[The exact same issue - closed as ‘wontfix’](https://github.com/mne-tools/mne-python/issues/3533)  
[An issue that arises when dealing with variable length epochs (though I don’t need to do TFR)](https://github.com/mne-tools/mne-python/issues/5612#issuecomment-481566216)

Thanks a lot!

Best,

Sophie Slaats

---

<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:** [January 6, 2021, 4:35pm UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449/2 "2021-01-06T16:35:25Z")

</div>

hi @sslaats

this is a tricky issue. It’s a trade off on the MNE side. Having a fixed length simplifies and accelerates a lot of code.

once you have epochs what do you do?

Alex

---

<div class="post-metadata">

**Author:** ![sslaats](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/sslaats/32/42_2.png) [@sslaats](https://mne.discourse.group/u/sslaats)\
**Post date:** [January 6, 2021, 4:54pm UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449/3 "2021-01-06T16:54:53Z")

</div>

Hi @agramfort,  
Thanks for your quick reply! I use them to estimate temporal response functions - so more specifically, I compute the covariance matrix of the data with a time-lagged stimulus feature.  
Sophie

---

<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:** [January 6, 2021, 8:53pm UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449/4 "2021-01-06T20:53:08Z")

</div>

have you add a look at

> **[MNE — MNE 0.22.0 documentation](https://mne.tools/dev/auto_tutorials/machine-learning/plot_receptive_field.html)**

or

> **[MNE — MNE 0.22.0 documentation](https://mne.tools/stable/auto_examples/stats/plot_linear_regression_raw.html#sphx-glr-auto-examples-stats-plot-linear-regression-raw-py)**

?

Alex

---

<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:** [January 7, 2021, 5:59pm UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449/5 "2021-01-07T17:59:37Z")

</div>

I’m not sure I understood what you want to do with your variable-length epochs, but you could always roll your own solution (e.g. put epochs in a list and then do whatever you need to do). Of course you could achieve this by epoching the data using the longest duration as tmax and then prune the individual epochs as needed. Another option if you must squeeze all data into a 2D table is to set samples exceeding individual epoch lengths to `np.nan`.

---

<div class="post-metadata">

**Author:** ![sslaats](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/sslaats/32/42_2.png) [@sslaats](https://mne.discourse.group/u/sslaats)\
**Post date:** [January 13, 2021, 10:18am UTC](https://mne.discourse.group/t/epochs-with-variable-durations-again/2449/6 "2021-01-13T10:18:48Z")

</div>

Hi @agramfort and @cbrnr,

Thanks a lot for your replies. I am aware of the possibilities for regressing continuous data (although I had not looked at MNE options for this in detail, so thanks for the references @agramfort 🙂). For some design reasons it was handy to epoch the data, and I actually already have (using FieldTrip). The rest of my pipeline is in Python with MNE, and I would like to rewrite the first part using MNE as well, for continuity reasons.

I’ll go for pruning the epochs then. Thanks again to you both!

Best,

Sophie
