Introduction to Expyriment

Christophe@pallier.org

May 2022

Requirements for this tutorial

  1. A Python environment

    E.g., Anaconda Python 3.

  2. The expyriment module

    Check installation instructions at https://docs.expyriment.org/Installation.html).

  3. A local copy of https://github.com/chrplr/tutorial-expyriment.git, which can obtain by by downloading this zip file or with git:

      git clone https://github.com/chrplr/tutorial-expyriment

Note: This tutorial assumes that you know how to execute Python code from a command line in a terminal window (if you don’t, see, e.g., https://www.youtube.com/watch?v=2yhcWvBt7ZE).

First example

Guess what the following piece of code is meant to do:

from expyriment import stimuli

fixation = stimuli.FixCross()
picture = stimuli.Picture("cat.png")
sound = stimuli.Audio("sentence.wav")

fixation.present()
exp.clock.wait(1000)
picture.present()
sound.present()

key, rt = exp.keyboard.wait_char()

Minimal skeleton for an expyriment script

To make the preceding code actually work, you need a bit of boilerplate (See https://docs.expyriment.org/Tutorial.html)

from expyriment import design, control, stimuli

exp = design.Experiment(name="Experiment")

control.initialize(exp)

fixation = stimuli.FixCross()
picture = stimuli.Picture("cat.png")
sound = stimuli.Audio("sentence.wav")

control.start()

fixation.present()
exp.clock.wait(1000)
picture.present()
sound.present()
key, rt = exp.keyboard.wait_char(' ')

control.end()

Execute this code in ipython (or with python example_01.py)

Note: You will need to press the space bar to quit it.

Stimuli

simple detection of visual events

  1. Download simple-detection-visual-expyriment.py

  2. Run it, then check the results in the subfolder data. Note that:

  1. Have a look at the source code.

    Its core consists of:

        target = stimuli.FixCross(size=(50, 50), line_width=4)
        blankscreen = stimuli.BlankScreen()
    
        for i_trial in range(N_TRIALS):
            blankscreen.present()
            waiting_time = random.randint(MIN_WAIT_TIME, MAX_WAIT_TIME)
            exp.clock.wait(waiting_time)
            target.present()
            key, rt = exp.keyboard.wait(duration=MAX_RESPONSE_DELAY)
            exp.data.add([i_trial, waiting_time, key, rt])

Simple detection of audio events

  1. Download simple-detection-audio-expyriment.py, as well as the

  2. Run it.

  3. Compare its source code with the one of the visual version, for example with:

     meld simple-detection-visual-expyriment.py simple-detection-audio-expyriment.py

    The only essential difference is the line:

     target = stimuli.FixCross(size=(50, 50), line_width=4)

    which was changed into:

     target = stimuli.Audio('click.wav')

    The code in the main loop remains the same!

Simple decision (parity task)

Let’ now see the case where the participant must take a decision at each trial and press one of two keys.

In parity.py, the participant must determine the parity of a single digit number.

parity_feedback.py adds auditory feedback when the response is wrong.

Note: See also parity_short.py, a very short example from expyriment’s documentation.

Simple decisions (left vs. right)

left_right_detection.py is a similar example as parity.

Trial and Block

The design submodule of expyriment provides Trial and Block objects to structure the experiment.

(Remark that these objects are in no way necessary to present stimuli! You can program without them if its makes the code more readable)

More complex examples:

Timing

Conclusion

Pros:

Cons:

Other ressources