Psychopy and pylink example
Asked Answered
F

3

6

I'm working on integrating an experiment in psychopy with the eyelink eyetracking system. The way to do this seems to be through pylink. Unfortunately I'm really unfamiliar with pylink and I was hoping there was a sample of an experiment that combines the two. I haven't been able to find one. If anyone would be able to share an example or point me towards a more accessible manual than the pylink api that sr-research provides I'd be really grateful.

Thanks!

Fleischman answered 28/1, 2016 at 20:31 Comment(0)
F
8

rather than PyLink, you might want to look into using the ioHub system within PsychoPy. This is a more general-purpose eye tracking system that also allows for saving data in a common format (integrated with PsychoPy events), and provides tools for data analysis and visualisation.

ioHUb is built to be agnostic to the particular eye tracker you are using. You just need to create a configuration file specific to your EyeLink system, and thereafter use the generic functions ioHiv provides for calibration, accessing gaze data in real-time, and so on.

There are some teaching resources accessible here: http://www.psychopy.org/resources/ECEM_Python_materials.zip

Furnishings answered 28/1, 2016 at 21:42 Comment(1)
Thanks! that's great, I found a Stroop task that I can probably modify. Cheers.Fleischman
A
10

I am glad you found your solution. I have not used iohub, but we do use psychopy and an eyelink and therefore some of the following code may be of use to others who wish to invoke more direct communication. Note that our computers use Archlinux. If none of the following makes any sense to you, don't worry about it, but maybe it will help others who are stumbling along the same path we are.

Communication between experimental machine and eye tracker machine

First, you have to establish communication with the eyelink. If your experimental machine is turned on and plugged into a live Eyelink computer then on linux you have to first set your ethernet card up, and then set the default address that Eyelink uses (this also works for the Eyelink 1000 - they kept the same address). Note your ethernet will probably have a different name than enp4s0. Try simply with ip link and look for something similar. NB: these commands are being typed into a terminal.

#To set up connection with Eyelink II computer:
#ip link set enp4s0 up
#ip addr add 100.1.1.2/24 dev enp4s0

Eyetracker functions

We have found it convenient to write some functions for talking to the Eyelink computer. For example:

Initialize Eyetracker

sp refers to the tuple of screenx, screeny sizes.

def eyeTrkInit (sp):
        el = pl.EyeLink()
        el.sendCommand("screen_pixel_coords = 0 0 %d %d" %sp)
        el.sendMessage("DISPLAY_COORDS  0 0 %d %d" %sp)
        el.sendCommand("select_parser_configuration 0")
        el.sendCommand("scene_camera_gazemap = NO")
        el.sendCommand("pupil_size_diameter = %s"%("YES"))
        return(el)

NB: the pl function comes from import pylink as pl. Also, note that there is another python library called pylink that you can find on line. It is probably not the one you want. Go through the Eyelink forum and get pylink from there. It is old, but it still works.

Calibrate Eyetracker

el is the name of the eyetracker object initialized above. sp screen size, and cd is color depth, e.g. 32.

def eyeTrkCalib (el,sp,cd):
     pl.openGraphics(sp,cd)
     pl.setCalibrationColors((255,255,255),(0,0,0))
     pl.setTargetSize(int(sp[0]/70), int(sp[1]/300)) 
     pl.setCalibrationSounds("","","")
     pl.setDriftCorrectSounds("","off","off")
     el.doTrackerSetup()
     pl.closeGraphics()
     #el.setOfflineMode()

Open datafile

You can talk to the eye tracker and do things like opening a file

def eyeTrkOpenEDF (dfn,el):
     el.openDataFile(dfn + '.EDF')

Drift correction

Or drift correct

