1 of 94

19/11/2022

Peer Herholz (he/him)

Research affiliate - NeuroDataScience-ORIGAMI lab at MNI, MIT, McGill & BRAMS

Member - BIDS, ReproNim, Brainhack, UNIQUE, CNeuroMod

@peerherholz

Michael Ernst

Phd student - Neurocognitive Psychology at Goethe University Frankfurt

M-earnest

Running experiments in Python:

Python for Psychologists - Winter term 2022

2 of 94

The course - the general idea - bringing everything together

All of these aspects will be used going forward, so watch out closely!

3 of 94

Objectives for this session

https://media.makeameme.org/created/look-at-the-25f4b725ed.jpg

  • running experiments using python
  • Exploring PsychoPy
    • basic setup & interaction
      • “Builder” vs “coder”
    • experiments, trials, stimuli, responses
    • a very simple experiment
  • Psychopy
    • Control-Flow Recap
  • Ask and answer questions
  • Have a great time

4 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

5 of 94

Running experiments (using python)

https://www.sr-research.com/wp-content/uploads/2020/10/experiment-viewer-1.png

SR Research Eyelink Experiment Builder

Presentation

https://www.neurobs.com/images/presentation_montage

E-Prime

https://pstnet.com/wp-content/uploads/2018/08/dragdrops-300x205.png

  • proprietary with very expensive single/group licenses
  • platform/OS/hardware dependencies
  • own programming languages
  • restricted purpose/functionality

Psychtoolbox-3

  • often used with matlab -> proprietary
  • also works with GNU Octave -> open
  • limited documentation
  • no direct support for online studies

https://peterscarfe.com/insertedCode/OvalDemo.html

https://osdoc.cogsci.nl/3.3/pages/manual/img/interface/interface.png

OpenSesame

PsychoPy

https://www.psychopy.org/_images/builder.png

  • completely open source & free -> based on Python
  • cross-platform/OS
  • great documentation & open support
  • online studies & general purpose

6 of 94

Running experiments (using python)

“Clearly open source can’t match the performance of proprietary software.” -> hold my computer…

The timing mega-study: comparing a range of

experiment generators, both lab-based and online

  • compared prominently used software

across OS, local & online

  • Psychopy showed performance

comparable to proprietary software

  • prominent differences between OS:

ubuntu > windows > macOS

(remember our first few lectures?)

  • also prominent differences between

browsers

  • online versions less precise,

independent of software but Psychopy

overall best

PsychoPy -> jack of all trades (?)

7 of 94

Outline for this session - what you need

Visual Studio Code, or short: VScode

Microsoft, Public domain, via Wikimedia Commons

8 of 94

Introduction to Psychopy - basic overview

  • Psychology software in Python, i.e. a Python library, i.e. completely written in Python
  • 2002-2003: Jon Pierce began work on this for his own lab (visual neuroscience)
  • 2003-2017: a purely volunteer-driven, evenings and weekends project
  • 2017-now: still open source and free to install but with professional support
  • allow scientists to run as wide a range of experiments as possible, easily and via standard computer hardware
  • precise enough for psychophysics
  • intuitive enough for (undergraduate) psychology (no offence)
  • flexible enough for everything else
  • capable of running studies in the lab or online
  • computer hardware settings & interfaces
  • rapid software development
    • always check version
    • set version in experiment
    • use environments for experiments
    • don’t change version in running experiments

What is PsychoPy (except awesome)?

Idea/Goals of PsychoPy

Things to check/evaluate

9 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

10 of 94

Introduction to Psychopy - basic overview

11 of 94

Introduction to Psychopy - basic overview

First things first: do you have a working PsychoPy installation?

As mentioned several times before: reproducibility is important and virtualization can help a great deal with that! Thus, we would obviously utilize the respective tools to create a new python environment specifically for using PsychoPy.

Once more: conda to the rescue!

12 of 94

Introduction to Psychopy - basic overview

At least that’s what we’d normally do, but due to time constraints and technical issues we’ll be switching to the full-scale standalone version, which you can download for your respective system here

13 of 94

Introduction to Psychopy - basic overview - A new hope…experiment

