def main(): parser = argparse.ArgumentParser() parser.add_argument('--total_assets_path', required=True) parser.add_argument('--intangible_assets_path', required=True) parser.add_argument('--total_liabilities_path', required=True) parser.add_argument('--prices_path', required=True) parser.add_argument('--outstanding_shares_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) ta_map = utils.read_map(args.total_assets_path) tl_map = utils.read_map(args.total_liabilities_path) p_map = utils.read_map(args.prices_path) s_map = utils.read_map(args.outstanding_shares_path) tickers = ta_map.keys() & tl_map.keys() & p_map.keys() & s_map.keys() # intangible assets are 0 by default ia_map = dict() for t in tickers: ia_map[t] = 0.0 ia_part = utils.read_map(args.intangible_assets_path) for k, v in ia_part.items(): ia_map[k] = v with open(args.output_path, 'w') as fp: for ticker in sorted(tickers): output = ((ta_map[ticker] - ia_map[ticker] - tl_map[ticker]) / s_map[ticker] / p_map[ticker]) print('%s %f' % (ticker, output), file=fp)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--operating_cashflow_path', required=True) parser.add_argument('--preferred_dividend_path', required=True) parser.add_argument('--prices_path', required=True) parser.add_argument('--outstanding_shares_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) cf_map = utils.read_map(args.operating_cashflow_path) p_map = utils.read_map(args.prices_path) s_map = utils.read_map(args.outstanding_shares_path) tickers = cf_map.keys() & p_map.keys() & s_map.keys() # preferred dividend is default to 0 pd_map = dict() for t in tickers: pd_map[t] = 0.0 pd_part = utils.read_map(args.preferred_dividend_path) for k, v in pd_part.items(): pd_map[k] = v with open(args.output_path, 'w') as fp: for ticker in sorted(tickers): output = ((cf_map[ticker] - pd_map[ticker]) / s_map[ticker] / p_map[ticker]) print('%s %f' % (ticker, output), file=fp)
def test4(self): ''' ####### ####### #.E...# #.....# #.#..G# #.#G..# G(200) #.###.# --> #.###.# #E#G#G# #.#.#.# #...#G# #G.G#G# G(98), G(38), G(200) ####### ####### Combat ends after 54 full rounds Goblins win with 536 total hit points left Outcome: 54 * 536 = 28944 ''' initial_data = ''' ####### #.E...# #.#..G# #.###.# #E#G#G# #...#G# ####### ''' m, p, w = read_map(initial_data.split('\n')[1:-1]) game = Game(m, w, p) step, score = game.combat() print(step, game) game.display() self.assertEqual(step, 54) self.assertEqual(score, 28944)
def test3(self): ''' ####### ####### #E.G#.# #G.G#.# G(200), G(98) #.#G..# #.#G..# G(200) #G.#.G# --> #..#..# #G..#.# #...#G# G(95) #...E.# #...G.# G(200) ####### ####### Combat ends after 35 full rounds Goblins win with 793 total hit points left Outcome: 35 * 793 = 27755 ''' initial_data = ''' ####### #E.G#.# #.#G..# #G.#.G# #G..#.# #...E.# ####### ''' m, p, w = read_map(initial_data.split('\n')[1:-1]) game = Game(m, w, p) step, score = game.combat() print(step, game) game.display() self.assertEqual(step, 35) self.assertEqual(score, 27755)
def test2(self): ''' ####### ####### #E..EG# #.E.E.# E(164), E(197) #.#G.E# #.#E..# E(200) #E.##E# --> #E.##.# E(98) #G..#.# #.E.#.# E(200) #..E#.# #...#.# ####### ####### Combat ends after 46 full rounds Elves win with 859 total hit points left Outcome: 46 * 859 = 39514 ''' initial_data = ''' ####### #E..EG# #.#G.E# #E.##E# #G..#.# #..E#.# ####### ''' m, p, w = read_map(initial_data.split('\n')[1:-1]) game = Game(m, w, p) step, score = game.combat() print(step, game) game.display() self.assertEqual(step, 46) self.assertEqual(score, 39514)
def test1(self): ''' ####### ####### #G..#E# #...#E# E(200) #E#E.E# #E#...# E(197) #G.##.# --> #.E##.# E(185) #...#E# #E..#E# E(200), E(200) #...E.# #.....# ####### ####### Combat ends after 37 full rounds Elves win with 982 total hit points left Outcome: 37 * 982 = 36334 ''' initial_data = ''' ####### #G..#E# #E#E.E# #G.##.# #...#E# #...E.# ####### ''' m, p, w = read_map(initial_data.split('\n')[1:-1]) game = Game(m, w, p) step, score = game.combat() print(step, game) game.display() self.assertEqual(step, 37) self.assertEqual(score, 36334)
def test27(self): ''' ####### #G....# G(200) #.G...# G(131) #.#.#G# G(119) #...#E# E(119) #...G.# G(200) ####### ''' m, p, w = read_map(test_data_battle) game = Game(m, w, p) for step in range(27): print(step) game.step() game.display() print(game) self.assertEqual(len(game.players), 5) self.assertEqual(game.players[0].hit_point, 200) self.assertEqual(game.players[1].hit_point, 131) self.assertEqual(game.players[2].hit_point, 119) self.assertEqual(game.players[3].hit_point, 119) self.assertEqual(game.players[4].hit_point, 200) self.assertFalse(game.done())
def test_find_in_range2(self): """ Targets: In range: Reachable: Nearest: Chosen: ####### ####### ####### ####### ####### #E..G.# #E.?G?# #E.@G.# #E.!G.# #E.+G.# #...#.# --> #.?.#?# --> #.@.#.# --> #.!.#.# --> #...#.# #.G.#G# #?G?#G# #@G@#G# #!G.#G# #.G.#G# ####### ####### ####### ####### ####### """ map_, players, walls = read_map(test_data_chosen) player = players[1] player.find_in_range(walls, players) self.assertEqual(len(player.in_range), 2) self.assertListEqual(player.in_range, [(1, 3), (1, 5)]) player = players[2] player.find_in_range(walls, players) self.assertEqual(len(player.in_range), 3) self.assertListEqual(player.in_range, [(2, 2), (3, 1), (3, 3)]) player = players[3] player.find_in_range(walls, players) self.assertEqual(len(player.in_range), 1) self.assertListEqual(player.in_range, [(2, 5)])
def _parse_all_corpus(corpus_path: str, wn2bn: Dict[str, str]) -> None: """ A method to parse all available corpus :param corpus_path: corpus folder path :param wn2bn: map Wordnet to Babelnet :return: None """ for subdir, dirs, files in os.walk(str(corpus_path)): data_path, gold_path, parsed_path = "", "", "" for file in files: if file.endswith("data.xml"): data_path = os.path.join(subdir, file) elif file.endswith("gold.key.txt"): gold_path = os.path.join(subdir, file) # if the corpus is not parsed yet parsed_path = os.path.join( config.SENTENCES, file.split(".")[0] + "_sentences.txt" ) if not os.path.isfile(parsed_path) and all( (path != "") for path in [data_path, gold_path] ): key_map = utils.read_map(gold_path, delimiter=" ") utils.write_sentences_and_labels( parsed_path, parser_raganato_format(data_path, key_map, wn2bn) )
def test5(self): ''' ######### ######### #G......# #.G.....# G(137) #.E.#...# #G.G#...# G(200), G(200) #..##..G# #.G##...# G(200) #...##..# --> #...##..# #...#...# #.G.#...# G(200) #.G...G.# #.......# #.....G.# #.......# ######### ######### Combat ends after 20 full rounds Goblins win with 937 total hit points left Outcome: 20 * 937 = 18740 ''' initial_data = ''' ######### #G......# #.E.#...# #..##..G# #...##..# #...#...# #.G...G.# #.....G.# ######### ''' m, p, w = read_map(initial_data.split('\n')[1:-1]) game = Game(m, w, p) step, score = game.combat() print(step, game) game.display() self.assertEqual(step, 20) self.assertEqual(score, 18740)
def preprocessing_domain( train_y: List[str], dev_y: List[str], dom_path: str, to_write: str, vocab: Dict[str, int], num_vocab: int, min_count: int, reverse: bool = False, bn: bool = True, ) -> Tuple[List[str], List[str], Dict[str, int], FreqDist]: """ This method is used to preprocess the labels. :param train_y: the labels to preprocess :param dev_y: the dev labels to preprocess :param dom_path: the path which from read the conversion map :param to_write: the path to write the vocabulary :param vocab: training vocabulary :param num_vocab: maximum number of vocab :param min_count: min number of word occurrences :param reverse: if True, it reads domain2bn. False otherwise :param bn: if True is done the Babelnet preprocess. False coarse-grained :return: train_y = train_y preprocessed dev_y = dev_y preprocessed out_vocab = the sense-inventory frequency = word frequency """ bn2domain = utils.read_map(dom_path, reverse=False) if not bn: train_y, dev_y = utils.domains_converters([train_y, dev_y], bn2domain) bn2domain = utils.read_map(dom_path, reverse=reverse) senses = utils.retrieve_senses(train_y, bn2domain, is_bn=bn) out_vocab, frequency = preprocesser.compute_vocabulary(senses, min_count=min_count, num_vocab=num_vocab, vocab=vocab) train_y, dev_y = preprocesser.text2id_corpus(corpus=[train_y, dev_y], vocab=out_vocab) utils.write_vocabulary(to_write, out_vocab) return train_y, dev_y, out_vocab, frequency
def main(): parser = argparse.ArgumentParser() parser.add_argument('--net_income_path', required=True) parser.add_argument('--total_equity_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) ni_map = utils.read_map(args.net_income_path) e_map = utils.read_map(args.total_equity_path) tickers = ni_map.keys() & e_map.keys() with open(args.output_path, 'w') as fp: for ticker in sorted(tickers): output = ni_map[ticker] / e_map[ticker] print('%s %f' % (ticker, output), file=fp)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--prices_path', required=True) parser.add_argument('--outstanding_shares_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) p_map = utils.read_map(args.prices_path) s_map = utils.read_map(args.outstanding_shares_path) tickers = p_map.keys() & s_map.keys() with open(args.output_path, 'w') as fp: for ticker in sorted(tickers): output = p_map[ticker] * s_map[ticker] print('%s %f' % (ticker, output), file=fp)
def parse_all(): """ A simple method used to handle both the corpus and the test sets :return: None """ wn2bn = utils.read_map(config.BABELNET2WORDNET_TR, reverse=True) _parse_all_corpus(config.TEST_SETS, wn2bn) _parse_all_corpus(config.TRAINING_SETS, wn2bn)
def test_find_in_range(self): map_, players, walls = read_map(test_data) player = players[1] player.find_in_range(walls, players) self.assertListEqual(player.in_range, [(1, 3), (1, 5), (2, 4)]) player = players[3] player.find_in_range(walls, players) self.assertListEqual(player.in_range, [(1, 3), (2, 2), (2, 4), (3, 3)])
def main(): parser = argparse.ArgumentParser() parser.add_argument('--trading_volumes_path', required=True) parser.add_argument('--prices_path', required=True) parser.add_argument('--outstanding_shares_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) tv_map = utils.read_map(args.trading_volumes_path) p_map = utils.read_map(args.prices_path) s_map = utils.read_map(args.outstanding_shares_path) tickers = tv_map.keys() & p_map.keys() & s_map.keys() with open(args.output_path, 'w') as fp: for ticker in sorted(tickers): output = tv_map[ticker] / (p_map[ticker] * s_map[ticker]) print('%s %f' % (ticker, output), file=fp)
def test_adjacent(self): """ all players always have 4 adjacent cells which doesn't me they are reachable """ map_, players, walls = read_map(test_data) player = players[1] print(player) self.assertEqual(len(player.adjacent), 4) player = players[3] self.assertEqual(len(player.adjacent), 4)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--scores_path', required=True) parser.add_argument('--prices_path', required=True) parser.add_argument('--mc_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) s_map = utils.read_map(args.scores_path) p_map = utils.read_map(args.prices_path) mc_map = utils.read_map(args.mc_path) tickers = s_map.keys() & p_map.keys() & mc_map.keys() with open(args.output_path, 'w') as fp: for ticker in sorted(tickers): if p_map[ticker] < MIN_PRICE: continue if mc_map[ticker] < MIN_MC: continue print('%s %f' % (ticker, s_map[ticker]), file=fp)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--er1_path', required=True) parser.add_argument('--er12_path', required=True) parser.add_argument('--tv2mc_path', required=True) parser.add_argument('--er2_path', required=True) parser.add_argument('--e2p_path', required=True) parser.add_argument('--roe_path', required=True) parser.add_argument('--b2p_path', required=True) parser.add_argument('--er6_path', required=True) parser.add_argument('--cf2p_path', required=True) parser.add_argument('--output_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) er1_map = utils.read_map(args.er1_path) er12_map = utils.read_map(args.er12_path) tv2mc_map = utils.read_map(args.tv2mc_path) er2_map = utils.read_map(args.er2_path) e2p_map = utils.read_map(args.e2p_path) roe_map = utils.read_map(args.roe_path) b2p_map = utils.read_map(args.b2p_path) er6_map = utils.read_map(args.er6_path) cf2p_map = utils.read_map(args.cf2p_path) tickers = (er1_map.keys() & er12_map.keys() & tv2mc_map.keys() & er2_map.keys() & e2p_map.keys() & roe_map.keys() & b2p_map.keys() & er6_map.keys() & cf2p_map.keys()) logging.info('%d tickers' % len(tickers)) logging.info('total weight: %f' % (ER1 + ER12 + TV2MC + ER2 + E2P + ROE + B2P + ER6 + CF2P)) with open(args.output_path, 'w') as fp: for t in sorted(tickers): score = (er1_map[t] * ER1 + er12_map[t] * ER12 + tv2mc_map[t] * TV2MC + er2_map[t] * ER2 + e2p_map[t] * E2P + roe_map[t] * ROE + b2p_map[t] * B2P + er6_map[t] * ER6 + cf2p_map[t] * CF2P) / 100 # accounting for % print('%s %f' % (t, score), file=fp)
def test_distance(self): """ Targets: In range: Reachable: Nearest: Chosen: ####### ####### ####### ####### ####### #E..G.# #E.?G?# #E.@G.# #E.!G.# #E.+G.# #...#.# --> #.?.#?# --> #.@.#.# --> #.!.#.# --> #...#.# #.G.#G# #?G?#G# #@G@#G# #!G.#G# #.G.#G# ####### ####### ####### ####### ####### """ map_, players, walls = read_map(test_data_chosen) player = players[0] distance = player.calculate_reachable_bfs(map_, walls, players) print(distance)
def test_step(self): """ In range: Nearest: Chosen: Distance: Step: ####### ####### ####### ####### ####### #.E...# #.E...# #.E...# #4E212# #..E..# #...?.# --> #...!.# --> #...+.# --> #32101# --> #.....# #..?G?# #..!G.# #...G.# #432G2# #...G.# ####### ####### ####### ####### ####### """ map_, players, walls = read_map(test_data_step) player = players[0] distances = player.step(map_, walls, players) print(distances) self.assertEqual(player.position, (1, 3))
def test_chosen(self): """ Targets: In range: Reachable: Nearest: Chosen: ####### ####### ####### ####### ####### #E..G.# #E.?G?# #E.@G.# #E.!G.# #E.+G.# #...#.# --> #.?.#?# --> #.@.#.# --> #.!.#.# --> #...#.# #.G.#G# #?G?#G# #@G@#G# #!G.#G# #.G.#G# ####### ####### ####### ####### ####### """ map_, players, walls = read_map(test_data_chosen) player = players[0] distance, chosen = player.chosen(map_, walls, players) self.assertEqual(distance, 2) self.assertEqual(chosen, (1, 3))
def test1(self): test_data = ''' ####### #.G.E.# #E.G.E# #.G.E.# ####### ''' test_data = test_data.split('\n')[1:-1] map_, players, walls = read_map(test_data) self.assertEqual(map_[0][0], '#') self.assertEqual(map_[1][2], 'G') self.assertEqual(map_[2][5], 'E') self.assertEqual(len(players), 7) self.assertEqual(players[5].type_, 'G') self.assertEqual(players[5].position, (3,2)) self.assertEqual(players[1].type_, 'E') self.assertEqual(players[1].position, (1,4))
def test2(self): ''' ####### #...G.# G(200) #..GEG# G(200), E(188), G(194) #.#.#G# G(194) #...#E# E(194) #.....# ####### ''' m, p, w = read_map(test_data_battle) game = Game(m, w, p) for _ in range(2): game.step() game.display() print(game) self.assertEqual(game.players[0].hit_point, 200) self.assertEqual(game.players[1].hit_point, 200) self.assertEqual(game.players[2].hit_point, 188) self.assertEqual(game.players[3].hit_point, 194) self.assertEqual(game.players[4].hit_point, 194) self.assertEqual(game.players[5].hit_point, 194)
def predict_babelnet(input_path: str, output_path: str, resources_path: str) -> None: """ DO NOT MODIFY THE SIGNATURE! This is the skeleton of the prediction function. The predict function will build your model, load the weights from the checkpoint and write a new file (output_path) with your predictions in the "<id> <BABELSynset>" format (e.g. "d000.s000.t000 bn:01234567n"). The resources folder should contain everything you need to make the predictions. It is the "resources" folder in your submission. N.B. DO NOT HARD CODE PATHS IN HERE. Use resource_path instead, otherwise we will not be able to run the code. If you don't know what HARD CODING means see: https://en.wikipedia.org/wiki/Hard_coding :param input_path: the path of the input file to predict in the same format as Raganato's framework (XML files you downloaded). :param output_path: the path of the output file (where you save your predictions) :param resources_path: the path of the resources folder containing your model and stuff you might need. :return: None """ print("Predicting Babelnet...") out_vocab = utils.read_vocabulary( os.path.join(resources_path, config.OUT_VOCAB_BN)) bn2wn = utils.read_map(os.path.join(resources_path, config.BABELNET2WORDNET), reverse=False) dense_layer = 0 is_bn = True _prediction( input_path, output_path, resources_path, out_vocab, dense_layer, is_bn, bn2domain=bn2wn, )
def test23(self): ''' After 23 rounds: ####### #...G.# G(200) #..G.G# G(200), G(131) #.#.#G# G(131) #...#E# E(131) #.....# ####### ''' m, p, w = read_map(test_data_battle) game = Game(m, w, p) for _ in range(23): game.step() game.display() print(game) self.assertEqual(len(game.players), 5) self.assertEqual(game.players[0].hit_point, 200) self.assertEqual(game.players[1].hit_point, 200) self.assertEqual(game.players[2].hit_point, 131) self.assertEqual(game.players[3].hit_point, 131) self.assertEqual(game.players[4].hit_point, 131)
def main(): #read list li = utils.read_arg_list_all(sys.argv[1]) #read map map = utils.read_map(sys.argv[2]) #read check_match match = utils.read_check_match(sys.argv[3]) if(sys.argv[4] == "truseq"): print 'gene\torg\ttruseq_f\ttruseq_r\texpected_length' else: print 'gene\torg\tconventional_pcr_f\tconventional_pcr_r\texpected_length' for x in li: gene = x[0] if(len(x) == 2): target1 = x[1] show_list(gene,target1,map,match) else: target1 = x[1] target2 = x[2] show_list(gene,target1,map,match) show_list(gene,target2,map,match)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--current_prices_path', required=True) parser.add_argument('--future_prices_path', required=True) parser.add_argument('--scores_path', required=True) parser.add_argument('--verbose', action='store_true') args = parser.parse_args() utils.setup_logging(args.verbose) cp_map = utils.read_map(args.current_prices_path) fp_map = utils.read_map(args.future_prices_path) s_map = utils.read_map(args.scores_path) tickers = cp_map.keys() & fp_map.keys() & s_map.keys() logging.info('%d tickers in this test' % len(tickers)) scores = s_map.values() maxs, mins, means = max(scores), min(scores), sum(scores)/len(scores) logging.info('max score = %f, min score = %f, mean score = %f' % (maxs, mins, means)) r_map = dict() for t in tickers: r_map[t] = (fp_map[t] - cp_map[t]) / (cp_map[t] + 0.01) # avoid div-by-0 ups, rups, corrects = 0, 0, 0 for t in tickers: if (s_map[t] > 0) == (r_map[t] > 0): corrects += 1 if s_map[t] > 0: ups += 1 if r_map[t] > 0: rups += 1 logging.info('%d of %d are predicted to go up' % (ups, len(tickers))) logging.info('%d of %d did go up' % (rups, len(tickers))) logging.info('%d of %d correct signs (%.2f%%)' % (corrects, len(tickers), 100.0 * corrects / len(tickers))) rs = [(t, s) for t, s in s_map.items()] rs.sort(key=lambda x: x[1]) bucket_size = int(len(tickers) / K) for i in range(K): f = i * bucket_size t = f + bucket_size if i == K - 1: t = len(tickers) meanr, count = 0.0, 0 for j in range(f, t): r = r_map[rs[j][0]] meanr += r count += 1 meanr /= count logging.info('decile %d: %d stocks, mean return = %.2f%%' % (i+1, count, meanr * 100)) tr = compute_return(rs, r_map, T) br = compute_return(rs, r_map, B) logging.info('Mean return for top %s is %s' % (T, tr)) logging.info('Mean return for bot %s is %s' % (B, br)) rss = [(p[0], p[1], r_map[p[0]]) for p in rs] rss.sort(key=lambda x: x[2]) logging.info('Top 10 gains: %s, average: %f' % ([x[2] for x in rss[-10:]], sum([x[2] for x in rss[-10:]])/10)) logging.info('Top 10 scores: %s, average: %f' % ([x[1] for x in rss[-10:]], sum([x[1] for x in rss[-10:]])/10)) logging.info('Bot 10 gains: %s, average: %f' % ([x[2] for x in rss[:10]], sum([x[2] for x in rss[:10]])/10)) logging.info('Bot 10 scores: %s, average: %f' % ([x[1] for x in rss[:10]], sum([x[1] for x in rss[:10]])/10))
def _prediction( input_path: str, output_path: str, resources_path: str, out_vocab: Dict, i_dense: int, is_bn: bool, bn2domain: Dict = None, ) -> None: """ This method is used to handle the prediction of a task :param input_path: the path of the input file to predict in the same format as Raganato's framework (XML files you downloaded). :param output_path: the path of the output file (where you save your predictions) :param resources_path: the path of the resources folder containing your model and stuff you might need. :param out_vocab: sense inventory :param i_dense: i-esime fully connected layer: 0 -> Babelnet 1 -> Wordnet domains 2 -> Lexicographer :param is_bn: if True, predicts Babelnet :param bn2domain: a map Babelnet to domain :return: None """ config_tf = tf.ConfigProto() config_tf.gpu_options.allow_growth = True tf.keras.backend.set_session(tf.Session(config=config_tf)) test_set = parser.parser_test_set(input_path) vocab = utils.read_vocabulary(os.path.join(resources_path, config.VOCAB)) wn2bn = utils.read_map(os.path.join(resources_path, config.BABELNET2WORDNET), reverse=True) out_vocab_bn = utils.read_vocabulary( os.path.join(resources_path, config.OUT_VOCAB_BN)) out_vocab_wnd = utils.read_vocabulary( os.path.join(resources_path, config.OUT_VOCAB_WND)) out_vocab_lex = utils.read_vocabulary( os.path.join(resources_path, config.OUT_VOCAB_LEX)) out_vocab_pos = utils.read_vocabulary( os.path.join(resources_path, config.POS_VOCAB)) pre_trained = gensim.models.KeyedVectors.load_word2vec_format(os.path.join( resources_path, config.SQUEEZED_EMB), binary=True) print("Downloading ELMo...") model = models.build_model( vocab_size=len(vocab), out_size_bn=len(out_vocab_bn), out_size_wnd=len(out_vocab_wnd), out_size_lex=len(out_vocab_lex), out_size_pos=len(out_vocab_pos), word2vec=pre_trained, is_elmo=config.IS_ELMO, attention=config.ATTENTION, is_sense_emb=config.SENSE_EMB, ) reversed_vocab = utils.reverse_vocab(out_vocab_bn) model.load_weights(str(os.path.join(resources_path, config.MODEL_WEIGHTS))) with open(str(output_path), mode="w") as file: for row in tqdm(test_set): if not config.IS_ELMO: tmp = preprocesser.text2id([row[0]], vocab) else: tmp = np.array([row[0]]) tmp_row = list(row[2]) inp = tmp if config.SENSE_EMB: sens_emb = np.ones((1, len(inp[0].split())), dtype=int) inp_pos = np.array([row[1]]) inp = [inp, sens_emb, inp_pos] predictions = model.predict(inp, verbose=0)[i_dense] for senses in tmp_row: sense_position = utils.senses_position_from_vocab( senses["lemma"], out_vocab_bn, bn2domain) synsets = [reversed_vocab[x] for x in sense_position] if not is_bn: synsets = [ bn2domain.get(syn.split("_")[-1]) for syn in synsets ] sense_position = [out_vocab[syn] for syn in synsets] to_compute = np.array([ predictions[0][senses["position"]][sen_pos] for sen_pos in sense_position ]) if len(to_compute) != 0: file.write(senses["id"] + " " + synsets[to_compute.argmax()].split("_")[-1] + "\n") else: file.write(senses["id"] + " " + utils.most_frequent_sense( senses["lemma"], senses["pos"], wn2bn, bn2domain=bn2domain, is_bn=is_bn, ) + "\n")
#this script match map file and blast result #output: organism, locus, ARG name #python match_map_blast.py mapfile blastfile > output.txt import sys import utils map = utils.read_map(sys.argv[1]) for line in open(sys.argv[2],'r'): spl = line.strip().split('\t') arg = spl[1].split('|') ar = arg[len(arg)-1] if(map.has_key(spl[0])): result = [ map[spl[0]], spl[0], ar] print '\t'.join(result)
#!/usr/bin/env python from __future__ import print_function from utils import read_map from game import Game if __name__ == '__main__': with open('data.txt', 'r') as f: m, p, w = read_map(f) game = Game(m, w, p) step, score = game.combat(5000) print(score) # 208413 too low # 210843 too low