def generate_header(module): if not Args().skip_header_generation: text = json.dumps(HeaderGenerator().visit(module), separators=(',', ':')) with open(Args().get_header_path(), 'w') as file: file.write(text) return handle_next_stage(module, generate_code)
def run_test_code(self, source_path: str, expect_fail: bool): args = Args() self.init_args(args) args.source_path = source_path args.expect_fail = expect_fail io = TestIo() run(args, io)
def get_exp_name() -> str: args = Args() name_list = [str(get_exp_no()), _time()] exp_name = args.get_args().experiment if str(exp_name) is not 'None': name_list.append(exp_name) name_list.append(args.get_name()) return '_'.join(name_list)
def __init__(self,args): signal.signal(signal.SIGINT,self.Sigint_handler) self.total_threads = 8 self.threads_flag = "fix" self.spin_lock_num = 0x0 self.apic_id_list = [] self.apic_id_list_all = [] self.perf_flag = False Args.__init__(self,args)
def test_header_inclusion(self): self.reset_module() a_code = '''module a type a<`a, `b> = { A, B, C = `a * `b, D = a<`a, a<bool, string>> * `b } let foo = fun(a, b) -> { a + b } let bar = fun(a, b) -> { foo(a, b) != 0 } let baz = fun(f, x, y) -> { f(x) = y } ''' b_code = '''module b type b<`a> = { A, B = `a * int } let foo = fun(a, b) -> { a = b } let bar = fun(a) -> { foo(a, 0) } let baz = fun(f, g, x) -> { f(g(x)) }''' c_code = '''module c import "a" open "b" let test1 = fun(a, b, c) -> { foo(a.baz(bar, a, b), c) } let test2 = baz let test3 = a.bar''' Args().source = 'a.tml' parse_source_code(a_code) self.reset_module() Args().source = 'b.tml' parse_source_code(b_code) self.reset_module() Args().source = 'c.tml' Args().skip_header_generation = True parse_source_code(c_code) assert_let_types( self, { 'test1': fun_type([t_int, t_bool, t_bool], t_bool), 'test2': fun_type([fun_type([t_a], t_b), fun_type([t_c], t_a), t_c], t_b), 'test3': fun_type([t_int, t_int], t_bool) })
def main(): """ Main function """ args = Args(sys.argv) if args.parse_args() == macro.ERROR: return macro.ERROR zappy = Zappy(args) return zappy.launch()
def go(): # setup logging log_queue = Manager().Queue(-1) log_thread = Thread(target=MultiGameManager.logger_thread, args=(log_queue, )) log_thread.start() # process pool args = ((i, log_queue) for i in range(Args.instance().args().n_games)) with Pool(Args.instance().args().n_parallel) as pool: while True: res = pool.map(SokobanManager.go, itertools.islice(args, 20)) if res is None: breaks
def main(): args = Args().get_args() kwargs = vars(args) checkpoint = torch.load(args.checkpoint) base_classifier = get_architecture(checkpoint["arch"], args.dataset) base_classifier.load_state_dict(checkpoint['state_dict']) attacker = SmoothAttack(base_classifier) smoothed_classifier = Smooth(base_classifier, get_num_classes(args.dataset), args.sigma) dataset = get_dataset(args.dataset, 'test') average_nat = [] average_adv = [] j_header('index', 'nat_y', 'adv_y', 'nat_rad', 'adv_rad', 'success') figure = FigureSaver() for i in range(0, len(dataset), args.skip): (x, label) = dataset[i] x = x.cuda() first_x = x.data nat_pred, nat_rad = smoothed_classifier.certify( x, args.N0, args.N, args.alpha, args.batch) if nat_pred is -1: continue if args.dataset == DATASETS[0]: # ImageNet targets = [j for j in range(0, 1000, 100) if j is not label] else: targets = [j for j in range(10) if j is not label] best_rad = -10.0 best_image = None best_target = -1 for target in targets: adv_x = attacker.perturb(x=first_x, y=target, **kwargs) # If you want to do wasserstein attack, uncomment the following and change the attacker to wasserstein # adv_x = attacker.perturb(x=first_x, y=target, eps=args.sigma, steps=args.steps, batch=args.batch) adv_pred, adv_rad = smoothed_classifier.certify( adv_x, args.N0, 2 * args.N0, args.alpha, args.batch) adv_suc = (adv_pred != label) and (adv_pred != -1) and (nat_pred != -1) adv_rad = adv_rad if adv_suc else -adv_rad if adv_rad > best_rad: best_rad = adv_rad best_image = adv_x.data best_target = target figure.save(best_image, i, 'best={}'.format(best_target)) figure.save(first_x, i, 'natural') best_pred, best_rad = smoothed_classifier.certify( best_image, args.N0, args.N, args.alpha, args.batch) j_print(i, label, best_target, nat_rad, best_rad) average_adv.append(best_rad) average_nat.append(nat_rad) average_nat = np.array(average_nat) average_adv = np.array(average_adv) print('Average nat radii {}, Average adv radii {}'.format( average_nat.mean(), average_adv.mean()))
def infer_types(module): if Args().stop_before_type_inferring: print(GlobalTypeInferer().dump()) exit(0) GlobalTypeInferer().infer() return handle_next_stage(module, generate_header)
def test_header_to_json_and_back(self): Args().skip_header_generation = True code = ''' module test type list<`a> = { Empty, Cons = `a * list<`a> } type a<`x, `y> = { B = ref<ref<ref<`x>>> * `y * `x * ref<ref<ref<`y>>>, A = a<bool, a<int, float>> } let a: `a -> `a -> bool = fun(a, b) -> {True} let b = fun(f, x, y) -> {f(x) = y} let c: string -> a<int, string> -> unit = fun(p, q) -> {()} let d = fun() -> { let a = 4; a + 4 } let foo = fun(a, b) -> { a + b } let bar = fun(a, b) -> { foo(a, b) != 0 } let baz = fun(f, x, y) -> { f(x) = y }''' parse_source_code(code) dic = HeaderGenerator().visit(GlobalModule()) module2 = HeaderReader().read_module(dic) dic2 = HeaderGenerator().visit(module2) self.assertEqual(json.dumps(dic), json.dumps(dic2))
def logger_thread(log_queue): logging.getLogger().setLevel(Args.instance().args().log_level) while True: record = log_queue.get() if record is None: break logging.log(record.levelno, record.message)
def get_graph_data(dataset, isModelDataset=False): """ Given a dataset name, loads in the graphs of that dataset. Creates a specific argument object for that dataset to allow for different datasets to be created. return: If isModelDataset: returns args, train, val, test split else: returgn args, data """ args_data = Args() args_data.change_dataset(dataset) # Load the graph data - Consider using presaved datasets! with graph load list graphs = create(args_data) graphs_len = len(graphs) random.seed(123) shuffle(graphs) # Display some graph stats graph_node_avg = 0 for graph in graphs: graph_node_avg += graph.number_of_nodes() graph_node_avg /= graphs_len print('Average num nodes', graph_node_avg) args_data.max_num_node = max( [graphs[i].number_of_nodes() for i in range(graphs_len)]) max_num_edge = max( [graphs[i].number_of_edges() for i in range(graphs_len)]) min_num_edge = min( [graphs[i].number_of_edges() for i in range(graphs_len)]) # show graphs statistics print('total graph num: {}'.format(graphs_len)) print('max number node: {}'.format(args_data.max_num_node)) print('max/min number edge: {}; {}'.format(max_num_edge, min_num_edge)) print('max previous node: {}'.format(args_data.max_prev_node)) if isModelDataset: # split datasets graphs_len = len(graphs) graphs_test = graphs[int(0.8 * graphs_len):] graphs_train = graphs[0:int(0.8 * graphs_len)] graphs_validate = graphs[0:int(0.2 * graphs_len)] return args_data, graphs_train, graphs_validate, graphs_test return args_data, graphs
def main(): logger = set_log() logger.info('程序运行开始') args = Args().get_all_args() set_seed(args) # 进行模型的训练 # 根据参数选择 trainer Trainer = getattr(trainers, args.trainer_name) trainer = Trainer(args) args.do_train = True if args.do_train: trainer.train() # 在做 test 之前,应该需要 load 以前保存的最佳模型 if args.do_test: trainer.test()
def parse_source_code(text: str): ast = parser.parse(text, tracking=True) if Args().stop_after_parsing and Errors().is_ok(): print(json.dumps(AstToDictVisitor().visit(ast))) exit(0) return handle_next_stage(ast, visit_ast)
def go(xargs): game_index, log_queue = xargs logging.getLogger().setLevel(Args.instance().args().log_level) logging.getLogger().handlers.clear() logging.getLogger().addHandler( logging.handlers.QueueHandler(log_queue)) SokobanManager.logger_set = True SokobanManager(game_index).start()
def main(): time1 = time.time() print('Start:', time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime(time1))) args = Args() print(args) ''' Ler arquivo args.reference_output searchLevel (achar abaixo e acima de Fermi) diferenca entre abaixo e acima (gap ref) ''' below, above = utils.search_level(args.reference_output) ref_gap = abs(above - below) print(ref_gap) particle = utils.read_particle(args.particle_path) print('>' + str(particle).strip() + '<') print(args.specie_type_1) print(args.specie_nat_1) print(args.specie_type_2) print(args.specie_nat_2) #montar liga na fracao informada pelo usuario #Sorteio de posicoes from random import sample at_species = [args.specie_type_1] * args.n_atoms random_indices = sample(list(range(args.n_atoms)), args.specie_nat_2) for idx in random_indices: at_species[idx] = args.specie_type_2 print(at_species) particle.at_species = at_species print('>' + str(particle).strip() + '<') template_file = open('input_template.in') content = template_file.read() template_file.close() content = content.replace('<#NAT#>', str(args.n_atoms)) content = content.replace('<#NTYP#>', str(args.n_species)) content = content.replace('<#PARTICLE#>', str(particle).strip()) espresso_input_file = open('input.in', 'w') print(content) ''' Colocar no template content = "" content = content.replace('<#NAT#>', args.n_atoms) content = content.replace('<#NTYP#>', args.n_species) content = content.replace('<#PARTICLE#>', conteudo_particula) #salvar arquivo -> new_content ''' '''
def main(): args = Args() lfm = lastfm.init() user = lfm.get_user(args.user) artists = get_artists(user, args) genres = get_genres(artists, args) chart.save(args, genres)
def main( game, configf, icon='kw.ico' ) : #MAPF = 'maps.zip' app = AutoSaverApp( game ) args = Args( configf, game=game ) args.icon = icon #if not os.path.isfile( MAPF ) : # msg = "Download map preview data?" # result = wx.MessageBox( msg, "Confirm", # wx.ICON_QUESTION|wx.YES_NO|wx.YES_DEFAULT ) # if result == wx.YES : # download_maps( MAPF ) frame = AutoSaverForm( icon ) frame.tray_icon.open_replay_viewer() #frame.Show( show=False ) dont need this, hidden app! app.MainLoop()
def main() -> None: args = Args() args.parse(sys.argv[1:]) # strip off the first argument (program name) if args.show_info: logging.basicConfig(level=logging.INFO) success = False try: io = UserIo() run(args, io) success = True except FileNotFoundError as e: logger.error(e) except ParseError as e: logger.error('Syntax error: ' + str(e)) except ProgramError as e: logger.error('Program failed: ' + str(e)) if not success: exit(1)
def get_args(): # create args # You just need to change the parameters here at = False # Todo: change here if not at: # for normal training return Args( dataset="imdb", model_short_name="cnn", batch_size=32, epochs=75, adversarial_training=False, orig_model_prefix="cnn-imdb", max_length=2500, # for evaluate attack_class_for_testing=attack_classes[ 1], # test robustness against which model num_attack_samples=50, # how many samples to test robustness with # for pre-generate attack_class_for_training=attack_classes[ 1], # specify which attack to generate adv samples on the trained model adv_sample_file= "cnn-imdb-bae.csv", # file name of where to save adv. samples adversarial_samples_to_train= 2000, # how many samples in adv_sample_file ) else: # for adversarial training return Args( dataset="kaggle-toxic-comment", model_short_name="lstm", batch_size=32, epochs=75, adversarial_training=True, at_model_prefix="lstm-at-tb-imdb", adv_sample_file= "lstm-kaggle-tb.csv", # from which file program will read adv. samples # for evaluate attack_class_for_testing=attack_classes[0], num_attack_samples=250, )
def fetch_resource(resource_type): args = Args(resource_type) db_manager = DbManager(args) scraper = get_scraper(args) if args.refetch: db_manager.delete_resource(args.db_key) if not db_manager.resource_exists(args.db_key): resource_data = scraper.get_resource(args.query_params) if scraper.driver: scraper.driver.quit() db_manager.save_resource(args.db_key, resource_data) return db_manager.fetch_resource(args.db_key)
def CAN(DX, c, k=15, r=-1, islocal=True): """ :param DX: a temporal sequence of n x n distance matrix for n data points :param c: number of clusters :param k: number of neighbors to determine the initial graph, and the parameter r if r<=0 :param r: paremeter, which could be set to a large enough value. If r<0, then it is determined by algorithm with k :param islocal: 1: only update the similarities of the k neighbor pairs, faster 0: update all the similarities :return: A: num*num learned symmetric similarity matrix evs: eigenvalues of learned graph Laplacian in the iterations """ arg = Args() N_ITER = arg.n_iter SDX = [] if arg.debug: print('\nInitial Parameters:', '\nc = ', c, '\nk = ', k, '\nr = ', r, '\nislocal = ', islocal, '\nNITER = ', N_ITER) # Initialization for distX in DX: num = distX.shape[0] distX1 = np.sort(distX, axis=1) idx = np.argsort(distX, axis=1) A = np.zeros((num, num)) rr = get_gamma(distX, k) eps = 10e-10 for i in range(0, num): A[i, idx[i, 1:k + 2]] = 2 * (distX1[i, k + 1] - distX1[i, 1:k + 2]) / (rr[i] + eps) SDX.append(A) if r < 0: r = np.mean(rr) lmd = np.mean(rr) wt_local = [ np.ones(args.c_dim) / len(connectome_list) for i in range(0, len(connectome_list)) ] for itr in range(0, N_ITER): for t in range(0, len(DX)): distX = DX[t] A = SDX[t] return A
def create_brain_net_node_files(sub, tentative_label=[]): """ :param sub: subject name :param tentative_label: optional label list which can influence the label numbers :return: """ args = Args() data_dir = os.path.join(os.path.join(args.root_directory, os.pardir), 'AD-Data_Organized') file_parcellation_table = os.path.join( os.path.join(os.path.join(data_dir, sub), 'helper_files/parcellationTable.json')) # Reading parcellation table to get the coordinates of each region with open(file_parcellation_table) as f: table = json.load(f) connectomes = readMatricesFromDirectory(os.path.join( data_dir, sub)) # Reading connectomes n_components, connectome_labels = get_number_of_components(connectomes) if len(tentative_label) > 0: print(len(tentative_label[0])) connectome_labels = [ replace_labels(connectome_labels[t], tentative_label[0]) for t in range(0, len(connectome_labels)) ] T = len(connectomes) # Number of time points N = len(table) # Number of regions node_size = 4 output_dir = os.path.join(data_dir, sub + '/helper_files/brain_net/node_file') if not os.path.exists(output_dir): os.makedirs(output_dir) for t in range(1, T + 1): print("\n----", t, "----\n") print(len(connectome_labels[t - 1]), N) assert (len(connectome_labels[t - 1]) == N), "Incorrect number of regions" with open(os.path.join(output_dir, sub + "_t" + str(t) + ".node"), 'w') as node_file: print("Writing: ", node_file.name) for i in range(0, N): node_file.write( str(table[i]["coord"][0]) + " " + str(table[i]["coord"][1]) + " " + str(table[i]["coord"][2]) + " " + str(connectome_labels[t - 1][i] * 10) + " " + str(node_size) + " " + table[i]["name"] + "\n")
def main(): args = Args() app = web.Application() app.add_routes([ web.post("/connection", routes.connection.connection), web.get("/printer", routes.get_status.get_status), web.get("/job", routes.get_job.get_job), web.post("/printer/command", routes.post_command.post_command), web.post("/files/local/{file}", routes.print_file.print_file), web.post("/job", routes.cancel_print.cancel_print) ]) get_printer(args.path) web.run_app(app, host='0.0.0.0', port=args.port)
def set_state(self, new_state: Map): new_state_key = StateKey(new_state) self.action_values = dict() self.best_action = None for action in Actions.ALL: self.action_values[action] = self.get_q_value( new_state_key, action) if self.best_action is None or self.best_action[ 0] < self.action_values[action]: self.best_action = (self.action_values[action], action) if self.last_state_key is not None: next_q_val = max(self.action_values.values()) updated_q_val = self.last_q_value + Args.instance().args( ).learning_rate * (self.last_reward + Args.instance().args().discount_factor * (next_q_val - self.last_q_value)) self.set_q_value(self.last_state_key, self.last_action, updated_q_val) self.last_q_value = updated_q_val self.last_state_key = new_state_key
def main(): args = Args() with Display(device=args.device, driver=args.driver) as display: monitor = display.connect_monitor(args.monitor) if isinstance(monitor, Virtual): monitor.orientation = args.orientation display.scale = 8 now = time.monotonic() rotori = 0 try: while True: start = now while now - start < 1.0: now = time.monotonic() if display.channels == 3: display.buffer[:] = intro_effect( display.width, display.height)(now) * 0.75 display.pixels[0:2, 0:2] = np.array((0xff, 0, 0)) display.pixels[2:6, 0] = np.array((0, 0xff, 0)) display.pixels[2:6, 1] = np.array((0, 0, 0)) display.pixels[0, 2:8] = np.array((0, 0, 0xff)) display.pixels[1, 2:8] = np.array((0, 0, 0)) elif display.channels == 1: display.buffer[:, :, 0] = ( intro_effect(display.width, display.height)(now) * 0.75)[:, :, 1] display.show() time.sleep(0.01) rotori += 1 display.rotation = rotori & 3 if isinstance(display, Virtual): display.orientation = (rotori >> 2) & 3 display.flip_horizontal = (rotori >> 4) & 1 display.flip_vertical = (rotori >> 5) & 1 except KeyboardInterrupt: pass
def parse_args(_=None): arg_parser = argparse.ArgumentParser() arg_parser.add_argument('source', help='Путь к файлу с исходным кодом.') arg_parser.add_argument('-p', '--stop-after-parsing', help='Остановиться после синтаксического анализа и вывести АСД в виде JSON.', action='store_true') arg_parser.add_argument('-i', '--stop-before-type-inferring', help='Остановиться перед выводом типов и вывести систему уравнений.', action='store_true') arg_parser.add_argument('-g', '--skip-header-saving', help='Пропустить сохранение заголовочного файла модуля.', action='store_true') args = arg_parser.parse_args() Args(args) return handle_next_stage(None, read_source_code)
def main(args): a = Args() if args.experiment == "nearestneighbor": nearestneighbor.run(a) elif args.experiment == "mcts": mcts.run(a) elif args.experiment == "exact": exact.run(a) elif args.experiment == "gurobi": gurobi.run(a) elif args.experiment == "insertion": insertion.run(a) elif args.experiment == "policy": policy.run(a) elif args.experiment == "parallel": parallel.run(a) elif args.experiment == "selfplay": selfplay.run(a) else: raise ValueError("Invalid experiment selection.")
def start(self): turn_number = 0 action_result = None for _ in range(1, Args.instance().args().n_turns + 1): turn_number += 1 for player_name, player in self.player.items(): player.set_state(self.map) action = player.get_action() action_result = self.map.apply_action(player_name, action) player.set_action_result(action_result) if action_result.all_boxes_in_target: logging.info("YES! Game %d was won in turn %d", self.game_number, turn_number) break if action_result.max_turns_reached: logging.info("Game %d was lost at turn %d", self.game_number, turn_number) logging.debug("Game %d final position at turn %d was:\n%s", self.game_number, turn_number, self.map)
class Logger(): args = Args().args cwd = os.getcwd() with open(cwd + "/logging.json", 'r') as logging_configuration_file: config_dict = json.load(logging_configuration_file) if 'console' in config_dict['handlers']: config_dict['handlers']['console']['level'] = args.verbosity config_dict['handlers']['console']['formatter'] = args.verbosity # if 'level' in config_dict['root']: # config_dict['root']['level'] = args.verbosity for handler in config_dict['handlers']: # config_dict['handlers'][handler]['formatter'] = args.verbosity if 'filename' in config_dict['handlers'][handler]: if path.isfile(config_dict['handlers'][handler]['filename']): print "Deleting logfile %s" % ( config_dict['handlers'][handler]['filename']) remove(config_dict['handlers'][handler]['filename']) logging.config.dictConfig(config_dict) logger = logging.getLogger(__name__) logger.info("Logger Initialized") logger.debug("DEBUG Logger Initialized")
def __init__(self, game_number): self.game_number = game_number logging.debug("Initializing map for game %d", game_number) self.map: Map = Map(Args.instance().args().n_turns) self.map.load_from_file(Args.instance().args().map) self.player: Dict[str, RandomPlayer] = dict() for player_name in self.map.players.keys(): logging.debug("Initializing player %s for game %s as %s", player_name, game_number, Args.instance().args().player) if Args.instance().args().player == "random": self.player[player_name] = RandomPlayer(player_name) elif Args.instance().args().player == "human": self.player[player_name] = HumanPlayer(player_name) elif Args.instance().args().player == "qlearn": self.player[player_name] = QlearnPlayer(player_name)
def parse_args(self): """Parse CLI args.""" Args(self.tcex.parser) self.args = self.tcex.args