Let’s assume we have obtained some data regarding favorite musical artists, snacks and pets from a group of fantastic students and now want to test how each respectively provided item is perceived/evaluated by our entire sample: how would we do that?

https://giphy.com/gifs/hacker-pOKrXLf9N5g76

14 of 94

Introduction to Psychopy - The good, the bad and the ugly

- provides logging

information/errors

The runner

- IDE that allows to

code in Python

- GUI that allows to create experiments

- recommend by developers

- has some “magic” functions

- focus of this session

15 of 94

Builder

Introduction to Psychopy - The good, the bad and the ugly

We’ll only be needing exactly 1 Button!

close the builder view via the “x”

Coder

GUI

16 of 94

Introduction to Psychopy - The good, the bad and the ugly

Jk, we’ll be needing exactly 3 Buttons!

File

-> open file

Run

https://giphy.com/embed/eax0rh3OERAYg

Programmer, “name”

17 of 94

Introduction to Psychopy - basic overview - A new hope…experiment

Some important guidelines on starting new experiments

  • starting new experiments follows the same guidelines as starting new projects in general

  • create and store everything in a dedicated place on your machine

  • create and use a dedicated computing environment

  • document everything or at least as much as possible

  • test and save things in very short intervals, basically after every change

my_cool_experiment/

- data/

- experiment.py

- Readme.md

- stimuli/

conda create -n experiment

18 of 94

Introduction to Psychopy - basic overview - A new hope…experiment

Setting up a new experiment - the beginning

  • you can also head on over to the “Intro to Psychopy” notebook on the website and copy - paste the following

19 of 94

Introduction to Psychopy - basic overview - A new hope…experiment

Setting up a new experiment via VScode

  • please use the terminal to create a new folder called psychopy_experiment on your Desktop

mkdir /path/to/your/Desktop/psychopy_experiment

mkdir /Users/Username/Desktop/psychopy_experiment

  • open VScode and via File -> Open Folder open the folder you have just created

  • open a terminal in VScode via Terminal -> New Terminal

  • create a new file, i.e. python script, called experiment.py via

touch experiment.py

  • open psychopy “coder” window

20 of 94

Introduction to Psychopy - basic overview - A new hope…experiment

Setting up a new experiment via VScode

the Editor within which you write code

the currently open script

your terminal within VScode

file explorer

21 of 94

Introduction to Psychopy - basic overview - A new hope…experiment

Setting up a new experiment - the beginning

  • we will use python scripts for this part of the course

  • while the transition from jupyter notebooks to python scripts might seem harsh at first, it’s actually straight-forward: the steps we conducted/commands we run in an incremental fashion we will also be indicated/run in an incremental fashion just within one python script line-by-line

  • What’s the first thing we also have to do while utilizing python?

  • That’s right: importing modules/functions we need

22 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

23 of 94

Introduction to Psychopy - basic overview

  • various basic functions, including timing & experiment termination
  • creation/management of dialog boxes allowing user input
  • handling of keyboard/mouse/other input from user

psychopy.core

psychopy.gui

psychopy.event

  • presentation of stimuli of various types (e.g. images, sounds, etc.)
  • handling of condition parameters, response registration, trial order, etc.
  • we unfortunately can’t check out due to time constraints

psychopy. visual/auditory/movie

psychopy.data

many more …

  • being a python library, PsychoPy is organized into several modules that implement/cover certain functions

What would we need to implement our experiment?

24 of 94

Introduction to Psychopy - basic overview

my_cool_experiment/

- data/

- experiment.py

- Readme.md

- stimuli/

conda create -n experiment

time

prepare prerequisites

gather/set basic

information

present

instructions

present stimuli &

collect responses

end of

experiment

psychopy.core

psychopy.visual

(psychopy.sound)

psychopy.event

Setting up a new experiment - the general idea

psychopy.core

psychopy.visual

psychopy.event

psychopy.core

psychopy.visual

psychopy.event

psychopy.core

psychopy.gui

25 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

