-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPySequence.py
More file actions
235 lines (180 loc) · 6.77 KB
/
Copy pathPySequence.py
File metadata and controls
235 lines (180 loc) · 6.77 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/python
###########################################
# Simple Sequence task
#
# Designed as an example of a button pressing
# task using Python.
#
# - John Schlerf
#
# See:
# http://github.com/JohnSchlerf/PySequence
###########################################
# This is the library stored here:
from helperfunctions import *
# Pandas is a great library for working with CSV files,
# storing data, etc:
import pandas as pd
###########################################
# I should make this a library someday:
def GridLabel(Parent,Text,Row,Column):
"""
This is a helper function which adds a label to a grid.
This is normally a couple of lines, so multiple labels
gets a little cumbersome...
"""
L = Label(Parent,text=Text)
L.grid(row=Row,column=Column)
return L
def GridEntry(Parent,DefaultText,Row,Column):
"""
This is a helper function which adds an Entry widget
to a grid. Also sets default text.
"""
E = Entry(Parent)
E.insert(0,DefaultText)
E.grid(row=Row,column=Column)
return E
class setupGUI():
# Options that we may decide to pass to the program:
optDict = {'WIDTH' :800, # set to -1 for fullscreen
'HEIGHT' :600, # set to -1 for fullscreen
'FONTSIZE':18,
'INSTRUCTIONFILE':'sequence_files/INSTRUCTIONS.txt',
'BETWEEN_TRIAL_PAUSE': 50.0/1000.0, # ie, 50 ms
'RESPONSE_TIMEOUT':None, # Set to None for no timeout
}
def __init__(self):
# Pandas is a great way to save data:
self.AllData = pd.DataFrame()
# Define my Tkinter window:
self.win = Tk()
self.win.update()
# This is a helper variable for packing widgets in:
nextRow = 0
# Enter the subject ID:
GridLabel(self.win,"Subject ID:",nextRow,0)
self.__subjIdEntry = GridEntry(self.win,"DEFAULT",nextRow,1)
nextRow += 1
# Enter the session number:
GridLabel(self.win,"Current Session:",nextRow,0)
self.__sessionEntry = GridEntry(self.win,"1",nextRow,1)
nextRow += 1
# Insert a blank line:
GridLabel(self.win,"",nextRow,0)
nextRow += 1
# Make Run/Quit buttons:
self.__runButton = Button(self.win,text="Begin",command=self.runBlock)
self.__runButton.grid(row=nextRow,column=1,pady=5,sticky=E+W)
self.__quitButton = Button(self.win,text="Quit",command=self.cleanUp)
self.__quitButton.grid(row=nextRow,column=0,pady=5,sticky=E+W)
# Run the loop (shows the GUI):
self.win.mainloop()
def cleanUp(self):
# This is called when the Quit button is pressed:
try:
self.AllData.to_csv(self.outputFile,index=False)
except:
print "Seems there's no data to save."
self.win.quit()
def runBlock(self):
# This is called when the Run button is pressed:
self.subjID = self.__subjIdEntry.get()
self.session = self.__sessionEntry.get()
# Session File:
sessionFile = "sequence_files/" + str(self.subjID) + "_" + str(self.session) + ".csv"
# Check if the session file actually exists:
try:
junkFile = open(sessionFile,'r')
junkFile.close()
except:
# If I got here, then trying to open the file raised an error.
# So, it must not exist! Load the default:
sessionFile = "sequence_files/SEQUENCE_" + str((eval(self.session) + 1) % 2 + 1) + ".csv"
# Output File:
self.outputFile = SafeFile("data/" + str(self.subjID) + \
"_" + str(self.session) + "_Data.csv",
".csv")
print "Will save data to " + self.outputFile
self.AllData = runSequenceBlock(sessionFile,self.optDict,self.AllData)
###########################################################3
# This will do the work, eventually:
def runSequenceBlock(sessionFile,optDict,theData):
if optDict:
for key in optDict.keys():
exec(key + "=optDict['" + key + "']")
myWindow = Display(width=WIDTH,height=HEIGHT)
myWindow.setSize(FONTSIZE)
myWindow.setColor('white')
# Put up some instructions:
if INSTRUCTIONFILE:
iFile = open(INSTRUCTIONFILE,'r')
instructionLines = iFile.readlines()
iFile.close()
instructionText = ""
for line in instructionLines:
instructionText += line + '\n'
myWindow.setText(instructionText)
throwThisAway = myWindow.getKeypress()
# Clear the screen:
myWindow.setText("")
myWindow.update()
#myWindow.close()
#return dataDict
myClocks=Clock(5)
runMe = pd.read_csv(sessionFile)
for i in runMe.index:
thisTrial = runMe.ix[i]
# Things to save:
RT = -1
numBadResponses = 0
badResponses = ''
TN = i+1
# Change the color to whatever it needs to be:
myWindow.setColor(thisTrial['color'])
# Move the text to where it needs to be:
myWindow.moveText(thisTrial['xpos'],-1*thisTrial['ypos'])
# ...multiplying 'ypos' by -1 means that positive is up.
# Show the text:
myWindow.setText(thisTrial['letter'])
myWindow.update()
# Get the response:
pressedCorrectKey = False
myClocks.reset(3)
while not(pressedCorrectKey):
theKey = myWindow.getKeypress(myClocks,timeout=RESPONSE_TIMEOUT)
if theKey == thisTrial['correct_key']:
RT = myClocks[3]
pressedCorrectKey = True
else:
print '\a'
numBadResponses += 1
badResponses += theKey
# Unmove the text:
myWindow.moveText(-1*thisTrial['xpos'],thisTrial['ypos'])
myWindow.setText("")
myWindow.update()
# Save the data:
thisRow = pd.Series()
thisRow['TN'] = TN
thisRow['RT'] = RT
thisRow['numBadResponses'] = numBadResponses
thisRow['badResponses'] = badResponses
# And save everything in the sequence file, just for grins:
for item in thisTrial.index.values:
thisRow[item] = thisTrial[item]
theData = theData.append(thisRow,ignore_index=True)
myClocks.reset(2)
while myClocks[2] < BETWEEN_TRIAL_PAUSE:
myClocks.update()
myWindow.close()
# Nice to explicitly order the data output sometimes:
orderedCols = ['TN','RT','numBadResponses','badResponses']
allCols = theData.columns.values
for col in allCols:
if col in orderedCols:
None
else:
orderedCols.append(col)
return theData[orderedCols]
setupGUI()