def test_search(): params = { "search_term": "smad2", "term_type": "All", "ptm_type": [ "Acetylation", "C-Glycosylation", "Myristoylation", "Ubiquitination", "N-Glycosylation", "S-Glycosylation", "Phosphorylation", "S-Nitrosylation", "O-Glycosylation", "Methylation", "Sumoylation" ], "role": "Enzyme or Substrate", "organism": [] } result = requests.get('{host}/search'.format(host=host), params=params) # assert if request was successful assert result.status_code == 200, result.text # assert if header contains count assert result.headers.get("count") != None # parse the returned response returned_search_results = json.loads(result.text) # load the expected response expected_search_results = helper.load_json("search.json") for index, search_result in enumerate(expected_search_results): assert (search_result in returned_search_results ) == True, "Item at index: {index} not found".format( index=index)
def main(): # Create a parser object parser = argparse.ArgumentParser(description="Neural Network Prediction") # Add argument to the parser object parser.add_argument('--top_k', action='store', dest='top_k', type=int, default=3, help='Number of top result') parser.add_argument('--category_names', action='store', dest='cat', type=str, default='cat_to_name.json', help='json name to map catgories') parser.add_argument('--gpu', action='store_true', dest='gpu', default=False, help='Use GPU if --gpu') parser.add_argument('--st', action='store_true', default='False', dest='start', help='--st to start predicting') parser.add_argument('--img', action = 'store', dest = 'img', type = str, default = 'Sample_image.jpg',help = 'Store Img Name') # Parse the argument from standard input args = parser.parse_args() # Print out parsing/default parameters print('---------Parameters----------') print('gpu = {!r}'.format(args.gpu)) print('img = {!r}'.format(args.img)) print('top_k = {!r}'.format(args.top_k)) print('cat = {!r}'.format(args.cat)) print('start = {!r}'.format(args.start)) print('-----------------------------') if args.start == True: model, class_labels = helper.load_saved_models() cat_to_name, label_order = helper.load_json(args.cat) ps, labels, index = helper.predict(args.img, model, args.top_k, cat_to_name, class_labels, args.gpu) print("------------------Prediction------------------") for i in range(len(ps)): print("The probability of the flower to be {} is {:.2f} %.".format(labels[i], ps[i] * 100))
def merge_json(self): main_data = {} t = {'trip_analysis': load_json('trip_analysis.json')} a = {'area_analysis': load_json('area_analysis.json')} b = {'basic_info': load_json('basic_info.json')} m = {'monthly_consumption': load_json('monthly_consumption.json')} u = {'user_portrait': load_json('user_portrait.json')} c = {'call_log': load_json('call_log.json')} h = {'head_info': load_json('head_info.json')} main_data.update(t) main_data.update(a) main_data.update(b) main_data.update(m) main_data.update(u) main_data.update(c) main_data.update(h) for name in main_data: os.remove('json/' + name + '.json') dump_json('main.json', main_data) print('数据分析完成')
def __init__(self, database=None): self.view = View() self.state = 0 # 0: Radio off, 1: Radio on # Set defaults self.settings = { 'urls': [ 'http://rhh.streamabc.net/rhh-rhhlivestream-mp3-192-5434905', 'https://stream.antenne1.de/a1stg/livestream2.mp3' ], 'stations': ['Radio Hamburg', 'Antenne 1'], 'vol': 20, 'current_station_id': 0, } # Define VLC instance self.instance = vlc.Instance( '--quiet') #'--input-repeat=-1', '--fullscreen' # Define VLC player self.player = self.instance.media_player_new() self.VOL_MIN = 0 self.VOL_MAX = 100 self.VOL_CHANGE = 5 if database is None: self.file_location = os.path.dirname(os.path.realpath(__file__)) self.database_loc = self.file_location + '/' + os.path.sep.join( ['.', 'data', 'database.json']) else: self.database_loc = database self.settings = load_json(self.database_loc, self) self._set_station( self.settings['urls'][self.settings['current_station_id']]) self.player.audio_set_volume(self.settings['vol']) # Define possible action commands (how to use stream) self.commands = { 'start': self.start, 'stop': self.stop, 'add_station': self.add_station, 'remove_station': self.remove_station, 'station_up': self.station_up, 'station_down': self.station_down, 'vol_up': self.vol_up, 'vol_down': self.vol_down, 'close': self.close, } self.show('Player instentiated.')
def upload_cars(self, filename): """ Upload cars from json to the game :param filename: json filename :return: nothing """ car_configs = helper.load_json(filename) for item in car_configs.keys(): new_car = car.Car(item, car_configs[item][0], car_configs[item][1], car_configs[item][2]) self.__cars.append(new_car) self.__board.add_car(new_car)
def add_cars_to_board(board, filename): """ Create the beginning board of the game. :param filename: the json filename with the cars arrange for the game. :return: None """ all_cars = helper.load_json(filename) for car, lst in all_cars.items(): if car in NAMES and lst[CAR_LENGTH] in LENGTH \ and lst[CAR_OR] in ORIENTATIONS: car_to_add = Car(car, lst[CAR_LENGTH], lst[CAR_LOC], lst[CAR_OR]) board.add_car(car_to_add)
def compute_primitive_score(email, use_psync): sent_stat = helper.load_json('sent_last.json') receive_stat = helper.load_json('receive_last.json') unread_stat = helper.load_json('unread_last.json') # unseen_stat = helper.load_json('unseen_last.json') # recent_contact = get_recent_contact([sent_stat,receive_stat,unread_stat,unseen_stat]) recent_contact = get_recent_contact([sent_stat, receive_stat, unread_stat]) # pprint.pprint(recent_contact) score = {} for contact in recent_contact: sent_score = 4 * get_count_if_exist_else_zero(sent_stat[0],contact) +\ 3 * get_count_if_exist_else_zero(sent_stat[1],contact) +\ 2 * get_count_if_exist_else_zero(sent_stat[2],contact) +\ 1 * get_count_if_exist_else_zero(sent_stat[3],contact) receive_score = 4 * get_count_if_exist_else_zero(receive_stat[0],contact)+\ 3 * get_count_if_exist_else_zero(receive_stat[1],contact) +\ 2 * get_count_if_exist_else_zero(receive_stat[2],contact) +\ 1 * get_count_if_exist_else_zero(receive_stat[3],contact) unread_score = 0 * get_count_if_exist_else_zero(unread_stat[0],contact)+\ 3 * get_count_if_exist_else_zero(unread_stat[1],contact) +\ 2 * get_count_if_exist_else_zero(unread_stat[2],contact) +\ 1 * get_count_if_exist_else_zero(unread_stat[3],contact) # unseen_score = 0 * get_count_if_exist_else_zero(unseen_stat[0],contact)+\ # 3 * get_count_if_exist_else_zero(unseen_stat[1],contact) +\ # 2 * get_count_if_exist_else_zero(unseen_stat[2],contact) +\ # 1 * get_count_if_exist_else_zero(unseen_stat[3],contact) # score[contact] = sent_score + receive_score - unread_score - unseen_score score[contact] = sent_score + receive_score - unread_score helper.save_json('score.json', score) return score
def compute_primitive_score(email, use_psync): sent_stat = helper.load_json('sent_last.json') receive_stat = helper.load_json('receive_last.json') unread_stat = helper.load_json('unread_last.json') # unseen_stat = helper.load_json('unseen_last.json') # recent_contact = get_recent_contact([sent_stat,receive_stat,unread_stat,unseen_stat]) recent_contact = get_recent_contact([sent_stat,receive_stat,unread_stat]) # pprint.pprint(recent_contact) score = {} for contact in recent_contact: sent_score = 4 * get_count_if_exist_else_zero(sent_stat[0],contact) +\ 3 * get_count_if_exist_else_zero(sent_stat[1],contact) +\ 2 * get_count_if_exist_else_zero(sent_stat[2],contact) +\ 1 * get_count_if_exist_else_zero(sent_stat[3],contact) receive_score = 4 * get_count_if_exist_else_zero(receive_stat[0],contact)+\ 3 * get_count_if_exist_else_zero(receive_stat[1],contact) +\ 2 * get_count_if_exist_else_zero(receive_stat[2],contact) +\ 1 * get_count_if_exist_else_zero(receive_stat[3],contact) unread_score = 0 * get_count_if_exist_else_zero(unread_stat[0],contact)+\ 3 * get_count_if_exist_else_zero(unread_stat[1],contact) +\ 2 * get_count_if_exist_else_zero(unread_stat[2],contact) +\ 1 * get_count_if_exist_else_zero(unread_stat[3],contact) # unseen_score = 0 * get_count_if_exist_else_zero(unseen_stat[0],contact)+\ # 3 * get_count_if_exist_else_zero(unseen_stat[1],contact) +\ # 2 * get_count_if_exist_else_zero(unseen_stat[2],contact) +\ # 1 * get_count_if_exist_else_zero(unseen_stat[3],contact) # score[contact] = sent_score + receive_score - unread_score - unseen_score score[contact] = sent_score + receive_score - unread_score helper.save_json('score.json',score) return score
def load_cars(filename, board): """This function loads cars configuration from an external file to an empty board""" CONFI = helper.load_json(filename) game_cars = [] for i in range(len(CONFI)): game_cars.append((list(CONFI.keys())[i], list(CONFI.values())[i])) for car in game_cars: if car[0] in Game.POSSIBLE_NAMES and 2 <= car[1][0] <= 4 and car[1][2] in Game.POSSIBLE_ORIENTATION: for j in range(3): fit_car = Car(car[0], car[1][0], tuple(car[1][1]), car[1][2]) if Game.DEST in fit_car.car_coordinates(): continue board.add_car(fit_car) return board
def test_get_info(): url = "{host}/Q15796/info".format(host=host) result = requests.get(url) # assert that the request succeeded assert result.status_code == 200, result.content # parse the returned response returned_info = json.loads(result.text) # load the expected response expected_info = helper.load_json("info.json") # assert if proper response is returned assert expected_info == returned_info
def monthly_consumption(self): monthly_consumption = [] data = load_json('basic_info.json') money_spent = data['monthly_consumption'] money_spent.insert(0, -1) for month in self.month_list: monthly_consumption_dict = { "call_cnt": 0, 'call_consumption': 0.00, "call_seconds": 0, "called_cnt": 0, "called_seconds": 0, "month": month.strftime('%Y-%m'), "msg_cnt": 0, "receive_cnt": 0, "send_cnt": 0, "talk_cnt": 0, "talk_seconds": 0, "unknown_cnt": 0 } monthly_consumption.append(monthly_consumption_dict) i = 0 for dict_ in monthly_consumption: dict_['call_consumption'] = money_spent[i] i += 1 for call in self.call_list: if call[2][:7] == dict_['month']: add_call_detail(dict_, call) for msg in self.msg_list: if msg[0][:7] == dict_['month']: add_msg_detail(dict_, msg) data.pop('monthly_consumption') dump_json('basic_info.json', data) dump_json('monthly_consumption.json', monthly_consumption)
length = data[name][0] pos = data[name][1] orientation = data[name][2] return Car(name, length, pos, orientation) if __name__ == "__main__": # Get arguments from cmd args = sys.argv if len(args) != 2: print("Please add json file as a parameter.") exit(1) # Create board board = Board() # Load basic board information from file data = load_json(args[1]) # Load cars to board for name in data: car = get_car_from_json(data) board.add_car(car) # create game object and start game game = Game(board) game.play()
def onNotification(self, sender, method, data): self.__onNotificationCallback(method, helper.load_json(data))
def connect_network(ex_neuron, inh_neuron, cfg): """ Args: ex_neuron: list o excitatory neuron ids inh_neuron: list of inhibitory neuron ids cfg: config file that defines the network layout, neuron dynamics and plasticity algorithm connecitivtes can be: "reproduce" For comparison we wrote out the exact connections from the original code and use them here so we can compare the resulting dynamics this step is necessary since we can"t reproduce all aspects of the connectivity drawing algorithm in nest "generate" The NEST implementation is used, we further distinguish: "uniform-non-random" where we draw the arbitrary not really random way of the original code Here every neuron has M/D connections with delay d where D is max delay and M the amount of post synaptic targets Works only when M/D is integer "uniform random" draws the a uniform distribution between min delay and max delay This woeuld be closest to what is usually understood of random disribution of delays Returns: """ conf=cfg["network-params"]["connectivity"] if conf["type"] == "reproduce": ########################## # # For comparison we wrote out the exact connections from the original code # and use them here so we can compare the resulting dynamics # this step is necessary since we can"t reproduce all aspects of the connectivity drawing algorithm in nest # ########################## file = helper.load_json(conf["from-file"].format(rep=args.repetition)) #read out all connections with respective delays delay = np.array([float(i['delay']) for i in file]) pre = np.array([i['pre'] for i in file]) post = np.array([i['post'] for i in file]) #loop through all presynaptic neurons and connect them to their targets #first connect for pre_neuron in np.unique(pre): idxes = np.where(pre_neuron == pre)[0] if pre_neuron in ex_neuron: nest.Connect([pre_neuron], post[idxes].tolist(), conn_spec='all_to_all', syn_spec='EX') elif pre_neuron in inh_neuron: nest.Connect([pre_neuron], post[idxes].tolist(), conn_spec='all_to_all', syn_spec='II') #then set delay for pr, po, de in zip(pre, post, delay): conn = nest.GetConnections(source=[pr], target=[po]) nest.SetStatus(conn, 'delay', de) elif conf["type"] == "generate": ############################### # # Use the NEST implementation for drawing connections # ############################### conn_dict_inh = {'rule': 'fixed_outdegree', 'outdegree': N_syn, 'multapses': False, 'autapses': False} conn_dict_ex = {'rule': 'fixed_outdegree', 'outdegree': N_syn, 'multapses': False, 'autapses': False} nest.Connect(inh_neuron, ex_neuron, conn_spec=conn_dict_inh, syn_spec='II') nest.Connect(ex_neuron, neurons, conn_spec=conn_dict_ex, syn_spec='EX') if conf["delay-distribution"] == "uniform-non-random": delay_range = range(int(conf["delay-range"][0]), int(conf["delay-range"][1])) delay_list = [[j for i in range(int(100 / len(delay_range)))] for j in delay_range] delay_list = np.reshape( np.array(delay_list).astype(float), (1, 100))[0] for n in ex_neuron: conns = nest.GetConnections(source=[n], target=np.random.permutation(ex_neuron + inh_neuron).tolist()) nest.SetStatus(conns, 'delay', delay_list) elif conf["delay-distribution"] == "uniform-random": for n in ex_neuron: delay_list = np.random.uniform(int(conf["delay-range"][0]), int(conf["delay-range"][1]-1), 100).astype( float) delay_list=np.around(delay_list,decimals=int(-np.log10(cfg["simulation-params"]["resolution"]))) nest.SetStatus(nest.GetConnections( source=[n], target=ex_neuron + inh_neuron), 'delay', delay_list) print(delay_list,np.min(delay_list),np.max(delay_list)) else: pass
def main(opt): """ Tests SRVP. Parameters ---------- opt : DotDict Contains the testing configuration. """ ################################################################################################################## # Setup ################################################################################################################## # -- Device handling (CPU, GPU) opt.train = False if opt.device is None: device = torch.device('cpu') else: os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.device) device = torch.device('cuda:0') torch.cuda.set_device(0) # Seed random.seed(opt.test_seed) np.random.seed(opt.test_seed) torch.manual_seed(opt.test_seed) # cuDNN assert torch.backends.cudnn.enabled # Load LPIPS model global lpips_model lpips_model = PerceptualLoss(opt.lpips_dir) ################################################################################################################## # Load XP config ################################################################################################################## xp_config = helper.load_json(os.path.join(opt.xp_dir, 'config.json')) nt_cond = opt.nt_cond if opt.nt_cond is not None else xp_config.nt_cond nt_test = opt.nt_gen if opt.nt_gen is not None else xp_config.seq_len_test ################################################################################################################## # Load test data ################################################################################################################## print('Loading data...') xp_config.data_dir = opt.data_dir xp_config.seq_len = nt_test dataset = data.load_dataset(xp_config, train=False) testset = dataset.get_fold('test') test_loader = DataLoader(testset, batch_size=opt.batch_size, collate_fn=data.collate_fn, pin_memory=True) ################################################################################################################## # Load model ################################################################################################################## print('Loading model...') model = srvp.StochasticLatentResidualVideoPredictor( xp_config.nx, xp_config.nc, xp_config.nf, xp_config.nhx, xp_config.ny, xp_config.nz, xp_config.skipco, xp_config.nt_inf, xp_config.nh_inf, xp_config.nlayers_inf, xp_config.nh_res, xp_config.nlayers_res, xp_config.archi) state_dict = torch.load(os.path.join(opt.xp_dir, 'model.pt'), map_location='cpu') model.load_state_dict(state_dict) model.to(device) model.eval() ################################################################################################################## # Eval ################################################################################################################## print('Generating samples...') torch.set_grad_enabled(False) best_samples = defaultdict(list) worst_samples = defaultdict(list) results = defaultdict(list) cond = [] cond_rec = [] gt = [] random_samples = [[] for _ in range(5)] # Evaluation is done by batch for batch in tqdm(test_loader, ncols=80, desc='evaluation'): # Data x = batch.to(device) assert nt_test <= len(x) x = x[:nt_test] x_cond = x[:nt_cond] x_target = x[nt_cond:] cond.append(x_cond.cpu().mul(255).byte().permute(1, 0, 3, 4, 2)) gt.append(x_target.cpu().mul(255).byte().permute(1, 0, 3, 4, 2)) # Predictions metric_best = {} sample_best = {} metric_worst = {} sample_worst = {} # Encode conditional frames and extracts skip connections skip = model.encode(x_cond)[1] if model.skipco != 'none' else None # Generate opt.n_samples predictions for i in range(opt.n_samples): # Infer latent variables x_rec, y, _, w, _, _, _, _ = model(x_cond, nt_cond, dt=1 / xp_config.n_euler_steps) y_0 = y[-1] if i == 0: x_rec = x_rec[::xp_config.n_euler_steps] cond_rec.append(x_rec.cpu().mul(255).byte().permute( 1, 0, 3, 4, 2)) # Use the model in prediction mode starting from the last inferred state y_os = model.generate(y_0, [], nt_test - nt_cond + 1, dt=1 / xp_config.n_euler_steps)[0] y = y_os[xp_config.n_euler_steps::xp_config. n_euler_steps].contiguous() x_pred = model.decode(w, y, skip).clamp(0, 1) # Pixelwise quantitative eval mse = torch.mean(F.mse_loss(x_pred, x_target, reduction='none'), dim=[3, 4]) metrics_batch = { 'psnr': 10 * torch.log10(1 / mse).mean(2).mean(0).cpu(), 'ssim': _ssim_wrapper(x_pred, x_target).mean(2).mean(0).cpu(), 'lpips': _lpips_wrapper(x_pred, x_target).mean(0).cpu() } x_pred_byte = x_pred.cpu().mul(255).byte().permute(1, 0, 3, 4, 2) if i < 5: random_samples[i].append(x_pred_byte) for name, values in metrics_batch.items(): if i == 0: metric_best[name] = values.clone() sample_best[name] = x_pred_byte.clone() metric_worst[name] = values.clone() sample_worst[name] = x_pred_byte.clone() continue # Best samples idx_better = _get_idx_better(name, metric_best[name], values) metric_best[name][idx_better] = values[idx_better] sample_best[name][idx_better] = x_pred_byte[idx_better] # Worst samples idx_worst = _get_idx_worst(name, metric_worst[name], values) metric_worst[name][idx_worst] = values[idx_worst] sample_worst[name][idx_worst] = x_pred_byte[idx_worst] # Compute metrics for best samples and register for name in sample_best.keys(): best_samples[name].append(sample_best[name]) worst_samples[name].append(sample_worst[name]) results[name].append(metric_best[name]) # Store best, worst and random samples samples = { f'random_{i + 1}': torch.cat(random_sample).numpy() for i, random_sample in enumerate(random_samples) } samples['cond_rec'] = torch.cat(cond_rec) for name in best_samples.keys(): samples[f'{name}_best'] = torch.cat(best_samples[name]).numpy() samples[f'{name}_worst'] = torch.cat(worst_samples[name]).numpy() results[name] = torch.cat(results[name]).numpy() ################################################################################################################## # Compute FVD ################################################################################################################## print('Computing FVD...') cond = torch.cat(cond, 0).permute(1, 0, 4, 2, 3).float().div(255) gt = torch.cat(gt, 0).permute(1, 0, 4, 2, 3).float().div(255) ref = torch.cat([cond, gt], 0) hyp = torch.from_numpy(samples['random_1']).clone().permute( 1, 0, 4, 2, 3).float().div(255) hyp = torch.cat([cond, hyp], 0) fvd = fvd_score(ref, hyp) ################################################################################################################## # Print results ################################################################################################################## print('\n') print('Results:') for name, res in results.items(): print(name, res.mean(), '+/-', 1.960 * res.std() / np.sqrt(len(res))) print(f'FVD', fvd) ################################################################################################################## # Save samples ################################################################################################################## np.savez_compressed(os.path.join(opt.xp_dir, 'results.npz'), **results) for name, res in samples.items(): np.savez_compressed(os.path.join(opt.xp_dir, f'{name}.npz'), samples=res)
if len(choose) != 1 and len(choose) != 3: print("wrong input") if choose[0] == '!': break if choose[0] not in self.board.car_names: print("Invalid color. Try again") continue if self.single_chage(choose): print("the change have been done") else: print("there is something wrong with the given\n" "direction, try again") if self.board.check_victory(): print("Congrats!! You made it") else: print("thanks for trying") if __name__ == "__main__": # Your code here # All access to files, non API constructors, and such must be in this # section, or in functions called from this section if len(sys.argv) == 2: b = Board() g = Game(b) car_dict = helper.load_json(sys.argv[1]) g.create_cars(car_dict) g.play() else: print("please enter a valid path for json File")
def fetch(parties, channels_resource, videos_per_channel, key, from_date, until_date, database): # Get specified parties party_channels = load_json(channels_resource) available_parties = list(party_channels.keys()) # Check input if len(parties) == 1 and parties[0] == "all": parties = available_parties elif not parties or not all(party in available_parties for party in parties): print(usage, "connect.py fetch [OPTIONS] PARTIES...") print( bold_blue("[Example] ") + "connect.py fetch afd union --from-date '01.01.2019' --videos-per-channel -1" ) print( bold_blue("Available parties: ") + ", ".join(["all"] + available_parties)) return try: # Format date from_date = datetime.strptime(from_date, '%d.%m.%Y') until_date = datetime.strptime(until_date, '%d.%m.%Y') except ValueError: print(usage, "connect.py fetch [OPTIONS] PARTIES...") print(bold_blue("Datetime: ") + "{DAY}.{MONTH}.{YEAR}") return # Print script information print_information(parties, videos_per_channel, from_date, until_date) # Get Google Dev handle handle = get_handle(key) # Download subtitles and data rows = [] for party in parties: print(status + "Party: ", bold_purple(party.upper())) for channel in party_channels[party]: print(status + "Channel: ", bold_blue(channel['name'])) # Get uploads playlist_id = get_channel_uploads_id(handle, channel['id']) playlist_items = get_playlist_items(handle, playlist_id, from_date, until_date, videos_per_channel) for video_id in playlist_items: print(status + "Scraping and processing video:", video_id) # Get subtitle subtitle_path = os.path.join("subtitles", party, video_id + ".txt") download_sub(party, video_id, subtitle_path) try: subtitle = filter_subtitles(subtitle_path) except FileNotFoundError: print(warning + "Subtitles couldn't be downloaded.") subtitle = None # Get video data raw_video_json = get_raw_video_json(handle, video_id) # Merge to data row data_row = create_datarow(video_id, party, channel['state'], channel['faction'], raw_video_json, subtitle) rows.append(data_row) updating_and_saving(rows, database)
import helper import argparse parser = argparse.ArgumentParser() parser.add_argument('--checkpoint_path', dest='checkpoint_path', action='store', default='checkpoint.pth') parser.add_argument('--image_path', dest='image_path', action='store', default='./flowers/test/1/image_06743.jpg') parser.add_argument('--topk', dest='topk', action='store', default=5, type=int) parser.add_argument('--json_file', dest='json_file', action='store', default='cat_to_name.json') args = parser.parse_args() cat_to_name = helper.load_json(args.json_file) checkpoint_model = helper.load_checkpoint(args.checkpoint_path) top_p, labels = helper.predict(args.image_path, checkpoint_model, cat_to_name, args.topk) print(top_p, labels)
if __name__ == "__main__": # Your code here # All access to files, non API constructors, and such must be in this # section, or in functions called from this section. from helper import load_json import sys from car import Car from board import Board VALID_CAR_NAME = ['Y', 'B', 'O', 'W', 'G', 'R'] MIN_LENGTH = 2 MAX_LENGTH = 4 VALID_ORIENTATION = [0, 1] filename = sys.argv[1] car_dict = load_json(filename) board_game = Board() for key in car_dict: if len(car_dict[key]) != 3: continue length = car_dict[key][0] location = car_dict[key][1] orientation = car_dict[key][2] if key not in VALID_CAR_NAME: continue if type(length) != int: continue if len(location) != 2 or type(location) != list: continue if length < MIN_LENGTH or length > MAX_LENGTH: continue
def get_fight_odds(): fighter_odds = h.load_json(self.fighter_lst_address) # with open(self.fighter_lst_addres,'r') as fp: # fighter_odds = json.load(fp) #get only the difference between the latest fighter odd IDs and what's in the database #send that through for data scraping and creating the dfs # fighter_odds = list(fighter_odds.keys())[100:] # fighter_odds_test=[] # fighter_odds_test.append('https://www.bestfightodds.com/fighters/Tony-Ferguson-2568') # fighter_odds_test.append("https://www.bestfightodds.com/fighters/Israel-Adesanya-7845") # fighter_odds_test.append("https://www.bestfightodds.com/fighters/Jan-Blachowicz-2371") finaldf = pd.DataFrame(columns=['Fighter_Name','Open','Close_range_Lower','Close_range_Upper','Event','Fighter_ID']) start = t.time() for fighter_url in fighter_odds: dfs= pd.read_html(self.bestfightOddsUrl+fighter_url) df=dfs[0] df.columns =['Fighter_Name','Open','Close_range_Lower','Remove','Close_range_Upper','Remove1','Remove2','Event'] df = df[df.index %3 !=0] df = df.drop(columns=["Remove","Remove1","Remove2"]) df['Fighter_ID']= fighter_url[39:] finaldf= finaldf.append(df) finaldf=finaldf.reset_index(drop=True) finaldf1 = finaldf[finaldf.index%2==0] finaldf2 = finaldf[finaldf.index%2!=0] finaldf1=finaldf1.reset_index(drop=True) finaldf2=finaldf2.reset_index(drop=True) finaldf1.columns=['Fighter_Name0','Open0','Close_range_Lower0','Close_range_Upper0','Event_Name','Fighter_Id_0'] finaldf2.columns=['Fighter_Name1','Open1','Close_range_Lower1','Close_range_Upper1','Event_Date','Fighter_Id_1'] finaldf3= finaldf1.join(finaldf2) stop = t.time() time= stop-start print('This took %s seconds' %time) h.load_to_db(finaldf3,'fight_odd_fact') # def get_all_bestfightOdds_IDs(opponent, cnt, fighter_lst): # for fighter_url in fighter_lst: # if fighter_url == opponent: # ##extract the web page for the fighter # r = requests.get("https://www.bestfightodds.com"+fighter_url) # soup = bs.BeautifulSoup(r.content,'lxml') # #for each fighter, go through their opponents # #each link to their opopnent contains the string "fighters" within it # for a in soup.find_all('a', href=re.compile('fighters')): # #filter out the fighter urls that are the fighter themselves # if fighter_url not in a['href']: # #if the fighter's opponent is not already in the dictoinary # if a['href'] not in fighter_lst: # #add the ID to the new dictionary # # fighter_lst[a['href']] = a.text # fighter_lst.append(a['href']) # #some checking # cnt+=1 # print(a.text,' added #',cnt) # #send the opponent to scrape all the IDs of their opponents # opponent= a['href'] # #then recursively get the IDs of their opponents # get_all_bestfightOdds_IDs(opponent, cnt, fighter_lst) # return fighter_lst # fighter_list= ['/fighters/Israel-Adesanya-7845'] # fighter_lst.append('/fighters/Israel-Adesanya-7845') # cnt=0 # opponent = '/fighters/Israel-Adesanya-7845' # get_all_bestfightOdds_IDs(opponent, cnt, fighter_lst) # #how to terminate? count was going down for some reason # ##save output # c = str(datetime.date.today()) # with open(r'data\fighter_odds_%s.json' %c,'w') as fp: # json.dump(fighter_lst,fp) # fidf = pd.DataFrame(fighter_lst, columns=['Fighter_ID']) # h.load_to_db(fidf,'fighter_id_recursive')
# start with a rectangular Figure fig = plt.figure(figsize=(8, 8)) N = 5 gs0 = gridspec.GridSpec(N, N) gs0.update(left=0.1, right=0.97, top=0.97, bottom=0.06, hspace=0.15) ax2Dhist = plt.subplot(gs0[1:, :N - 1]) axHistx = plt.subplot(gs0[0, :N - 1]) axHisty = plt.subplot(gs0[1:, N - 1]) connectivity = hf.load_json(args.c) all_w, ex_ex_w, ex_in_w = hf.conn_dist(connectivity, 'weight') all_d, ex_ex_d, ex_in_d = hf.conn_dist(connectivity, 'delay') weights_ex_in, delays_ex_in, counts_ex_in = hf.weight_delay_histograms(ex_in_w, ex_in_d) weights_ex_ex, delays_ex_ex, counts_ex_ex = hf.weight_delay_histograms(ex_ex_w, ex_ex_d) weights, delays, counts = hf.weight_delay_histograms(ex_ex_w + ex_in_w, ex_ex_d + ex_in_d) phf.plot_2D_weights(weights, delays, counts, ax2Dhist) xhist = np.mean(counts, axis=0) yhist = np.mean(counts, axis=1) xhist_ex_in = np.mean(counts_ex_in, axis=0) yhist_ex_in = np.mean(counts_ex_in, axis=1)
def trip_analysis(self): gsd = load_json('basic_info.json')['phone_location'] trip_analysis = [] trip_location = set() calls_by_trip = {} for call in self.call_list: if gsd not in call[6]: trip_location.add(call[6]) if call[6] not in calls_by_trip: calls_by_trip[call[6]] = [] else: calls_by_trip[call[6]].append(call) msg_by_trip = {} for msg in self.msg_list: if gsd not in msg[-1] and msg[-1] != '': if msg[-1] not in msg_by_trip: msg_by_trip[msg[-1]] = [] else: msg_by_trip[msg[-1]].append(msg) for location in trip_location: trip_analysis_dict = { "call_cnt": 0, "call_seconds": 0, "called_cnt": 0, "called_seconds": 0, "detail": [], "msg_cnt": 0, "receive_cnt": 0, "send_cnt": 0, "talk_cnt": 0, "talk_seconds": 0, "unknown_cnt": 0, 'date_distribution': [], 'location': location } trip_analysis.append(trip_analysis_dict) for trip in trip_analysis: for location in calls_by_trip: if trip['location'] == location: date_distribution = set() for call in calls_by_trip[location]: date_distribution.add(call[2][:7]) add_call_detail(trip, call) trip['date_distribution'] = list(date_distribution) trip['date_distribution'].sort(reverse=True) try: for msg in msg_by_trip[location]: add_msg_detail(trip, msg) except KeyError: pass for month in trip['date_distribution']: detail = { "call_cnt": 0, "call_seconds": 0, "called_cnt": 0, "called_seconds": 0, "month": month, "msg_cnt": 0, "receive_cnt": 0, "send_cnt": 0, "talk_cnt": 0, "talk_seconds": 0, "unknown_cnt": 0 } trip['detail'].append(detail) for detail in trip['detail']: for call in calls_by_trip[location]: if call[2][:7] == month: add_call_detail(detail, call) try: for msg in msg_by_trip[location]: add_msg_detail(detail, msg) except KeyError: pass dump_json('trip_analysis.json', trip_analysis)
if MIN_CAR_LEN <= length <= MAX_CAR_LEN: # legal length: 2-4 if orientation == VERTICAL or orientation == HORIZONTAL: # legal orientation # all car params are met, car is valid return True # car is invalid return False if __name__ == "__main__": # initializing board game board = Board() car_config = helper.load_json(sys.argv[1]) car_dict = {} # adding cars to game from json file, if valid for car_name, car_values in car_config.items(): length = car_values[LENGTH_IDX] location = tuple(car_values[LOCATION_IDX]) orientation = car_values[ORIENTATION_IDX] # check that params are valid, creates car and add to board if is_car_valid(car_name, length, orientation): car = Car(car_name, length, location, orientation) board.add_car(car) car_dict[car_name] = car game = Game(board)
} response = localize_as(form_data) # Path logic (Creating timestamp directory with reconstructions ids directories with jsons and plys) if response.json()['status']['message'] != 'Cannot localize image': rec_id = response.json()['reconstruction_id'] rec_dir = f"{timestamp_dir}/{rec_id}" if not path.exists(rec_dir): os.mkdir(rec_dir) # Downloading ply response_with_ply = get_ply(rec_id) with open(f"{rec_dir}/{rec_id}.ply", 'wb') as f1: f1.write(response_with_ply.content) with open(f"{rec_dir}/{pathlib.Path(image_file.name).stem}.json", 'w') as f2: f2.write(load_json(response)) # if reference directory presented in commandline arguments if ref_directory is not None: print(''.center(80, '=')) print('Reference option was set') print(''.center(80, '=')) ref_dir = f'{timestamp_dir}/references' if not path.exists(ref_dir): os.mkdir(ref_dir) ref_dataset = read_dataset_simple(ref_directory) for file_ref in ref_dataset: image_file = open(file_ref, 'rb') description = get_exif(image_file.name) hint = get_hint_from_cmd(args.hint)
continue # if the length is bigger than the board size, if wont fit the board. if length > board_size: continue if location[0] > board_size or location[0] < 0 or\ location[1] > board_size or location[1] < 0: continue # if the car size with its location will be out of the board. if orientation == 1 and (((length + location[1]-1) >= board_size) and location[0] != target_location[0]): continue if orientation == 0 and ((length + location[0]-1) >= board_size): continue dict_of_valid_cars[key] = (length, location, orientation) return dict_of_valid_cars if __name__ == "__main__": # creates a board for the game game_board = Board() # Load json file json_file_output = helper.load_json(sys.argv[1]) # check for the valid cars in json file. valid_json_file_cars = check_json_file(json_file_output, game_board) game_board = add_cars_to_board(valid_json_file_cars, game_board) # starts the game print(game_board) run_game = Game(game_board) run_game.play()
dest = 'hidden_units', type = int, default = 512, help = 'Number of hidden units') parser.add_argument('--arch', action = 'store', dest = 'arch', type = str, default = 'densenet', help = 'PreTrained Model Architecture, densenet or vgg') results = parser.parse_args() print('---------Parameters----------') print('gpu = {!r}'.format(results.gpu)) print('img = {!r}'.format(results.img)) print('top_k = {!r}'.format(results.top_k)) print('cat = {!r}'.format(results.cat)) print('start = {!r}'.format(results.start)) print('-----------------------------') if results.start == True: model, class_labels = helper.load_saved_model() cat_to_name, label_order = helper.load_json(results.cat) ps, labels, index = helper.predict(results.img, model, results.top_k, cat_to_name, class_labels, results.gpu) print("------------------Prediction------------------") for i in range(len(ps)): print("The probability of the flower to be {} is {:.2f} %.".format(labels[i], ps[i] * 100))
if self.board.cell_content(target) is not None: print(self.board) print(self.WIN_MESSAGE) if __name__ == "__main__": # Get json file from sys argv json_file = sys.argv[1] # Create a board b = Board() # Get legal locations on board board_locations = b.cell_list() # Add cars from json file to board car_dict = helper.load_json(json_file) # For each car in the json file for car_name in car_dict.keys(): # Get values of the car val = car_dict[car_name] # Check if car values are valid if val[0] > 0: location = tuple(val[1]) if location in board_locations: # Create car and add to board car = Car(car_name, val[0], location, val[2]) b.add_car(car) # Create game game = Game(b)