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 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 remove(self, kwargs): table = kwargs['table_remove'] posts = mt.MongoAdmin("datamaster").db["%s_%s" % (table, cherrypy.user.name)].posts ul_files = mt.MongoAdmin("datamaster").db["user_ul_files"].posts for k in kwargs.keys(): if kwargs != 'table_remove': posts.remove({'source_file':k}) ul_files.remove({'file_name':k}) common.activity_log('upload', 'remove', table, {'filename': k})
def query(self, table, kwargs): posts = mt.MongoAdmin("datamaster").db[table].posts condition = parseCondition(kwargs) set_text = kwargs['set_text'] set_val = kwargs['set_val'] set_op = kwargs['set_op'] set_col = kwargs['set_col'] if (set_text or set_val) and set_op: if set_val: for row in posts.find(condition): print set_col, row.has_key(set_col), set_val, row.has_key(set_val) if row.has_key(set_val): if set_op == '=': print set_col, set_val row[set_col] = row[set_val] posts.save(row) elif set_op == '+=': if row.has_key(set_col): row[set_col] += row[set_val] posts.save(row) elif set_op == '-=': if row.has_key(set_col): row[set_col] -= row[set_val] posts.save(row) query = "set %s %s %s" % (set_col, set_op, set_val) else: query = {} st = mt.StringToType(kwargs['set_text']) if kwargs['set_op'] == '=': query = {'$set' : {kwargs['set_col'] : st}} elif kwargs['set_op'] == '+=': query = { '$inc': { kwargs['set_col']: st}} elif kwargs['set_op'] == '-=': query = { '$inc': { kwargs['set_col']: -st}} posts.update(condition, query, multi=True) common.activity_log("modify", "modify", table, kwargs) text = "variable %s was modified, condition: %s command: %s" % (kwargs['set_col'], condition, query) return getAlert(text, "good") else: return getAlert("No modifications made")
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
def merge(self, table, kwargs): posts =mt.MongoAdmin("datamaster").db[table].posts name = kwargs['merge_var'] op = kwargs['op'] var_l = kwargs['var_left'] var_r = kwargs['var_right'] var_type = kwargs['merge_var_type'] if var_l and var_r and op: q = {var_l : {'$exists': True}, var_r : {'$exists':True}} if op == "+": for row in posts.find(q): row[name] = row[var_l] + row[var_r] posts.save(row) elif op == "-": for row in posts.find(q): row[name] = row[var_l] - row[var_r] posts.save(row) elif op == "*": for row in posts.find(q): row[name] = row[var_l] * row[var_r] posts.save(row) elif op == "/": for row in posts.find(q): row[name] = row[var_l] / row[var_r] posts.save(row) elif op == "join text": for row in posts.find(q): row[name] = str(row[var_l]) + "_" + str(row[var_r]) posts.save(row) common.activity_log("modify", "merge", table, kwargs) #make a note of this new column tableName = "%s_vars" % table var_posts = mt.MongoAdmin("datamaster").db[tableName].posts var_posts.update({'name':name}, {'$set':{'var_type':var_type}}, upsert=True) output = "<div class='alert alert-success'>new variable %s created from %s and %s</div>" % (name, var_l, var_r) else: output = "<div class='alert alert-error'>no merge performed</div" 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 upload(self, myFiles): p = mt.MongoAdmin("datamaster").db["user_tables"].posts cookie = cherrypy.request.cookie tableName = cookie["datamaster_table"].value uf = mt.MongoAdmin("datamaster").db["user_ul_files"].posts output = "<p>" output += str(type(myFiles)) if type(myFiles) != list: myFiles = [myFiles] for myFile in myFiles: lines = [] while True: data = myFile.file.readlines(100) if not data: break lines += data if len(lines) == 1: lines = lines[0].split('\r') d = {'source_file' : myFile.filename} r = mt.ReadTable(None, "datamaster", "%s_%s" % (tableName, cherrypy.user.name), data=lines, kind="eprime", addrow = d) output += "The contents of %s have been uploaded.<br/>" % myFile.filename uf.insert({'user':cherrypy.user.name, 'table':tableName, 'file_name' : myFile.filename}) output += "</p>" common.activity_log('upload', 'upload', tableName) #take a look at what variables we might have VARs = common.inspect("%s_%s" % (tableName, cherrypy.user.name)) print VARs #mark 'em common.markVariables("%s_%s" % (tableName, cherrypy.user.name), VARs) return output
def create(self, table, kwargs): posts =mt.MongoAdmin("datamaster").db[table].posts var_type = kwargs['new_var_type'] name = kwargs['new_var'] if name: if kwargs['orig_var']: if not kwargs['op']: for row in posts.find(): row[name] = row[kwargs['orig_var']] posts.save(row) else: ot = mt.StringToType(kwargs['op_text']) for row in posts.find(): if row.has_key(kwargs['orig_var']): if kwargs['op'] == '-=': row[name] = row[kwargs['orig_var']] - ot elif kwargs['op'] == '+=': row[name] = row[kwargs['orig_var']] - op posts.save(row) else: posts.update({}, {'$set':{name : 'NA'}}) output = getAlert("variable %s created" % name, "good") common.activity_log("modify", "create", table, kwargs) #make a note of this new column tableName = "%s_vars" % table var_posts = mt.MongoAdmin("datamaster").db[tableName].posts var_posts.update({'name':name}, {'$set':{'var_type':var_type}}, upsert=True) else: output = getAlert("no columns created") return output
def index(self, **kwargs): cookie = cherrypy.request.cookie u = cherrypy.user.name #establish a table from the cookie table = common.getCookie("datamaster_table") #user has selected a table if kwargs.has_key('select'): table = kwargs['select'] common.setCookie('datamaster_table', table) #user has removed a table if kwargs.has_key('remove'): p = mt.MongoAdmin("datamaster").db["user_tables"].posts t = kwargs['remove'] #is this the currently active table? if table == t: common.removeCookie('datamaster_table', table) table = None mt.MongoAdmin("datamaster").db.drop_collection("%s_%s.posts" % (t, u)) mt.MongoAdmin("datamaster").db.drop_collection("%s_%s_vars.posts" % (t, u)) p.remove({'user':u, 'table':t}) mt.MongoAdmin("datamaster").db["user_ul_files"].posts.remove({'user':u, 'table':t}) common.activity_log('manage', 'remove table', t, kwargs) select_table, remove_table = self.table_choice(table, kwargs) #user has selected some variable if kwargs.has_key('choose'): if kwargs['choose'] == 'vars': datatable = "%s_%s" % (table, cherrypy.user.name) tableName = "%s_%s_vars" % (table, cherrypy.user.name) posts = mt.MongoAdmin("datamaster").db[tableName].posts for k in kwargs.keys(): posts.remove({'name':k}) posts.update({'name':k}, {'$set':{'var_type':kwargs[k]}}, upsert=True) common.activity_log("manage", "choose", table, kwargs) #if there's an active table if table: choose_vars = self.chooseVars(table) review_vars = common.variableTable("%s_%s" % (table, u)) preview = common.preview("%s_%s" % (table, u), kwargs, manage_url) else: choose_vars = no_table review_vars = no_table preview = no_table items = [['select table', select_table], ['choose variables', choose_vars], ['review variables', review_vars], ['remove table', remove_table], ['preview', preview]] accordion = getAccordion(items, contentID='manage-small') output = getPage(accordion, "manage", "manage") return output