26 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - dialog box(es) to get user input

  • many experiments start with a GUI dialog box that allow users/participant to input certain information, for example participant id, session, group, data storage path, etc.

  • we can implement this crucial aspect via the psychopy.gui module

  • initially, we need to import it and thus need to start a new section in our python script

  • after that, we can define the GUI dialog box we want to create via a dictionary with respective key-value pairs

27 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - dialog box(es) to get user input

import the modules

create a Dictionary with:

keys - information we want

value - information we get

(Notice the different data types we

set for the values: string,

boolean, tuple)

if the user presses cancel the experiment will be quit

create the GUI

dialog box from our dict

CTRL/CMD + v

28 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - dialog box(es) to get user input

29 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - dialog box(es) to get user input

  • that’s actually all we need to test our GUI dialog box

  • in order to do that, we need to run/execute our python script called experiment.py

  • this is achieved via open it in our psychopy coder via

-> files -> open file

  • next hit the run button (green arrow)
  • this will run/execute the python script experiment.py via the python version included in psychopy

30 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - dialog box(es) to get user input

  • if everything works/is set correctly you should see a GUI dialog box appearing on your screen asking for the information we indicated in our experiment.py python script (chances are the layout on your end looks a bit different than mine, that’s no biggie)

  • after entering all requested information and clicking ok the GUI dialog box should close and no errors should appear

  • if you click cancel the same thing should happen

31 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - defining file names & paths

  • the next aspect we should take care of is the data handling, i.e. defining a data filename and path where it should be saved

  • we can make use of the exp_info dictionary right away and extract important information from there, for example, the participant ID

  • additionally, we will obtain the date and time via the psychopy.core module

  • we will also create a unique filename for the resulting data and check if the set data path works out via the os module

32 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - dialog box(es) to get user input

import the modules

get the current working directory for easier file storage

create unique filename

(Notice how we get values from the

dictionary and combine strings)

get date/time and experiment name

check if data path exist, if

not create it

(Notice how we we use if and

combine it with a boolean operator)

CTRL/CMD + v

33 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

