Howdy, Stranger!

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

Supported by

Recording Keyboard Responses from Stimulus Onset while Keeping Stimuli on Screen

Hi All,

I've been having some trouble with an experiment in which I want to present a target briefly (100ms) and then fixation for 1500 ms and record responses from the onset of the target. I would like the timing to be the same for everyone, so that the target or the fixation screen remain up even after a key is pressed. From reading other discussions on the forum, it seemed easiest to do this in an inline script, but two problems arise with the script I'm currently running

1 - I've tried to keep the stimuli on screen by using the self.sleep function; however, it looks like my target still disappears as soon as something is pressed (I've experimented with long target duration to be sure)
2 - It doesn't look like responses are being recorded, which makes me think that there is something wrong with how I am setting up the keyboard.

I'm including the code I've used (similar to what is posted on another discussion). Any thoughts as to what may be going on here?

Note: my_target_canvas and my_fix_canvas are prepared elsewhere. Also, most variable (e.g. tar_present_time and Correct_resp are specified elsewhere. Of note could be that the variable Correct_resp is specified within the loop.

`from openexp.canvas import canvas
from openexp.keyboard import keyboard

#settings
self.experiment.set("timeout1",self.get("tar_present_time"))
self.experiment.set("timeout2",self.get("keyboard2_time"))

#create keyboard
my_keyboard = keyboard(exp, keylist=['l','k'])

#set actual timout taking into account current time and timestamp of target #sketchpad
timeout = self.get("timeout1")-self.time()+exp.get('time_target')

#Collect a keyboard response with tar duration timeount
resp, time = my_keyboard.get_key(timeout=timeout)

if resp!= None:
    my_target_canvas.show()
    response_time = time - self.get('time_target')
    response = my_keyboard.to_chr(resp)
    if response == self.get('Correct_resp'):
        correct = 1
    else:
        correct = 0
    self.experiment.set("tar_sleep",self.get("timeout1")-self.get("response_time"))
    self.sleep(self.get("tar_sleep"))
    my_fix_canvas.show()
    self.sleep(self.get("timeout2"))

#if no response has been given, wait another duration with fixation screen
if resp == None:
    my_fix_canvas.show()
    timeout = self.get("timeout2") - self.time() + exp.get('time_target')
    resp, time = my_keyboard.get_key(timeout=timeout)
    response_time = time - self.get('time_target')
    response = my_keyboard.to_chr(resp)
    if response == self.get('Correct_resp'):
        correct = 1
    else:
        correct = 0
    self.experiment.set("fix_sleep",self.get("timeout2")-self.get("response_time"))
    self.sleep(self.get("fix_sleep"))



#set experimental variables
exp.set('response_time', response_time)
exp.set('response', response)
exp.set('correct', correct)


# Maintain feedback variables. This ensures that
# you can use the feedback item as usual
exp.total_responses += 1
if correct:
    exp.total_correct += 1
exp.total_response_time += response_time
exp.acc = 100. * exp.total_correct / exp.total_responses
exp.avg_rt = exp.total_response_time / exp.total_responses
exp.accuracy = exp.acc
exp.average_response_time = exp.avg_rt`

Thank you for your time and and any assistance you can provide!!

Best,
Kristin

Comments

  • Hi Kristin,

    Which version of opensesame do you use? I adapted your code for OpenSesame 3.x.x, as some of bits of your code were really outdated. I hope this is fine by you.

    There were quite a few things a little weird about your code. Here, the general outline your code should have:

    • stimulus presentation and response collection should occur in a while loop
    • depending on the time that has passed, you either show the stimulus or the fixation
    • if a response is made, prepare to break the loop once the minimum amount of time has passed

    The code below should do the trick. You can also have a look on this discussion here (even though it was unsolved so far)

    <br />#settings
    timeout1  = var.tar_present_time
    timeout2  = var.keyboard2_time
    timeout3 = 1500 # fixation canvas duration
    
    timeout = timeout1 + timeout3
    ready_for_break = False
    #create keyboard
    my_keyboard = keyboard(keylist=['l','k'])
    
    var.response_time, var.correct,var.resp = None,None,None # initialize important variables
    t0 = my_target_canvas.show()
    while clock.time()-t0 < timeout:
        if clock.time()-t0 > 95: # taking frame rate into account
            my_fix_canvas.show()
        if not ready_for_break:
             var.resp, time = my_keyboard.get_key(timeout=10) # a  very small value is enough
             if var.resp != None:
                  ready_for_break = True
                  if var.resp == var.Correct_resp:
                      var.correct = 1
                  else:
                      var.correct=0
                   var.response_time = time-t0
    
    # Maintain feedback variables. This ensures that
    # you can use the feedback item as usual
    var.total_responses += 1
    if var.correct:
        var.total_correct += 1
    var.total_response_time += var.response_time
    var.acc = 100. * var.total_correct / var.total_responses
    var.avg_rt = var.total_response_time / var.total_responses
    var.accuracy =var.acc
    var.average_response_time =var.avg_rt`
    

    I hope this works (haven't had the chance to test it).

    Good luck,

    Eduard

  • edited September 2016

    Hi Eduard,

    This was extremely helpful! Thank you so much for your response. I used the code that you provided and just had to make a couple of tweaks for it to work for my particular paradigm, because it includes catch trials in which the correct response would actually be no response. The final script I've been using is pasted below, and working well! Thanks again for your help with this!!

    from openexp.canvas import canvas
    from openexp.keyboard import keyboard
    
        #settings
        timeout1  = var.tar_present_time #target presentation duration
        timeout2  = var.keyboard2_time #fixation canvas duration
    
        timeout = timeout1 + timeout2
        ready_for_break = False
        #create keyboard
        my_keyboard = keyboard(exp,keylist=['l','k'])
    
        # initialize important variables
        var.response_time = 0
        var.correct = 0
        var.resp = None
    
        #create while loop for what should show when target is present for target present trials
        if var.Tar_present == 'yes':
            t0 = my_target_canvas.show()
            while clock.time()-t0 < timeout:
                if clock.time()-t0 > timeout1: 
                    my_fix_canvas.show()
                if not ready_for_break:
                    var.resp, time = my_keyboard.get_key(timeout=10) # a very small value is enough
                    var.response = var.resp
                    if var.resp != None:
                        ready_for_break = True
                        if var.resp == var.Correct_resp:
                            var.correct = 1
                        else:
                            var.correct=0
                        var.response_time = time-t0
                    if var.resp == None:
                        var.response_time = time - t0
    
        #create while loop for same duration but only fixation screen for catch trials              
        if var.Tar_present == 'no':
            t0 = my_fix_canvas.show()
            while clock.time()-t0 < timeout:
                if not ready_for_break:
                    var.resp, time = my_keyboard.get_key(timeout=10) # a very small value is enough
                    var.response = var.resp
                    if var.resp != None:
                        ready_for_break = True
                        var.response_time = time-t0
                    if var.resp == None:
                        var.correct = 1
                    else:
                        var.correct = 0
    
        # Maintain feedback variables. This ensures that
        # you can use the feedback item as usual
        var.total_responses = (var.total_responses + 1)
        if var.correct:
            var.total_correct = (var.correct +1)
        var.total_response_time = (var.total_response_time + var.response_time)
        var.acc = 100. * var.total_correct / var.total_responses
        var.avg_rt = var.total_response_time / var.total_responses
        var.accuracy =var.acc
        var.average_response_time =var.avg_rt
    

    Much appreciated,
    Kristin

Sign In or Register to comment.