| Reference | Downloads | Github

Recording cursor time in stimulus, capable of re-entry and exit

What are you trying to achieve?:

  • Cursor dwell time within a certain ‘correct’ quadrant (four circles presented, one randomly chosen to turn red, signaled as target).
  • Must be able to record time even if subject removes cursor from stimulus and re-enters - or doesn’t! Which brings up one of the issues I am trying to solve: ensuring that if the subject decides to stay within the stimulus, or out, until the end of the trial, I am able to still record the time.
    *Each routine is 3 seconds long, with the target circle presenting itself 1 second in. Thus, 2 seconds is the theoretical max for cursor dwell time.

What did you try to make it work?:

#begin routine
inQuad = False
outQuad = True

totalTimeIn = []
totalTimeOut = []

#each frame-----
#inside quad
if CorrectQuad.contains(mouse):
    countIn = core.CountdownTimer(2)
    inQuad = True
    outQuad = False

#exits quad
if not CorrectQuad.contains(mouse) and inQuad:
    timeIn = 2 - countIn.getTime()

#outside quad
if not CorrectQuad.contains(mouse):
    countOut = core.CountdownTimer(2)
    inQuad = False
    outQuad = True

#enters quad
if CorrectQuad.contains(mouse) and not inQuad:
    timeOut = 2 - countOut.getTime()

#end routine-----
#for when participant dwells till trial is done
totalTime = int(sum(totalTimeIn) + sum(totalTimeOut))
finalCountIn = int(countIn.getTime())
finalCountOut = int(countOut.getTime())

if totalTime < 2 and finalCountIn < 2:
    totalTimeIn.append(2 - finalCountIn)

elif totalTime < 2 and finalCountOut < 2:
    totalTimeOut.append(2 - finalCountOut)

thisExp.addData ('timeInQuad (s)', sum(totalTimeIn))
thisExp.addData ('timeOutQuad (s)', sum(totalTimeOut))

What specifically went wrong when you tried that?:

  • It seems to (incorrectly) record dwell time within the correct quadrant, but not out (see attached). I suppose once I figure out how to record dwell time within, I could simply subtract from 2 to find dwell time outside.
  • The issue is that I want it to getTime on variable change (ex. inQuad changes from true to false), but as of now, I think I’ve set impossible conditions with if CorrectQuad.contains(mouse) and not inQuad. Help!
    psychopy data issue

At the moment, on every screen refresh where the mouse is in the target, you reset the timer. So you should change that check so that it only happens when you first detect the entry into the target:

if CorrectQuad.contains(mouse) and not inQuad:

You also don’t want to create a countdown timer here, as the mouse can enter at any time, and can occur multiple times. There is no reason to keep using 2 seconds as some sort of landmark: you are interested in measuring intervals between entering and exiting a region. That has no relationship to the duration of the trial. So just use a regular timer and count up to measure the time spent inside.

Also you need to set inQuad to False in the second if statement. Then the final one becomes:

if not CorrectQuad.contains(mouse) and not outQuad:

And again, use a regular timer instead of a countdown.

Lastly, you will need to insert some code in the “end routine” tab, because in the last frame the mouse will either be in or outside either region and you will need to decide which and capture the last time.

1 Like

Thank you Michael, it’s evident I need to work on my booleans more. Your suggestions helped guide me to the solution.