Howdy, Stranger!

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

Supported by

[solved] show sketchpad for set time even if response is made and feedback arrives

edited March 2014 in OpenSesame

Hi, I a very similar problem to the one exposed here, but from that description I din't unedrstand how it has been solved and since it was marked as "solved" I thought to open a new post.

Basically, I need that during a single trial, lasting for expample 6 sec, the subject should be able to give mouse responses and receive a different feedback (visualize an image for 500ms) for each response without ending the trial until 6 sec elapsed.

This is the inline code that follows a sketchpad (named 'choice') which lasts 0 sec, in which different squares are visualized as possible choices. The subject is required to choose with a mouse-click on one:

from openexp.mouse import mouse
my_mouse = mouse(exp)
my_mouse.set_visible()
my_mouse.set_pos(pos=(660,260))
my_mouse.set_buttonlist( [1] )
my_mouse.flush()

trial = exp.get('choice_cond')

button, position, timestamp = my_mouse.get_click()
x, y = position

from openexp.canvas import canvas
my_canvas = self.copy_sketchpad('choice')

uno = exp.get_file(u'/media/File/experiment/stim/O1.jpg') # feedback one
due = exp.get_file(u'/media/File/experiment/stim/O2.jpg') # feedback two
delay = 500

if trial == (12):

    if x > (114) and x < (294) and y > (300) and y < (480):
        exp.set('response', 1)
        exp.set('response_____keyboard_response', "R1")
                    # show the appropriate feedback
        my_canvas.image(uno, center=True, x=None, y=600, scale=0.1) 
        my_canvas.show() 

    if x > (432) and x < (612) and y > (300) and y < (480):

        exp.set('response', 1)
        exp.set('response_____keyboard_response', "R2")
                    # show the appropriate feedback
        my_canvas.image(due, center=True, x=None, y=600, scale=0.1) 
        my_canvas.show() 

    if (x < (114) and x > (294)) or (x < (432) and x > (612)) and (y < (300) or y > (480)):
        exp.set('response', 0)

 self.sleep(6000)  # to make each trial end after 6 sec

I've tried to use self.sleep() after my_canvas.image to set the duration of this image but, of course, it ends the whole canvas.

I know there is probably more than one wrong thing in this script, but I cant' figure it out!Please help!

Thanks!

Comments

  • edited March 2014

    I have improved the code in this way and it seems to work:

    start_time = self.time()
    trial_duration = 10000
    lastRes1_time = 0
    lastRes2_time = 0
    lastRes3_time = 0
    lastRes4_time = 0
    import random
    schedule = random.randint(2000,3000)
    
    
    from openexp.mouse import mouse
    my_mouse = mouse(exp)
    my_mouse.set_visible()
    my_mouse.set_pos(pos=(660,260))
    my_mouse.set_buttonlist( [1] )
    my_mouse.flush()
    
    from openexp.canvas import canvas
    sketch = self.copy_sketchpad('choice')
    my_canvas = self.copy_sketchpad('choice')
    # Determine the absolute path:
    no = exp.get_file(u'/media/experiment/stim/no.jpg')
    uno = exp.get_file(u'/media/experiment/stim/O1.jpg')
    due = exp.get_file(u'/media/experiment/stim/O2.jpg')
    
    # Get ... as python variables
    trial = exp.get('choice_cond')
    
    
    while self.time() < start_time+trial_duration:
    
    
        # Wait for a mouse click
        button, position, timestamp = my_mouse.get_click()
        x, y = position
    
    
    ##############################
        if trial == (12):
            exp.set('correct', "RR")
                # button 1
            if x > (114) and x < (294) and y > (300) and y < (480):
                    exp.set('response', 1)
                    exp.set('response_____keyboard_response', "R1")
                    if  self.time() > lastRes1_time + schedule:
                        my_canvas.image(uno, center=True, x=None, y=670, scale=0.15)
                        lastRes1_time = timestamp
                        my_canvas.show()
                        self.sleep(600)
            # button 2
            if x > (432) and x < (612) and y > (300) and y < (480):
                    exp.set('response', 2)
                    exp.set('response_____keyboard_response', "R2")
                    if  self.time() > lastRes2_time + schedule:
                        my_canvas.image(due, center=True, x=None, y=670, scale=0.15)
                        lastRes2_time = timestamp
                        my_canvas.show()
                        self.sleep(600)
    
        sketch.image(no, center=True, x=None, y=650, scale=0.01)
        sketch.show()   
    

    is this the best solution?
    I hope it can be helpful for someone!

  • edited March 2014

    Hi Sara,

    Depending on what you want to do, you seem to have come up with a very good solution. Keep in mind that with your script, the interaction continues to run after the first click. This means that participants can alter their choice after clicking.

    If you do not want to do so, the following example shows a single-click-and-wait procedure:

    # put the beginning of you script here, up until the while loop
    
    # get the starting time
    start_time = sketch.show()
    
    # wait until a buttonpress is made
    button = None
    passed_time = None
    while button == None and passed_time < trial_duration:
        # check for button presses
        button, pos, click_time = my_mouse.get_click(timeout=1)
        # check if button 1 was clicked
        if (114 < x < 294) and (300 < y < (480):
                    exp.set('response', 1)
                    exp.set('response_____keyboard_response', "R1")
        # check if button 2 was clicked
        elif (432 < x < 612) and (300 < y < (480):
                    exp.set('response', 2)
                    exp.set('response_____keyboard_response', "R2")
        # check if button 1 was clicked
        else:
                    # reset button
                    button = None
        # calculate the passed time
        passed_time = click_time - start_time
    
    # reset the screen
    sketch.clear()
    # show a blank screen
    end_time = sketch.show()
    passed_time = end_time - start_time
    
    # wait for a bit
    self.sleep(trial_duration - passed_time)
    
  • edited 1:18AM

    Actually, I do want them to click several times (and, thus, make more than one choice) during a certain time interval while visualizing the same sketchpad) and subsequently go to the next trial. Thus, in this case, I don't need to reset the screen or to show a blank screen.
    I did not specified that, in this task, there are several buttons and in each trial two of them are available.

    Than you for the optimized script. By looking at it I have improved some things in mine!

  • edited 1:18AM

    Excellent! I just included the script for the single button responses for future reference, it was by no means a comment on your design :)

    Good luck with testing!

Sign In or Register to comment.