Multiple tasks & shared variables

Hello,

We are developing several tasks with the PsychoPy builder, that will form a task battery in an experiment.

Our question is: Is there a way to “share” information/variable (e.g. LSL stream, participant ID, session number, etc) between these different tasks, without having to enter these infos again for each task?

There is the solution of doing everything in a single PsychoPy experiment (.psyexp file), but this doesn’t really suit us because of the lack of flexibility. We would prefer to have a metascript with the shared variables, in which we can launch individual tasks through a GUI.

Thank you in advance!

Is this running locally or online?

Locally there is a new session class. You can see some information about it here:

Online you could use the shelf or daisy chaining.

Thank you for your response!

The experiments will be run locally.

We tried implementing your suggestion and here’s what we did (as an example):

Create a Session object

thisSession = session.Session(
win=win,
root=root_path,
experiments={
‘SimpleRTT’: “SimpleRTT/simpleRTT_lastrun.py”,
‘DoubleRTT’: “DoubleRTT/doubleRTT_lastrun.py”,
}
)

Run the SimpleRTT experiment

thisSession.runExperiment(“SimpleRTT”)

Run the DoubleRTT experiment

thisSession.runExperiment(“DoubleRTT”)

This setup works well; both tasks run sequentially (the complete battery will have more tasks), and we successfully collect the data from both tasks in the data folder.

However, we would like to initialize variables at the beginning of the session that can be passed to the task scripts. For example, our participants can be either French or English speakers, and we want the instructions to adapt accordingly based on the language selected at the start of the experiment.

Is there a way to pass such variables to the task scripts within this “metascript”?

Thank you in advance!

Session.runExperiment takes an input expInfo, so you can do:

thisSession.runExperiment(“DoubleRTT”, expInfo={'language': "en"})

and you’d be able to access language in the experiment as expInfo['language']

1 Like

Thank you!

We were able to access language with this function.

Here is the code we used as an example:

Check if expInfo exists

if ‘expInfo’ not in globals():
expInfo = {}

Set default values to English if it is missing from expInfo keys

if ‘language’ not in expInfo:
expInfo[‘language’] = ‘English’

Create the language variable

language = expInfo.get(‘language’)

Define instructions based on the selected language

if language == “English”:
instructions = {
‘Introduction’: “text_instruction”,
‘Practice’: “text_practice”,
‘Start_Task’: “text_start_task”
}
else:
instructions = {
‘Introduction’: “texte_consignes”,
‘Practice’: “texte_entrainement”,
‘Start_Task’: “texte_début_tache”
}

It can be launched both from
thisSession.runExperiment(“DoubleRTT”, expInfo={‘language’: language_participant})
where language_participant is decided with a language questionnaire prior to this task;
And from stand alone with psychopy builder (default instructions will be english language).

Thanks to this function, we were also able to add more variables such as these:
thisSession.runExperiment(“DoubleRTT”, expInfo={‘participant’: participant_id, ‘session’: session_n, ‘language’: language_participant, ‘date’: date_str})

OS : macOS Ventura 13.4
PsychoPy version : 2024.1.5
Standard Standalone? (y/n) If not then what?: Y

Hello,
I am trying to use this session object in Builder (I need Builder for several reasons). I had a look at the posts linked and above but I am getting an error.

thisSession = session.Session(
NameError: name ‘session’ is not defined

My guess is that I am declaring the object in the wrong place.
I have a simple experiment, showing a dialog box, then calling another experiment.
I have a code component with the following:

thisSession = session.Session(
    win=win,
    root="root_path",
    experiments={
        # paths should be relative to the root folder
        'task_checklist': "./task_checklist/task_checklist.psyexp",
        'task_saturn': "./task_saturn/task_saturn.psyexp",
    }
)

I placed the code in the ‘Before experiment’ tab (tried also the ‘Begin experiment’ but i always get the error. Any idea what I am missing?

Thank you in advance,
Sara

Hello,

I also encountered this error. I needed to create variables for the expInfo. Here is the work-in-progress code that I use as an example (note that I am not using the builder and this is a standalone python script).

import os
from psychopy import visual, core, session
from datetime import datetime

# Get the directory where the script is located
script_dir = os.path.dirname(os.path.abspath(__file__))

# Variables for expInfo
participant_id = 'sub-001'
language_participant = 'English'
session_n = '001'
date_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

# Create a window
win = visual.Window(size=[1920, 1080], fullscr=True, screen=0, 
                    winType='pyglet', allowGUI=True, allowStencil=False,
                    monitor='testMonitor', color='black', colorSpace='rgb',
                    blendMode='avg', useFBO=True)

# Define the path of the project root
root_path = script_dir

# Save the participant ID to a data file
data_file = os.path.join(root_path, f"data_{participant_id}.csv")

# Create a Session object
thisSession = session.Session(
    win=win,
    root=root_path,
    experiments={
        'DoubleRTT': "DoubleRTT/DoubleRTT_lastrun.py", 
        'SimpleRTT': "SimpleRTT/simpleRTT_lastrun.py",      
    }
)


# Run the DoubleRTT experiment
thisSession.runExperiment("DoubleRTT", expInfo={'participant': participant_id, 'session': session_n, 'language': language_participant, 'date': date_str})

# Run the SimpleRTT experiment
thisSession.runExperiment("SimpleRTT", expInfo={'participant': participant_id, 'session': session_n, 'language': language_participant, 'date': date_str})

# Close the window
win.close()
core.quit()