/
download.py
365 lines (276 loc) · 11.2 KB
/
download.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
the dataserf - a digital laborer for behavioral scientists
Copyright (C) 2013 Christian Battista
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 3 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. If not, see <http://www.gnu.org/licenses/>.
"""
#file_download.py
import os
import mt
from config import *
import cherrypy
import lg_authority
from mako_defs import *
from client import *
import zipfile
import common
import makePRTs
@lg_authority.groups('auth')
class download(object):
@cherrypy.expose
def index(self, **kwargs):
u = cherrypy.user.name
up = mt.MongoAdmin("datamaster").db["user_files"].posts
output = ""
aggregate = ""
table = common.getCookie('datamaster_table')
#select any tables
if kwargs.has_key('select_table'):
table = kwargs['select_table']
common.setCookie('datamaster_table', table)
if table:
tableName = "%s_%s_vars" % (table, cherrypy.user.name)
posts = mt.MongoAdmin("datamaster").db[tableName].posts
IVs = posts.find({'var_type': 'IV'}).distinct('name')
DVs = posts.find({'var_type': 'DV'}).distinct('name')
#aggregate, download
datatable = "%s_%s" % (table, cherrypy.user.name)
agg_out = common.checkVariables(datatable, ['subject', 'trial'])
if agg_out:
aggregate = agg_out
download_raw = agg_out
multi_sub = agg_out
else:
aggregate = self.agg(table)
download_raw = self.raw(table)
multi_sub = self.onefile(table)
#make_prts
prts_out = common.checkVariables(datatable, ['subject', 'trial', 'run'])
if prts_out:
make_prts = prts_out
else:
make_prts = self.make_prts(table)
if kwargs.has_key('dl'):
if kwargs['dl'] == 'agg':
output += self.aggregate(table, kwargs)
elif kwargs['dl'] == 'multi':
output += self.get_onefile(table, kwargs)
else:
output += self.get_raw(table, kwargs)
elif kwargs.has_key('prt_onset'):
output += self.prts(table, kwargs)
else:
aggregate = no_table
download_raw = no_table
make_prts = no_table
multi_sub = no_table
dl = "<p>"
fcount = 1
for row in up.find({'user':u}):
dl += "File %s : <a href='%s/output/%s'>%s</a><br/>" % (fcount, domain, row['fname'], row['fname'])
fcount+=1
dl += "</p>"
items = [['select table', select_table(download_url, table)], ['aggregate', aggregate], ['download multi-subject file', multi_sub], ['create single subject files', download_raw], ['make PRTs', make_prts], ['download existing file', dl]]
output += getAccordion(items, contentID='download-small')
return getPage(output, "download", "download")
def agg(self, table):
"""aggregate interface
table(string)
"""
datatable = "%s_%s" % (table, cherrypy.user.name)
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=False)
check_vars = common.checkVariables(datatable, ['subject', 'trial', 'IV', 'DV'])
if check_vars:
output = check_vars
else:
form = ""
form += getCheckbox(IVs)
form += getCheckbox(DVs)
form += getCondition(IVs + DVs + [trial], 'Include only data where:')
output = "<p>Here are the variables you have labelled. Select the ones you want to combine into your new file. You will get a single file with each subject's average DV(s) for each IV(s).</p>"
output += getForm(form, download_url, hidden=['dl', 'agg'])
return output
def aggregate(self, table, kwargs):
u = cherrypy.user.name
datatable = "%s_%s" % (table, u)
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=False)
output = ""
ivs = []
dvs = []
for k in kwargs.keys():
if kwargs[k] == 'on':
if k in IVs:
ivs.append(k)
elif k in DVs:
dvs.append(k)
q = parseQuery(kwargs)
w = mt.WriteTable(dvs, ivs, q, "datamaster", datatable, subject=sid)
w.Compute()
w.WriteForSPSS()
w.WriteForR()
spss_name = w.name + ".csv"
r_name = w.name + ".dat"
up = mt.MongoAdmin("datamaster").db["user_files"].posts
if not up.find_one({'user':u, 'fname':spss_name}):
up.insert({'user':u, 'fname':spss_name})
if not up.find_one({'user':u, 'fname':r_name}):
up.insert({'user':u, 'fname':r_name})
common.activity_log("download", "aggregate", table, kwargs)
output += "<p>Your data is ready. <a href='%s/output/%s'>Click here for SPSS format</a> or <a href='%s/output/%s'>click here for R format.</p>" % (domain, spss_name, domain, r_name)
return output
def make_prts(self, table):
"""make prt interface
"""
datatable = "%s_%s" % (table, cherrypy.user.name)
sid, trial, IVs, DVs, sids, run, outlier = common.getVariables(datatable, sids=False)
output = "<p>Let's make some PRTs (files for BrainVoyager).</p>"
form = "<label>Experiment Onset Delay (ms):</label>"
form += "<input type=\"number\" name=\"prt_onset\" value=0>"
form += "<label>Stimulus Onset:</label>" + getOptions(IVs, ID="prt_stim_onset")
form += "<label>Stimulus Offset (ms):</label><input type=\"number\" name=\"prt_offset\" value=2000>"
form += "<hr>"
form += "<label>Condition:</label>" + getOptions(IVs, ID="prt_cond")
form += "<label>Specify IV levels(e.g., <em>level1, level2</em> - leave blank to simply use levels present in run):</label>"
form += "<input type ='text' name='prt_con_names'/></input><br/>"
form += "<hr>"
form += "<label>Check Accuracy?</label>" + getRadios(["yes", "no"], name='check_error')
form += "<label>Accuracy listed in...</label>" + getOptions(DVs, ID="prt_ACC")
form += "<label>Reaction time listed in...:</label>" + getOptions(DVs, ID="prt_RT")
form += "<hr>"
output += getForm(form, download_url)
return output
def prts(self, table, kwargs):
"""make prts function
"""
datatable = "%s_%s" % (table, cherrypy.user.name)
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=False)
#get the values
onset_start = int(kwargs['prt_onset'])
stim_onset = kwargs['op-prt_stim_onset']
stim_offset = int(kwargs['prt_offset'])
if kwargs['check_error'] == 'no':
check_errors = False
else:
check_errors = True
ACC = kwargs['op-prt_ACC']
RT = kwargs['op-prt_RT']
conditions = []
if kwargs['prt_con_names']:
for con in kwargs['prt_con_names'].split(','):
conditions.append(con.strip())
output = ""
myCond = kwargs['op-prt_cond']
settings = settings = """FileVersion: 2\n\nResolutionOfTime: msec\n\nExperiment: %s\n\nBackgroundColor: 0 0 0\nTextColor: 255 255 255\nTimeCourseColor: 255 255 255\nTimeCourseThick: 3\nReferenceFuncColor: 0 0 80\nReferenceFuncThick: 3\n\n""" % (table)
prt_maker = makePRTs.prtFile("datamaster", datatable, sid, run, settings, onset_start, stim_offset, check_errors)
prt_maker.make(myCond, conditions, stim_onset, ACC, RT, trial, balance=False)
files = prt_maker.fileList
zipname = "%s_%s_prts" % (datatable, myCond)
zippath = os.path.join("output", "%s.zip" % zipname)
z = zipfile.ZipFile(zippath, "w")
for f in files:
z.write(f)
os.system('rm %s' % f)
z.close()
up = mt.MongoAdmin("datamaster").db["user_files"].posts
if not up.find_one({'user':cherrypy.user.name, 'fname':zipname + ".zip"}):
up.insert({'user':cherrypy.user.name, 'fname':zipname + ".zip"})
common.activity_log("download", "download prts", table, kwargs)
output += getAlert("Your files are ready. <a href='%s/%s'>Click here to download.</a>" % (domain, zippath), "good")
return output
def onefile(self, table):
"""Get one file with all subject data interface
table(string)
"""
datatable = "%s_%s" % (table, cherrypy.user.name)
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=False)
form = ""
form += getCondition([trial] + IVs + DVs, 'Include only data where:')
output = "<p>You will get one file with each subject's data in it.</p>"
output += getForm(form, download_url, hidden=['dl','multi'])
return output
def get_onefile(self, table, kwargs):
"""Print the raw data (csv format) into a single file for the user to download
table(string) - name of table to pull the vars from
kwargs - optional query info
"""
u = cherrypy.user.name
datatable = "%s_%s" % (table, u)
dm = mt.MongoAdmin("datamaster")
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=True)
q = parseQuery(kwargs)
#put the headers together
headers = [sid, trial] + IVs + DVs
sort = [sid, trial]
if run:
headers.insert(3, run)
sort = [sid, run, trial]
if outlier:
headers += [outlier]
#write that biznass out
name = dm.write(datatable, q, headers = headers, sort=sort)
name = os.path.basename(name)
name += ".csv"
up = mt.MongoAdmin("datamaster").db["user_files"].posts
if not up.find_one({'user':u, 'fname':name}):
up.insert({'user':u, 'fname':name})
common.activity_log("download", "download single raw", table, kwargs)
output = getAlert("Your file is ready. <a href='%s/output/%s'>Click here to download.</a>" % (domain, name), "good")
return output
def raw(self, table):
"""Raw data interface
table(string)
"""
datatable = "%s_%s" % (table, cherrypy.user.name)
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=False)
form = ""
form += getCondition([trial] + IVs + DVs, 'Include only data where:')
output = "<p>You will get one file for each subject you have.</p>"
output += getForm(form, download_url, hidden=['dl','raw'])
return output
def get_raw(self, table, kwargs):
"""Print the raw data (csv format) and put data in a zip file for download
table(string) - name of table to pull the vars from
kwargs - optional query info
"""
u = cherrypy.user.name
datatable = "%s_%s" % (table, u)
dm = mt.MongoAdmin("datamaster")
sid, trial, IVs, DVs, run, outlier, sids = common.getVariables(datatable, sids=True)
q = parseQuery(kwargs)
files = []
for sub in sids:
headers = [sid, trial] + IVs + DVs
sort = trial
if run:
headers.insert(3, run)
sort = [run, trial]
if outlier:
headers += [outlier]
name = dm.write(datatable, dict(q, **{sid:sub}), headers = headers, sort=sort)
files.append(name)
zipname = datatable
zipname = os.path.join("output", zipname + ".zip")
z = zipfile.ZipFile(zipname, 'w')
for f in files:
z.write(f + ".csv")
os.system('rm %s.csv' % f)
z.close()
up = mt.MongoAdmin("datamaster").db["user_files"].posts
if not up.find_one({'user':u, 'fname':datatable + ".zip"}):
up.insert({'user':u, 'fname':datatable + ".zip"})
common.activity_log("download", "download raw", table, kwargs)
output = getAlert("Your files are ready. <a href='%s/%s'>Click here to download.</a>" % (domain, zipname), "good")
return output
if __name__ == '__main__':
#Turn on lg_authority for our website
cherrypy.config.update(cherry_settings)
cherrypy.config.update({'server.socket_port':download_port,})
cherrypy.quickstart(download())