PsychoPy & Behaviour experiment I

In behavioural science, particularly psychology and neuroscience research, conducting experiments is crucial for verifying hypotheses. Two decades ago, executing such experiments necessitated engineering and electrical skills to operate devices that could present image or sound stimuli. Researchers used stopwatches or other precise timing tools to collect data. Fortunately, with technological advancements, researchers no longer need to retreat to garages to test cumbersome equipment and draw far-fetched conclusions. Gone are the days of stopwatch-wielding researchers; now, it’s all about elegant screen clicks proving the Stroop Effect, perhaps while leisurely sipping coffee.

As noted in the preface of Building Experiments in PsychoPy, modern programming languages like Python and JavaScript enable the design of studies without requiring a Ph.D. in computer science. PsychoPy stands out as the most popular software in behaviour research. It offers a Graphic User Interface (GUI) for the ‘out-of-the-box’ enthusiasts and a command line for those who enjoy ‘reading binary for fun.’

While the GUI allows researchers an intuitive setup for ‘equipment’ (presenting stimuli and collecting response times), it arguably lacks the flexibility to fully customize the entire experiment’s process. We must admit that constructing experiments with mouse clicks and drags can appear as if one is a novice in an introductory computer science course, unfamiliar with coding. However, as a professional scientist, your realm should be coding in the terminal amidst a sea of black and green text. Therefore, I am crafting a post for my blog to assist beginners in learning how to use code to develop their Stroop experiments.

0x00 IMPORT MODULES & PATH SETTINGS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# =====================
# IMPORT MODULES
# =====================
# -import numpy and/or numpy functions *
# -import psychopy functions
# -import file save functions
# -(import other functions as necessary: os...)

import os
import time
import random
import itertools
import numpy as np
import pandas as pd
from datetime import datetime
from psychopy import core, visual, event, monitors, gui

First, import the OS package to manage experiment files. Use the Time and Datetime packages, akin to the traditional stopwatch, for measuring response times in behavioural studies. The PsychoPy package, central to your study, allows selective function imports based on your research needs, avoiding the slowdown associated with loading the entire package. Utilize Random and Itertools for random stimulus presentation. For data formatting and analysis, Pandas and Numpy are your go-to tools. These packages are optional; you might not need them all depending on your study.

1
2
3
4
5
6
7
8
# =====================
# PATH SETTINGS
# =====================
main_dir = os.getcwd()
path = os.path.join(main_dir, 'dataFiles')
# -check that these directories exist
if not os.path.exists(path):
os.makedirs(path)

0x01 COLLECT PARTICIPANT INFO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# =====================
# COLLECT PARTICIPANT INFO
# =====================
# -create a dialogue box that will collect current participant number, age, gender, handedness
exp_info = {"subject_nr": (), "age": (), "gender": (),"handedness": ('left', 'right', 'ambi'),"session": () }
# wait for 2 seconds
print('All variables have been created! Now ready to show the dialog box!')
time.sleep(2)
# Customize my_dlg so that you have a title for your dialog box: "subject info"
my_dlg = gui.DlgFromDict(dictionary=exp_info, title='subject_info')
# get date and time
date = datetime.now()
exp_info['date'] = str(date.day) + str(date.month) + str(date.year)
# -create a unique filename for the data
filename = (str(exp_info['subject_nr']) + '_outputFile.csv')
# print(filename)