(5) Combining Instructions, Stimuli, and Responses¶
Here, we combine instructions, stimuli, and response to create a complete task switching experiment. In this experiment, participants alternate between two tasks: color naming and word reading. Each trial begins with a fixation cue—a "+" indicates a color-naming task, while an "x" signals a word-reading task. The stimulus, displayed for 2000 ms, consists of a word ("RED" or "GREEN") presented in a color (red or green). For color-naming tasks, participants identify the text's color, ignoring the word; for word-reading tasks, they read the word, ignoring its color. Responses are made using keys ('f' or 'j'), with the correct key determined by the task and the stimulus properties. The experiment assesses cognitive flexibility and the ability to manage task switching, including potential interference effects from conflicting information.
Installing sweetbean¶
!pip install sweetbean
Given the following timeline, could you program a task switching experiment?
timeline = [
{'color': 'red', 'word': 'RED', 'task': 'color_naming'},
{'color': 'green', 'word': 'GREEN', 'task': 'color_naming'},
{'color': 'green', 'word': 'RED', 'task': 'word_reading'},
{'color': 'red', 'word': 'GREEN', 'task': 'word_reading'},
]
First we want to declare the timeline variables
# Enter your code here:
Solution¶
# imports
from sweetbean.variable import TimelineVariable
color = TimelineVariable('color')
word = TimelineVariable('word')
task = TimelineVariable('task')
Creating the instructions¶
Now we can generate some instructions for the experiment:
from sweetbean.stimulus import Text
instruction_welcome = # Enter your code here
instruction_list = [
# Enter your code here
]
# Create the instruction block
instruction_block = Block(instruction_list)
Solution¶
from sweetbean.stimulus import Text
from sweetbean import Block
# Define the instruction text blocks
instruction_welcome = Text(
text='Welcome to our task-switching experiment.<br><br> \
In this experiment, you will alternate between two tasks: color naming and word reading.<br><br> \
Press the SPACE key to continue.',
choices=[' ']
)
instruction_fixation = Text(
text='At the beginning of each trial, you will see a fixation cue:<br><br> \
A "+" means you should perform the color-naming task.<br> \
An "x" means you should perform the word-reading task.<br><br> \
Press the SPACE key to continue.',
choices=[' ']
)
instruction_tasks = Text(
text='For the color-naming task:<br> \
Identify the COLOR of the text, ignoring the word.<br><br> \
For the word-reading task:<br> \
Read the WORD, ignoring its color.<br><br> \
Press the SPACE key to continue.',
choices=[' ']
)
instruction_responses = Text(
text='You will respond using the following keys:<br><br> \
For RED (color or word): press the "f" key.<br> \
For GREEN (color or word): press the "j" key.<br><br> \
The stimulus will be displayed for a short period of time, so respond quickly.<br><br> \
Press the SPACE key to continue.',
choices=[' ']
)
instruction_note = Text(
text='Remember:<br> \
Pay attention to the fixation cue ("+" for color naming or "x" for word reading)<br><br> \
to determine the task.<br><br> \
Press the SPACE key to BEGIN the experiment.',
choices=[' ']
)
# Create a list of instruction stimuli for the instruction block
instruction_list = [
instruction_welcome,
instruction_fixation,
instruction_tasks,
instruction_responses,
instruction_note
]
# Create the instruction block
instruction_block = Block(instruction_list)
\ In this experiment, you will alternate between two tasks: color naming and word reading.
\ Press the SPACE key to continue.', choices=[' '] ) instruction_fixation = Text( text='At the beginning of each trial, you will see a fixation cue:
\ A "+" means you should perform the color-naming task.
\ An "x" means you should perform the word-reading task.
\ Press the SPACE key to continue.', choices=[' '] ) instruction_tasks = Text( text='For the color-naming task:
\ Identify the COLOR of the text, ignoring the word.
\ For the word-reading task:
\ Read the WORD, ignoring its color.
\ Press the SPACE key to continue.', choices=[' '] ) instruction_responses = Text( text='You will respond using the following keys:
\ For RED (color or word): press the "f" key.
\ For GREEN (color or word): press the "j" key.
\ The stimulus will be displayed for a short period of time, so respond quickly.
\ Press the SPACE key to continue.', choices=[' '] ) instruction_note = Text( text='Remember:
\ Pay attention to the fixation cue ("+" for color naming or "x" for word reading)
\ to determine the task.
\ Press the SPACE key to BEGIN the experiment.', choices=[' '] ) # Create a list of instruction stimuli for the instruction block instruction_list = [ instruction_welcome, instruction_fixation, instruction_tasks, instruction_responses, instruction_note ] # Create the instruction block instruction_block = Block(instruction_list)
Creating the fixation¶
The fixation cross will vary between the two tasks, maybe we can show a +
, when the task is color_naming, and a x
when the task is word_reading. We can do this with a function variable.
# Enter your code here:
Solution¶
from sweetbean.variable import FunctionVariable
# Predicates
def fixation_shape_fct(task):
if task == 'color_naming':
return '+'
return 'x'
# variable
fixation_shape = FunctionVariable('fixation_shape', fixation_shape_fct, [task])
Creating the correct response¶
Now let's create a correct response parameter. This one is tricky! It will depend on the color, the word and the task. So the predicate will have three input arguments. Let's say we want the participant to press f when the color is "red" in the color_naming task or the word is "RED" in the word_reading task. They should press j when the color is "green" in the color_naming task or the word is "GREEN" in the word_reading task.
# Predicate for f
def correct_key_fct(word, color, task):
if (task == 'word_reading' and word == 'RED') or \
(task == 'color_naming' and color == 'red'):
return 'f'
return 'j'
# variable for the response
correct_key = FunctionVariable('correct_key', correct_key_fct, [word, color, task])
Finishing the experiment¶
Now, create the stimuli, the block and the experiment
# Enter your code here:
Solution¶
from sweetbean.stimulus import Text
from sweetbean import Block, Experiment
# Stimuli
fixation = Text(1000, fixation_shape)
so_s = Text(800)
stroop = Text(2000, word, color, ['f', 'j'], correct_key)
so_f = Text(300)
# Block
train_block = Block([fixation, so_s, stroop, so_f], timeline)
experiment = Experiment([instruction_block, train_block])
# Experiment
experiment.to_html('index.html')