Assigning participant-specific parameters automatically in OpenSesame

OpenSesame offers options to counterbalance properties of the stimulus across participants. However, in cases of more involved assignments of session parameters across participants, it becomes necessary to write a bit of Python code in an inline script, which should be placed at the top of the timeline. In such a script, the participant-specific parameters are loaded in from a csv file. Below is a minimal example of the csv file.

participant language training_list test_list experiment_list
1 Mini-English 1 1 1
2 Mini-Norwegian 1 1 1
3 Mini-English 2 2 1
4 Mini-Norwegian 2 2 1
5 Mini-English 1 3 2
6 Mini-Norwegian 1 3 2
7 Mini-English 2 4 2
8 Mini-Norwegian 2 4 2

Below is the corresponding inline script. The code .iloc[0] at the end of the lines is used to select a cell.

# Assign participant-specific parameters. For each participant-specific factor in the 
# stimulus files, take the level corresponding to the index position specified in the 
# participant parameters file.

import csv  # handle csv file
import pandas as pd  # handle data frames

participant_parameters = pd.read_csv(exp.get_file('stimuli/parameters per participant.csv'))

var.participant = var.subject_nr

var.language = participant_parameters.loc[participant_parameters['participant'] == var.subject_nr]['language'].iloc[0]

var.training_list = participant_parameters.loc[participant_parameters['participant'] == var.subject_nr]['training_list'].iloc[0]

var.test_list = participant_parameters.loc[participant_parameters['participant'] == var.subject_nr]['test_list'].iloc[0]

var.experiment_list = participant_parameters.loc[participant_parameters['participant'] == var.subject_nr]['experiment_list'].iloc[0]

Finally, the variables created in the script can be used in Run if conditions (e.g., [language] == 'Mini-English'), as replacements inside file names (e.g., [language] training, List [training_list].csv), and as input in sketchpads (e.g., Current list: [training_list]).

comments powered by Disqus