Basic error and response time analyses in Python
A Jupyter notebook for computing individual RTs and error rates, and collating these data across participants.
- Jupyter Notebook (Python 3.x)
- shutil
- numpy
- pandas
- glob
- os
- errno
- re
- matplotlib
- seaborn
- sys
- Reject extreme values (below/above a given RT threshold)
- Reject outlier RTs (based on SDs, trimming, or median absolute deviations)
- Define conditions based on information in the input file
- Compute mean or median RTs
- Compute accuracies/error rates
- Note: BerryPy rejects extreme values before calculating accuracies
BerryPy reads input files produced by an experiment control programme (e.g., PsychoPy), and produces a .csv output file that can be imported into SPSS or other statistical software packages.
BerryPy looks for its input files in the folder input
(located in the same folder as the notebook itself), and writes its output to the folder output
.
Aims | Objectives |
---|---|
Easy to use | Typical user interaction via text configuration files |
Flexible | Defining conditions quite powerful out of the box |
Extensible | Open source – modify code, add code |
Additional aims from a teaching perspective | Objectives |
---|---|
Think about preprocessing steps | Need to define parameters in config files |
No unnecessary user interaction | Automatically processes all .csv files in input directory |
Visualise individual data | Creates Markdown output file with RT distributions and plausibility checks |
BerryPy works best with standard PsychoPy output files. For example:
- BerryPy expects the input files to have a header
- BerryPy expects one trial per line
All of this can of course be adapted, but this will require some Python knowledge.
Please note that the description below assumes that you know how to open a Jupyter notebook.
- Download and unzip BerryPy. Change your working directory to the newly created BerryPy folder.
- Add raw data to the folder
input
(by default, BerryPy expects .csv files, but this can be changed in the notebook) - Edit configuration files (see below for details)
- Open the notebook
- Use
Cell
→Run All
to run the notebook - Results are written to the folder
output
(details below)
Please note that there will be no centrally installed BerryPy package. Instead, add BerryPy to each project folder.
Note that the configuration files address the most common options. If the analysis needs to go beyond these options, extra functions can be added to the notebook (the notebook contains an example function that has been commented out to illustrate this).
- File name:
config_general_settings.txt
- Contains info about relevant column names in input files (e.g.
response.corr
for accuracy of response) - Set thresholds for extreme values (e.g., exclude anything below 100 ms and above 4000 ms)
- Outlier rejection using SD, trimmed mean, or median absolute deviation
- Option to list IDs of participants to exclude from analysis
- Option to define columns that should be kept for the output file (e.g., age or gender)
- File name:
config_rts.txt
- Name and define conditions for output file
- Define measure of central tendency (i.e., mean or median)
- Basic syntax:
<columnName>: [operator]<columnValue>
, where<columnName>
is the column name in the input file (e.g.,response.corr
) and<columnValue>
is the value (e.g.,0
for incorrect)
- Operators
- equal (default):
=
- does not equal:
!=
- below:
<
- below or equal:
<=
- above:
>
- above or equal:
>=
- equal (default):
- File name:
config_errors.txt
- Same syntax rules and operators as for RTs
- Major difference to RT configuration file: need to define ratio using
/
- Basic syntax:
<trialsOfInterest>/<allTrials>
- Basic syntax:
Minimal input file example:
condition | response.corr | response.rt | trials.thisN | stimVisibility | stimLocation |
---|---|---|---|---|---|
congruent | 1 | 0.624 | 0 | high | left |
incongruent | 0 | 0.683 | 1 | low | left |
incongruent | 1 | 0.727 | 2 | high | right |
congruent | 1 | 0.386 | 3 | low | centre |
incongruent | 1 | 0.495 | 4 | low | centre |
incongruent | 1 | 0.528 | 5 | high | right |
incongruent | 0 | 0.581 | 6 | high | centre |
congruent | 0 | 0.669 | 7 | low | right |
congruent | 1 | 0.421 | 8 | high | left |
... | |||||
incongruent | 1 | 0.484 | 99 | low | centre |
Example content of RT configuration file
# compute mean congruent and incongruent RTs for correct trials only
# conRT and inconRT will be the column headers in the output file
# not necessary to add default operator equal
conRT; condition: congruent; response.corr: 1; onColumn: response.rt; measure: mean
inconRT; condition: incongruent; response.corr: 1; onColumn: response.rt; measure: mean
# as above, but exclude the first 20 trials (i.e., trial numbers 0 to 19)
conRT; condition: congruent; response.corr: 1; trials.thisN: [>=]20; onColumn: response.rt; measure: mean
inconRT; condition: incongruent; response.corr: 1; trials.thisN: [>=]20; onColumn: response.rt; measure: mean
# mean con RTs separately for low and high visibility trials
conRT_lowVis; condition: congruent; response.corr: 1; stimVisibility: low; onColumn: response.rt; measure: mean
conRT_highVis; condition: congruent; response.corr: 1; stimVisibility: high; onColumn: response.rt; measure: mean
# mean con RTS for lateralised stimuli
conRT_lateral; condition: congruent; response.corr: 1; stimLocation: left,right; onColumn: response.rt; measure: mean
# alternatively
conRT_lateral; condition: congruent; response.corr: 1; stimLocation: [!=]centre; onColumn: response.rt; measure: mean
# to compute medians instead of means
conRT; condition: congruent; response.corr: 1; onColumn: response.rt; measure: median
inconRT; condition: incongruent; response.corr: 1; onColumn: response.rt; measure: median
Example content of error configuration file
# accuracies for congruent and incongruent trials
conAcc; condition: congruent; response.corr: 1/0,1
inconAcc; condition: incongruent; response.corr: 1/0,1
# alternatively, using error rates
conErr; condition: congruent; response.corr: 0/0,1
inconAcc; condition: incongruent; response.corr: 0/0,1
# accuracies for high-vis trials only
conAcc_highVis; condition: congruent; response.corr: 1/0,1; stimVisibility: high
inconAcc_highVis; condition: incongruent; response.corr: 1/0,1; stimVisibility: high
The values provided can also be strings, allowing the computation of ratios more generally:
# calculate the percentage of congruent trials
percentCongr; congruency: congruent/congruent,incongruent
- In the folder
output
, there will be a file calledoutput.csv
: This file contains the means or medians (dependent on what you chose in the general settings file) and error rates (if you asked for them) for every participant and condition - In the same folder, there will be a Markdown file called
report.md
- It provides basic sanity checks for every participant (e.g., overall percentage of timeouts and errors)
- It provides information about the distribution of RTs
- It tells you how many trials were rejected as outliers
- You can view the Markdown output file using a dedicated Markdown viewer, or a Markdown extension for your browser