-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
196 lines (148 loc) · 6.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import sys
import argparse
import logging
import numpy as np
import os
from text_preprocessor import TextPreprocessor
from feature_extraction import features as mesh_features
from feature_extraction import get_target
from listnet import ListNet
from sklearn.preprocessing import MinMaxScaler
def get_candidates(citations, pmid):
# Find candidate mesh terms (neighbor/cited articles)
candidate_terms = set()
attributes = citations[pmid]
for pmid, _ in attributes['neighbors']:
if pmid in citations:
candidate_terms |= set(citations[pmid]["mesh"])
for pmid in attributes['cites']:
if pmid in citations:
candidate_terms |= set(citations[pmid]["mesh"])
return candidate_terms
def get_neighbor_candidates(citations, pmid):
# Find candidate mesh terms (neighboring articles)
candidate_terms = set()
attributes = citations[pmid]
for pmid, _ in attributes['neighbors']:
if pmid in citations:
candidate_terms |= set(citations[pmid]["mesh"])
return candidate_terms
def get_citation_candidates(citations, pmid):
# Find candidate mesh terms (cited articles)
candidate_terms = set()
attributes = citations[pmid]
for pmid in attributes['cites']:
if pmid in citations:
candidate_terms |= set(citations[pmid]["mesh"])
return candidate_terms
def engineer_features(citations, pmid):
candidates = get_candidates(citations, pmid)
data = [
np.asarray([func(citations, pmid, candidate)
for func in mesh_features])
for candidate in candidates
]
return data
class LNet():
def __init__(self):
self.current_file = None
def switch(self, num_iters, learning_rate):
with open('models/model_iter%d_gamma%.02f' % (num_iters, learning_rate)) as params_file:
self.weights = np.asarray([float(weight) for weight in params_file.readlines()])
def get_score(self, citations, pmid, mesh_term):
features = np.asarray([func(citations, pmid, mesh_term) for func in mesh_features])
logging.info("Features: {}".format(features))
return self.weights @ features
def listnet_score(lnet, citations, pmid, mesh_term):
return lnet.get_score(citations, pmid, mesh_term)
def generate_targets(citations, pmid):
candidates = get_candidates(citations, pmid)
targets = np.asarray([get_target(citations, pmid, candidate)
for candidate in candidates])
return targets
def scale_features(features):
# Scale the features
feature_matrix = np.vstack(features)
min_max_scaler = MinMaxScaler()
return min_max_scaler.fit_transform(feature_matrix)
if __name__ == "__main__":
parser = argparse.ArgumentParser('Ranker for MeSH terms')
parser.add_argument('--citations-file', '-c', type=str, default='')
parser.add_argument('--num-mesh-terms', '-k', type=int)
parser.add_argument('-v', '--verbosity', action='count', default=0)
parser.add_argument('-t', '--test', action='store_true')
args = parser.parse_args()
if(args.verbosity == 0):
level = logging.ERROR
elif(args.verbosity == 1):
level = logging.WARN
elif(args.verbosity == 2):
level = logging.INFO
elif(args.verbosity >= 3):
level = logging.DEBUG
logging.basicConfig(
format='[%(filename)s] %(levelname)s: %(message)s',
level=level,
)
logging.info('Loading dataset')
if args.citations_file:
logging.debug('Loading from ' + args.citations_file)
logging.error('Not implemented yet!')
sys.exit(1)
citations = {}
else:
logging.debug('Loading from raw sources')
if args.test:
citations = TextPreprocessor(use_cfg='config/test.cfg')
else:
citations = TextPreprocessor()
metadata = {
'input_size': len(citations.articles),
'scores': [1]
}
logging.debug(
'Number of training examples: {}'.format(metadata['input_size']))
ranker = ListNet(n_stages=1, hidden_size=6)
ranker.initialize_learner(metadata)
logging.info('Write features out to file')
''' Train ListNet:
candidates - is a list of feature vectors (ndarray)
targets - is a list of relevance scores - whether or not
the candidate MeSH term is a correct choice (0 or 1)
query - is the pmid of the current article.
'''
if args.test:
with open('test.txt', 'w') as f:
for pmid in citations.articles:
candidates = get_candidates(citations, pmid)
logging.debug('Building features for %d' % pmid)
logging.debug(engineer_features)
features = engineer_features(citations, pmid)
# Scale the features
# features = scale_features(features)
for feature_vec in features:
current_features = 'qid:{} '.format(pmid)
current_features += ' '.join(('%s:%s' % (feat_num + 1, val) for feat_num, val in enumerate(feature_vec)))
f.write(current_features + '\n')
# model = LNet()
# model.switch(50, .01)
# logging.info("{}"
# .format(model.get_score(citations, 9317033, 'mutation')))
# logging.info("weights:{}".format(model.weights))
# Score for MeSH term {} in article{} is:\n
else:
with open('features.txt', 'w') as f:
for pmid in citations.articles:
logging.debug('Ranking MeSH terms for %s' % pmid)
candidates = get_candidates(citations, pmid)
logging.debug('Building features for %d' % pmid)
logging.debug(engineer_features)
features = engineer_features(citations, pmid)
# Scale the features
# features = scale_features(features)
targets = generate_targets(citations, pmid)
training_example = [features, targets, pmid]
for target, feature_vec in zip(targets, features):
current_features = '{} qid:{} '.format(target, pmid)
current_features += ' '.join(('%s:%s' % (feat_num + 1, val) for feat_num, val in enumerate(feature_vec)))
f.write(current_features + '\n')