34 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - saving data

  • now if we would want to stop here, the next aspect we should talk about would be saving our data,

  • for this we will make use of the exp_info dictionary right away and set the data path equal to the unique filename

  • next we’ll make use of the pandas (https://pandas.pydata.org/docs/) library to create a “table” object (called a DataFrame) containing our exp_info

  • afterwards we’ll save the DataFrame as a .csv file (comma-separated file)

35 of 94

import the modules

save our data by providing the data_path + the file ending ‘.csv’ + the separator (‘,’) and encoding

create a DataFrame from our exp_info dictionary

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - saving data

CTRL/CMD + v

36 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

37 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - instructions

  • after having set some crucial backbones of our experiment, it’s time to actually start it

  • quite often, experiments starts with several messages of instructions that explain the experiment to the participant

  • thus, we will add a few here as well, starting with a common “welcometext message

  • to display things in general but also text, the psychopy.visual module is the way to go

  • what we need to do now is define a general experiment window to utilize during the entire experiment and a text to be displayed on it

38 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - instructions

import the modules

create window and define messages

set up a general experiment window (Notice the different input arguments)

create a common welcome message with some instructions

(Notice the different input arguments)

CTRL/CMD + v

39 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - instructions

40 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

  • here, we came across one of PsychoPy’s core working principles: we need a general experiment window, i.e. a place we can display/present something on

  • you can define a variety of different windows based on different screens/monitors which should however be adapted to the setup and experiment at hand (e.g. size, background color, etc.)

  • basically all experiments you will set up will require to define a general experiment window as without it no visual stimuli (e.g. images, text, movies, etc.) can be displayed/presented or how PsychoPy would say it: drawn

  • speaking of which: this is the next core working principle we are going to see and explore - the difference between drawing something and showing it

41 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

  • in PsychoPy (and many other comparable software) there’s a big difference between drawing and showing something

  • while we need to draw something on/in a window that alone won’t actually show it

  • this is because PsychoPy internally uses “two screens” one background or buffer screen which is not seen (yet) and one front screen which is (currently) seen

  • when you draw something it’s always going to be drawn on the background/buffer screen, thus “invisible”

  • you’ll need to flip it to the front screen to be “visible”

  • let’s see how that looks in python code

draw.()

flip.()

Welcome to the experiment.

Please press the spacebar to continue.

Welcome to the experiment.

Please press the spacebar to continue.

42 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

import the modules

create window and define messages

set up a general experiment window (Notice the different input arguments)

create a common welcome message with some instructions

(Notice the different input arguments)

draw the text on the buffer screen

flip (& show) it on the front screen

CTRL/CMD + v

43 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

44 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

win = visual.Window(size=(800,600), color='gray', units='pix', fullscr=False)

Welcome to the experiment.

Please press the spacebar to continue.

welcome_message = visual.TextStim(win, text="Welcome to the experiment. Please press the spacebar to continue.", color='black', height=40)

welcome_message.draw()

buffer screen

(not seen yet)

front screen

(currently seen)

buffer screen

(not seen yet)

front screen

(currently seen)

win.flip()

Welcome to the experiment.

Please press the spacebar to continue.

45 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

  • why does PsychoPy (and other comparable software) work like that?

  • the idea/aim is always the same: increase performance and minimize delays (as addressed in introduction)

  • drawing something might take a long time, depending on the stimulus at hand, but flipping something already drawn from the buffer to the front screen is fast(er)

  • can ensure better and more precise timing

  • can work comparably for images, sounds, movies, etc. where things are set/drawn/pre-loaded and presented exactly when needed

46 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - testing the experiment

  • let’s give it a try

  • if everything works/is set correctly you should see the GUI dialog box again but this time after clicking OK, the text we defined as a welcome message should appear next

  • however, it only appears very briefly and in contrast

to our instructure we don’t need to press the spacebar

to advance

  • this is because we didn’t tell PsychoPy that we want to wait for a distinct key press before we advance further, we need the psychopy.event module

47 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

draw the text on the buffer screen

flip it on the front screen

wait for key press before advancing

create window and define messages

set up a general experiment window (Notice the different input arguments)

create a common welcome message with some instructions

(Notice the different input arguments)

48 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - testing the experiment

  • let’s give it a try via display_instructions.py

  • if everything works/is set correctly you should see the GUI dialog box and after clicking OK, the text we defined as a welcome message should appear next

  • however, this time it should remain on the screen until

you either press space to continue or escape to quit the experiment

  • that’s the magic of the event.waitKeys() function through which we specified a list of keys PsychoPy should wait for before doing anything

CTRL/CMD + v

49 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - more instructions

  • with these things set up, let’s add a few more messages to our experiment

  • one will be presented right after the welcome message and explain very generally what will happen in the experiment

  • another one will be presented at the end of the experiment and display a general “that’s it, thanks for taking part” message

  • the syntax for creating, draw and presenting these message is identical to the one we just explored,

we only need to change the text

50 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - more instructions

set up a general experiment window (Notice the different input arguments)

create a common welcome message with some instructions

(Notice the different input arguments)

create a common explainer message with some instructions

(Notice the different input arguments)

create a common end message

(Notice the different input arguments)

CTRL/CMD + v

51 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - displaying/presenting things

52 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - starting the experiment - more instructions

indicate start

of experiment

draw text, flip it from buffer to front screen and wait for certain key presses before continuing

(Notice the different necessary steps)

indicate end

of experiment

53 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - testing the experiment

  • let’s give it a try via display_instructions.py

  • if everything works/is set correctly you should see the GUI dialog box and after clicking OK, the text we defined as a welcome message should appear next

  • followed by the start message and finally the end message

  • having this rough frame of our experiment it’s actually time to add the experiment itself: the evaluation of our movies, snacks and animals

time

54 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

55 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

  • quick reminder: our experiment should collect responses from participants regarding our lists of artists, snacks and animals, specifically their respective rating

  • thus we need to add/implement two aspects in our experiment: the presentation of stimuli and their rating

  • starting with the presentation of stimuli, we will keep it simple for now and present them via text

  • any ideas how we could begin working on this?

  • that’s right: we need to define lists with our stimuli!

artists = [‘Taylor Swift’, ‘Mac Miller’,

‘Nirvana’, …]

56 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

indicate start of stimuli definition

create a list called “artists” that stores

all names to be presented to participants as strings

CTRL/CMD + v

57 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

58 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

  • quite often experiments shuffle the order of stimuli across participants to avoid sequence/order effects

  • we will do the same and implement that via the numpy.random module, specifically its .shuffle() function which will allow us to randomly shuffle our previously created list

shuffle

artists = [‘Taylor Swift’, ‘Mac Miller’,

‘Nirvana’, …]

artists = [‘Nirvana’, ‘Taylor Swift’

‘Mac Miller’, …]

59 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

import modules

shuffle the list “movies”

CTRL/CMD + v

60 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

  • and now we can simply loop over the elements in the stimulus list and during each iteration draw and flip the respective stimulus” to present the stimuli one-by-one after one another

  • additionally, we want to display the question “How much do you like the following?” above the respective stimulus to remind participant about the task

  • within each iteration of our for-loop we will also allow participants to quit the experiment by pressing “escape” via the event.getKeys() function

artists = [‘Interstellar’}, ‘Love Actually’, ‘Forrest Gump’},…]