def driftCor(el,sp,cd):
    blockLabel=psychopy.visual.TextStim(expWin,text="Press the space bar to begin drift correction",pos=[0,0], color="white", bold=True,alignHoriz="center",height=0.5)
    notdone=True            
    while notdone:
        blockLabel.draw()
        expWin.flip()
        if keyState[key.SPACE] == True:
            eyeTrkCalib(el,sp,cd)
            expWin.winHandle.activate()
            keyState[key.SPACE] = False
            notdone=False

Sending and getting messages.

There are a number of built-in variables you can set, or you can add your own. Here is an example of sending a message from your python program to the eyelink

eyelink.sendMessage("TRIALID "+str(trialnum))
    eyelink.startRecording(1,1,1,1)

eyelink.sendMessage("FIX1")
    tFix1On=expClock.getTime()

Gaze contingent programming

Here is a portion of some code that uses the eyelink's most recent sample in the logic of the experimental program.

while notdone:
            if recalib==True:
                dict['recalib']=True
                eyelink.sendMessage("RECALIB END")
                eyelink.startRecording(1,1,1,1)
                recalib=False
            eventType=eyelink.getNextData()
            if eventType==pl.STARTFIX or eventType==pl.FIXUPDATE or eventType==pl.ENDFIX:
                sample=eyelink.getNewestSample()
        
                if sample != None:
                    if sample.isRightSample():
                        gazePos = sample.getRightEye().getGaze()
                    if sample.isLeftSample():
                        gazePos = sample.getLeftEye().getGaze()
            
                gazePosCorFix = [gazePos[0]-scrx/2,-(gazePos[1]-scry/2)]
                
                posPix = posToPix(fixation)
                eucDistFix = sqrt((gazePosCorFix[0]-posPix[0])**2+(gazePosCorFix[1]-posPix[1])**2)
            
                if eucDistFix < tolFix:
                    core.wait(timeFix1)
                    notdone=False
                    eyelink.resetData()
                    break

Happy Hacking.

Abott answered 1/2, 2016 at 15:4 Comment(4)
Do you run this in psychopy coder or something else?Marr
Mostly we write our experiment as 'someName.py' and the above example would be in that "program." You could start with code generated by the coder, and then edit it. We usually just write our programs as a plain text file in a word processor saved with the .py extension. In short we use psychopy as library and not as a programming environment.Abott
Where can I find a list of accepted commands for sendMessage()?Livonia
If you look at the dates you will see that this conversation is from a decade ago. I suspect it is all out of date. I recommend going to the psychopy forum for psychopy questions: discourse.psychopy.org. For eyelink stuff try sr-research.com/support. It is a free signup.Abott
F
8

rather than PyLink, you might want to look into using the ioHub system within PsychoPy. This is a more general-purpose eye tracking system that also allows for saving data in a common format (integrated with PsychoPy events), and provides tools for data analysis and visualisation.

ioHUb is built to be agnostic to the particular eye tracker you are using. You just need to create a configuration file specific to your EyeLink system, and thereafter use the generic functions ioHiv provides for calibration, accessing gaze data in real-time, and so on.

There are some teaching resources accessible here: http://www.psychopy.org/resources/ECEM_Python_materials.zip

Furnishings answered 28/1, 2016 at 21:42 Comment(1)
Thanks! that's great, I found a Stroop task that I can probably modify. Cheers.Fleischman
B
1

For future readers, I wanted to share my library for combining pylink and psychopy. I've recently updated it to work with python 3. It provides simple to use, high level functions.

https://github.com/colinquirk/templateexperiments/tree/master/eyelinker

You could also work at a lower level with the PsychoPyCustomDisplay class (see the pylink docs for more info about EyeLinkCustomDisplay).

For an example of it in use, see:

https://github.com/colinquirk/ChangeDetectionEyeTracking

(At the time of writing, this experiment code is not yet python 3 ready, but it should still be a useful example.)

The repo also includes other modules for creating experiments and recording EEG data, but they are not necessary if you are just interested in the eyelinker code.

Beforetime answered 22/12, 2019 at 1:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.