0

I am using a rating scale. Participants use the 't' and 'b' keys to move the cursor along the scale. Each trial is currently 6 seconds long. If a participant stops pressing 't' or 'b' before 6 seconds are up, I want to log the time of the last keypress in my logfile. However, I'm not sure how to check which keypress is the last. I was thinking of logging the RT of the last keypress in the list, but code is checking for keypresses on every refresh. This is what I have so far:

trialNum=0
for eachPic in catPictures:
    prevPos = 0
    key=[]
    b_list=[]
    t_list=[]
    timer = core.CountdownTimer(TrialDuration)
    event.clearEvents() # get rid of other, unprocessed events
    while timer.getTime() > 0:
    for key in event.getKeys():
        if key in ['escape']:
            core.quit() # quit if they press escape
        if key in ['b']:
            # add keypress to list for each keypress. then move cursor proportionally to length of this list
            b_list.append(key)
            prevPos+=len(b_list)
        if key in ['t']:
            t_list.append(key)
            prevPos-=len(t_list)
Maria
  • 1,247
  • 1
  • 13
  • 21

2 Answers2

2
  1. I would just have one list of keys and check the last element once the timer is up, i.e. after the while-loop (upon finish trial).
  2. Don't initiate a whole new timer in each loop. Just reset it. Much more ressource-efficient.
  3. Indent stuff in the while loop.
  4. I don't understand why you move the cursor the distance of the number of previous key presses in that trial. It seems more reasonable to move it a fixed distance per key press. So I did that below.
  5. Definitely check out Jeremy Gray's proposal of using the built-in psychopy.visual.RatingScale (another answer to this question).

Untested code:

timer = core.CountdownTimer(TrialDuration)
stepSize = 1
for eachPic in catPictures:
    prevPos = 0  # keeps track of the slider position
    rts=[]  # used to keep track of what the latest reaction time was. Reset in the beginning of every trial.

    timer.reset()
    event.clearEvents() # get rid of other, unprocessed events
    while timer.getTime() > 0:
    for key, rt in event.getKeys(timeStamped=timer):  # time keys to this clock
        rts += [rt]  # add this reaction time to the list
        if key in ['escape']:
            core.quit() # quit if they press escape
        if key in ['b']:
            # add keypress to list for each keypress. then move cursor proportionally to length of this list
            prevPos+=stepSize
        if key in ['t']:
            prevPos-=stepSize

    # Log here instead of print
    print rts[-1]
Jonas Lindeløv
  • 5,442
  • 6
  • 31
  • 54
  • 1. good call. 2. also good call. I pasted from gedit, where everything was indented correctly. 3. It's because it lets the cursor move more quickly if they press a lot of keys quickly. otherwise it can take a long time to move the cursor from one side of the rating scale to the other. – Maria Sep 08 '15 at 21:35
  • I want to print (or log) the time when the last key is pressed, not what the last key is. I'm not sure how to do that, but it would probably involve setting a variable to equal `clock.getTime()` when the last key is pressed. edit: I made it work. thanks! – Maria Sep 08 '15 at 21:38
  • I updated the answer to get the reaction time instead. I also removed two unnecessary lines (``b_list.append(key)`` and the other). And made a point 5 that you should definitely check out the other answer that popped in yesterday :-) – Jonas Lindeløv Sep 10 '15 at 20:32
2

For a given rating scale, rs, all of the subject's activity is available in rs.history, both during a trial and afterwards. The history is just a list of tuples, where each tuple is (rating, time). If the scale has started, the first tuple is always (None, 0.0). If the last two ratings are the same, then the subject accepted that rating. If they are different, the subject was moving around on the scale but had not accepted a rating at the point when the scale timed out.

Example history: [(None, 0.0), (3, 0.777), (3, 1.396)]

from psychopy import visual, core
win = visual.Window()
rs = visual.RatingScale(win)

c = core.CountdownTimer(3)
while c.getTime() > 0:
    rs.draw()
    win.flip()

# print or log:
print rs.history  # entire history
print rs.history[-1][1]  # just the time of the last rating
jrgray
  • 415
  • 3
  • 9