|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Fri Jun 12 11:33:22 2020 |
| 4 | +
|
| 5 | +@author: luol2 |
| 6 | +""" |
| 7 | +import argparse |
| 8 | +from nn_model import bioTag_CNN,bioTag_BERT |
| 9 | +from dic_ner import dic_ont |
| 10 | +from evaluate import GSCplus_corpus,JAX_corpus |
| 11 | +from tagging_text import bioTag |
| 12 | +import os |
| 13 | +import time |
| 14 | +import json |
| 15 | +import tensorflow as tf |
| 16 | + |
| 17 | +''' |
| 18 | +config = tf.ConfigProto() |
| 19 | +config.gpu_options.allow_growth = True |
| 20 | +session = tf.Session(config=config) |
| 21 | +''' |
| 22 | +def run_gsc_test(files,biotag_dic,nn_model): |
| 23 | + |
| 24 | + |
| 25 | + fin_test=open(files['testfile'],'r',encoding='utf-8') |
| 26 | + all_test=fin_test.read().strip().split('\n\n') |
| 27 | + fin_test.close() |
| 28 | + test_out=open(files['outfile'],'w',encoding='utf-8') |
| 29 | + #i=0 |
| 30 | + for doc_test in all_test: |
| 31 | + #i+=1 |
| 32 | + #print(i) |
| 33 | + lines=doc_test.split('\n') |
| 34 | + pmid = lines[0] |
| 35 | + test_result=bioTag(lines[1],biotag_dic,nn_model,onlyLongest=False,abbrRecog=False,Threshold=0.95) |
| 36 | + test_out.write(pmid+'\n'+lines[1]+'\n') |
| 37 | + for ele in test_result: |
| 38 | + test_out.write(ele[0]+'\t'+ele[1]+'\t'+lines[1][int(ele[0]):int(ele[1])]+'\t'+ele[2]+'\t'+ele[3]+'\n') |
| 39 | + test_out.write('\n') |
| 40 | + test_out.close() |
| 41 | + GSCplus_corpus(files['outfile'],files['testfile'],subtree=True) |
| 42 | + |
| 43 | +def run_jax_test(files,biotag_dic,nn_model): |
| 44 | + inpath=files['testfile'] |
| 45 | + test_out=open(files['outfile'],'w',encoding='utf-8') |
| 46 | + i=0 |
| 47 | + preds_result={} |
| 48 | + for file in os.listdir(inpath): |
| 49 | + i+=1 |
| 50 | + print(i) |
| 51 | + pmid=file[:-4] |
| 52 | + temp_result=[] |
| 53 | + fin=open(inpath+file,'r',encoding='utf-8') |
| 54 | + intext=fin.read().rstrip() |
| 55 | + fin.close() |
| 56 | + test_result=bioTag(intext,biotag_dic,nn_model,onlyLongest=False,abbrRecog=True,Threshold=0.95) |
| 57 | + for ele in test_result: |
| 58 | + if ele not in temp_result: |
| 59 | + temp_result.append(ele) |
| 60 | + preds_result[pmid]=temp_result |
| 61 | + json.dump(preds_result, test_out ,indent=2) |
| 62 | + test_out.close() |
| 63 | + JAX_corpus(files['outfile'], files['goldfile']) |
| 64 | + |
| 65 | + |
| 66 | +if __name__=="__main__": |
| 67 | + |
| 68 | + parser = argparse.ArgumentParser(description='build weak training corpus, python build_dict.py -i infile -o outpath') |
| 69 | + parser.add_argument('--modeltype', '-m', help="the model type (cnn or biobert or bioformer?)",default='biobert') |
| 70 | + parser.add_argument('--corpus', '-c', help="HPO corpus (gsc or jax?)",default='jax') |
| 71 | + parser.add_argument('--output', '-o', help="the output prediction file ",default='../results/gsc_bioformer_new1.tsv') |
| 72 | + |
| 73 | + args = parser.parse_args() |
| 74 | + model_type=args.modeltype |
| 75 | + test_set=args.corpus |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + ontfiles={'dic_file':'../dict/noabb_lemma.dic', |
| 80 | + 'word_hpo_file':'../dict/word_id_map.json', |
| 81 | + 'hpo_word_file':'../dict/id_word_map.json'} |
| 82 | + biotag_dic=dic_ont(ontfiles) |
| 83 | + |
| 84 | + if model_type=='cnn': |
| 85 | + vocabfiles={'w2vfile':'../models_v1.1/bio_embedding_intrinsic.d200', |
| 86 | + 'charfile':'../dict/char.vocab', |
| 87 | + 'labelfile':'../dict/lable.vocab', |
| 88 | + 'posfile':'../dict/pos.vocab'} |
| 89 | + |
| 90 | + modelfile='../models_v1.1/cnn_hpo_v1.1.h5' |
| 91 | + nn_model=bioTag_CNN(vocabfiles) |
| 92 | + nn_model.load_model(modelfile) |
| 93 | + |
| 94 | + elif model_type=='biobert': |
| 95 | + vocabfiles={'labelfile':'../dict/lable.vocab', |
| 96 | + 'config_path':'../models_v1.1/biobert_v11_pubmed/bert_config.json', |
| 97 | + 'checkpoint_path':'../models_v1.1/biobert_v11_pubmed/model.ckpt-1000000', |
| 98 | + 'vocab_path':'../models_v1.1/biobert_v11_pubmed/vocab.txt'} |
| 99 | + modelfile='../models_v1.1/biobert_hpo_v1.1.h5' |
| 100 | + nn_model=bioTag_BERT(vocabfiles) |
| 101 | + nn_model.load_model(modelfile) |
| 102 | + else: |
| 103 | + vocabfiles={'labelfile':'../dict/lable.vocab', |
| 104 | + 'config_path':'../models_v1.1/bioformer-cased-v1.0/bert_config.json', |
| 105 | + 'checkpoint_path':'../models_v1.1/bioformer-cased-v1.0/bioformer-cased-v1.0-model.ckpt-2000000', |
| 106 | + 'vocab_path':'../models_v1.1/bioformer-cased-v1.0/vocab.txt'} |
| 107 | + modelfile='../models_v1.1/bioformer_hpo_v1.1.h5' |
| 108 | + nn_model=bioTag_BERT(vocabfiles) |
| 109 | + nn_model.load_model(modelfile) |
| 110 | + |
| 111 | + if test_set=='gsc': |
| 112 | + files={'testfile':'../data/corpus/GSC/GSCplus_test_gold.tsv', |
| 113 | + 'outfile':'../results/gsc_test_bioformer_p5n5.tsv'} |
| 114 | + files['outfile']=args.output |
| 115 | + start_time=time.time() |
| 116 | + run_gsc_test(files,biotag_dic,nn_model) |
| 117 | + print('gsc done:',time.time()-start_time) |
| 118 | + else: |
| 119 | + files={'testfile':'../data/corpus/JAX/txt/', |
| 120 | + 'goldfile':'../data/corpus/JAX/JAX_gold.json', |
| 121 | + 'outfile':'../results/jax_test_bert_p5n5.json'} |
| 122 | + start_time=time.time() |
| 123 | + files['outfile']=args.output |
| 124 | + run_jax_test(files,biotag_dic,nn_model) |
| 125 | + print('jax done:',time.time()-start_time) |
0 commit comments