Gaze-contingency Loop only Works on First Iteration

Hello everyone!

I am seeking help to make my gaze-contingent trials work.

I am currently programming an experiment where the trials do not progress without proper gaze fixation on the center of the screen (using an Eyelink 1000 plus eye tracker). I have built a few nested loops to ensure proper fixation before flashing the display for a given trial. This works for the first iteration/trial, but as you can guess from my posting here, there are problems. Every trial after the first just continues without measuring gaze. Does anyone know why this would be?

I will attach the part of my code that feels involved with the problem, and I will attach a shortened version of my entire script to this post.

Thank you all for your help! I really appreciate it.

example_psychopy.py (13.9 KB)

if eye_track: # if we're using the eyetracker, use the gaze-contingency check before showing the trial

            raw_sample = eye_tracker.getNewestSample()
            if raw_sample == None: # if eye tracker cannot find an eye
                gaze_pos = [-32768, -32768] # setting a random position 
            else: 
                if eye_used == 0 or eye_used == 2: 
                    gaze_pos = raw_sample.getLeftEye().getGaze()
                elif eye_used == 1:
                    gaze_pos = raw_sample.getRightEye().getGaze()

            x_py = gaze_pos[0] - 960 # x coordinate of eye-tracker translated into pixel on monitor 
            y_py = 540 - gaze_pos[1] # y coordinate of eye-tracker translated into pixel on monitor 

            # waiting for fixation to display Gabors 
            min_fixation_gaze_time = .5 # minimum fixation time of .5 seconds 

            display = False # setting display as false so the trial doesn't continue until conditions are met 
            while not display: # while we are waiting to show the trial
                
                outside = False
                
                if (x_py < minimum_x) or (x_py > maximum_x) or (y_py < minimum_y) or (y_py > maximum_y):
                    outside = True
                    break
                    
                elif (minimum_x < x_py < maximum_x) and (minimum_y < y_py < maximum_y): # if the participant's gaze is within the boundaries

                    fixation_gaze_time = core.Clock() # initialize the clock
                    
                    while fixation_gaze_time.getTime() <= min_fixation_gaze_time:
                        
                        if (x_py < minimum_x) or (x_py > maximum_x) or (y_py < minimum_y) or (y_py > maximum_y):
                            outside = True
                            break
                    
                    if not outside and fixation_gaze_time.getTime() >= min_fixation_gaze_time: 
                        display = True
                    
            eye_tracker.sendMessage(f'trial {trial} display start') # send a message to the eye tracker when the display has started

            # display the Gabors!            
            while display: # while display is active 
                
                if (x_py < minimum_x) or (x_py > maximum_x) or (y_py < minimum_y) or (y_py > maximum_y): # if gaze x/y is less than min or max x/y while the display is active and time is a' tickin
                    list_of_objects.append(condition) # make the participant view this set of conditions again later 
                    trials += 1 # and add a trial to the total number of trials

                # displaying the gabors for one frame (refresh rate of eyeTrackMon is 60Hz, then each frame is ~16 msec)
                win.flip() # display!
                win.flip() # clear screen

                display = False
            
            eye_tracker.sendMessage(f'trial {trial} display end') # send a message to the eye tracker when the trial has ended
    
        else: # just do the display if no eyetracking

            for p in pos: 
                gabor.pos = p
            gabor.draw()
            win.flip() # display Gabors
            win.flip() # Gabors offset

Hello @lilih

1. Gaze is sampled only once (main bug)
getNewestSample() and the calculation of x_py/y_py happen once, before the loops. Inside the while loops the same stale values are compared over and over, so the fixation check never reflects the participant’s actual gaze. The 0.5 s “fixation” is just a busy-wait on a frozen value.

2. break on “outside” exits the wait loop
If the gaze is outside the box, the code breaks out of while not display instead of continuing to wait. display stays False, so the trial proceeds without a valid fixation check. This is the most likely reason later trials seem to run without measuring gaze.

3. Fixation timer logic is broken
The inner loop checks static values, and outside is never reset properly when fixation is interrupted. The timer is not restarted on a break in fixation, so continuous fixation is not actually enforced.

4. while display: also uses stale coordinates
The check during the display phase relies on the old x_py/y_py. display is only set to False after win.flip(), which can cause repeated append/trials += 1 calls or an endless loop.

5. Stimulus drawing is missing in the eye-tracking branch
gabor.draw() appears only in the else branch (no eye tracking), so in the eye-tracking branch nothing is drawn before win.flip() (unless it was omitted from the pasted snippet).

6. Missing-data handling
raw_sample == None should be is None. Blinks (EyeLink missing value -32768) only work by accident because the value happens to fall outside the box. This should be handled explicitly.

7. Timing of the eye-tracker messages
sendMessage is called before/around win.flip() rather than synchronized with it, so the “display start” marker can be off by up to a frame. win.callOnFlip(...) is the better approach.

9. No escape or timeout
There is no way to abort the waiting loop if the tracker loses the eye or the participant doesn’t fixate.

Conceptual note: With a single-frame display (~16 ms), monitoring gaze “while display” isn’t meaningful. It’s better to check gaze just before and right after onset, and verify fixation offline using the EDF messages.

Best wishes Jens