for stim in trials:

present(artist)

iteration in for-loop

61 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

loop over trials in trials object

if participants press “escape”,

the experiment should stop

(Notice that we use event.getKeys() instead

of event.waitKeys() as we don’t want to wait for a key to

be pressed but want to do something whenever a

certain key is pressed.)

draw a question each iteration

draw the stimulus, i.e. the value of the key “stimulus” at the current iteration (i.e. position in trial object)

(Notice that we draw multiple things, here texts, on the same buffer screen and then flip all at once/together.)

CTRL/CMD + v

62 of 94

Psychopy - experiments & trials

So the simplest “psychopy experiment” could look something like this (psychopy_for_loop.py)

63 of 94

Psychopy - experiments & trials

So the simplest “psychopy experiment” could look something like this (psychopy_while_loop.py)

64 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - testing the experiment

  • let’s give it a try

  • if everything works/is set correctly you should see the GUI dialog box and after clicking OK, the text we defined as a welcome message should appear next, followed by the start message

  • subsequently you should see all of our stimuli one after another and the same question above them every trial

  • finally you should see the end message

time

65 of 94

Psychopy - experiments & trials

Setting up a new experiment - stimuli & trials

  • while this is already great, the same thing as during our initial tests with the instruction screens happened: the text, i.e. our stimuli, is only very briefly shown on screen and disappears before we can do anything

  • that’s because we didn’t specify that we want to collect responses before moving on to the next stimulus yet

  • we need to add the rating to our experiment

wait for response

wait for

response

66 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

67 of 94

Psychopy - input/output

Setting up a new experiment - collecting responses

  • PsychoPy offers quite a bit of possible options to collect responses: simple yes/no questions, rating scales, visual analog scales, voice recordings, etc.

  • for the experiment at hand a simple rating scale (yes, a Likert scale to make it Psychology, also recently know as “feeling integers”) should be sufficient

  • as with the other components we have explored so far, we need to implement/add this via two steps: defining/creating a rating scale and drawing/presenting it

68 of 94

Psychopy - input/output

Setting up a new experiment - collecting responses

  • we can easily define and tweak a rating scale via PsychoPy’s visual.RatingScale() function which allows us to set the range of values, labels, size, etc.

  • we then need to draw it and indicate that we want to wait until a rating was conducted before we advance to the next trial

  • we want to display a small helpful message describing the rating

  • finally we want to make sure that the rating scale is reset back to its default status before the next trial starts

69 of 94

Psychopy - input/output

Setting up a new experiment - stimuli & trials

indicate the creation of a rating scale

define and tweak the rating scale including:

low/high values, i.e. the range of values

showing the current value

where the marker should initially be placed

labels that explain/describe the scale/range

position on screen

(Notice the different input arguments types)

CTRL/CMD + v

70 of 94

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - stimuli & trials

71 of 94

Psychopy - input/output

Setting up a new experiment - stimuli & trials

loop over elements in list

draw a small helpful message that explains/describes the rating scale/values

(Notice the combination and placement/indentation

of for- and while-loops, as well as code blocks therein.)

