Q:

Psychopy and pylink example

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!

A:

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

A:

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.

