async def main(self): """The main loop.""" opus_file = os.environ.get("OPUS", "dev_opus.yaml") sync_assets = os.environ.get( "SCREENCRASH_SYNC_ASSETS", "true") == "true" exit_on_validation_failure = os.environ.get( "SCREENCRASH_EXIT_ON_VALIDATION_FAILURE", "true") == "true" print("Loading opus...") self._opus = await load_opus( Path("resources") / opus_file, read_asset_data=sync_assets, exit_on_validation_failure=exit_on_validation_failure ) self._performance = Performance(self._opus) self._ui = UI(self._opus, self._performance.history) self._components: Dict[str, ComponentPeer] = { "internal": InternalPeer(sync_assets), "media": MediaPeer(sync_assets), "inventory": InventoryPeer(sync_assets), } self._setup_events() self._distribute_assets() print("Started!") async with websockets.serve(self.socket_listener, "0.0.0.0", self._port): await asyncio.Future() # run forever
def __init__(self, url, access_id, access_key): self.timer = Timer(Config.request_interval, self.task) self.sessions = Sessions() self.performance = Performance() self.url = url + Config.endpoint self.auth = (access_id, access_key) self.timestamp = int(time.time()) self.start()
def exec_operation(): arg_type = ArgType() args_res = arg_type.analyze_type conf = Config() if arg_type.is_monitor: perform = Performance(arg_type.process_name, arg_type.message) if LifecycleBase._export_to is None: LifecycleBase._export_to = conf.export_path if Performance._export_to is None: Performance._export_to = conf.export_path operations = { ArgType.SIGN_IN | ArgType.FIRST_SIGN_IN :\ lambda var, msg: lifecycle_cases.NonSSOLifecycle().first_sign_in(var, msg), ArgType.SIGN_IN | ArgType.SECOND_SIGN_IN:\ lambda var, msg: lifecycle_cases.NonSSOLifecycle().second_sign_in(var, msg), ArgType.SIGN_IN | ArgType.FIRST_SIGN_IN | ArgType.SECOND_SIGN_IN:\ lambda var, msg: lifecycle_cases.NonSSOLifecycle().first_second_sign_in_out(var, msg), ArgType.SSO | ArgType.SIGN_IN | ArgType.FIRST_SIGN_IN:\ lambda var, msg: lifecycle_cases.SSOLifecycle().first_sign_in(var, msg), ArgType.SSO | ArgType.SIGN_IN | ArgType.SECOND_SIGN_IN:\ lambda var, msg: lifecycle_cases.SSOLifecycle().second_sign_in(var, msg), ArgType.SSO | ArgType.SIGN_IN | ArgType.FIRST_SIGN_IN | ArgType.SECOND_SIGN_IN:\ lambda var, msg: lifecycle_cases.SSOLifecycle().first_second_sign_in_out(var, msg), ArgType.MONITOR:\ lambda: perform.begin_monitor(time_span=arg_type.timespan), } method = operations.get(args_res) if method is not None: if arg_type.is_monitor: method() else: log_list = LogParser(conf.log_file_path, conf.log_file_tag) method(log_list, arg_type.message) if arg_type.clean: log_list.clean_log() logging.info("Log cleaned")
def main(): """主程序""" EI = Essential_Information() PF = Performance() BF = Basic_Function() while True: IF = Interfac() option = input("请输入您的操作:") if option == '1': # 添加学生基本信息 EI.input_info() elif option == '2': # 添加学生成绩信息 PF.InputPerformance() elif option == '3': # 显示所有学生信息 BF.Ess_Info() elif option == '4': # 查询指定学生信息 BF.Assig_Ess() elif option == '5': # 显示所有学生成绩 BF.stu_info() elif option == '6': # 查询指定学生成绩 BF.AssignInfo() elif option == '7': # 修改学生信息 # BF.ChangeInformation() pass elif option == '8': # 修改学生成绩 pass elif option == '9': # 删除学生信息 BF.del_info() elif option == 'a': # 删除学生成绩 BF.del_performance() elif option == '0': # 退出系统 print("正在退出系统,请稍等...") time.sleep(1) break
def main(): parser = argparse.ArgumentParser( "Script to compare bed file against reference bed") parser.add_argument("input", nargs="+", help="The BED file to analyse") parser.add_argument("-r", "--reference", required=True, help="The reference BED file to compare against") parser.add_argument("-o", "--output", help="The output venn plot") args = parser.parse_args() ref_bed = bed12.loadbed(args.reference, False, False) print("Loaded Reference BED file. # junctions: ", len(ref_bed)) # Load all bed files print("Results:") print("File\t#junc\t", Performance.shortHeader()) recall = 0 precision = 0 f1 = 0 for bf in args.input: bed_data = bed12.loadbed(bf, False, False) # Build table tab = list() p = Performance() p.tp = len(ref_bed & bed_data) p.fp = len(bed_data - ref_bed) p.fn = len(ref_bed - bed_data) print(bf, "\t", len(bed_data), "\t", p) recall += p.recall() precision += p.precision() f1 += p.F1() if len(args.input) > 1: print("Mean recall: ", recall / len(args.input)) print("Mean precision: ", precision / len(args.input)) print("Mean f1: ", f1 / len(args.input)) if not args.output == None and len(args.input) == 1: # Create Venns plt = figure(1, figsize=(6, 6)) venn2(subsets=(p.fn, p.fp, p.tp), set_labels=(args.reference, args.input)) plt.show() plt.savefig(args.output)
def main(): parser = argparse.ArgumentParser( "Script to create the Venn Plots from BED files") parser.add_argument( "input", nargs='+', help="The directory containing BED files from pipeline") parser.add_argument("-r", "--reference", required=True, help="The reference BED file to compare against") parser.add_argument("-o", "--output", required=True, help="The output prefix") parser.add_argument( "-f", "--filter", ) args = parser.parse_args() ref_bed = bed12.loadbed(args.reference, False, False) print("Loaded Reference BED file. # junctions: " + str(len(ref_bed))) # Load all bed files bed_data = {} aligners = set() reads = set() junc_analysers = set() for bed_file in args.input: bed_base = os.path.splitext(os.path.basename(bed_file))[0] parts = bed_base.split('-') if (not parts[0] == "trinity"): aligners.add(parts[0]) reads.add(parts[1]) junc_analysers.add(parts[2]) bed_data[parts[2] + "-" + parts[0]] = bed12.loadbed( bed_file, False, False) print("Loaded: " + bed_file + "; # junctions: " + str(len(bed_data[parts[2] + "-" + parts[0]]))) print("Found these aligners: " + ', '.join(aligners)) print("Found these reads: " + ', '.join(reads)) print("Found these junction analysis tools: " + ', '.join(junc_analysers)) # Build table tab = [] for a in aligners: for j in junc_analysers: p = Performance() p.tp = len(bed_data[j + "-" + a] & ref_bed) p.fp = len(bed_data[j + "-" + a] - ref_bed) p.fn = len(ref_bed - bed_data[j + "-" + a]) tab.append(a + "\t" + j + "\t" + p.__str__()) # Output table to disk with open(args.output + "-junc_analysis.tab", "w") as tab_out: print("Aligner\tFilter\t" + Performance.shortHeader(), file=tab_out) for p in tab: print(p, file=tab_out)
def __init__(self) -> None: self.DB_HOST = os.environ.get('DB_HOST') self.DB_USER = os.environ.get('DB_USER') self.DB_PASS = os.environ.get('DB_PASS') self.DB_NAME = os.environ.get('DB_NAME') self.FUNCTION_NAME = os.environ.get('FUNCTION_NAME', '') self.db = None self.db_c = None self.connect_db() # Analysis attributes self.report = request.args.get('report', 'home') # objects self.performance = Performance(self.db_c, self.FUNCTION_NAME) self.pricing = Pricing(self.db_c, self.FUNCTION_NAME) self.weekend_vs_weekdays = WeekendVsWeekdays(self.db_c, self.FUNCTION_NAME)
def search(self, im_file, query): perf = Performance(False) im_bytes = base64.b64decode(im_file) search_page = 0 try: search_page = query["page"] except KeyError as e: pass res = cmd.search(perf, self.es, self.evaluate, im_bytes, int(search_page)) if res: hits, palettes, w_dists, w_scores, palette, query_tags, rating = res zipped = list(zip(hits, palettes, w_dists, w_scores)) processed = data_process(zipped) should_plot = True try: if not query["plot"]: should_plot = False except KeyError as e: pass plt_bytes = None if should_plot: perf.begin_section("plotting") plt_bytes = plotting.plot(im_bytes, res, True) perf.end_section("plotting") out_dict = { "palette": palette.tolist(), "query_tags": query_tags, "query_rating": rating[0], "results": processed, "performance": perf.gen_report() } send_img = base64.b64encode(plt_bytes).decode("utf-8") if should_plot else "*" return { "data": out_dict, "plot": send_img, "success": True } return { "success": False }
def calcPerformance(passed, failed, invert=False): tp = 0 tn = 0 fp = 0 fn = 0 passed_res = pd.value_counts(passed['genuine'].values, sort=True) failed_res = pd.value_counts(failed['genuine'].values, sort=True) if invert: tn = passed_res['False'] fn = passed_res['True'] tp = failed_res['True'] fp = failed_res['False'] else: tp = passed_res['True'] fp = passed_res['False'] tn = failed_res['False'] fn = failed_res['True'] return Performance(tp=tp, tn=tn, fp=fp, fn=fn)
def get_performances(self, start_after=None): from performance import Performance start_after = Performance.make_time_str(start_after or datetime.datetime.now()) return self._performace_list.filter('time_sort >', start_after).order('time_sort')
from performance import Performance from goody import irange from graph_goody import random_graph,spanning_tree # Put script below to generate data for Problem #1 # In case you fail, the data appears in sample8.pdf in the helper folder for i in range(8): size=(2**i)*1000 g=random_graph(size,lambda x:x*10) p=Performance(lambda :spanning_tree(g),lambda :None,5,'\n\nSpanning Tree of size ' + str(size)) p.evaluate() p.analyze()
def create_performance(): return Performance()
"page": { "size": "20", "page": "1" }, # "userId": userId } } #初始化测试次数i、所有线程总花费时间 i = 0 time_total = 0 #构造线程组测试接口 while i < thread_count: #实例化类,传入url、请求头、请求方法、post方法要传输参数body pf = Performance( url=url, header=header, body=data, ) status = pf.test_performance()[0] spend_time = pf.test_performance()[1] print(pf.test_performance()[2]) print(i) #respond = pf.send_request() spend_time_list.append(spend_time) total = total + 1 if status == "success": success_total += 1 elif status == "fail": fail_total += 1 elif status == "except":
import random from goody import irange from priorityqueue import PriorityQueue from performance import Performance def setup(N): global pq pq = PriorityQueue([random.randrange(0, N) for i in range(0, N)]) def code(N): global pq for i in range(N): pq.remove() for i in irange(0, 8): N = 10000*2**i P = Performance(lambda: code(10000), lambda: setup(N), 8, title = "\nPriority Queue of size " + str(N)) P.evaluate() P.analyze()
from performance import Performance from goody import irange from graph_goody import random_graph, spanning_tree # Put script here to generate data for Problem #1 # In case you fail, the data appears in sample8.pdf in the helper folder def creat_random(n): global correct_size correct_size = random_graph(n, lambda n: n * 10) for i in irange(0, 7): n = 1000 n = n * (2**i) p = Performance(lambda: spanning_tree(correct_size), lambda: creat_random(n), 5, title='Spanning Tree of size {}'.format(n)) p.evaluate() p.analyze() print()
def create_training_sets(args): # Load portcullis junctions into dataframe print("Loading input junctions ... ", end="", flush=True) original = DataFrame.from_csv(args.input, sep='\t', header=0) fieldnames = [key for key in dict(original.dtypes)] print("done.", len(original), "junctions loaded.") # Before we go further make sure we have a sufficent number of junctions to work with. Minimum 1000. if len(original) < 500: raise ValueError("Not enough junctions to create training set") if args.genuine: glist = load_genuine(args.genuine) if len(glist) != len(original): raise ValueError( "Genuine list and input junctions do not contain the same number of elements. Genuine:" + len(glist) + "; input:" + len(original)) original["genuine"] = pd.Series(glist, index=original.index) print() print("Creating initial positive set for training") print("------------------------------------------") print() print( "Applying the following set of rule-based filters to create initial positive set." ) for i, pjson in enumerate(args.pos_json, start=1): print("\t".join([str(i), pjson])) print() print("LAYER\t", end="") if args.genuine: print(Performance.longHeader()) else: print("PASS\tFAIL") df = original.copy() # Required for pandas eval pos_juncs = None # Run through layers of logic to get the positive set for i, json_file in enumerate(args.pos_json, start=1): print(str(i) + "\t", end="", flush=True) # Create pandas command pandas_cmd = json2pandas(open(json_file), fieldnames) # print(pandas_cmd) # Execute the pandas command, result should be a filtered dataframe pos_juncs = eval(pandas_cmd) nb_not_pos = len(original) - len(pos_juncs) if args.genuine: not_pos_juncs = original.reset_index().merge( pos_juncs, indicator=True, how='outer').set_index('index') not_pos_juncs = not_pos_juncs.loc[not_pos_juncs['_merge'] == 'left_only'] del not_pos_juncs['_merge'] print(calcPerformance(pos_juncs, not_pos_juncs).longStr()) else: print(str(len(pos_juncs)) + "\t" + str(nb_not_pos)) if args.save_layers: pos_juncs.to_csv(args.prefix + ".pos_layer_" + str(i) + ".tab", sep='\t') # Check we have enough junctions left in positive se (100), if not then stop here if len(pos_juncs) <= 100: print("WARNING: We recommend at least 100 junctions in the positive set and this set of rules lowered " + \ "the positive set to", len(pos_juncs), ". Will not filter positive set further.", file=sys.stderr) pos_juncs = df.copy() # Override previous filter break df = pos_juncs.copy() # Get L95 for intron sizes if len(pos_juncs) == 0: raise ValueError( "Can't build training sets, positive set filter left no junctions remaining." ) pos_intron_sizes = pos_juncs["size"].tolist() pos_intron_sizes.sort(key=int) L95 = pos_intron_sizes[int(len(pos_intron_sizes) * 0.95)] pos_length_limit = int(L95 * 1.2) print("Intron size L95 =", L95, " positive set maximum intron size limit set to L95 x 1.2:", pos_length_limit) # Also save this to file as we'll need it back in the C program with open(args.prefix + ".L95_intron_size.txt", 'w') as l95out: print("Length of intron at 95th percentile", file=l95out) print(L95, file=l95out) if len(pos_juncs) > 100: pos_juncs = pos_juncs.loc[pos_juncs["size"] <= pos_length_limit] print("\t".join([ str(x) for x in [i + 1, len(pos_juncs), len(original) - len(pos_juncs)] ])) if args.save_layers: pos_juncs.to_csv(args.prefix + ".pos_layer_intronsize.tab", sep='\t') print() print("Positive set contains:", len(pos_juncs), "junctions") print() print("Saving positive set to disk ... ", end="", flush=True) if args.genuine: del pos_juncs["genuine"] pos_file = args.prefix + ".pos.junctions.tab" pos_juncs.to_csv(pos_file, sep='\t') print("done. File saved to:", pos_file) not_pos_juncs = original.reset_index().merge( pos_juncs, indicator=True, how='outer').set_index('index') not_pos_juncs = not_pos_juncs.loc[not_pos_juncs['_merge'] == 'left_only'] del not_pos_juncs['_merge'] print(len(not_pos_juncs), "remaining for consideration as negative set") print() print("Creating initial negative set for training") print("------------------------------------------") print( "Applying a set of rule-based filters to create initial negative set.") for i, njson in enumerate(args.neg_json, start=1): print("\t".join([str(i), njson])) print() print("LAYER\t", end="") if args.genuine: print(Performance.longHeader()) else: print("PASS\tFAIL") neg_juncs = [] df = not_pos_juncs # Run through layers of logic to get the positive set for i, json_file in enumerate(args.neg_json, start=1): print(str(i) + "\t", end="", flush=True) # Create pandas command pandas_cmd = json2pandas(open(json_file), fieldnames) #print(pandas_cmd) # Execute the pandas command, result should be a filtered dataframe neg_juncs.append(eval(pandas_cmd)) nb_not_neg = len(df) - len(neg_juncs[-1]) if args.genuine: print(calcPerformance(neg_juncs[-1], df).longStr()) else: print(str(len(neg_juncs[-1])) + "\t" + str(nb_not_neg)) if args.save_layers: neg_juncs[-1].to_csv(args.prefix + ".neg_layer_" + str(i) + ".tab", sep='\t') neg_length_limit = int(L95 * 10) print("Intron size L95 =", L95, "negative set will use junctions with intron size over L95 x 10:", neg_length_limit) neg_juncs.append(df.loc[df["size"] > neg_length_limit]) if args.genuine: print(str(i + 1) + "\t" + calcPerformance(neg_juncs[-1], df).longStr()) else: print( str(i + 1) + "\t" + str(len(neg_juncs[-1])) + "\t" + str(len(df) - len(neg_juncs[-1]))) if args.save_layers: neg_juncs[-1].to_csv(args.prefix + ".neg_layer_intronsize.tab", sep='\t') neg_set = pd.concat(neg_juncs) print() print("Negative set contains:", len(neg_set), "junctions") if args.genuine: del neg_set["genuine"] print("Saving negative set to disk ... ", end="", flush=True) neg_file = args.prefix + ".neg.junctions.tab" neg_set.to_csv(neg_file, sep='\t') print("done. File saved to:", neg_file) if args.save_failed: print( "Creating file containing junctions not in positive or negative set ... ", end="", flush=True) training = pd.concat([pos_juncs, neg_set]) remaining = original.reset_index().merge( training, indicator=True, how='outer').set_index('index') others = remaining.loc[remaining['_merge'] == 'left_only'] other_file = args.prefix + ".others.tab" others.to_csv(other_file, sep='\t') print("done. File saved to:", other_file) print() print("Final train set stats:") print(" - Positive set:", len(pos_juncs), "junctions.") print(" - Negative set:", len(neg_set), "junctions.") print(" - Others:", len(original) - len(pos_juncs) - len(neg_set), "junctions.") print()
from performance import Performance from goody import irange from graph_goody import random, spanning_tree # Put script here to generate data for Problem #1 g = None def create_random(n): global g g = random(n, lambda n : 10*n) for i in irange(0,7) : n = 1000 * 2**i p = Performance(lambda : spanning_tree(g), lambda : create_random(n),5,'Spanning Tree of size {}'.format(n)) p.evaluate() p.analyze() print()
# print(model) print('trg_vocal_len: ', len(TRG.vocab)) print('src_vocab_len: ', len(SRC.vocab)) vocab_to_json(TRG.vocab, params.word_json_file, params.trg_lang) vocab_to_json(SRC.vocab, params.word_json_file, params.src_lang) print("write data to json finished !") # optimizer = torch.optim.Adam(model.parameters(), lr=params.lr, betas=(0.9, 0.98), eps=1e-9) optimizer = ScheduledOptim( torch.optim.Adam(model.parameters(), lr=params.lr, betas=(0.9, 0.98), eps=1e-09), params.d_model, params.n_warmup_steps) performance = Performance(len(TRG.vocab), trg_pad, is_smooth=params.is_label_smooth) print("\nbegin training model") if os.path.exists("models/tfs.pkl"): print('load previous trained model') model.load_state_dict(torch.load("models/tfs.pkl")) best_loss = None train_global_steps = 0 writer = SummaryWriter() for epoch in range(params.epochs): start = time.time() total_loss = 0.0 step = 0
from performance import Performance from goody import irange from graph_goody import random_graph, spanning_tree # Put script below to generate data for Problem #1 # In case you fail, the data appears in sample8.pdf in the helper folder def multby10(x: int) -> int: return x * 10 random_graph(8, multby10) print(random_graph(8, multby10)) for x in range(8): nodes = 1000 * (2**x) print(nodes) def create_random(): return random_graph(nodes, multby10) def spamming_tree(): return spanning_tree(create_random()) p = Performance(spamming_tree, create_random, 5, 'spanning tree of size ' + str(nodes)) p.evaluate() p.analyze() print("Done")
def random_graph(nodes: int, edges: int) -> {str: {str}}: g = {str(n): set() for n in range(nodes)} for i in range(int(edges)): n1 = str(randrange(nodes)) n2 = str(randrange(nodes)) if n1 != n2 and n1 in g and n2 not in g[n1]: g[n1].add(n2) g[n2].add(n1) return g # Put code here to generate data for Quiz 8 problem #1 def create_random(): global grph grph = random_graph(nodes, nodes * 5) nodes = 100 while nodes <= 12800: create_random() performance = Performance(code=lambda: find_influencers(graph=grph), setup=lambda: create_random(), times_to_measure=5, title="find_influencers of size " + str(nodes)) for i in irange(5): performance.evaluate() performance.analyze() nodes = nodes * 2 print()
def create_performance(self): return Performance(self.scalers)
from performance import Performance from goody import irange from graph_goody import random, spanning_tree # Put script here to generate data for Problem #1 def create_random(): global dog dog = random(x*1000, lambda n : 10*n) if __name__ == '__main__': x=1 while x<129: y = Performance(lambda:spanning_tree(dog), lambda:create_random(), title = "Spanning Tree of Size " + str(x*1000)) y.evaluate() y.analyze() print() x = 2*x
class Core: """ This is the main class, keeping track of all state. Parameters ---------- port The port to run a websocket server on """ def __init__(self, port: int): self._port = port async def main(self): """The main loop.""" opus_file = os.environ.get("OPUS", "dev_opus.yaml") sync_assets = os.environ.get( "SCREENCRASH_SYNC_ASSETS", "true") == "true" exit_on_validation_failure = os.environ.get( "SCREENCRASH_EXIT_ON_VALIDATION_FAILURE", "true") == "true" print("Loading opus...") self._opus = await load_opus( Path("resources") / opus_file, read_asset_data=sync_assets, exit_on_validation_failure=exit_on_validation_failure ) self._performance = Performance(self._opus) self._ui = UI(self._opus, self._performance.history) self._components: Dict[str, ComponentPeer] = { "internal": InternalPeer(sync_assets), "media": MediaPeer(sync_assets), "inventory": InventoryPeer(sync_assets), } self._setup_events() self._distribute_assets() print("Started!") async with websockets.serve(self.socket_listener, "0.0.0.0", self._port): await asyncio.Future() # run forever def _setup_events(self): self._ui.add_event_listener("next-node", self._performance.next_node) self._ui.add_event_listener("prev-node", self._performance.prev_node) self._ui.add_event_listener("goto-node", self._performance.goto_node) self._ui.add_event_listener( "run-actions", self._performance.run_actions) self._ui.add_event_listener( "run-actions-by-id", self._performance.run_actions_by_id) self._ui.add_event_listener( "choose-path", self._performance.choose_path) self._ui.add_event_listener( "component-action", self._run_action_on_the_fly) self._ui.add_event_listener("component-reset", self._reset_component) self._ui.add_event_listener( "component-restart", self._restart_component) self._performance.add_event_listener( "history-changed", self._ui.changed_history) self._performance.add_event_listener( "run-action", self._run_action_by_id) for component in self._components.values(): component.add_event_listener("effect-added", self._ui.effect_added) component.add_event_listener( "effect-changed", self._ui.effect_changed) component.add_event_listener( "effect-removed", self._ui.effect_removed) component.add_event_listener( "info-updated", self._ui.component_info_updated) component.add_event_listener( "state-updated", self._ui.component_state_updated) component.add_event_listener("log-message", self._ui.log_message) component.add_event_listener( "disconnected", self._ui.component_removed) def _distribute_assets(self): for asset in self._opus.assets.values(): for component in self._components.values(): if any([component.handles_target(target) for target in asset.targets]): component.add_asset(asset) def _run_action_by_id(self, action_id): try: action = self._opus.action_templates[action_id] self._run_action(action) except KeyError: print( f"Failed to find action or asset for action {action_id}. Skipping.") return def _run_action_on_the_fly(self, target, cmd, asset_names, params): action = ActionTemplate( "on_the_fly_action", target, cmd, "Live command", asset_names, params) self._run_action(action) def _run_action(self, action): handled = False assets = [self._opus.assets[key] for key in action.assets] for peer in self._components.values(): if peer.handles_target(action.target) and peer.nof_instances() > 0: try: peer.handle_action( action.target, action.cmd, assets, action.params) handled = True except Exception as e: print(f"Failed to run handle_action: {e}") if not handled: print( f"Warning: Action {action.id} not handled by anyone ({action.target})") for subaction in action.subactions: self._run_action(subaction) def _reset_component(self, component_id: str): for peer in self._components.values(): if peer.has_component(component_id): peer.reset_component(component_id) def _restart_component(self, component_id: str): for peer in self._components.values(): if peer.has_component(component_id): peer.restart_component(component_id) async def socket_listener(self, websocket: WebSocketServerProtocol, _path: str): """ This function handles an incoming websocket connection. Parameters ---------- websocket The websocket """ # Wait for a hello message = await websocket.recv() message_dict = json.loads(message) client_type = message_dict["client"] if client_type == "ui": await self._ui.handle_socket(websocket) elif client_type in self._components: print(f"Accepted client of type {client_type}") await self._components[client_type].handle_socket(websocket, message_dict) else: print( f"An unsupported client type tried to connect: {client_type}")
class Monitor(object): def __init__(self, url, access_id, access_key): self.timer = Timer(Config.request_interval, self.task) self.sessions = Sessions() self.performance = Performance() self.url = url + Config.endpoint self.auth = (access_id, access_key) self.timestamp = int(time.time()) self.start() def __del__(self): self.stop() def start(self): self.timer.start_timer() def stop(self): self.timer.cancel_timer() def done(self): return Config.number_requests == self.sessions.completed and (not self.sessions.pending) def build_body(self, start_time, end_time): return '{"%s":[{"%s":"%s","%s":"%s"}],"%s": %d,"%s":%d, "%s": %d, "%s": %d}' % \ (Header.query, Header.query, Config.metric_query, Header.row_id, Config.row_id, Header.start_time, start_time, Header.end_time, end_time, Header.requested_data_points, Config.requested_data_points, Header.max_data_points, Config.max_data_points) def task(self): if self.sessions.completed < Config.number_requests: self.send() else: self.stop() def query(self, time_range): index = Index(self.sessions.completed, time_range) end_time = int(self.timestamp + self.sessions.completed) * 1000 body = self.build_body(end_time - time_range, end_time) logger.debug("sending request %s ", body) self.sessions.add(index, body) perf_start = time.time() response = requests.post(self.url, data=body, headers=Config.query_headers, auth=self.auth) perf_end = time.time() results = response.json() try: data_points = results['response'][0]['results'][0]['datapoints']['value'] quantization = results['queryInfo']['actualQuantizationInSecs'] except Exception as e: logger.warning("Metrics sla monitor query exception %s" % str(e)) data_points = [] quantization = 1 query = Query(perf_start, perf_end, body, time_range, data_points, quantization) self.performance.add_query(index, query) self.sessions.delete(index) logger.debug("finishing request (%d, %d) %s %s with %s in %f seconds", index.index, index.time_range, str(Config.query_headers), body, str(response), (perf_end - perf_start)) def send(self): logger.debug("sending request with time_ranges %s", str(Config.query_ranges)) self.sessions.complete() for time_range in Config.query_ranges: self.query(time_range) logger.debug("finishing request with time_ranges %s", str(Config.query_ranges))
class Analysis: """ This class is responsible for managing and showing some basic metrics and also links for plots generated on demand. It can be served either by Waitress + Flask, or it can also be served as a plain python app. It is also ready to run in GCP Cloud Function. """ def __init__(self) -> None: self.DB_HOST = os.environ.get('DB_HOST') self.DB_USER = os.environ.get('DB_USER') self.DB_PASS = os.environ.get('DB_PASS') self.DB_NAME = os.environ.get('DB_NAME') self.FUNCTION_NAME = os.environ.get('FUNCTION_NAME', '') self.db = None self.db_c = None self.connect_db() # Analysis attributes self.report = request.args.get('report', 'home') # objects self.performance = Performance(self.db_c, self.FUNCTION_NAME) self.pricing = Pricing(self.db_c, self.FUNCTION_NAME) self.weekend_vs_weekdays = WeekendVsWeekdays(self.db_c, self.FUNCTION_NAME) def connect_db(self): # DB Connection self.db = MySQLdb.connect(host=self.DB_HOST, db=self.DB_NAME, user=self.DB_USER, passwd=self.DB_PASS) # Setting to use dictionaries instead of tuples (default) self.db_c = self.db.cursor(MySQLdb.cursors.DictCursor) def route(self): """Based on the report GET/query string parameter, routes and returns a specific function Returns: response, http code """ if self.report == 'home': return self.home() elif self.report == 'performance': return self.performance_analysis() elif self.report == 'pricing': return self.pricing_analysis() elif self.report == 'weekend-vs-weekdays': return self.weekend_vs_weekdays_analysis() return f"Oops... I didn't find anything here... <a href='/{self.FUNCTION_NAME}'>Go back to home</a>?", 404 def home(self): return render_template( 'home.html', summary=self.get_summary(), performance_links=self.performance.get_reports_links(), pricing_links=self.pricing.get_reports_links(), weekend_vs_weekdays_links=self.weekend_vs_weekdays. get_reports_links(), ) def get_summary(self): query = """ SELECT min(datetime) as first_date, max(datetime) as last_date, datediff(max(datetime),min(datetime)) duration_days, count(id) as tests_executed, sum(http_reqs_count) as total_request, ROUND(sum(data_received_count)/1e+9,2) as downloaded_gb, ROUND(sum(data_sent_count)/1e+9,2) as uploaded_gb, count(distinct(csp)) as caas_tested, csp FROM metrics WHERE csp != 'docker' """ # total self.db_c.execute(query) total = self.db_c.fetchall() # per csp self.db_c.execute(query + " GROUP BY csp") csps = self.db_c.fetchall() total_csps = {} for item in csps: total_csps[item['csp']] = item return {'total': total[0], 'csp': total_csps} def performance_analysis(self): cpu = request.args.get('cpu', None) memory = request.args.get('memory', None) factorial = request.args.get('factorial', None) type_of_app = request.args.get('app', None) avg_only = bool(int(request.args.get('avg_only', False))) all_in_one = bool(int(request.args.get('all_in_one', False))) if all_in_one: diagrams = [ self.performance.get_diagram(type_of_app, cpu, memory, '10', avg_only), self.performance.get_diagram(type_of_app, cpu, memory, '1000', avg_only), self.performance.get_diagram(type_of_app, cpu, memory, '10000', avg_only), self.performance.get_diagram(type_of_app, cpu, memory, '32000', avg_only), self.performance.get_diagram(type_of_app, cpu, memory, '43000', avg_only), self.performance.get_diagram(type_of_app, cpu, memory, '50000', avg_only), self.performance.get_diagram(type_of_app, cpu, memory, '60000', avg_only), ] html = '' for item in diagrams: html += item return html return self.performance.get_diagram(type_of_app, cpu, memory, factorial, avg_only) def pricing_analysis(self): pricing_scenario = request.args.get('scenario', None) if pricing_scenario == 'crossing': return self.pricing.get_crossing_diagram() elif pricing_scenario == 'pricing_factorial_60000': return self.pricing.get_pricing_based_on_factorial_60000_execution_time( ) else: return self.pricing.get_diagram(pricing_scenario) def weekend_vs_weekdays_analysis(self): type = request.args.get('type', None) return self.weekend_vs_weekdays.get_diagram_app_execution_time(type)
patch_size=project.patchSize, nkerns=project.nKernels, kernel_sizes=project.kernelSizes, hidden_sizes=project.hiddenUnits, train_time=project.trainTime, momentum=project.momentum, learning_rate=project.learningRate, path=project.path_offline, id=project.id) # Generate offline performance results # NOTE: this assumes an offline model has been fully trained. This code # will generate variation of information and pixel errors for test images # specified in Paths.TestGrayscale nTests = 10 Performance.measureOffline(model, project.id, mean=project.mean, std=project.std,maxNumTests=nTests) print 'measuring online performance...' model = CNN( rng=rng, input=x, batch_size=project.batchSize, patch_size=project.patchSize, nkerns=project.nKernels, kernel_sizes=project.kernelSizes, hidden_sizes=project.hiddenUnits, train_time=project.trainTime, momentum=project.momentum, learning_rate=project.learningRate,
def multiclass(): analisys = Performance() for r in releases: analisys.average_results(input_dir + 'dto_multiclass_results_' + r + '.csv', kind='multiclass', release=r) analisys.run_rank_choose_parameters( input_dir + 'average_results_multiclass_' + r + '.csv', kind='multiclass', release=r) analisys.grafico_variacao_alpha(kind='multiclass', release=r) analisys.best_alpha(kind='multiclass') r = 'v1' analisys.run_global_rank(input_dir + 'average_results_multiclass_' + r + '.csv', kind='multiclass', release=r) analisys.overall_rank('geo', 'multiclass', 7.5) analisys.overall_rank('iba', 'multiclass', 7.5) df = pd.read_csv('./../output/overall_rank_results_multiclass_7.5_iba.csv') analisys.cd_graphics(df, datasetlen=7, kind='multiclass') df = pd.read_csv('./../output/overall_rank_results_multiclass_7.5_geo.csv') analisys.cd_graphics(df, datasetlen=7, kind='multiclass')
def PySedSim(file_name = 'PySedSim_Input_Specifications.csv', Simulation_mode = 'regular', start_stop = None, parallel_label = None, borg_dict = None, dps_dict = None, decision_vars = None, scenario_name=None, re_eval = None, policy_name=None): ''' Purpose: Conducts all processes necessary to run a simulation, including basic data import, monte carlo data import, network and system element creation, simulation, and export of results. All of the work in this function is done by imported functions/methods. Note: The user is required to provide a .csv file titled "PySedSim_Input_Specifications" that contains assumptions related to input and output file paths, simulation title, and Monte Carlo parameter generation preferences. This file must be located in the same directory as this top-level file. Inputs: (1) file_name [OPTIONAL] is the name of the input file, which must be a .csv file (as string) located in the same directory as the file that contains this pysedsim.py file. (Default: 'PySedSim_Input_Specifications.csv'). (2) simulation_mode [OPTIONAL] (Options: 'regular' or 'debug'. 'debug' stores all variable output temporarily and uses more RAM. Default: 'regular'.). (3) start_stop [OPTIONAL] is a list containing the start and stop integers defining the range of simulations to be conducted on a given processor (either through distributed computing over processors on a single computer, or through many processors on a cluster). Example of list: [8, 15]. This would mean 7 simulations are to be conducted. This 7 simulations may represent a subset of simulations being conducted as part of a monte carlo ( i.e., this processor is being asked to conduct 7 simulations out of 1000 simulations). (4) parallel_label is a label to append to the end of any output files produced from a given simulation or batch of simulations, to reflect the output produced by different processors. For example, in a parallellized monte carlo simulation, this could be the rank of the processor on which the pysedsim_wrapper calling this function is running, so that the distributed output from the processors can be aggregated into a single file. (5) Borg_dict is an optimization dictionary. This is defined automatically in the optimization_pysedsim.py module. (6) decision_vars (optional) is a list of decision variable values specified by an external optimization model ( e.g., Borg) in the form of a numpy array. Currently, the only thing this input can be used for is to populate the parameters for a RBF-type reservoir operating policy. (7) scenario_name: Optional. List containing one string that represents the name of the current scenario ( internal to optimization or post-optimization) from which this pysedsim function is being called for purposes of policy reevaluation. Example: ['Scenario 1']. Ensures that if pysedsim is called from within an optimzation loop ( over multiple scenarios) for purposes of re-evaluating policies from a reference set, only the current optimization scenario gets re-evalutad here, rather than all scenarios listed in the input file. (8) re_eval: List of preferences related to reevaluation of operating policies from the reference set, such as what variables/locations should be exported, whether simulation is deterministic/stochastic, and if stochastic how many realizations. (9) policy_name: Name of the policy being evaluated, if pysedsim is being run in a reevaluation setting (from processing_reference_set.Policy_Reevaluation()). Policy number is used as the name, and sub-folder is created. Outputs: 1. Data are written to files and saved 2. If user has connected PySedSim to an external optimization model, wherein PySedSim() is the "function evaluation", then PySedSim returns objs, the objective function values. Example function call: (1) PySedSim(file_name = 'PySedSim_Input_File.csv', simulation_mode = 'debug') (2) PySedSim() ''' # Import entire file "PySedSim_Input_Specification.csv", then load specific information to variables [num_scenarios, simulation_titles_list, imported_specs, main_input_files_dir, main_output_file_dir, monte_carlo_file_list, external_mc_data] = Determine_Num_Scenarios(file_name) if borg_dict is not None: # An optimization is being performed. Scenarios can be looped through in optimization_pysedsim(), but not here given optimization # is being performed. opt_dict = borg_dict['opt_dict'] num_scenarios = 1 simulation_titles_list = [opt_dict['Scenario Name']] export_data = 'No' # Do not dump output to .csv file in optimization setting. export_data_scenario = 'No' # Ensures export_data is 'No' for all scenarios being looped through. else: # Optimization will not take place. opt_dict = None if scenario_name is not None: num_scenarios = 1 simulation_titles_list = scenario_name export_data_scenario = None # Data export preferences for particular scenario in chain, to prevent carryover. if dps_dict is not None: dps_dict['Policy Function'] = {'Raw Parameters': decision_vars[0:dps_dict['n_vars']]} try: dps_dict['Flushing Optimization']['Flushing Parameters'] = decision_vars[dps_dict['n_vars']:dps_dict['total_vars']] except KeyError: pass if start_stop is not None: parallelize = 1 # Processing in parallel. Has implications for how to read in data if stochastic simulation. else: parallelize = None for i in range(num_scenarios): # In case looping through scenarios, re-initiate start_stop if parallelize is None: start_stop = None if export_data_scenario is None: export_data = None start_time = time.time() # Keep track of PySedSim model execution time simulation_title = simulation_titles_list[i] # Feed in a string as the simulation title monte_carlo_file = monte_carlo_file_list[i] # Import simulation preferences from user-provided input file. [export_data, export_file_type, var_sub_list, element_export_list, Input_Data_File, T, Sim_Dur, Stochastic_Sim, Num_Realizations, simulation_dates, simulation_dates_no_leap, Col_Names, Monte_Carlo_Parameters_File_Name, external_mc_data, simulation_title, start_stop, Sampled_Parameter_Dict, Synthetic_Inflow_dataframe_name_LIST, Synthetic_Inflows_dictionary] = Import_Simulation_Preferences(imported_specs, simulation_title, main_input_files_dir, re_eval=re_eval, start_stop=start_stop, export = export_data, Monte_Carlo_Parameters_File_Name=monte_carlo_file, external_mc_data = external_mc_data) # Create an initial network of reservoirs, channels, juntions, etc. that will be simulated. [SystemObjects, Time_Series_Output_Dictionary, Output_Object_Dict, Element_Dict, Flushing_Group_Dict, element_stochastic_components, Parameter_Input_Dictionary] = Simulation_Network_Creation(Input_Data_File) if Stochastic_Sim == 1: [Parameter_Input_Dictionary, element_stochastic_components, Sampled_Parameter_Dict, Synthetic_Inflow_dataframe_name_LIST, Synthetic_Inflows_dictionary] = Monte_Carlo_Import(main_input_files_dir, Col_Names, element_stochastic_components, SystemObjects["Ordered Simulation List"], external_mc_data, Monte_Carlo_Parameters_File_Name, simulation_dates, simulation_dates_no_leap, Num_Realizations, Sampled_Parameter_Dict, Synthetic_Inflow_dataframe_name_LIST, Synthetic_Inflows_dictionary, start_stop=start_stop, parallelize_import=parallelize) # Run main simulation [state_list_excel, Time_Series_Output_Dictionary, Output_Object_Dict] = SedSim_Main_Simulation(Num_Realizations, T, Input_Data_File, element_stochastic_components, SystemObjects, Element_Dict, Flushing_Group_Dict, Stochastic_Sim, Parameter_Input_Dictionary, simulation_dates_no_leap, Time_Series_Output_Dictionary, Sim_Dur, simulation_dates, Col_Names, Simulation_mode, Output_Object_Dict, var_sub_list, element_export_list, Sampled_Parameter_Dict, Synthetic_Inflow_dataframe_name_LIST, Synthetic_Inflows_dictionary, op_policy_params=dps_dict) print("--- Simulation(s) Complete in %s seconds ---" % (time.time() - start_time)) # Export output data if user indicates this should be conducted, or if pysedsim is being called as part of a # re-evaluation process, in which case output is (assumed to be) desired to be stored. if export_data == 'Yes' or scenario_name is not None: Export_Simulation_Output(export_file_type, Time_Series_Output_Dictionary, state_list_excel, main_output_file_dir, simulation_title, var_sub_list, rank = parallel_label, policy_name=policy_name) # If PySedSim() is connected to an external optimization model, evaluate performance over the realizations. if opt_dict is not None: objs = Performance(Time_Series_Output_Dictionary, Sim_Dur, opt_dict=opt_dict, sim_title = simulation_title) return objs
from graph_goody import random_graph, spanning_tree from graph import Graph #Submitter edwardc6(Chen,Edward) # Put script here to generate data for Problem #1 # In case you fail, the data appears in sample8.pdf in the helper folder # random(100,lambda n : 10*n) def create_random(n): global rand_graph rand_graph = random_graph(n, lambda n: 10 * n) def span_time(n): global rand_graph spanning_tree(rand_graph) z = 500 while True: n = z * 2 if (n == 256000): break zoon = 'Spanning Tree of size {}'.format(str(n)) jk = Performance(lambda: span_time(rand_graph), lambda: create_random(n), 5, zoon) jk.evaluate() jk.analyze() print() z = n
def main(): parser = argparse.ArgumentParser("Script to produce a list labelling whether each entry in the portcullis tab input belong to the bed reference or not. Please note that this will only work with reference files that have distinct entries.") parser.add_argument("input", help="The filtered bed file to assess") parser.add_argument("-r", "--reference", required=True, help="The input bed file prior to filtering") parser.add_argument("-l", "--labels", required=True, help="The labels which should match entries in reference") parser.add_argument("-o", "--output", default="bedref_out.labels", help="Output prefix for output files") args = parser.parse_args() # Load reference and labels, divide into tp and tn rp = set() rn = set() labs = open(args.labels, "r") refs = open(args.reference, "r") # read header line from bed file header = refs.readline() # Now we should have the same number of lines in both files line = 1 while 1: ref_line = refs.readline() lab_line = labs.readline() if not ref_line and not lab_line: break if (lab_line and not ref_line) or (not lab_line and ref_line): print("ERROR: reference file and labels file have a different number of entries.", file=sys.stderr); exit(1) ref_line = ref_line.strip() lab_line = lab_line.strip() if lab_line == "1": rp.add(bed12.BedEntry.create_from_line(ref_line, False, False)) elif lab_line == "0": rn.add(bed12.BedEntry.create_from_line(ref_line, False, False)) else: print("ERROR: Label file contains an entry that is not either \"0\" or \"1\" at line:", line, file=sys.stderr); exit(1) line += 1 labs.close() refs.close() print("Reference contains", len(rp), "positive and", len(rn), "negative entries.") p = set() with open(args.input) as f: # Skip header h = f.readline() for line in f: cleanline = line.strip() if not cleanline == "": p.add(bed12.BedEntry.create_from_line(line, False, False)) print("Input contains", len(p), "entries") tp = p & rp fp = p & rn fn = rp - p tn = rn - p perf = Performance(tp = len(tp), fp = len(fp), fn = len(fn), tn = len(tn)) print(Performance.longHeader()) print(perf.longStr())
n_out=len(project.labels), train_time=project.trainTime, batch_size=project.batchSize, patch_size=project.patchSize, path=project.path_offline) nTests = 1 print 'measuring offline performance...' #Performance.measureOffline(model, project.id, mean=project.mean, std=project.std,maxNumTests=nTests) x = T.matrix('x') model = MLP(id=project.id, rng=rng, input=x, n_in=project.patchSize**2, n_hidden=project.hiddenUnits, n_out=len(project.labels), train_time=project.trainTime, batch_size=project.batchSize, patch_size=project.patchSize, path=project.path) print 'measuring online performance...' Performance.measureOnline(model, project.id, mean=project.mean, std=project.std, maxNumTests=nTests) #Performance.measureBaseline(model, project.id,maxNumTests=nTests) #Performance.measureGroundTruth(model, project.id,maxNumTests=nTests)
import random from goody import irange from performance import Performance from priorityqueue import PriorityQueue def setup(size): global pq alist = [i for i in range(size)] random.shuffle(alist) pq = PriorityQueue(alist) def code(removes): global pq for i in range(removes): pq.remove() for i in irange(0,8): size = 10000 * 2**i p = Performance(lambda : code(10000), lambda:setup(size),5,'\n\nPriority Queue of size ' + str(size)) p.evaluate() p.analyze()
def evaluation(self, samples): performance = Performance() vote_list = [] self.no_match = 0 if THRESHOLD == 1: bi_partition = one_threshold elif THRESHOLD == 2: bi_partition = rank_cut else: raise Exception("prediction threshold method unidentified!") def get_prediction_prob(sample): self.population.make_eval_matchset(sample[0]) vote0 = {} if not self.population.matchset: self.no_match += 1 else: if PREDICTION_METHOD == 1: # TODO max prediction not consistent with the remainder label_prediction = max_prediction([ self.population.popset[ref] for ref in self.population.matchset ], self.rng.randint) else: vote0 = aggregate_prediction([ self.population.popset[ref] for ref in self.population.matchset ]) if DEMO: self.demo(sample, vote0, self.data.sim_matrix) vote_list.append(vote0) self.population.clear_sets() [get_prediction_prob(sample) for sample in samples] target_list = [sample[1] for sample in samples] theta = optimize_theta(vote_list, target_list) for t, vote in zip(target_list, vote_list): prediction = bi_partition(vote, theta) performance.update_example_based(vote, prediction, t) performance.update_class_based(prediction, t) performance.micro_average() performance.macro_average() performance.roc(vote_list, target_list) multi_label_perf = performance.get_report(samples.__len__()) class_precision = {} for label in self.data.label_ref.keys(): class_measure = performance.class_based_measure[label] class_precision[ self.data.label_ref[label]] = class_measure['TP'] / ( class_measure['TP'] + class_measure['FP'] + 1) sample_coverage = 1 - (self.no_match / samples.__len__()) return [multi_label_perf, class_precision, sample_coverage]
from performance import Performance from goody import irange from graph_goody import random_graph,spanning_tree from graph import Graph # Put script below to generate data for Problem #1 # In case you fail, the data appears in sample8.pdf in the helper folder global nodes global graph if __name__ == '__main__': nodes = 1000 while nodes <= 128000: graph = random_graph(nodes,lambda n : 10*n) perf = Performance(lambda: spanning_tree(graph),setup=lambda:None,times_to_measure=5,title='Spanning Tree Timings for '+str(nodes)+' nodes') perf.evaluate() perf.analyze() nodes *= 2