only advance to the next when the participant provided a rating

draw the rating scale

reset the rating scale before every trial (i.e. clearing previous ratings befor the current trial)

72 of 94

Psychopy - input/output

Setting up a new experiment - collecting responses

  • even though participants could already perform the rating of the stimuli, we don’t track and collect the respective responses yet

  • these need to be obtained from the rating scale before we reset it at the end of the trial

  • as indicated before visual.RatingScale() creates an object/class/data type with many inbuilt functions, this includes .getRating() and .getRT() to collect the provided rating and corresponding response time

  • we can then store both values per trial in the trials object via its .addData() function

rating = visual.RatingScale().getRating()

rt = visual.RatingScale().getRT()

73 of 94

Psychopy - input/output

Setting up a new experiment - stimuli & trials

collect rating and response time

from rating scale

before (so i.e. above) our loop, we define an empty dictionary, in which we want to store our trial data in

and we successively add the data collected in the current trial to the end of our dictionary values

74 of 94

Psychopy - input/output

Setting up a new experiment - testing the experiment

  • let’s give it a try

  • if everything works/is set correctly you should see the GUI dialog box and after clicking OK, the text we defined as a welcome message should appear next, followed by the start message

  • subsequently you should see all of our stimuli one after another and the same question above them every trial, this time not advancing until you provided a rating

  • finally you should see the end message

time

75 of 94

Psychopy - input/output

Setting up a new experiment - collecting responses

  • our experiment works as expected but with just this we won’t get any output files

  • the reason is once again simple: we actually didn’t tell PsychoPy that we would like to save our data to an output file

  • importantly, our data is stored in a list contained in a dictionary

  • we’ll call on our old(new) friend pandas to help us out

76 of 94

save our data by providing the data_path + the file ending ‘.csv’ + the delimiter (‘,’)

create a DataFrame from our data_info dictionary

Introduction to Psychopy - basic working principles - A new hope…experiment

Setting up a new experiment - saving data

we additionally add data from our exp_info dictionary to our DataFrame via the syntax

df_data['name_of_colum'] = list_of_data

77 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

78 of 94

Psychopy - a very simple experiment

Setting up a new experiment - testing the experiment

  • let’s give it a try via rating_artists.py

  • if everything works/is set correctly you should see the GUI dialog box and after clicking OK, the text we defined as a welcome message should appear nex, followed by the start message

  • subsequently you should see all of our stimuli one after another and the same question above them every trial, this time not advancing until you provided a rating

  • finally you should see the end message

  • after finishing the experiment you should see a new file within the indicated data path containing all information stored about the experiment: trials, stimuli, responses, reaction times, etc.

CTRL/CMD + v

time

79 of 94

Psychopy - a very simple experiment

Setting up a new experiment - testing the experiment

  • Now this is all good and well, but rather simplistic. Let’s keep adding

  • most experiments are structured into multiple blocks, with short breaks between each

  • So let’s also collect responses on how well participants like certain snacks and animals, respectively

  • This can be achieved by simply nesting our existing loops

time

80 of 94

Psychopy - a somewhat simple experiment - adding experimental blocks

Setting up a new experiment - adding block structure

time

1.Block

animals

2.Block

artists

3.Block

snacks

x

trials

x

trials

x

trials

81 of 94

Psychopy - a somewhat simple experiment - adding experimental blocks

Setting up a new experiment - adding block structure

create lists of stimuli separately for each category

shuffle lists of stimuli respectively to randomize stimuli order

create a dictionary with the name of stimuli category as keys and the respective lists as values

transform dictionary into nested list

shuffle list

transform shuffled list back to dictionary

82 of 94

Psychopy - experiments & trials

loop over trials in trials object

loop while no rating was logged

draw the stimulus, i.e. the value of the key “stimulus” at the current iteration

key value

{animals : (… ,… ,… ),

artists : (… ,… ,… ),

snacks : (… ,… ,… )}

blocks =

loop over elements of dict; for key in dict:

draw instruction for following block,

flip it from buffer to front screen and

wait for certain key presses before continuing

collect rating and response time

from rating scale

Setting up a new experiment - adding block structure

