Creating (random) inter-trial intervals#

Inter-trial intervals (ITIs) are often of random duration to avoid e.g. habituation effects or to reset induced entrainment.

Here we show how to create concatenated trials with random inter-trial intervals between them.


We start by importing the Sequence class from thebeat. In addition we import NumPy.

[1]:
from thebeat import Sequence
import numpy as np

When we have a Sequence object, we can simply add a number to it which will be the ITI, like so:

[3]:
seq1 = Sequence.generate_isochronous(n_events=5, ioi=500)
seq2 = Sequence.generate_isochronous(n_events=5, ioi=300)

long_seq = seq1 + 777 + seq2  # Concatenate a Sequence with an ITI with a Sequence
print(long_seq)
Object of type Sequence (ends with event)
10 events
IOIs: [500. 500. 500. 500. 777. 300. 300. 300. 300.]
Onsets: [   0.  500. 1000. 1500. 2000. 2777. 3077. 3377. 3677. 3977.]
Sequence name: Not provided


If we want to have a random number there, we can use one of NumPy’s functions for generating one. For more info on NumPy random number generators, see here.

[4]:
random_iti = np.random.default_rng().integers(low=200, high=1000)

long_seq = seq1 + random_iti + seq2
print(long_seq)
Object of type Sequence (ends with event)
10 events
IOIs: [500. 500. 500. 500. 873. 300. 300. 300. 300.]
Onsets: [   0.  500. 1000. 1500. 2000. 2873. 3173. 3473. 3773. 4073.]
Sequence name: Not provided