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
The course - the general idea - bringing everything together
All of these aspects will be used going forward, so watch out closely!
Objectives for this session
https://media.makeameme.org/created/look-at-the-25f4b725ed.jpg
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
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
Psychtoolbox-3
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
Running experiments (using python)
“Clearly open source can’t match the performance of proprietary software.” -> hold my computer…
across OS, local & online
comparable to proprietary software
ubuntu > windows > macOS
(remember our first few lectures?)
browsers
independent of software but Psychopy
overall best
PsychoPy -> jack of all trades (?)
Outline for this session - what you need
Visual Studio Code, or short: VScode
Microsoft, Public domain, via Wikimedia Commons
Introduction to Psychopy - basic overview
What is PsychoPy (except awesome)?
Idea/Goals of PsychoPy
Things to check/evaluate
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Introduction to Psychopy - basic overview
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!
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
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
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
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
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”
Introduction to Psychopy - basic overview - A new hope…experiment
Some important guidelines on starting new experiments
my_cool_experiment/
- data/
- experiment.py
- Readme.md
- stimuli/
conda create -n experiment
Introduction to Psychopy - basic overview - A new hope…experiment
Setting up a new experiment - the beginning
Introduction to Psychopy - basic overview - A new hope…experiment
Setting up a new experiment via VScode
mkdir /path/to/your/Desktop/psychopy_experiment
mkdir /Users/Username/Desktop/psychopy_experiment
touch experiment.py
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
Introduction to Psychopy - basic overview - A new hope…experiment
Setting up a new experiment - the beginning
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Introduction to Psychopy - basic overview
psychopy.core
psychopy.gui
psychopy.event
psychopy. visual/auditory/movie
psychopy.data
many more …
What would we need to implement our experiment?
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
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - dialog box(es) to get user input
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
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - dialog box(es) to get user input
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - dialog box(es) to get user input
-> files -> open file
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - dialog box(es) to get user input
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - defining file names & paths
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
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - saving data
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
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - instructions
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
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - instructions
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - displaying/presenting things
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - displaying/presenting things
draw.()
flip.()
Welcome to the experiment.
Please press the spacebar to continue.
Welcome to the experiment.
Please press the spacebar to continue.
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
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - displaying/presenting things
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.
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - displaying/presenting things
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - testing the experiment
to our instructure we don’t need to press the spacebar
to advance
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)
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - testing the experiment
you either press space to continue or escape to quit the experiment
CTRL/CMD + v
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - more instructions
we only need to change the text
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
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - starting the experiment - displaying/presenting things
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
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - testing the experiment
time
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Psychopy - experiments & trials
Setting up a new experiment - stimuli & trials
artists = [‘Taylor Swift’, ‘Mac Miller’,
‘Nirvana’, …]
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
Psychopy - experiments & trials
Setting up a new experiment - stimuli & trials
Psychopy - experiments & trials
Setting up a new experiment - stimuli & trials
shuffle
artists = [‘Taylor Swift’, ‘Mac Miller’,
‘Nirvana’, …]
artists = [‘Nirvana’, ‘Taylor Swift’
‘Mac Miller’, …]
Psychopy - experiments & trials
Setting up a new experiment - stimuli & trials
import modules
shuffle the list “movies”
CTRL/CMD + v
Psychopy - experiments & trials
Setting up a new experiment - stimuli & trials
artists = [‘Interstellar’}, ‘Love Actually’, ‘Forrest Gump’},…]
for stim in trials:
present(artist)
iteration in for-loop
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
Psychopy - experiments & trials
So the simplest “psychopy experiment” could look something like this (psychopy_for_loop.py)
Psychopy - experiments & trials
So the simplest “psychopy experiment” could look something like this (psychopy_while_loop.py)
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - testing the experiment
time
Psychopy - experiments & trials
Setting up a new experiment - stimuli & trials
wait for response
wait for
response
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Psychopy - input/output
Setting up a new experiment - collecting responses
Psychopy - input/output
Setting up a new experiment - collecting responses
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
Introduction to Psychopy - basic working principles - A new hope…experiment
Setting up a new experiment - stimuli & trials
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)
Psychopy - input/output
Setting up a new experiment - collecting responses
rating = visual.RatingScale().getRating()
rt = visual.RatingScale().getRT()
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
Psychopy - input/output
Setting up a new experiment - testing the experiment
time
Psychopy - input/output
Setting up a new experiment - collecting responses
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
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
Psychopy - a very simple experiment
Setting up a new experiment - testing the experiment
CTRL/CMD + v
time
Psychopy - a very simple experiment
Setting up a new experiment - testing the experiment
time
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
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
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
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
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
Outline for this session
https://twitter.com/OfficeMemes_/status/1298982572848869380/photo/1
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!
Recap for this session
Were your expectations for this session fulfilled?
Your sixth and last homework assignment will entail the generation of a “new” psychopy experiment
- 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
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%
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?
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
https://twitter.com/PDLComics/status/1572971481352392704
https://giphy.com/gifs/season-17-the-simpsons-17x6-xT5LMB2WiOdjpB7K4o