Howdy, Stranger!

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

Supported by

[open] Running timers for two stimuli concurrently with inline script

edited April 2016 in OpenSesame

Hello all, I'm quite new to programming and to Open Sesame (thanks for the software and all the feedback you give on here!) so my apologies if this question seems very basic.

I'm currently designing an experiment where I will have two forms of sound stimuli being played to the participants. One form is a metronome that will repeat every 500ms. The second form is a randomly chosen sound that will play back at a randomly chosen time interval. At the moment I have both of these as if statements embedded in a while loop. Each of the pieces of code will work individually but when executed together the metronome plays every 500ms until the randomly chosen sound plays, at which time the metronome completely stops.

Here is the code as it currently is:

time_range = range(3000, 6000)
play_time = random.choice(time_range)   # Choose a time value between 3-6s
print play_time
start_time = self.time()    # Initialise a starting clock value 
deviants = [deviant1, deviant2, deviant3, deviant4, deviant5]
reaction_keys = keyboard(exp, keylist = ['space'], timeout = 2000)
iteration = 0
metronome_timer = self.time()
while iteration < 5:
    deviant_time = self.time() - start_time
    reaction_timer = self.time()
    metro_range = self.time() - metronome_timer
    if metronome_timer + 500 == self.time():
        metronome_timer = self.time()
        print metronome_timer
        metronome.play()
        outlet.push_sample([2]) # LSL Marker
    if deviant_time == play_time:
        deviant_chosen = random.choice(deviants) # Choose sound
        deviant_chosen.play() # Play sound
        outlet.push_sample([1]) # LSL Marker
        if deviant_chosen == deviant1:
            print 'Deviant 1 played'
        if deviant_chosen == deviant2:
            print 'Deviant 2 played'
        if deviant_chosen == deviant3:
            print 'Deviant 3 played'
        if deviant_chosen == deviant4:
            print 'Deviant 4 played'
        if deviant_chosen == deviant5:
            print 'Deviant 5 played'
        response, timestamp = reaction_keys.get_key()
        response_time = self.time() - reaction_timer
        #while response_time < 2000: # This while loop is used for gathering reaction times still doesn't work without it
        #   if response != None:
        #       response_time = self.time() - reaction_timer
        #       print 'Response time is %d' %response_time
        #       break
        #   else:
        #       print 'User did not respond'
        #   response_time = self.time() - reaction_timer
        if deviant_chosen in deviants: 
            deviants.remove(deviant_chosen) # Remove used items from list
        play_time = random.choice(time_range)
        start_time = self.time()
        iteration = iteration + 1
        if iteration == 5:
            standard.stop()




exp.set('response', response)
exp.set('response_time', response_time)

From the reading I've done it seems as though threading may be necessary to execute this but as a novice coder I would appreciate any help pointing me towards the simplest solution.

Thanks!

Comments

  • edited 8:41PM

    Hi Tom,

    I don't think threading will be necessary. Probably this can all be fixed by using conditions in a smart way.

    One thing that isn't clear to me is how often/how long is this loop supposed to last? Until participants responded, or is there some timeout?

    Here a bit of code, that might simplify a few things.

    import random
    
    play_time = random.randrange(3000,6000)   # Choose a time value between 3-6s
    print play_time
    
    orig_deviants =  [deviant1, deviant2, deviant3, deviant4, deviant5]
    deviants = [deviant1, deviant2, deviant3, deviant4, deviant5]
    random.shuffle(deviants)
    
    # A timeout of 2000 ms is pretty long. Opensesame can't do anything
    # else during a timeout. So, it is better to have a brief timeout, 
    # but loop over it
    reaction_keys = keyboard(exp, keylist = ['space'], timeout = 20)
    
    deviant = True
    iteration = 0
    # Initialise a starting clock value. One is enough 
    start_time = self.time()    
    while iteration < 5:
        if deviant and self.time()-start_time()>play_time:
            deviant_chosen = deviants.pop() # Choose sound
            deviant_chosen.play() # Play sound
            outlet.push_sample([1]) # LSL Marker 
            deviant = False # used to make this if statement to be entered only once
            print 'deviant %s played'%(orig_deviants.index(deviant_chosen)+1)
        # plays every 490-510 ms (use the modulo for that)
        met_time = (self.time-start_time)%500
        if  met_time<10 or met_time > 490:
            print metronome_timer
            metronome.play()
            outlet.push_sample([2]) # LSL Marker
    
            response, timestamp = reaction_keys.get_key()
            if response != None:
                response_time = self.time() - timestamp
            #else:
            #    print 'User did not respond'
            if deviant_chosen in deviants: 
                iteration = iteration + 1
            play_time = random.choice(time_range)
            start_time = self.time()
    
    
    # What is this "standard?"
    standard.stop()
    # note your response will now only be set one to the values of the last response
    # if you want to log each response you have to do it in the while loop
    exp.set('response', response)
    exp.set('response_time', response_time)
    

    I haven't tested the code. It's probably not entirely correct, but I still hope it is useful for you. Let me know, if you need more advice, or if I misunderstood your intentions.

    Good luck.

    Eduard

    Buy Me A Coffee

  • edited 8:41PM

    Hi Eduard,

    Thank you very much! This has been very helpful. I've had to alter your code slightly to get it closer to what I want it to do, and remove a few parts that were giving error messages. The length of the loop is dictated by the deviants; i.e. when all five deviants have been played the loop should finish. To this end I have moved some of the code around. I have also specified that the metronome should play at exactly 500ms. The audio playback now seems to be working well so thank you very much.

    I do have a follow up question that I hope you can help me with regarding collecting keyboard responses. What I want is to have two separate keyboard responses with the space bar recording the response time after the deviant stimulus and the shift key recording how early or late the participant's estimation of the metronome time is. You mentioned above that these should be within the while loop but I am unsure of how to code them. Any advice you could offer would be much appreciated. Here's my code as it is currently:


    time_range = random.randrange(3000,6000) # Choose a time value between 3-6s print time_range deviants = [deviant1, deviant2, deviant3, deviant4, deviant5] random.shuffle(deviants) # A timeout of 2000 ms is pretty long. Opensesame can't do anything # else during a timeout. So, it is better to have a brief timeout, # but loop over it reaction_keys = keyboard(exp, keylist = ['space'], timeout = 20) standard.play() deviant = True iteration = 0 # Initialise a starting clock value. start_time_1 = self.time() start_time_2 = self.time() while iteration < 5: if deviant and self.time()-start_time_1>time_range: deviant_chosen = random.choice(deviants) # Choose sound deviant_chosen.play() # Play sound outlet.push_sample([1]) # LSL Marker if deviant_chosen in deviants: iteration = iteration + 1 deviants.remove(deviant_chosen) #print 'deviant %s played'%(orig_deviants.index(deviant_chosen)+1) time_range = random.randrange(3000,6000) start_time_1 = self.time() # plays every 490-510 ms (use the modulo for that) met_time = self.time()-start_time_2 if met_time == 500: print met_time metronome.play() outlet.push_sample([2]) # LSL Marker response, timestamp = reaction_keys.get_key() if response != None: response_time = self.time() - timestamp #else: # print 'User did not respond' start_time_2 = self.time() standard.stop()

    By the way the "standard" within the code is a longer piece of audio that is playing constantly in the background.

    Cheers,

    Tom

  • edited 8:41PM

    Hi Tom,

    You can have to separate keyboard items that allow either for a shift or a space response. Inside the while loop you would then do keyboard.get_key() twice and save each result in a separate variable.

    Does this make sense?

    Eduard

    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