-
Notifications
You must be signed in to change notification settings - Fork 4
/
experiments.py
executable file
·188 lines (136 loc) · 5.28 KB
/
experiments.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
#usr/bin/env/python
"""
C. Battista PsychoMetric Experiments Module
Copyright (C) 2011 Christian Joseph Battista
email - battista.christian@gmail.com
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program, 'LICENSE.TXT'; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
#here is the standard subject info GUI you use at the start of most experiments
#we ask for the subject #, the session #, the subject's sex and their handedness
import pygame
import Image
import VisionEgg
from VisionEgg.GUI import *
from VisionEgg.Core import *
from VisionEgg.FlowControl import Presentation
from VisionEgg.Text import *
from VisionEgg.Textures import *
class Point:
pass
def printWord (screen, theText, fontSize, theColor):
x = (screen.get_size()[0] / 2)
y = screen.get_size()[1] / 2
myFont = pygame.font.Font(None, fontSize)
x = x - (myFont.size(theText)[0] / 2)
y = y - (myFont.size(theText)[1] / 2)
word = Text(text = theText, position = (x,y), color = theColor, font_size = fontSize)
viewport = Viewport (screen = screen, stimuli = [word])
return word, viewport
def printText (screen, theText, fontSize, theColor):
instructions = []
textColor = theColor
screensize = screen.get_size()
spacebuff = Point()
spacebuff.x = screensize[0]/12
spacebuff.y = screensize[1] - (screensize[1]/12)
myFont = pygame.font.Font(None, fontSize)
fittedText = []
theText = theText.split('\n')
for line in theText:
myWords = line.split(' ')
doesItFit = ""
while len(myWords):
word = myWords.pop(0)
itFits = doesItFit
doesItFit += word + (" ")
fitVal = screensize[0] - spacebuff.x * 2
if myFont.size(doesItFit)[0] < fitVal:
itFits = doesItFit
else:
fittedText.append(doesItFit)
doesItFit = ""
fittedText.append(itFits)
xpos = screen.size[0] / 2
ypos = screen.size[1] / 2 + len(fittedText) / 2 * myFont.size(fittedText[0])[1]
for line in fittedText:
instructions.append(Text
(text = line,
anchor = 'center',
position = (xpos, ypos - fittedText.index(line) * myFont.size(line)[1]),
color = theColor,
font_size = fontSize)
)
viewport = Viewport( screen=screen, stimuli=instructions)
return instructions, viewport
#this function blits some parsed text on the screen
#it takes the screen to blit to as an argument and also the file which holds the text
#since it just shows text and then quits once SPACEBAR is pressed, it is good for showing experiment instructions, hence the name
def showInstructions(screen, text, textSize=60, textcolor=(255, 255, 255)):
#create our instruction screen
#load up the instruction text
insStim, insView = printText(screen, text, textSize, textcolor)
instructions = Presentation(go_duration=('forever',), viewports=[insView])
#add a quit function and a handler to go with it
def quitKey(event):
if event.key == pygame.locals.K_SPACE:
quit()
def click(event):
quit()
def quit(dummy_arg=None):
instructions.parameters.go_duration = (0,'frames')
instructions.parameters.handle_event_callbacks=[(pygame.locals.QUIT, quit),(pygame.locals.KEYDOWN, quitKey), (pygame.locals.MOUSEBUTTONDOWN, click)]
#show instructions
instructions.go()
def makeText(screen, text, textSize=60, textcolor=(255, 255, 255), duration=0, quittable=True):
#create our instruction screen
#load up the instruction text
insStim, insView = printText(screen, text, textSize, textcolor)
if duration:
gd = (duration, 'seconds')
else:
gd = ('forever',)
instructions = Presentation(go_duration=gd, viewports=[insView])
#add a quit function and a handler to go with it
def quitKey(event):
if event.key == pygame.locals.K_SPACE:
quit()
def click(event):
quit()
def quit(dummy_arg=None):
instructions.parameters.go_duration = (0,'frames')
if quittable:
instructions.parameters.handle_event_callbacks=[(pygame.locals.QUIT, quit),(pygame.locals.KEYDOWN, quitKey), (pygame.locals.MOUSEBUTTONDOWN, click)]
#show instructions
return instructions
def showImage(screen, img, duration=1):
def quitKey(event):
if event.key == pygame.locals.K_SPACE:
quit()
def click(event):
quit()
def quit(dummy_arg=None):
image.parameters.go_duration = (0,'frames')
img = Image.open(img)
#create textures
tex = Texture(img)
x = screen.size[0] / 2
y = screen.size[1] / 2
stimulus = TextureStimulus(texture = tex, position = (x, y), anchor = 'center')
#create viewports
viewport = Viewport(screen=screen, stimuli=[stimulus])
if duration:
image = Presentation(go_duration=(duration, 'seconds'), viewports=[viewport])
else:
image = Presentation(go_duration=('forever', ''), viewports=[viewport])
image.parameters.handle_event_callbacks=[(pygame.locals.QUIT, quit),(pygame.locals.KEYDOWN, quitKey), (pygame.locals.MOUSEBUTTONDOWN, click)]
image.go()