-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot_experiment.py
92 lines (75 loc) · 2.49 KB
/
dot_experiment.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
import random
from psychopy import visual, event, core
import time
def get_typed_answer(win, guess):
"""
Retrieve user input.
Args:
win(object): The Psychopy window used.
guess(object): The answer_text object where we will save the guess.
Returns:
int: The user's guess.
"""
instruction_text = visual.TextStim(win, text=u'How many dots did you see?', pos=(0, 100))
guess.text = ''
timeout = time.time() + 5
while True:
key = event.waitKeys()[0]
# Add a new number
if key in '1234567890':
guess.text += key
# Delete last character, if there are any chars at all
elif key == 'backspace' and len(guess.text) > 0:
guess.text = guess.text[:-1]
# Stop collecting response and return it
elif key == 'return':
return (guess.text)
# Show current answer state
instruction_text.draw()
guess.draw()
win.flip()
def run_experiment(win, answer_text, n_dots, contrast):
"""
Runs the dot experiment using the Psychopy Toolbox, which presents
a uniform distribution of dots across a 500x500 screen. This is used by the Bayesian
active model selection code in `psychopy_learner.py` which manipulates a combination of these variables
in order to converge to the kernel grammar which best describes the relationship between stimuli and
human behavior.
Args:
win(object): The Psychopy window used.
answer_text(object): The answer_text object where we will save the guess.
n_dots(int): The number of dots presented.
contrast(float): The contrast value of the dots.
Returns:
int: The user's guess.
"""
dot_xys = []
for dot in range(n_dots):
dot_x = random.uniform(-250, 250)
dot_y = random.uniform(-250, 250)
dot_xys.append([dot_x, dot_y])
# print(dot_xys)
dot_stim = visual.ElementArrayStim(
win=win,
units="pix",
elementTex=None,
elementMask="circle",
sizes=10,
contrs=contrast,
nElements=n_dots,
xys=dot_xys,
)
dot_stim.draw()
win.flip()
# Collect response
return (get_typed_answer(win, answer_text))
if __name__ == "__main__":
# Run a test of the experiment.
win = visual.Window(
size=[500, 500],
units="pix",
fullscr=False
)
answer_text = visual.TextStim(win)
guess = run_experiment(win, answer_text, 10, .90)
print(guess)