Howdy, Stranger!

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

Supported by

[solved]Playing beep during showing of a picture; trial ends after 6 seconds not when key is pressed

edited May 2014 in OpenSesame

Hi there,

Can you help me correct my code? (I think I will need to use the sleep function, but don't know how..)
What I want:

-every trial starts with a fixation dot for 1 second
-then a picture shows for 6 seconds
-during the picture a beep plays (at SOA 1 or 4 seconds)
-person presses key 'z' or '/' to respond to the beep (high or low)
-(trial ends when picture is shown for 6 seconds, not when key is pressed after the beep)

-next trial...

So far I have made this code:

#first a sketchpad with a fixation dot for 1 sec
#then the following inline script:


from openexp.synth import synth

#create empty canvas to draw stimulus on in prepare phase
my_canvas = exp.canvas = self.offline_canvas()   
my_canvas.set_bgcolor('white')
my_canvas.clear()


#find right picture for current trial
if self.get('pict_valence') == 'positive':
    path_positive = exp.get_file('positive.png')
    my_canvas.image(path_positive)
elif self.get('pict_valence') == 'neutral':
    path_neutral = exp.get_file('neutral.png')
    my_canvas.image(path_neutral)
elif self.get('pict_valence') == 'negative':
    path_negative = exp.get_file('negative.png')
    my_canvas.image(path_negative)



#define low and high tone (beep)
if exp.get('tone_pitch') == 'high':
    exp.beep = synth(exp, freq= 2000, osc = "square", length=500)
elif exp.get('tone_pitch') == 'low':
    exp.beep = synth(exp, freq= 400, osc = "square", length=500)


#define SOA for the tone
SOA = exp.get("tone_SOA") 

#play tone
self.sleep(abs(SOA))
exp.beep.play()
self.sleep(6000 - abs(SOA))

Comments

  • edited April 2014

    Hi,

    First of all: you're almost there!

    Second: let's clean up the code a bit, to make sure the experiment runs a bit more smoothly. Since you use the pictures and the sounds throughout the entire experiment, it makes more sense to initialize them at the start of the experiment. To do so, add an inline_script item to the very beginning of your experiment, and add the following contents:

    # load the libraries that we will need
    from openexp.canvas import canvas
    from openexp.synth import synth
    from openexp.keyboard import keyboard
    
    # create three canvasses. each showing a different picture
    # we will store these canvasses in a dictionary variable
    # (see: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/dictionaries.html)
    my_canvas = {}
    for valence in ['positive', 'negative', 'neutral']:
        # create a new canvas, store it using the name for the valence
        exp.my_canvas[valence] = canvas(exp)
        # draw the image for this valence
        exp.my_canvas[valence].image(exp.get_file(valence+".png"))
    
    # create two beeps, and store these in another dict
    exp.beeps = {}
    exp.beeps['high'] = synth(exp, freq= 2000, osc = "square", length=500)
    exp.beeps['low'] = synth(exp, freq= 400, osc = "square", length=500)
    
    # finally, create a keyboard object
    exp.my_keyboard = keyboard(exp, keylist=['z','/'], timeout=None)
    

    Now, in the trial sequence, insert the following code in the Run phase of an inline_script item (placed directly after the fixation_dot item):

    # draw the correct canvas (and save the timestamp)
    t0 = exp.my_canvas[self.get('pict_valence')].show()
    
    # wait for the correct SOA
    self.sleep(self.get('SOA'))
    
    # play the correct beep (and save a timestamp)
    exp.beeps[exp.get('tone_pitch')].play()
    t1 = self.time()
    
    # wait for input or a timeout (i.e. when 6 seconds pass from stimulus onset)
    response, presstime = exp.my_keyboard.get_key(timeout=6000-self.get('SOA'))
    
    # wait for some more to fill up the 6 seconds (only if a response was given)
    if response != None:
        passed = presstime - t0
        self.sleep(6000 - passed)
    
    # store some variables that can be accessed in other OpenSesame items
    exp.set("response", response)
    exp.set("response_time", presstime-t1)
    

    (small disclaimer: code is untested, as I'm in a bit of a rush: please do not hesitate to post any potential errors arising when you run the scripts!)

  • edited 9:18AM

    Dear Edwin,

    Thank you so much for your help.
    It works :-)

    I now need to add a small extra thing: there are 12 positive, 12 negative and 24 neutral pictures from which I randomly want to take one each trial.

    I thought I could make lists of numbers, shuffle them and each time add the first item of this list to the image path (I named all images positive1.png, positive2.png, etc..)
    But the canvasses are made in the beginning.

    Can you help me?
    Maybe make lists of canvasses in the beginning and later take a random one of this list?
    Or do somethig with a if loop in the beginning?

    Here is the code which I have made now (In the wrong place now).


    # inline script beginning of experiment # load the libraries that we will need from openexp.canvas import canvas from openexp.synth import synth from openexp.keyboard import keyboard # create three canvasses. each showing a different picture # we will store these canvasses in a dictionary variable # (see: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/dictionaries.html) exp.my_canvas = {} for valence in ['positive', 'negative', 'neutral']: # create a new canvas, store it using the name for the valence exp.my_canvas[valence] = canvas(exp) # draw the image for this valence exp.my_canvas[valence].image(exp.get_file(valence+".png")) # create two beeps, and store these in another dict exp.beeps = {} exp.beeps['high'] = synth(exp, freq= 2000, osc = "square", length=500) exp.beeps['low'] = synth(exp, freq= 400, osc = "square", length=500) # finally, create a keyboard object exp.my_keyboard = keyboard(exp, keylist=['z','/'], timeout=None) #Run phase, trial sequence: import random #make 3 lists from numbers 1 till 12 or 24 #list1 and list2 are for positive and negative pictures (12 each) #list3 will be for the neutral pictures (24 each) list1 = [i for i in range(1,13) ] list2 = [i for i in range(1,13) ] list3 = [i for i in range(1,25) ] #shuffle each list random.shuffle(list1) random.shuffle(list2) random.shuffle(list3) #save the lists exp.list1 = list1 exp.list2 = list2 exp.list3 = list3 # draw the correct canvas (and save the timestamp) #if positive, take first item from list 1, then delete this item from the list. So that each trial one of the random numbers is taken. if exp.get('pict_valence') == 'positive': t0 = exp.my_canvas[self.get('pict_valence' + 'list1[0]' + '.png')].show() del(list1[0]) list1 = list1 # wait for the correct SOA self.sleep(self.get('tone_SOA')) # play the correct beep (and save a timestamp) exp.beeps[exp.get('tone_pitch')].play() t1 = self.time() # wait for input or a timeout (i.e. when 6 seconds pass from stimulus onset) response, presstime = exp.my_keyboard.get_key(timeout=6000-self.get('tone_SOA')) # wait for some more to fill up the 6 seconds (only if a response was given) if response != None: passed = presstime - t0 self.sleep(6000 - passed) # store some variables that can be accessed in other OpenSesame items exp.set("response", response) exp.set("response_time", presstime-t1)
  • edited April 2014

    Again, almost there!

    This should be in the Prepare Phase of an inline_script at the start of your experiment:

    # load the libraries that we will need
    import random
    from openexp.canvas import canvas
    from openexp.synth import synth
    from openexp.keyboard import keyboard
    
    #make 3 lists from numbers 1 till 12 or 24
    #list1 and list2 are for positive and negative pictures (12 each)
    #list3 will be for the neutral pictures (24 each)
    imglist = {}
    imglist['positive'] = range(1,13)
    imglist['negative'] = range(1,13)
    imglist['neutral'] = range(1,25)
    
    # randomize all of the lists
    for valence in imglist.keys():
        random.shuffle(imglist[valence])
    
    # create an empty canvas
    exp.my_canvas = canvas(exp)
    
    # create two beeps, and store these in another dict
    exp.beeps = {}
    exp.beeps['high'] = synth(exp, freq= 2000, osc = "square", length=500)
    exp.beeps['low'] = synth(exp, freq= 400, osc = "square", length=500)
    
    # finally, create a keyboard object
    exp.my_keyboard = keyboard(exp, keylist=['z','/'], timeout=None)
    

    This following should be in the Prepare Phase of the inline_script in your trial sequence:

    # clear the canvas
    exp.my_canvas.clear()
    
    # get the path to the image
    valence = exp.get("pict_valence'")
    imgfile = exp.get_file(valence + str(imglist[valence].pop(0)) + ".png")
    
    # draw the image for this valence
    exp.my_canvas.image(imgfile)
    

    And the following should be in the Run Phase of the same inline_script:

    # draw the canvas (and save the timestamp)
    t0 = exp.my_canvas.show()
    
    # wait for the correct SOA
    self.sleep(self.get('SOA'))
    
    # play the correct beep (and save a timestamp)
    exp.beeps[exp.get('tone_pitch')].play()
    t1 = self.time()
    
    # wait for input or a timeout (i.e. when 6 seconds pass from stimulus onset)
    response, presstime = exp.my_keyboard.get_key(timeout=6000-self.get('SOA'))
    
    # wait for some more to fill up the 6 seconds (only if a response was given)
    if response != None:
        passed = presstime - t0
        self.sleep(6000 - passed)
    
    # store some variables that can be accessed in other OpenSesame items
    exp.set("response", response)
    exp.set("response_time", presstime-t1)
    

    Good luck!

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