Help with "while False:" loop
Disclaimer: Whilst my experiment implements PyGaze functions, I felt that this was more of an OpenSesame issue. Hence, I posted my question in the OpenSesame sub-forum.
Background: I am programming a gaze-contingent eye-tracking design, starting with a fixation dot. Originally this is a sketchpad, but for the current status I found it easier to use the canvas
functions. I have some Python experience (I created an ssVEP/EEG experiment with PsychoPy) and used the for-loop in Matlab/PsychToolbox, but the while-loop is reasonably new to me.
What I want it to do: A fixation dot is presented, and an area of interest is drawn around it, let's call this fixation-area. If the participant does not fixate the fixation-area, nothing will happen until it times out. Once the gaze is directed at the fixation-area, a timer will start. After 500 ms the next screen will be presented.
Please note that the following coordinates are not final: I currently present the experiment in a small window to keep an eye on the debug-window. And I noticed that PyGaze does not use uniform coordinates, so I unticked this. The fixation dot and AOI on this screen are in the centre.
Prepare
phase:
# Set up constants. Note that I am drawing a visual representation of my AOI. Coordinates are not final.
my_canvas = canvas()
my_canvas.fixdot()
my_canvas.circle(350, 195, 100, fill=False, color='red')
# Set the Area around the fixtion dot (type, pos, size)
FixAOI = AOI('circle', (350,195), 100)
FixDuration = 495
timeout = 10000
Run
phase:
# Show the screen. Eventually I want to use sketchpad items instead of canvases.
my_canvas.show()
# Get the current time and print this for debugging purposes.
t0 = self.time()
print('T0 %f' % t0)
# Get and print the x,y tuple of the gaze position. Start the timer.
GazePos = eyetracker.sample()
print('GazePos = ', GazePos)
var.current_time = self.time() - t0
# As long as the gaze position is not within the AOI, wait until it is or time out. This calls on the imported AOI plugin. Sleep command set for now to avoid drowning the debug window
while False:
FixAOI.contains(GazePos)
var.current_time = self.time() - t0
if var.current_time < timeout:
print('Duration if not hit =', var.current_time)
clock.sleep(50)
elif var.current_time >= timeout:
print('timed out')
break
else:
print('FML')
break
# When the gaze position IS within the AOI, count the timer until the given fixatio duration is reached.
while True:
FixAOI.contains(GazePos)
var.current_time = self.time() - t0
if var.current_time < FixDuration:
#t1 >= FixDuration
print(u'T1 during hit', var.current_time)
clock.sleep(10)
elif var.current_time >= FixDuration:
print('Fixated!')
break
else:
print('hit but FML')
break
What it does: it skips the False
part altogether, even though GazePos
is printed as outside of the fixation-area. The timer seems to work okay and OpenSesame succesfully presents the next screen, but it appears to just see FixAOI.contains(GazePos)
as True
.
It is probably just a rookie error in creating the loop. Any help would be greatly appreciated!
Comments
Hi,
You're misunderstanding the semantics of a
while
loop!This:
Means: enter an infinite loop that runs
something()
over and over again. It does not mean: run the loop whilesomething()
isTrue
.Analogously, this:
Means: never enter the loop (because the criterion is already
False
on the first iteration)—in other words, it does nothing. It does not mean: run the loop whilesomething()
isFalse
. This does:However, I think it's best to take a step back and familiarize yourself with basic Python syntax. That won't take long, and will avoid you from getting stuck every step of the way.
Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Thanks @sebastiaan, that really cleared things up! I made it work with the following script. It needs some tweaking as it is ugly, but moreover, even with the
clock.sleep()
function the execution seems to carry on after the onset of the next screen (although maybe that was just when I printed debugging data, but I don't want to chance it).Wonderful, thanks again!