Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] Pass value from Python script to experiment variable in OpenSesame?

edited November 2012 in OpenSesame

I'm not very experienced with Python, so I'm not having any luck figuring this out. Previously, Sebastiaan gave me a very helpful boost in that direction, creating a Python script that will run two OpenSesame experiments, back to back. This has been priceless for running my study so far. However, now I find the need to run both males and females (previously just males), and to have the RA (not the participant, to avoid errors) enter the person's sex at the time the study starts. But both OpenSesame experiments have a number of things that vary between male and female participants.

I've set a variable in the OpenSesame experiments (i.e., participant_sex, with values 'm' or 'f', declared in an inline script item at the beginning of each of the two experiments), and I use it to decide whether to show or not show certain items, instructions, etc. while the OS experiments run. I could also prompt for the RA to enter 'm' or 'f' in the Python script that runs before the OS experiments, but I don't know how to pass the value entered by the RA to the participant_sex variable in OS.

What I imagine is that there might be some kind of option in the command that would let me set a variable value in the OS experiments from the Python script? Something like... (I'm wildly imagining, here) maybe this in the script to get the value...

p_sex = str(raw_input('Enter the Participant's Sex (m or f): '))

then, at the end of the script..."participant_sex" = p_sex))

Or something like that, so that the m or f typed by the RA becomes the value of the participant_sex variable in OpenSesame. But I don't know if this is even the right direction. I've tried to find the options for the run() command (I'm assuming it's in the opensesame Python module... a concept I'm only starting to grok), but haven't found anything.

Any suggestions? It doesn't have to be in the form noted above; I just need to pass that information to a variable in both OS experiments, without relying on the participant to do it--therefore, it should be collected via the Python script that controls the running of the OS experiments, I think.

Below is the code (with a few modifications of my own) that Sebastiaan helpfully provided to make this possible. You can apply my hypothetical suggestions, above, to this:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import datetime
from libopensesame.experiment import experiment

# Ask for the subject no. and other info
# subject = int(raw_input('Please enter subject number '))
cons_form = str(raw_input('Which consent form (dw or nw)? '))
ra_sex = str(raw_input('Sex of RAs in lab (mm, ff, fm, etc.): '))
sp_code = str(raw_input('Special codes (e.g., pilot, prevRA, testing, etc.) - ENTER for none: '))
ra_init = str(raw_input('Your initials (consistent number and capitalization): '))

d =

# A list of full paths to your opensesame experiments
exp_files = ['/home/bepsoap/Desktop/2part/essa1.opensesame.tar.gz', '/home/bepsoap/Desktop/2part/essa2.opensesame.tar.gz']

i = 0
for exp_file in exp_files:
    exp = experiment('Exp', exp_file)
    exp.fullscreen = True

    # The logfile contains both the subject nr and the experiment nr
    exp.logfile = d.strftime("%Y-%m-%d_%H%M_") + 'rX_%s_%s_%s_c%s_%d.csv' % (cons_form, ra_sex, ra_init, sp_code, i)
    i += 1'


  • edited 4:20AM

    OK, wait! I found a way (I think; not fully tested yet) to do this. I don't know how to pass the value of a variable from the Python script to an OS experiment, but I do know how to set the subject number.

    So, in my python script I can ask participant sex and then set the subject number (which I don't use at all, usually; instead, I set the datafile names to encode all the information I need about each subject). This part hasn't been tested, yet:

    p_sex = str(raw_input("What is the PARTICIPANT'S SEX ('m' or 'f'): "))

    Later in the script, I can set the subject number (of both experiments?) based on the sex that was input by the RA:

    i = 0
    for exp_file in exp_files:
        exp = experiment('Exp', exp_file)
        if p_sex == 'm':
        elif p_sex == 'f':
            print "Enter only 'm' or 'f' (no quotes), in lowercase. Try running the script again."
        exp.fullscreen = True
        # The logfile contains both the subject nr and the experiment nr
        exp.logfile = d.strftime("%Y-%m-%d_%H%M_") + 'rX_%s_%s_%s_c%s_%d.csv' % (cons_form, ra_sex, ra_init, sp_code, i)
        i += 1

    And in the beginning of each OpenSesame experiment file, I have this in an inline script, to set the value of participant_sex by referring to subject_nr:

    # set participant sex (from subject number)
    if self.get("subject_nr") == 0:
        self.experiment.set("participant_sex", "f")
    elif self.get("subject_nr") == 1:
        self.experiment.set("participant_sex", "m")
        self.experiment.set("participant_sex", "not_set")

    It feels like a hacky workaround, but it seems to be working. If anyone can see problems with it, or has a more robust/elegant solution, I'm open to these, but as long as my further testing works out, I think I can use this.

  • Hi,

    Although your solution works, you could achieve the same result in a simpler way, too. You could use the set() function (like all experiment functions) in a Python script in the same way as you used set_subject(). Like so:

    # Determine the value of the variable 'p_sex' from the user input:
    p_sex = str(raw_input("Enter the Participant's Sex (m or f): "))

    # Set the variable for future use in OpenSesame
    exp.set("p_sex", p_sex)

    Now, you can obtain the value of the variable 'p_sex' in a Python inline_script item in OpensSesame like you did above for 'subject_nr':

    participant_sex = self.get("p_sex")

    I hope this helps!

    Best wishes,


Sign In or Register to comment.