Commit 8d5ca3d0 by Laura Rodríguez

16 jun

parent 65c73dc7
from pygaze.display import Display
from pygaze.screen import Screen
from pygaze.eyetracker import EyeTracker
from pygaze.logfile import Logfile
from pygaze.keyboard import Keyboard
from random import choice
from time import sleep, time
# Inicialización
disp = Display()
screen = Screen()
tracker = EyeTracker(disp)
kb = Keyboard()
log = Logfile()
# Parámetros globales
n_trials = 5
arrow_length = 300
arrow_color = (255, 255, 255)
center = (disp.dispsize[0] // 2, disp.dispsize[1] // 2)
offset = 400
left_pos = (center[0] - offset, center[1])
right_pos = (center[0] + offset, center[1])
# Función para comprobar tecla de salida
def check_exit_key():
key, _ = kb.get_key(keylist=['e'], timeout=0.01)
return key == 'e'
# Espera con posibilidad de salida
def wait_with_exit(timeout):
start = time()
while time() - start < timeout:
if check_exit_key():
cerrar_experimento("Experimento terminado por el usuario con tecla E.")
sleep(0.01)
# Cierre ordenado
def cerrar_experimento(msg):
disp.close()
tracker.close()
log.close()
print(msg)
exit()
# Mostrar instrucciones
def mostrar_instrucciones():
screen.clear(colour=(0, 0, 0))
screen.draw_text(
"INSTRUCCIONES DETALLADAS:\n\n"
"🟢 Círculo VERDE: mira al CÍRCULO GRANDE en la dirección que indica la flecha.\n"
"🔴 Círculo ROJO: mira al CÍRCULO PEQUEÑO, en dirección contraria a la flecha.\n"
"🟠 Círculo NARANJA: NO muevas la vista, mantén la mirada en el centro.\n\n"
"Pulsa ESPACIO para comenzar la prueba.\n"
"Pulsa E para salir.",
fontsize=30, colour=(255, 255, 255)
)
disp.fill(screen)
disp.show()
while True:
key, _ = kb.get_key(keylist=['space', 'e'])
if key == 'space':
break
elif key == 'e':
cerrar_experimento("Experimento terminado por el usuario con tecla E antes de empezar.")
# Ejecutar todos los trials
def run_trials():
for trial in range(n_trials):
# Mensaje previo al trial
screen.clear(colour=(0, 0, 0))
screen.draw_text(f"Trial {trial + 1} de {n_trials}\nPulsa E para salir o espera para comenzar...",
fontsize=30, colour=(255, 255, 255))
disp.fill(screen)
disp.show()
wait_with_exit(2.0)
# Dirección aleatoria
direction = choice(["left", "right"])
condition = choice(["pro", "anti", "nogo"])
# Color central según condición
if condition == "pro":
central_color = (0, 255, 0) # Verde
elif condition == "anti":
central_color = (255, 0, 0) # Rojo
else:
central_color = (255, 165, 0) # Naranja
# Mostrar estímulo central
screen.clear(colour=(0, 0, 0))
screen.draw_circle(colour=central_color, pos=center, r=28, fill=True)
disp.fill(screen)
disp.show()
wait_with_exit(1.0)
# Pantalla con flecha y círculos
screen.clear(colour=(0, 0, 0))
screen.draw_circle(colour=central_color, pos=center, r=28, fill=True)
if direction == "left":
big_circle_pos = left_pos
small_circle_pos = right_pos
else:
big_circle_pos = right_pos
small_circle_pos = left_pos
screen.draw_circle(colour=(255, 255, 255), pos=big_circle_pos, r=52, fill=True)
screen.draw_circle(colour=(255, 255, 255), pos=small_circle_pos, r=38, fill=True)
# Dibujar flecha si no es NoGo
if condition != "nogo":
if condition == "pro":
arrow_dir = direction
else:
arrow_dir = "left" if direction == "right" else "right"
if arrow_dir == "right":
end = (center[0] + arrow_length, center[1])
screen.draw_line(colour=arrow_color, spos=center, epos=end, pw=6)
screen.draw_line(colour=arrow_color, spos=end, epos=(end[0] - 30, end[1] - 30), pw=6)
screen.draw_line(colour=arrow_color, spos=end, epos=(end[0] - 30, end[1] + 30), pw=6)
else:
end = (center[0] - arrow_length, center[1])
screen.draw_line(colour=arrow_color, spos=center, epos=end, pw=6)
screen.draw_line(colour=arrow_color, spos=end, epos=(end[0] + 30, end[1] - 30), pw=6)
screen.draw_line(colour=arrow_color, spos=end, epos=(end[0] + 30, end[1] + 30), pw=6)
# Mostrar estímulo y comenzar registro
disp.fill(screen)
tracker.start_recording()
tracker.log("TRIAL_%d_COND_%s_DIR_%s" % (trial + 1, condition.upper(), direction.upper()))
disp.show()
wait_with_exit(1.5)
tracker.stop_recording()
# Pausa entre trials
screen.clear(colour=(0, 0, 0))
screen.draw_text("+", fontsize=40)
disp.fill(screen)
disp.show()
wait_with_exit(0.8)
# Preguntar si repetir
def preguntar_repetir():
screen.clear(colour=(0, 0, 0))
screen.draw_text("Test completado. ¿Quieres volver a intentarlo?\n\nPulsa S para repetir o N para salir.",
fontsize=30, colour=(255, 255, 255))
disp.fill(screen)
disp.show()
while True:
key, _ = kb.get_key(keylist=['s', 'n', 'e'])
if key == 's':
return True
elif key in ['n', 'e']:
return False
# ---------- Ejecución principal ----------
mostrar_instrucciones()
while True:
run_trials()
if not preguntar_repetir():
screen.clear(colour=(0, 0, 0))
screen.draw_text("Gracias por participar.", fontsize=30)
disp.fill(screen)
disp.show()
sleep(2.0)
cerrar_experimento("Finalización voluntaria tras test completado.")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment