{ "cells": [ { "cell_type": "markdown", "id": "dfebabd3-4120-4e8d-b148-62a7ca258854", "metadata": {}, "source": [ "# Creating (random) inter-trial intervals\n", "Inter-trial intervals (ITIs) are often of random duration to avoid e.g. habituation effects or to reset induced entrainment.\n", "\n", "Here we show how to create concatenated trials with random inter-trial intervals between them." ] }, { "cell_type": "markdown", "id": "26585c50-3fc2-43c0-828a-a5529a0968f9", "metadata": { "raw_mimetype": "text/restructuredtext", "tags": [] }, "source": [ "---" ] }, { "cell_type": "raw", "id": "1f01d77c-b80c-46fb-9548-a16e46d66154", "metadata": { "raw_mimetype": "text/restructuredtext", "tags": [] }, "source": [ "We start by importing the :py:class:`~thebeat.core.Sequence` class from *thebeat*. In addition we import *NumPy*." ] }, { "cell_type": "code", "execution_count": 1, "id": "636a2c03-5374-4d65-ae42-a09e719053a1", "metadata": {}, "outputs": [], "source": [ "from thebeat import Sequence\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "b12160c8-825f-4839-8081-54afe84c63a0", "metadata": { "nbsphinx": "hidden", "tags": [] }, "outputs": [], "source": [ "# We suppress warnings, but let's hide that to avoid confusion\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "%matplotlib inline" ] }, { "cell_type": "raw", "id": "b4c3ae9a-2a54-480c-ba82-f96b494c773a", "metadata": { "raw_mimetype": "text/restructuredtext", "tags": [] }, "source": [ "When we have a :py:class:`~thebeat.core.Sequence` object, we can simply add a number to it which will be the ITI, like so:" ] }, { "cell_type": "code", "execution_count": 3, "id": "930395e3-9103-496f-b98e-6c4d9c402b3b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Object of type Sequence (ends with event)\n", "10 events\n", "IOIs: [500. 500. 500. 500. 777. 300. 300. 300. 300.]\n", "Onsets: [ 0. 500. 1000. 1500. 2000. 2777. 3077. 3377. 3677. 3977.]\n", "Sequence name: Not provided\n", "\n" ] } ], "source": [ "seq1 = Sequence.generate_isochronous(n_events=5, ioi=500)\n", "seq2 = Sequence.generate_isochronous(n_events=5, ioi=300)\n", "\n", "long_seq = seq1 + 777 + seq2 # Concatenate a Sequence with an ITI with a Sequence\n", "print(long_seq)" ] }, { "cell_type": "markdown", "id": "b1879089-2c95-41b2-b1bb-48a3918be516", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "id": "e109b81f-9bc8-468a-af39-969313d8b458", "metadata": {}, "source": [ "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](https://numpy.org/doc/stable/reference/random/generator.html)." ] }, { "cell_type": "code", "execution_count": 4, "id": "2c3ecabc-c5e0-41ee-a958-1be76b2f1ddf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Object of type Sequence (ends with event)\n", "10 events\n", "IOIs: [500. 500. 500. 500. 873. 300. 300. 300. 300.]\n", "Onsets: [ 0. 500. 1000. 1500. 2000. 2873. 3173. 3473. 3773. 4073.]\n", "Sequence name: Not provided\n", "\n" ] } ], "source": [ "random_iti = np.random.default_rng().integers(low=200, high=1000)\n", "\n", "long_seq = seq1 + random_iti + seq2\n", "print(long_seq)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }