forked from neurotech-berkeley/neurotech-course
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stroop_test.py
executable file
·198 lines (149 loc) · 4.95 KB
/
stroop_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# from myo_raw import MyoRaw
# from csv_collector_myo import CSVCollector
import pygame
from pygame.locals import *
import random
import time
import sys
import os
fullscreen_on = False
size = width, height = int(2560/2), int(1600/2)
black = (0,0,0)
white = (255, 255, 255)
background_color = white
def stroop(congruent = False, speed=1.0, duration=20):
"""Stroop Test HARD"""
colors = {"BLUE" : (0,0,225), "GREEN" : (0,100,0), "YELLOW" : (230,212,41),
"RED" : (255,0,0), "PURPLE" : (160,32,240), "BLACK" : (0,0,0)}
def newcolor():
# any color but black or white
return (random.choice(list(colors.values())))
def write(msg="Hello", color = (0,100,0)):
myfont = pygame.font.SysFont("None", 250)
mytext = myfont.render(msg, True, color)
mytext = mytext.convert_alpha()
return mytext
x = width / 2.0
y = height / 2.0
dx = 0
dy = 0
background = pygame.Surface((screen.get_width(), screen.get_height()))
background.fill(background_color) # white
background = background.convert()
screen.blit(background, (0,0)) # clean whole screen
clock = pygame.time.Clock()
# FPS = 1.5 * speed # desired framerate in frames per second.
wait_time = 1.0 / speed
start = time.time()
while time.time() - start < duration:
screen.blit(background, (0,0)) # clean whole screen
# milliseconds = clock.tick(FPS) # milliseconds passed since last frame
# seconds = milliseconds / 1000.0 # seconds passed since last frame
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
text = random.choice(["RED", "GREEN", "BLUE", "BLACK", "PURPLE"])
color = (newcolor())
if congruent:
color = colors[text]
textsurface = write(text, color)
#screen.blit(background, (0,0)) # clean whole screen
rect = textsurface.get_rect()
rect.center = (x, y)
screen.blit(textsurface, rect)
pygame.display.flip()
time.sleep(wait_time)
check_collection()
# pygame.quit()
return True
def draw_text(text, center, color=black, size=22, bold=False, background=None,
left=False):
# font = pygame.font.Font(font_path, size, bold=bold)
font = pygame.font.SysFont("None", size, bold=bold)
if background:
surface = font.render(text, True, color, background)
else:
surface = font.render(text, True, color)
rect = surface.get_rect()
rect.center = tuple(center)
if left:
rect.left = center[0]
screen.blit(surface, rect)
return rect
def text_slide(text, duration=20):
screen.fill(background_color)
draw_text(text, (width/2.0, height/2.0), size=250)
pygame.display.flip()
start = time.time()
while time.time() - start < duration:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return False
time.sleep(0.01)
check_collection()
return True
def focus_slide(duration=20):
return text_slide('+', duration)
condname = sys.argv[1]
# collector = CSVCollector(fname=fname)
# collector.tag('start')
def check_collection():
# if collector.time_since_last_sample() > 1.2:
# print('detected disconnect!')
# print('attempting reconnect')
# # collector.stop()
# time.sleep(1.5)
# # collector.start()
# time.sleep(2)
pass
# print("get ready...")
# time.sleep(2)
# collector.start()
# print("a bit more...")
# time.sleep(2)
# print("go!")
pygame.init()
if fullscreen_on:
screen = pygame.display.set_mode(size, FULLSCREEN)
else:
screen = pygame.display.set_mode(size)
pygame.mouse.set_visible(False)
# relax, movement
# low, medium, hard
# speed 0.5, 1.0, 2.0
trial_duration = 20
focus_duration = 5
num_repeats = 2
# trial_duration = 2
# focus_duration = 1
# num_repeats = 1
conditions = {
'easy': ('easy', 1.1, True),
'medium': ('medium', 1.1, False),
'hard': ('hard', 1.8, False)
}
conds = [conditions[condname]]
# collector.tag('focus')
for cond_name, speed, congruent in conds:
x = focus_slide(focus_duration)
if not x:
break
if speed is None:
# collector.tag(cond_name)
x = text_slide(cond_name, trial_duration)
else:
# collector.tag(cond_name)
x = stroop(congruent, speed, trial_duration)
# check_collection()
if not x:
break
pygame.quit()
os._exit(0)