psychopy.org | Reference | Downloads | Github

Getting a mouse trajectory timestamp

Hello,
I’d like to get a timestamp for a mouse trajectory like [42870.0, 42880.0, 42890.0, 42900.0, 42910.0, 42920.0, 429…].
Specifically, I’d like to get a dataframe with column of ‘timestamp’, ‘pos_x’ and ‘pos_y’.

Please give me advice how can I track the time of mouse trajectory movement.

Thank you!

        mouse = event.Mouse(visible = True, newPos=[0,-10])
        mouse.clickReset()
        timer = core.Clock()
        routineTimer = core.CountdownTimer()
        responseClock = core.Clock()

        t = 0
        responseClock.reset()  # clock
        frameN = -1
        continueRoutine = True
        routineTimer.add(2.500000)
        # update component parameters for each repeat
        # setup some python lists for storing info about the mouse
        # mouse.time = []
        pos_x = []
        pos_y = []
        gotValidClick = False  # until a click is received
        
        stimulus = self.drawer.draw_stimulus(design['cue'], train)
        tent = self.drawer.draw_tent()
        self.window.flip()

        # keep track of which components have finished
        responseComponents = [mouse, stimulus, tent]
        for thisComponent in responseComponents:
            if hasattr(thisComponent, 'status'):
                thisComponent.status = NOT_STARTED
        
        # -------Start Routine "response"-------
        while continueRoutine and routineTimer.getTime() > 0:
            # get current time
            t = responseClock.getTime()
            frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
            # update/draw components on each frame
            # *mouse* updates
            if t >= 0.0 and mouse.status == NOT_STARTED:
                # keep track of start time/frame for later
                mouse.tStart = t
                mouse.frameNStart = frameN  # exact frame index
                mouse.status = STARTED
                mouse.mouseClock.reset()
                prevButtonState = mouse.getPressed()  # if button is down already this ISN'T a new click
            frameRemains = 0.0 + 2.5 - self.window.monitorFramePeriod * 0.75 # most of one frame period left
            if mouse.status == STARTED and t >= frameRemains:
                mouse.status = STOPPED
            if mouse.status == STARTED:  # only update if started and not stopped!
                mouse.getPos()
                pos_x.append(mouse.getPos()[0])
                pos_y.append(mouse.getPos()[1])

In your Builder experiment, add a code component to the same routine as the tracked mouse. Then, add the following code to the code component relevant tabs:

# Begin Routine
mouseT = []  # Time
mouseX = []  # X pos
mouseY = []  # Y pos

# Each Frame
mouseT.append(t)
mouseX.append(mouse.getPos()[0])
mouseY.append(mouse.getPos()[1])

# End Routine
thisExp.addData("mouseT", mouseT)
thisExp.addData("mouseX", mouseX)
thisExp.addData("mouseY", mouseY)

Regarding the time, this will be relative to each trial, starting from 0 at the beginning of each trial.