forked from plazar/Ratings2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_pfds.py
executable file
·170 lines (151 loc) · 6.94 KB
/
rate_pfds.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
#!/usr/bin/env python
import sys
import argparse
import copy
import multiprocessing
import traceback
import textwrap
import candidate
import raters
import utils
def rate_pfd(pfdfn, rater_instances):
"""Given the name of a *.pfd file and a list of Rater instances
compute the ratings.
Inputs:
pfdfn: Name of the *.pfd file.
rater_instances: A list of Rater instances to compute ratings.
Outputs:
cand: The resulting (rated) Candidate object.
***NOTE: RatingValues are added directly to the Candidate object.
"""
cand = candidate.read_pfd_file(pfdfn)
for rater in rater_instances:
ratval = rater.rate(cand)
cand.add_rating(ratval)
return cand
def main():
if not args.raters:
print "No raters are loaded."
args.list_raters = True
if args.list_raters:
utils.print_raters_list(args.verbosity)
sys.exit(0)
rater_instances = []
for rater_name in args.raters:
rater_module = getattr(raters, rater_name)
rater_instances.append(rater_module.Rater())
cands = []
if args.num_procs > 1:
print "Using %d rater threads" % args.num_procs
rater_pool = multiprocessing.Pool(processes=args.num_procs)
apply_async = lambda pfdfn: rater_pool.apply_async(rate_pfd, \
(pfdfn, rater_instances))
inprogress = [apply_async(pfdfn) for pfdfn in args.infiles]
failed = []
while inprogress:
for ii in range(len(inprogress))[::-1]:
result = inprogress[ii]
if result.ready():
if result.successful():
cands.append(result.get())
else:
failed.append(result)
inprogress.pop(ii)
print "Number of failures detected: %d" % len(failed)
for fail in failed:
try:
print type(fail), fail
fail.get()
except:
traceback.print_exc()
else:
for pfdfn in args.infiles:
cands.append(rate_pfd(pfdfn, rater_instances))
for cand in cands:
if args.write_to_file:
cand.write_ratings_to_file()
if args.write_to_screen:
print cand.pfdfn
print cand.get_ratings_overview()
print '-'*25
class RemoveAllRatersAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, [])
class AddAllRatersAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
ratlist = copy.deepcopy(raters.registered_raters)
setattr(namespace, self.dest, ratlist)
class RemoveOneRaterAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
rater_name = values[0]
if rater_name not in raters.registered_raters:
sys.stderr.write("Unrecognized rater: %s\nThe following " \
"raters are registered:\n %s\n" % \
(rater_name, "\n ".join(raters.registered_raters)))
sys.exit(1)
curr_raters = getattr(namespace, self.dest)
# Remove any instances of 'values' from curr_raters
while rater_name in curr_raters:
curr_raters.remove(rater_name)
setattr(namespace, self.dest, curr_raters)
class AddOneRaterAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
rater_name = values[0]
if rater_name not in raters.registered_raters:
sys.stderr.write("Unrecognized rater: %s\nThe following " \
"raters are registered:\n %s\n" % \
(rater_name, "\n ".join(raters.registered_raters)))
sys.exit(1)
curr_raters = getattr(namespace, self.dest)
# Add 'rater_name' to curr_raters
if rater_name not in curr_raters:
curr_raters.append(rater_name)
setattr(namespace, self.dest, curr_raters)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Rate PFD files.")
parser.add_argument('infiles', metavar="INFILES", \
nargs='+', type=str, \
help="PRESTO *.pfd files to rate.")
parser.add_argument('-v', '--more-verbose', dest='verbosity', \
default=0, action='count', \
help="Turn up verbosity by one notch. " \
"(Default: Don't be verbose (verbosity=0).)")
parser.add_argument('-d', '--debug', dest='debug', \
default=False, action='store_true', \
help="Turn on debugging output. " \
"(Default: Don't print debugging info.)")
parser.add_argument('-L', '--list-raters', dest='list_raters', \
default=False, action='store_true', \
help="List registered raters and exit.")
parser.add_argument('-x', '--exclude', dest='raters', \
type=str, default=[], nargs=1, \
action=RemoveOneRaterAction, \
help="Remove rater from list of ratings to apply.")
parser.add_argument('--exclude-all', dest='raters', \
default=[], nargs=0, \
action=RemoveAllRatersAction, \
help="Clear list of ratings to apply.")
parser.add_argument('-i', '--include', dest='raters', \
type=str, default=[], nargs=1, \
action=AddOneRaterAction, \
help="Include rater from list of ratings to apply.")
parser.add_argument('--include-all', dest='raters', \
default=[], nargs=0, \
action=AddAllRatersAction, \
help="Include all registered ratings in list " \
"of ratings to apply.")
parser.add_argument('-P', '--num-procs', dest="num_procs", \
type=int, default=1, \
help="The number of rater processes to use. " \
"Each thread rates a separate candidate. " \
"(Default: use one rater thread.)")
parser.add_argument('--no-write-to-file', dest='write_to_file', \
default=True, action='store_false', \
help="Do not write out ratings to file. " \
"(Default: Write out file.)")
parser.add_argument('--no-write-to-screen', dest='write_to_screen', \
default=True, action='store_false', \
help="Do not write out ratings to screen. " \
"(Default: Write ratings to screen.)")
args = parser.parse_args()
main()