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 exp.run() 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...

exp.run(experiment.set("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 = datetime.datetime.now()

# 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.set_subject(1)
    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)
    exp.run()
    i += 1'

Comments

  • edited 5:08AM

    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':
            exp.set_subject(1)
        elif p_sex == 'f':
            exp.set_subject(0)
        else:
            print "Enter only 'm' or 'f' (no quotes), in lowercase. Try running the script again."
            break
    
        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)
        exp.run()
        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")
    else:
        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,

    Lotje

    Did you like my answer? Feel free to Buy Me A Coffee :)

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games