1.level of indentation

2.level of indentation

3.level of indentation

83 of 94

Psychopy - experiments & trials

Setting up a new experiment - adding block structure

1.level of indentation

2.level of indentation

collect rating and response time

from rating scale - trial basis

successively add the data collected in the current trial to the end of our dictionary values

(additionally we add the block or momentary condition to our data_info dict

3.level of indentation

draw rating scale

84 of 94

Psychopy - a very simple experiment

my_cool_experiment/

- data/

- experiment.py

- Readme.md

- stimuli/

conda create -n experiment

time

prepare prerequisites

gather/set basic

information

present

instructions

present stimuli &

collect responses

end of

experiment

psychopy.core

psychopy.gui

psychopy.core

psychopy.visual

psychopy.event

psychopy.core

psychopy.visual

psychopy.event

psychopy.data

psychopy.core

psychopy.visual

psychopy.event

psychopy.data

Setting up a new experiment - the general idea

85 of 94

Outline for this session

  • Running experiments (using python)
  • Introduction to Psychopy
    • basic overview
    • basic working principles
      • getting participant/session info
      • saving data
      • displaying instructions
  • Psychopy
    • experiments & trials
    • input/output
    • a very simple experiment
  • Outro/Q&A

https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1

86 of 94

Outro

https://giphy.com/gifs/hallmarkecards-wow-otter-otterly-amazing-Y2bC3o5BkYNndT4vbK

The transition from basic Python aspects to applied together is definitely no cake-walk, especially when simultaneously switching to python scripts from jupyter notebooks but you did a great job!

Thank so much for sticking with us throughout this!

87 of 94

Recap for this session

  • various options/resources/software to run experiments and acquire data
    • proprietary vs. open-source/free -> OS-dependency, $$, performance, etc.
    • PsychoPy jack of all trades -> Python 2nd best for everything again?
  • PsychoPy - running experiments in Python
    • completely written in Python -> comparably fast, “easy” to read/write
    • heavily tested in many different settings
    • great website, forum & open support & many open tutorials
  • setting up experiments
    • PsychoPy has modules/functions for all commonly required parts/tasks of experiments -> build experiment step-by-step
    • buffer vs. front screen -> first draw on buffer screen then flip to front screen
    • setup trials and loop over them, collect responses in each iteration

88 of 94

Were your expectations for this session fulfilled?

89 of 94

Your sixth and last homework assignment will entail the generation of a “new” psychopy experiment

  • You are free to copy any of the existing experiments
  • but at least change the instruction, stimuli and file/experiment name
  • you get full marks, if the experiment runs without crashes, collects participant input in a gui dialogue, logs responses and outputs a .csv file which contains the collected data
  • Bonus: Use a different stimulus modality

- save it as a .py file and e-mail it to ernst@psych.uni-frankfurt.de

- deadline: 23/12/2023, 11:59 PM EST

Outro/Q&A - homework assignment

https://giphy.com/gifs/crying-2WxWfiavndgcM

90 of 94

A few more details

Your grade will be based on 2 aspects with different weights depending on exam regulations:

80%, deadline: ???

(10) homework assignments + class participation, 20%

deadline: throughout semester

The course - tasks & credits

course work/participation

participation/readings

reviews/feedback

homework assignments

Started winter term 21/22 or later - graded credit

experiment

final version of experiment

runnable locally

documentation, stability & reproducibility

final exam

Bash basics

Python basics

executable & reproducible research report

or

Only course credits:

Final exam reduced in capacity by ~ 40%

91 of 94

Questions you could/should ask based on this session

Are there reasons not to use PsychoPy?

Does each experiment really needs its own computing environment?

How much documentation is “sufficient”?

How should experiments be tested and evaluated?

92 of 94

Introduction to Psychopy - The good, the bad and the ugly

For more info on how to build experiments using the builder:

https://giphy.com/gifs/zach-galifianakis-deception-disappoint-zYjezbxqhP18k

93 of 94

https://twitter.com/PDLComics/status/1572971481352392704

94 of 94

https://giphy.com/gifs/season-17-the-simpsons-17x6-xT5LMB2WiOdjpB7K4o