def find_match(bucket_name, key, input_format, output_format, offsets, params): util.print_read(input_format, key, params) [combine, keys, last] = util.combine_instance(bucket_name, key, params) if combine: print("Finding match") best_match = None match_score = 0 format_lib = importlib.import_module(params["format"]) iterator = getattr(format_lib, "Iterator") s3 = boto3.resource("s3") keys.sort() with open(util.LOG_NAME, "a+") as f: for key in keys: obj = s3.Object(bucket_name, key) it = iterator(obj, params["chunk_size"]) if params["find"] == "max sum": score = it.sum(params["identifier"]) else: raise Exception("Not implemented", params["find"]) print("key {0:s} score {1:d}".format(key, score)) f.write("key {0:s} score {1:d}\n".format(key, score)) if score > match_score: best_match = key match_score = score if best_match is None: best_match = keys[0] output_format["ext"] = "match" file_name = util.file_name(output_format) util.write(bucket_name, file_name, str.encode(best_match), {}, params)
def draw_mm_ruler(self, c, dpi, width=130): c.save() c.set_line_cap(cairo.LINE_CAP_SQUARE) c.translate(*c.get_current_point()) c.move_to(0, 0) c.set_line_width(4) c.line_to(mm(dpi, width), 0) for x in [mm(dpi, xm) for xm in range(0, width+1, 10)]: c.move_to(x, 0) c.rel_line_to(0, mm(dpi, 3)) c.stroke() c.set_line_width(3) for x in [mm(dpi, xm) for xm in range(0, width, 5)]: c.move_to(x, 0) c.rel_line_to(0, mm(dpi, 2.2)) for x in [mm(dpi, xm) for xm in range(0, width, 1)]: c.move_to(x, 0) c.rel_line_to(0, mm(dpi, 2)) c.stroke() base = mm(dpi, 7) for x, xm in [(mm(dpi, xm), xm) for xm in range(0, width+1, 10)]: c.move_to(x, base) write(c, "%d" % xm, self.font, mm(dpi, 2), centered=True) c.move_to(mm(dpi, 1.2), base) write(c, "mm", self.font, mm(dpi, 1.5)) c.restore()
def get_page_info(driver, path): print("function:{0} start".format(sys._getframe().f_code.co_name)) try: info = {} info['title'] = driver.title info['url'] = driver.current_url element_list = driver.find_elements_by_css_selector( "meta[name='description']") for i in element_list: if len(i.get_attribute("content")) != 0: info['description'] = i.get_attribute("content") break element_list = driver.find_elements_by_css_selector( "meta[name='keywords']") for i in element_list: if len(i.get_attribute("content")) != 0: info['keywords'] = i.get_attribute("content") break except: print("Unexpected error:", sys.exc_info()[0]) util.write(info, ini.get("html", "info"), path) print("function:{0} end".format(sys._getframe().f_code.co_name)) return info
def cuhk_result_eval(predict_path, test_info, probe_info, log_path='grid_eval.log'): pids4probes = np.genfromtxt(predict_path, delimiter=' ') probe_shoot = [0, 0, 0, 0, 0] for i, pids in enumerate(pids4probes): for j, pid in enumerate(pids): if probe_info[i][0] == test_info[int(pid)][0]: if j == 0: for k in range(5): probe_shoot[k] += 1 elif j < 5: for k in range(1, 5): probe_shoot[k] += 1 elif j < 10: for k in range(2, 5): probe_shoot[k] += 1 elif j < 20: for k in range(3, 5): probe_shoot[k] += 1 elif j < 50: for k in range(4, 5): probe_shoot[k] += 1 break probe_acc = [shoot / len(pids4probes) for shoot in probe_shoot] write(log_path, predict_path + '\n') write(log_path, '%.2f\t%.2f\t%.2f\n' % (probe_acc[0], probe_acc[1], probe_acc[2])) print(predict_path) print(probe_acc)
def draw(self, c, dpi): self.c = c self.dpi = dpi set_background_color(c, self.w, self.h) nw, nh = dimensions_mm(dpi, self.w, self.h) c.set_antialias(cairo.ANTIALIAS_GRAY) self.draw_ruler_pair(c, dpi, mm(dpi, 20)) # Only calculate offsets if on an OLPC XO-1, 1.5, 1.75 # Not applicable to XO 3.0 if self.hw[0:2] == 'xo' and dpi in [200, 201]: self.offset_of_xo_side_from_screen = mm(dpi, -38.5) c.move_to(self.offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, 180) c.save() c.move_to(mm(dpi, 20), mm(dpi, 75)) write(c, _('Use this ruler from the outside edge of the computer.'), self.font, mm(dpi, 4)) c.restore() offset_of_molding_from_screen = mm(dpi, -0.4) c.move_to(offset_of_molding_from_screen, mm(dpi, 95)) self.draw_cm_ruler(c, dpi, 150) self.draw_custom_ruler(self.custom_unit_in_mm, int(nw / 10 * 10)) else: self.offset_of_xo_side_from_screen = mm(dpi, int(nw / 20)) c.move_to(self.offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, int(nw / 11 * 10)) self.draw_custom_ruler(self.custom_unit_in_mm, int(nw / 11 * 10))
def script2gist(input_stream): print input_stream lexer = GistScriptLexer(input_stream) token_stream = CommonTokenStream(lexer) parser = GistScriptParser(token_stream) tree = parser.gistscript() lisp_tree_str = tree.toStringTree(recog=parser) print(lisp_tree_str) walker = ParseTreeWalker() listner = GistScriptListener() walker.walk(listner, tree) root = etree.XML('<?xml version="1.0" ?>' + listner.output) pretty_print(root) alternatives(root) newroot = xslt(root, 'xslt/script2gist.xsl') print "###### script2gist ######" pretty_print(newroot) name_temporarys(newroot) #name_unmapped(newroot) # print "###### name temps ######" # pretty_print(newroot) newroot = xslt(newroot, 'xslt/script2gist2.xsl') write(newroot, 'graph.xml') return newroot
def main(args): base_histos = [util.get_tobject(args, r) for r in args.base_histos] util.validate_hists(args.base_histos, base_histos) base_histos = [util.resize_histo(args, util.get_tobject(args, r)) for r in args.base_histos] cut_histos = [util.get_tobject(args, r) for r in args.cut_histos] util.validate_hists(args.cut_histos, cut_histos) cut_histos = [util.resize_histo(args, util.get_tobject(args, r)) for r in args.cut_histos] canvas = ROOT.TCanvas("canvas", "Canvas", 250,100,700,500) all_histos = [] for i, (base_histo,cut_histo) in enumerate(zip(base_histos,cut_histos)): ratio = cut_histo.Clone() ratio.Divide(base_histo) ratio.SetLineColor(util.fillcolors(i)) ratio.SetLineWidth(2) if i == 0: util.style(args, canvas, ratio) ratio.Draw("PE") else: ratio.Draw("PE SAME") all_histos.append(ratio) if args.names: name = args.names[i] ratio.SetTitle(name) legend = ROOT.gPad.BuildLegend(*(args.legend_position + [""])) if args.title is not None: all_histos[0].SetTitle(args.title) canvas.Update() util.wait(args) util.write(args, canvas)
def end_sections(): print 'Ending sections:' for s in config.sections: b = '#define __SECTION_%s\\\n' % s.upper() for t in config.sections[s][2]: b += '\t%s\\\n' % t util.write(config.sections[s][0], b, "a") b = "\n#endif" util.write(config.sections[s][0], b, "a") print '\t%s' % config.sections[s][0] util.write(config.file_ent_sections, "}\n", "a") util.write(config.file_levelup_c, "};", "a") lh = "#ifndef LEVELUP_H_\n#define LEVELUP_H_\n\ #define MAX_LEVELUP %d\n\ struct levelup_s {\n\ const char *dst;\n\ const char *src;\n\ };\n\ extern struct levelup_s levelup[MAX_LEVELUP];\n\ #endif" % config.no_levelup util.write(config.file_levelup_h, lh, "w")
def g(self): result = yield db.user_shuo.find_one({'user': self.current_user}) util.log( "MyShuoHandler.get" + str(result)) if result: util.write(self, 1, "", result) else: util.errorHandle(self, 1)
def draw(self, c, dpi): set_background_color(c, self.w, self.h) nw, nh = dimensions_mm(dpi, self.w, self.h) c.set_antialias(cairo.ANTIALIAS_GRAY) self.draw_ruler_pair(c, dpi, mm(dpi, 20)) # only calculate offsets if on an OLPC XO-1 if self.hw[0:2] == 'XO': offset_of_xo_side_from_screen = mm(dpi, -38.5) #XXX needs checking c.move_to(offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, 180) c.save() c.move_to(mm(dpi, 20), mm(dpi, 75)) write(c, _('Use this ruler from the outside edge of the computer.'), self.font, mm(dpi, 4)) c.restore() offset_of_molding_from_screen = mm(dpi, -0.4) #XXX +- 0.2 ?? c.move_to(offset_of_molding_from_screen, mm(dpi, 95)) self.draw_cm_ruler(c, dpi, 150) else: offset_of_xo_side_from_screen = mm(dpi, 0) c.move_to(offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, int(nw / 10 * 10))
def commit(message): ri = filespace.read_index() # list of (blob_hash, rel_path) ti = sorted([ re[0:2] for re in ri ], key=lambda x: x[0]) # buffer of the tree tb = '\n'.join(map(lambda x: "%s\t%s" %(x[0], x[1]), ti)) th = filespace.hashbuf(tb) # if the tree hash already exists, then an exact copy of the tree already exists, we'll just reuse it, otherwise create the tree object if not objects.exists(th): objects.write(th, tb) # tree hash | timestamp (seconds from epoch) | message # create new commit cb = "%s\t%s\t%s" %(th, time.time(), message) ch = filespace.hashbuf(cb) objects.write(ch, cb) # previous head pid = id() # (branch-name, head-hash) bd = current_branch() # update head, and ref log refs.update_head(ch) refs.append(ch) # if at the head of a branch, add the commit record to the branch if bd is not None and pid == bd[1]: append_branch(bd[0], ch) util.write(os.path.join('.bvc', 'branch'), bd[0] + '\t' + ch)
def gen(self, outputDir): result = [] result.append(headTmpl % {"title": self.app.config.HEADER}) result.append("<h1>" + self.app.config.HEADER + "<h1>") d = { "aut": self.app.config.AUT_URL, "ref": self.app.config.REF_URL, "start": time.strftime("%d.%m.%Y %H:%M:%S", time.localtime(self.start)), "duration": self.end - self.start, "total": reduce(lambda a, b: a + b, map(lambda x: len(x.results), self.scenarios)), "failed": reduce(lambda a, b: a + b, map(lambda x: x.countFailed(), self.scenarios)), } result.append(statsTmpl % d) result.append(tableStart) for s in self.scenarios: for name, (countRef, countOrig) in s.results.items(): d = { "class": (countRef == 0 and countOrig == 0) and "green" or "red", "countRef": countRef, "countOrig": countOrig, "name": "%s.%s" % (s.__class__.__name__, name), "fn": self.app.fnHtmlDiff(name), "fnDiffLib": self.app.fnDiffLib(name), "fnOrig": self.app.fnOrig(name), "fnRef": self.app.fnRef(name), } # print name, left, right result.append(lineTmpl % d) result.append(tableEnd) result.append(footerTmpl) # write index.html fn = os.path.join(outputDir, "index.html") util.write(fn, "\n".join(result))
def search(self): if self.get_argument("search"): search = self.get_argument("search") data = [] cursor = db.users.find({"$or": [ {"user":search}, {"nick_name":search} ]}) while (yield cursor.fetch_next): user = cursor.next_object() if user["user"] == self.current_user: continue result = {} result["icon"] = user["icon"] result["nick_name"] = user["nick_name"] result["user"] = user["user"] data.append(result) # db.users.find({"$or": # [ # {"user":search}, # {"nick_name":search} # ]},callback=(yield gen.Callback("key"))) # user = yield gen.Wait("key") util.write(self, 1, "", json.dumps(data)) self.finish() else: util.errorHandle(self, 0) self.finish()
def remove_player(self, world_name, name): print("Removing player", name) util.load(self.world_dir(world_name)) if name in util.all_dict: del util.all_dict[name] util.write() return self.players(world_name)
def draw(self, c, dpi): self.c = c self.dpi = dpi set_background_color(c, self.w, self.h) nw, nh = dimensions_mm(dpi, self.w, self.h) c.set_antialias(cairo.ANTIALIAS_GRAY) self.draw_ruler_pair(c, dpi, mm(dpi, 20)) # Only calculate offsets if on an OLPC XO-1, 1.5, 1.75 # Not applicable to XO 3.0 if self.hw[0:2] == 'xo' and dpi in [200, 201]: self.offset_of_xo_side_from_screen = mm(dpi, -38.5) c.move_to(self.offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, 180) c.save() c.move_to(mm(dpi, 20), mm(dpi, 75)) write(c, _('Use this ruler from the outside edge of the computer.'), self.font, mm(dpi, 4)) c.restore() offset_of_molding_from_screen = mm(dpi, -0.4) c.move_to(offset_of_molding_from_screen, mm(dpi, 95)) self.draw_cm_ruler(c, dpi, 150) self.draw_custom_ruler(self.custom_unit_in_mm, int(nw / 10 * 10)) else: self.offset_of_xo_side_from_screen = mm(dpi, 0) c.move_to(self.offset_of_xo_side_from_screen, mm(dpi, 65)) self.draw_cm_ruler(c, dpi, int(nw / 10 * 10)) self.draw_custom_ruler(self.custom_unit_in_mm, int(nw / 10 * 10))
def seed_map_multiprocess((subj_id,file_path,seed_partition,brain_mask,output_path,window,gs)): vol_file = nib.load(file_path).get_data() dynamic_data,avg_data = dynamic_rmaps(vol_file,seed_partition.get_data(),brain_mask,window=window,gs) del vol_file #np.savez_compressed(output_path,dynamic_data=dynamic_data,avg_data=avg_data) util.write({'dynamic_data':dynamic_data,'avg_data':avg_data},output_path) del dynamic_data,avg_data
def _report(f,test_set,weight,output): doc = test_set['doc'] deep_write(f,1,'Document') for sentence in doc: deep_write(f,2,viewable(sentence)) deep_write(f,1,'Question') questions = test_set['q'] for i in range(0,len(questions)): deep_write(f,2, str(i+1) + '. ' + viewable(questions[i]['q_str'])) candidates = questions[i]['answer'] deep_write(f,2, "System's Answer: " + (str(CHOICE[questions[i]['output']]) if questions[i]['output'] is not None else '-') + " ["+questions[i]['evaluation']+"]") if questions[i]['evaluation'] == 'Correct': output.append('_') elif questions[i]['evaluation'] == 'Incorrect': output.append(str(CHOICE[questions[i]['output']])) else: output.append('?') for j in range(0,len(candidates)): deep_write(f,3, is_correct(CHOICE[j], candidates[j]) + '. ' + viewable(candidates[j]['value'])) features = candidates[j]['score'] weighted = candidates[j]['weighted_score'] for feature, feat_value in features.items(): deep_write(f,4,'- ' + feature + ' : ' + str(feat_value) + ' * ' + str(weight[feature]) + ' = ' + str(weighted[feature])) deep_write(f,4,"-" * 60 + '+') deep_write(f,4,"Total Score: " + str(candidates[j]['total_score'])) write(f)
def post(self): token = self.xsrf_token if self.get_argument("user") and self.get_argument("pwd"): util.log("RegistrationHandler.post") util.log( self.get_argument("user")) db.users.find_one({"user":self.get_argument("user")}, callback=(yield gen.Callback("key"))) user = yield gen.Wait("key") if user[0][0]: util.write(self, 0, "用户名已经存在,请选用其他用户名", {}) self.finish() else: nick = "" time = datetime.utcnow() if self.get_argument("nick"): nick = self.get_argument("nick") else: nick = "fbt_"+str(uuid.uuid1())[0:10] icon = self.static_url('images/user_icon/fbtLogo.png') new_user = {'user':self.get_argument("user"),'time':time, 'icon':icon,'desc':'点击编辑', 'real_name':'', 'phone':'', 'qq':'', 'gender':'', 'love_state':'', 'school':'', 'address':'', 'password':self.get_argument("pwd"), 'nick_name':nick, 'friends':[] } result = yield db.users.insert(new_user) #msg_handle.initMsg(self.get_argument("user")) if self.get_argument("next"): self.redirect(self.get_argument("next")) else: util.write(self, 1, "", {}) self.finish() else: util.errorHandle(self, 0) self.finish()
def grid_result_eval_idt(predict_path, log_path='grid_eval_idt.log'): pids4probes = np.genfromtxt(predict_path, delimiter=' ') probe_shoot = [0, 0, 0, 0, 0] for i, pids in enumerate(pids4probes): for j, pid in enumerate(pids): if pid - i == 775: if j == 0: for k in range(5): probe_shoot[k] += 1 elif j < 5: for k in range(1, 5): probe_shoot[k] += 1 elif j < 10: for k in range(2, 5): probe_shoot[k] += 1 elif j < 20: for k in range(3, 5): probe_shoot[k] += 1 elif j < 50: for k in range(4, 5): probe_shoot[k] += 1 break probe_acc = [shoot / len(pids4probes) for shoot in probe_shoot] write(log_path, predict_path + '\n') write( log_path, '%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n' % (probe_acc[0], probe_acc[1], probe_acc[2], probe_acc[3], probe_acc[4])) print(predict_path) print(probe_acc)
def post(self): version = self.get_argument("version","") user = self.get_argument("user", None) page = self.get_argument("page", 1) sort_by = self.get_argument("sort_by", ResourceStoreManager.res_sort_by["time"]) which_type = self.get_argument("type", None) private = self.get_argument("private", None) key_word = self.get_argument("key_word", "") try: user = long(user) page = int(page) assert page>0 sort_by = int(sort_by) assert sort_by==ResourceStoreManager.res_sort_by["time"] or sort_by==ResourceStoreManager.res_sort_by["download_num"] sort_by = ResourceStoreManager.get_sort_item(sort_by) key_word=Cipher.decrypt(key_word).strip() assert len(key_word)>0 except: util.errorHandle(self, 0) self.finish() else: if private and '1' == private: result = yield ResourceStoreManager.search_resources_private(user, version, key_word,page,sort_by,RES_CNT_IN_A_PAGE) else: result = yield ResourceStoreManager.search_resources(version, key_word,page,sort_by,RES_CNT_IN_A_PAGE) resource_list = result["res"] cipher_resource_list(resource_list, version) add_download_info_to_resource_list(resource_list) if version >= "1.8": util.write(self, 1, "", {"size": getPages(result["size"]), "res":resource_list}) else: util.write(self, 1, "", resource_list) self.finish() yield LogForUser.log_user_search(user, long(time()), key_word, result["size"])
def print_traceback(error): out = u"" if len(error.stacktrace) > 0: out = u"Traceback:\n" for frame, start, stop in reversed(error.stacktrace): out += u" %s: %s %s\n" % ( frame.module.name, start.repr(), stop.repr()) out += error.__class__.__name__.decode('utf-8') write(STDERR, out + u": " + error.message + u"\n")
def print_traceback(error): out = u"" if len(error.stacktrace) > 0: out = u"Traceback:\n" for frame, start, stop in reversed(error.stacktrace): out += u" %s: %s %s\n" % (frame.module.name, start.repr(), stop.repr()) out += error.__class__.__name__.decode('utf-8') write(STDERR, out + u": " + error.message + u"\n")
def market_result_eval(predict_path, log_path='market_eval.log'): res = np.genfromtxt(predict_path, delimiter=' ') print('predict info get, extract gallery info start') test_info = extract_info(TEST) print('extract probe info start') query_info = extract_info(QUERY) print('start evaluate map and rank acc') rank1, mAP = map_rank_eval(query_info, test_info, res) write(log_path, predict_path + '\n') write(log_path, '%f\t%f\n' % (rank1, mAP))
def checkout(ch): cc = objects.commit(ch) tc = objects.tree(cc[0]) # writes the contents of the tree back to workspace for tf in tc: util.write(tf[1], objects.blob(tf[0])) # update index filespace.update_index(tc)
def run(feature_dir, selected_feature_dir, config): ff = FeatureFilter(config) util.makedir(selected_feature_dir) for name in os.listdir(feature_dir): util.write('begin %s' % name) feature_path = os.path.join(feature_dir, name) selected_path = os.path.join(selected_feature_dir, name) ff.filter_and_dump(feature_path, selected_path) return None
def gfcToDot(gfc, outputDotFile): contentDotFile = "Digraph G {\n" for node in gfc: for nodeTarget in gfc[node][constants._iNTargets]: contentDotFile = "{contentDotFile}\t{node} -> {nodeTarget};\n".format( contentDotFile = contentDotFile, node = node, nodeTarget = nodeTarget) contentDotFile = "{contentDotFile}}} ".format(contentDotFile = contentDotFile) util.write(outputDotFile, contentDotFile)
def print_explanation(explanation, html=False): if html: exp_html = explanation.as_html() write("uncertainty-exp.html", exp_html) exp_list = explanation.as_list() print "======== Uncertainty Explanations ==========" for exp, weight in exp_list: print " * " + exp, "(weight: {:.3f})".format(weight)
def save(simulation): log( 'Saving ',simulation.name+'_iter'+str(simulation.pde.time.iteration) ) case_dir = simulation.name+'.case' case_iter_dir = case_dir+'/iter'+str(simulation.pde.time.iteration).zfill(8)+'.case' mkdir(case_dir) mkdir(case_iter_dir) simulation.write_mesh( case_iter_dir+'/mesh.cf3mesh' ) write( simulation, case_iter_dir+'/case.p' ) simulation.solver.children.history.write(URI(case_iter_dir+'/history.tsv')) symlink( 'iter'+str(simulation.pde.time.iteration).zfill(8)+'.case', case_dir+'/current.case' )
def save_html(driver, path, ini): print("function:{0} start".format(sys._getframe().f_code.co_name)) try: # HTML保存 html_path = path + ini.get("html", "folder") util.mkdir(html_path) util.write(driver.page_source, ini.get("html", "file"), html_path) # util.download(url, html_path) except: print("Unexpected error:", sys.exc_info()[0]) print("function:{0} end".format(sys._getframe().f_code.co_name))
def mergeFiles(self, file1, base, file2, outputfile): merge_proc = self._exec(['merge-file', '-p', file1, base, file2]) conflicts = merge_proc.returncode if conflicts > 0: self.logger.debug("Automatic Merge failed. " + str(merge_proc.returncode) + ' conflicts need to be resolved manually') if not self.openMergeTool(file1, base, file2, outputfile): raise MergeException(outputfile, "Manual merge failed.") elif conflicts < 0: raise MergeException(outputfile, "Error during automatic merge: " + merge_proc.stderr) else: write(outputfile, merge_proc.stdout)
def draw_numbers(p, c, dpi, nw, nh, m=1, unit="cm"): c.save() v = 0.7 c.set_source_rgb(v, v, v) for n in range(1, 11): c.move_to(mm(dpi, n * 10), mm(dpi, n * 10)) c.rel_move_to(mm(dpi, 0), mm(dpi, 3)) write(c, "%d" % (n * m), p.font, mm(dpi, 4), centered=True) if n == 1: c.rel_move_to(mm(dpi, -0.5), mm(dpi, -0.5)) write(c, " "+unit, p.font, mm(dpi, 3)) c.restore()
def main(args): f = ROOT.TFile(args.input) effs = [] puritys = [] signal_base = get_integral(f, args.variable, args.base_cut, args.signal_names) background_base = get_integral(f, args.variable, args.base_cut, args.background_names) effs.append(1.) puritys.append(signal_base / (signal_base + background_base)) for cut in args.cuts: this_signal = get_integral(f, args.variable, cut, args.signal_names) this_background = get_integral(f, args.variable, cut, args.background_names) this_eff = this_signal / signal_base this_purity = this_signal / (this_signal + this_background) effs.append(this_eff) puritys.append(this_purity) effs = array('d', effs) puritys = array('d', puritys) xvals = array('d', range(len(effs))) g_eff = ROOT.TGraph(len(effs), xvals, effs) g_eff.SetName("Efficiency") g_eff.SetTitle("Efficiency") g_purity = ROOT.TGraph(len(puritys), xvals, puritys) g_purity.SetName("Purity") g_purity.SetTitle("Purity") canvas = ROOT.TCanvas("canvas", "Canvas", 250, 100, 700, 500) g_eff.Draw("AL") g_eff.SetLineColor(ROOT.kGreen) util.style(args, g_eff) for i in range(len(effs)): g_eff.GetXaxis().SetBinLabel(g_eff.GetXaxis().FindBin(i), args.names[i]) g_purity.Draw("SAME") g_purity.SetLineColor(ROOT.kRed) legend = ROOT.gPad.BuildLegend(0.75, 0.75, 0.95, 0.95, "") canvas.Update() util.wait(args) util.write(args, canvas)
def step_impl(ctx, key): mappings = { "enter": "\x0a", "up": "\x1b[A", "down": "\x1b[B", "right": "\x1b[C", "left": "\x1b[D", "esc": "\x1b", "c-c": "\x03", "c-d": "\x04", "c-p": "\x10", "c-q": "\x11", } util.write(ctx.pty, mappings[key.lower()])
def main(): train = read("train.rd") develop = read("develop.rd") text_clf = Pipeline([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', SVC())]) text_clf = text_clf.fit(train.features[0], train.labels) write("model.rd", text_clf) predicted = text_clf.predict(develop.features[0]) print np.mean(predicted == develop.labels)
def post(self): if self.get_argument("user"): user = self.get_argument("user") if user: user = json.loads(user) util.log("MyInfoHandler.post") util.log( user) isError = False for item in user.keys(): if item not in MyInfoHandler.keys: isError = True break if not isError: if user.has_key("icon"): user["icon"] = self.static_url(user["icon"]) future = db.users.update({'user': self.current_user},{'$set': user}) result = yield future util.log( result) if result: re = {"result":"保存成功"} util.write(self, 1, "", re) self.finish() else: util.write(self, 0, "保存失败,请重试", {}) self.finish() else: util.write(self, 0, "保存失败,请重试", {}) self.finish() else: util.write(self, 0, "保存失败,请重试", {}) self.finish() else: util.errorHandle(self, 0) self.finish()
def check(self, name): data = self.getSource() fnRef = self.fnRef(name) fnOrig = self.fnOrig(name) # save references only? if self.save: util.mkdir(self.config.REF_DIR) util.write(fnRef, data) else: # compare assert os.path.exists(fnRef), "Cannot compare without reference file: %s" % fnRef util.mkdir(self.outputDir) # first save original file util.write(fnOrig, data) ref = self.cleanup(util.read(fnRef)) data = self.cleanup(data) # htmldiff result = htmldiff.htmldiff(ref, data, True) util.write(self.fnHtmlDiff(name), result) self.scenario.results[name] = self._eval_diff(result) # difflib linesRef = ref.splitlines() linesData = data.splitlines() result = difflib.HtmlDiff(wrapcolumn=80).make_file(linesRef, linesData, fnRef, fnOrig, context=True) util.write(self.fnDiffLib(name), result)
def scan_anomaly_streamacoe(ts_data, y_true, threshold, coldstart=10000, win=100000, batch=64, query_rate=0.02, seasonal=1440, dataset='dontkonw'): pathout = str(dataset) + '_result' + str(win) + c_time + '.csv' util.write( pathout, ',precision, recall, f1, roc, pr, precision_eval, recall_eval, f1_eval, samplenum\n', 'w') num_feedback = 0 weight, feedback, y_pre = ColdStart(ts_data[:coldstart], y_true[:coldstart], threshold, seasonal) print('--- 冷启动完毕 ---') num_feedback += countfeedback(feedback) acoe = StreamACOE(weight=weight, winsize=win, threshold=threshold, seasonal=seasonal, history=ts_data[:coldstart], feedback=feedback, query_rate=query_rate, batch=batch) feedback = np.array([-1] * batch) for i in range(coldstart, len(ts_data), batch): if i + batch > len(ts_data): break data = ts_data[i:i + batch] label = y_true[i:i + batch] is_anomaly, need_labeled, is_stresstest = acoe.fit(data, feedback) y_pre = np.concatenate([y_pre, is_anomaly]) feedback = [ label[i] if need_labeled[i] == 1 else -1 for i in range(batch) ] num_feedback += countfeedback(feedback) acoe.update_feedback(feedback) acoe.update_weight() print('', acoe.weight) temp_y_true = y_true[:len(y_pre)] precision, recall, f1, roc, pr, precision_eval, recall_eval, f1_eval, newresult, text = evaluate.evaluate( i, False, y_pre, temp_y_true, threshold, str(num_feedback)) util.write(pathout, text) return y_pre
def seed_map_multiprocess((subj_id, file_path, seed_partition, brain_mask, output_path, window, gs)): vol_file = nib.load(file_path).get_data() dynamic_data, avg_data = dynamic_rmaps(vol_file, seed_partition.get_data(), brain_mask, window=window, gs=gs) del vol_file # np.savez_compressed(output_path,dynamic_data=dynamic_data,avg_data=avg_data) util.write({ 'dynamic_data': dynamic_data, 'avg_data': avg_data }, output_path) del dynamic_data, avg_data
def draw(self, c): c.identity_matrix() if self.arrow == ToolContext.ARROW_START: util.write(c, 'Select arrow start component.', 40,40, 32) elif self.arrow == ToolContext.ARROW_END: util.write(c, 'Select arrow end component.', 40,40, 32) if self.is_selection: c.new_path() x,y,w,h = self.selected_xywh c.rectangle(x,y,w,h) c.set_source_rgb(1,0,0) c.close_path() c.stroke()
def run(matrix_dir, cosine_dir, similarity_method): util.makedir(cosine_dir) count = 0 for file_name in os.listdir(matrix_dir): name = file_name.split('.')[0] count += 1 util.write('begin %s: %s' % (count, name)) file_path = os.path.join(matrix_dir, file_name) matrix = util.load_matrix(file_path) # sim_matrix = cosine(matrix) sim_matrix = compute_similarity(matrix, similarity_method) cosine_path = os.path.join(cosine_dir, '%s.matrix' % name) util.dump_matrix(sim_matrix, cosine_path) return None
def add_player(self, world_name, name, ignoresPlayerLimit, permission): print("Adding player", name) util.load(self.world_dir(world_name)) player = { "name": name, "ignoresPlayerLimit": ignoresPlayerLimit, "xuid": "" } if permission != "default": player["permission"] = permission util.all_dict[name] = player util.write() return self.players(world_name)
def main(args): hists = [[util.get_tobject(args, h.lstrip("-")) for h in hlist] for hlist in args.hstack] util.validate_hists(args.hstack, hists) hists = [[util.resize_histo(args, h) for h in hlist] for hlist in hists] canvas = ROOT.TCanvas("canvas", "Canvas", 250, 100, 700, 500) hstack = ROOT.THStack() for i, (hlist, hnames) in enumerate(zip(hists, args.hstack)): h = hlist[0] if len(hlist) > 1: for hadd, hname in zip(hlist[1:], hnames[1:]): #if "InTime-" in hname: # h.Add(hadd, 10. / 1837.) if hname.startswith("-"): h.Add(hadd, -1) else: h.Add(hadd) if args.area_normalize and h.Integral() > 1e-4: h.Scale(1. / h.Integral()) color = util.namecolors(args.names[i] if args.names else hnames[0]) if args.stack: h.SetFillColor(color) else: h.SetLineColor(color) h.SetLineWidth(3) if args.names: name = args.names[i] if args.nevent_in_legend: name += " (%i)" % int(h.Integral()) h.SetTitle(name) hstack.Add(h) drawstr = "HIST" if args.stack else "NOSTACK HIST" hstack.Draw(drawstr) util.style(args, canvas, hstack) if args.legend_position: legend = ROOT.gPad.BuildLegend(*(args.legend_position + [""])) if args.logy: canvas.SetLogy() box = util.draw_text(args) canvas.Update() util.wait(args) util.write(args, canvas)
def addMsg(user, content, s, suc, fail): uid = content['id'] pipe.multi() pipe.zadd('msg-list:' + user, time.time(), uid) pipe.hset('msg-box:' + user, uid, json.dumps(content)) res = pipe.execute() util.log("addMsg" + user) if s: if res[-1]: result = {"result": suc} util.write(s, 1, "", result) s.finish() else: util.write(s, 0, fail, {}) s.finish()
def print_traceback(exception): traceback = exception.getattr(u"traceback") if not isinstance(traceback, space.List): raise space.unwind(space.LError(u"Expected null or list as .traceback: %s" % traceback.repr())) out = u"" if len(traceback.contents) > 0: out = u"\033[31mTraceback:\033[36m\n" for entry in reversed(traceback.contents): if not isinstance(entry, TraceEntry): continue name, col0, lno0, col1, lno1 = entry.pc_location() out += u" %s: %d,%d : %d,%d\n" % (name.repr(), lno0, col0, lno1, col1) out += u"\033[31m" out += space.get_interface(exception).name out += u":\033[0m" write(STDERR, out + u" " + exception.repr() + u"\n")
def draw(self, c, x, y, active): w, size = self.w, self.h content = self.modelF()[self.name] if active: c.new_path() c.rectangle(x,y,w,size) c.close_path() c.set_source(cairo.SolidPattern(1,0,.7, .2)) c.fill_preserve() content = self.label + content if self.align == TextfieldAct.CENTER: util.write_center(c, content, x, w, y+size, size) else: util.write(c, content, x, y+size, size)
def report(test_data, information,weight): output = [[] for i in range(0, sum([len(x) for x in test_data]))] # prepare to write with prepare_to_write() as f: # loop for each test-data index = 0 for i in range(0,len(test_data)): write(f, 'Writing report for test data - ' + str(information[i])) for j in range(0, len(test_data[i])): deep_write(f,1,'Test Doc - [' + str(j+1) + ']') _report(f,test_data[i][j],weight,output[j]) index += 1 print_output(f,output) print >> sys.stderr, 'Finished writing a report to report/report.txt'
def save_url(driver, url, device, savename, path, ini): print("function:{0} start".format(sys._getframe().f_code.co_name)) try: # アンカーURL取得 if device == "PC": pageurl_path = path + ini.get("pageurl", "folder") elif device == "Mobile": pageurl_path = path + ini.get("pageurl", "folder") else: print(ini.get("message", "DEVICE_REQUIRE")) sys.exit() util.mkdir(pageurl_path) text = "" element_list = driver.find_elements_by_tag_name("a") li_uniq = list(set(element_list)) # 重複除去 counter = 0 for i in li_uniq: try: href = i.get_attribute("href") if len(href) == 0: continue text = text + "href:{0}".format(href) + "\n" # newdriver = window_init(href, device) # time.sleep(10) # info = get_page_info(newdriver) # print(info) # switch_to_alert(newdriver, ini) # save_href_capture(newdriver, href, savename, path, ini , str(counter)) # save_href_image(newdriver, device, path, ini, str(counter)) pageurl_counter_path = pageurl_path + str(counter) + "/" util.mkdir(pageurl_counter_path) util.write(href, ini.get("pageurl", "listname"), pageurl_counter_path) counter = counter + 1 # newdriver.close() except: counter = counter + 1 # newdriver.close() util.write(text, ini.get("pageurl", "listname"), pageurl_path) except: print("Unexpected error:", sys.exc_info()[0]) print("function:{0} end".format(sys._getframe().f_code.co_name))
def main(args): f = ROOT.TFile(args.input) graph = f.Get(args.graph) canvas = ROOT.TCanvas("canvas", "Canvas", 250, 100, 700, 500) util.style(args, graph) graph.Draw("AC") util.resize_graph(args, graph) graph.Draw("AC") line = ROOT.TGraph(2, array('d', [0., 1.]), array('d', [1., 0.])) line.SetLineColor(ROOT.kRed) line.Draw("SAME") canvas.Update() util.wait(args) util.write(args, canvas)
def draw_frame(self, c, tc, label, mx, my): x,y,w,h = self.xywh() c.new_path() c.rectangle(x,y,w,h) c.close_path() if self in tc.selected_comps: c.set_source_rgb(1,0,0) else: c.set_source_rgb(0,0,0) c.stroke() util.write(c, label, x,y-10, 40) if self.is_close(mx,my): c.new_path() c.rectangle(x,y,w,h) c.close_path() c.set_source(cairo.SolidPattern(.8,.9,1, .2)) c.fill_preserve()
def write(self, data): """:raise IOError: If not all bytes could be written :return: lenght of incoming data""" self.sha1.update(data) cdata = self.zip.compress(data) bytes_written = write(self.fd, cdata) if bytes_written != len(cdata): raise self.exc return len(data)
def get(self): token = self.xsrf_token #when logout, close the socket if SocketHandler.client_user.has_key(self.current_user): util.log("sock close") cursor = yield motor.Op(db.users.find_one,{"user":self.current_user}) util.log("LogoutHandler.get") util.log( cursor) tell = {"type":1, "msg":self.current_user+" left"} #use send_to_all to test SocketHandler.send_to_all(SocketHandler.client_user[self.current_user], json.dumps(tell)) #SocketHandler.send_to_friends(cursor["friends"], json.dumps(tell)) sock = SocketHandler.client_user[self.current_user] del(SocketHandler.client_socket[sock]) del(SocketHandler.client_user[self.current_user]) sock.close() self.clear_cookie("fbt_user") util.write(self, 1, "", {}) self.finish()
def post(self): token = self.xsrf_token if self.get_argument("user") and self.get_argument("pwd"): db.users.find_one({"user":self.get_argument("user"), "password":self.get_argument("pwd")}, callback=(yield gen.Callback("key"))) user = yield gen.Wait("key") #find = False if user[0]: util.log("LoginHandler.post") util.log( user[0]) if user[0][0] and user[0][0]["user"]: #find = True self.set_secure_cookie("fbt_user",self.get_argument("user")) if self.get_argument("remember") and self.get_argument("remember") == "1": self.set_cookie("fbt_u",self.get_argument("user")) self.set_cookie("fbt_pwd",self.get_argument("pwd")) if self.get_argument("next"): self.redirect(self.get_argument("next")) else: result = {} result["result"] = self.get_argument("user") util.write(self, 1, "", result) self.finish() #self.write("ok") return else: util.write(self, 0, "用户名不存在或者用户名与密码不匹配", {}) self.finish() else: util.write(self, 0, "用户名不存在或者用户名与密码不匹配", {}) self.finish() else: util.errorHandle(self, 0) self.finish()
def delete(self): if self.get_argument("user") and self.get_argument("star"): user = self.get_argument("user") star = self.get_argument("star") if star[0] >= '0' and star[0] <= '9': u1 = {"user":self.current_user, "isStar":0} u2 = {"user":self.current_user, "isStar":1} yield db.users.update({'user': user}, {'$pull':{"friends":u1}}) yield db.users.update({'user': user}, {'$pull':{"friends":u2}}) u = {"user":user, "isStar": int(star)} result = yield db.users.update({'user': self.current_user}, {'$pull':{"friends":u}}) util.log( "MyFriend.delete" + str(result)) if result.has_key("nModified") and result["nModified"] == 1: if not result.has_key("writeConcernError"): result = {"result" : "删除好友成功"} util.write(self, 1, "", result) self.finish() else: util.log( result["writeConcernError"]) util.write(self, 0, "删除好友请求失败,请重试", {}) self.finish() else: #util.log( result["writeError"]) util.write(self, 0, "删除好友请求失败,请重试", {}) self.finish() else: util.errorHandle(self, 0) self.finish() else: util.errorHandle(self, 0) self.finish()
def confirmAdd(self): if self.get_argument("user") and self.get_argument("id"): user = self.get_argument("user") re = {"user":self.current_user, "isStar":0} result = yield db.users.update({'user': user}, {'$push':{"friends":re}}) re = {"user":user, "isStar":0} result = yield db.users.update({'user': self.current_user}, {'$push':{"friends":re}}) yield motor.Op(msg_handle.ReadMsg, self.current_user, self.get_argument("id")) util.log( "MyFriend.confirmAdd" + str(result)) if result.has_key("nModified") and result["nModified"] == 1: if not result.has_key("writeConcernError"): result = {"result" : "添加好友成功"} util.write(self, 1, "", result) self.finish() else: util.log( result["writeConcernError"]) util.write(self, 0, "添加好友请求失败,请重试", {}) self.finish() else: #util.log( result["writeError"]) util.write(self, 0, "添加好友请求失败,请重试", {}) self.finish() else: util.errorHandle(self, 0) self.finish()
def draw_cm_ruler(self, c, dpi, width=130): c.save() c.set_line_cap(cairo.LINE_CAP_SQUARE) c.translate(*c.get_current_point()) c.set_line_width(5) c.move_to(0, 0) c.line_to(mm(dpi, width), 0) for x in [mm(dpi, xm) for xm in range(0, width+1, 10)]: c.move_to(x, 0) c.rel_line_to(0, mm(dpi, -3)) c.stroke() for x, xm in [(mm(dpi, xm), xm) for xm in range(0, width+1, 10)]: n = xm/10 c.move_to(x, mm(dpi, -4)) write(c, "%d" % n, self.font_bold, mm(dpi, 2.5), centered=True) c.move_to(mm(dpi, 1.5), mm(dpi, -4)) write(c, "cm", self.font_bold, mm(dpi, 2)) c.restore()
def draw_custom_ruler(self, units_per_mm, width=130): if not self.c: return self.c.set_source_rgb(255, 255, 255) self.c.rectangle(0, mm(self.dpi, 85), width, mm(self.dpi, 85) + 10) self.c.fill() self.c.set_source_rgb(0, 0, 0) self.c.move_to(self.offset_of_xo_side_from_screen, mm(self.dpi, 85)) self.c.save() self.c.set_line_cap(cairo.LINE_CAP_SQUARE) self.c.translate(*self.c.get_current_point()) self.c.set_line_width(5) self.c.move_to(0, 0) self.c.line_to(mm(self.dpi, width), 0) for x in [mm(self.dpi, xm / 100.) for xm in xrange( 0, (width + 1) * 100, int(units_per_mm * 100))]: self.c.move_to(x, 0) self.c.rel_line_to(0, mm(self.dpi, -3)) self.c.stroke() pt_list = [x / int(units_per_mm * 100) for x in \ range(0, (width + 1) * 100, int(units_per_mm * 100))] coord_list = [mm(self.dpi, xm / 100) for xm in range( 0, (width + 1) * 100, int(units_per_mm * 100))] if units_per_mm > 7: # Avoid overlapping labels for a in range(0, len(coord_list)): xm = pt_list[a] x = coord_list[a] n = xm self.c.move_to(x, mm(self.dpi, -4)) write(self.c, "%d" % n, self.font_bold, mm(self.dpi, 2.5), centered=True) self.c.restore()
def get_res_list(self): # user = self.get_argument("user", None) page = self.get_argument("page", 1) sort_by = self.get_argument("sort_by", ResourceStoreManager.res_sort_by["online_num"]) res_type = self.get_argument("res_type", ResourceInfo.get_main_index_by_type("电影")) try: page = int(page) assert page>=0 sort_by = int(sort_by) assert sort_by == ResourceStoreManager.res_sort_by["online_num"] res_type = int(res_type) assert ResourceInfo.is_valid_main_type(res_type) except: util.errorHandle(self, 0) self.finish() return cache_key="fb:online:type:"+str(res_type)+":page:"+str(page) redis_cache=RedisCacheClient().get_instance() RES_CNT_IN_A_PAGE=20 msg = {} msg["type"] = 1 msg["error"] = "" resource_list = redis_cache.get(cache_key) if resource_list: resource_list=json.loads(resource_list) else: resource_list = yield OnlineResources.get_online_resources_by_type(res_type, page, RES_CNT_IN_A_PAGE) redis_cache.set(cache_key, json.dumps(resource_list)) ONE_MINUTES=1 * 60 redis_cache.expire(cache_key,ONE_MINUTES) if self.get_argument("version", "") >= "1.8": size = OnlineResources.get_online_resources_count(res_type) util.write(self, 1, "", {"size":size, "res":resource_list}) else: util.write(self, 1, "", resource_list) self.finish()
def addMsg(user, content, s, suc, fail): result = yield db_msg.user_msg.update({'user': user}, {'$addToSet':{"msg_list":content}}, True) util.log( "addMsg" + user ) if result.has_key("nModified") and result["nModified"] == 1: if not result.has_key("writeConcernError"): result = {"result" : suc} util.write(s, 1, "", result) s.finish() else: util.log( result["writeConcernError"]) util.write(s, 0, fail, {}) s.finish() else: #util.log( result["writeError"]) util.write(s, 0, fail, {}) s.finish()