1 |
5079
|
aaronmk
|
#!/usr/bin/env python
|
2 |
5640
|
aaronmk
|
# Scrubs the taxonlabels in VegBIEN using TNRS.
|
3 |
5079
|
aaronmk
|
|
4 |
9998
|
aaronmk
|
# runtime: 162 ms/name ("real 458m50.126s" for "169,539 name(s)" [1])
|
5 |
|
|
# [1] $ tail -c +12953807 ../inputs/.TNRS/tnrs/logs/tnrs.make.log.sql|head -15
|
6 |
|
|
|
7 |
9999
|
aaronmk
|
# total runtime: 10 days ("Rows (counted) 5221748" (TNRS.tnrs @r9998)
|
8 |
|
|
# * 162 ms/name (above) * 1s/1000ms * 1h/3600s * 1day/24h = 9.79 days)
|
9 |
|
|
|
10 |
9530
|
aaronmk
|
# to estimate total runtime:
|
11 |
|
|
# bin/psql_vegbien <<<'SELECT COUNT(*) FROM tnrs_input_name'
|
12 |
|
|
# # names from above * 1.5 multiplier for scrubbing accepted names
|
13 |
|
|
# (the test_taxonomic_names sample from Brad produces 8 accepted names for
|
14 |
|
|
# 15 input names)
|
15 |
|
|
# * ((# ms/name from log file * 1 sec/1000 ms) + (# sec to run
|
16 |
|
|
# `SELECT * FROM "tnrs_input_name"` in log file / tnrs.max_names names/batch))
|
17 |
|
|
# * 1 hr / 3600 sec * 1 day / 24 hr = # days
|
18 |
|
|
|
19 |
5079
|
aaronmk
|
import os.path
|
20 |
|
|
import sys
|
21 |
|
|
|
22 |
|
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
23 |
|
|
|
24 |
|
|
import csvs
|
25 |
|
|
import opts
|
26 |
5098
|
aaronmk
|
import profiling
|
27 |
5079
|
aaronmk
|
import sql
|
28 |
|
|
import sql_gen
|
29 |
|
|
import sql_io
|
30 |
|
|
import strings
|
31 |
|
|
import tnrs
|
32 |
|
|
|
33 |
5669
|
aaronmk
|
tnrs_input = sql_gen.Table('tnrs_input_name')
|
34 |
5079
|
aaronmk
|
tnrs_data = sql_gen.Table('tnrs')
|
35 |
|
|
|
36 |
|
|
def main():
|
37 |
|
|
# Input
|
38 |
|
|
env_names = []
|
39 |
|
|
db_config = opts.get_env_vars(sql.db_config_names, None, env_names)
|
40 |
|
|
verbosity = float(opts.get_env_var('verbosity', 3, env_names))
|
41 |
|
|
if not 'engine' in db_config: raise SystemExit('Usage: '
|
42 |
|
|
+opts.env_usage(env_names)+' '+sys.argv[0]+' 2>>log')
|
43 |
|
|
|
44 |
|
|
def log(msg, level=1):
|
45 |
|
|
'''Higher level -> more verbose'''
|
46 |
|
|
if level <= verbosity:
|
47 |
|
|
sys.stderr.write(strings.to_raw_str(msg.rstrip('\n')+'\n'))
|
48 |
|
|
|
49 |
|
|
# Connect to DB
|
50 |
|
|
db = sql.connect(db_config, log_debug=log)
|
51 |
|
|
|
52 |
9522
|
aaronmk
|
cumulative_tnrs_profiler = profiling.ItersProfiler(iter_text='name')
|
53 |
5123
|
aaronmk
|
|
54 |
5640
|
aaronmk
|
# Iterate over unscrubbed verbatim taxonlabels
|
55 |
9515
|
aaronmk
|
while True:
|
56 |
5123
|
aaronmk
|
# Fetch next set
|
57 |
5837
|
aaronmk
|
cur = sql.select(db, tnrs_input, limit=tnrs.max_names, cacheable=False)
|
58 |
5123
|
aaronmk
|
this_ct = cur.rowcount
|
59 |
5640
|
aaronmk
|
log('Processing '+str(this_ct)+' taxonlabels')
|
60 |
9518
|
aaronmk
|
if this_ct == 0: break
|
61 |
5123
|
aaronmk
|
# otherwise, rows found
|
62 |
|
|
names = list(sql.values(cur))
|
63 |
|
|
|
64 |
9515
|
aaronmk
|
def process():
|
65 |
|
|
# Run TNRS
|
66 |
|
|
log('Making TNRS request')
|
67 |
9526
|
aaronmk
|
stream = tnrs.tnrs_request(names,
|
68 |
|
|
cumulative_profiler=cumulative_tnrs_profiler)
|
69 |
9515
|
aaronmk
|
|
70 |
|
|
log('Storing TNRS response data')
|
71 |
9517
|
aaronmk
|
sql_io.append_csv(db, tnrs_data, *csvs.reader_and_header(stream))
|
72 |
9516
|
aaronmk
|
# start transaction *before* submitting data, so Time_submitted is
|
73 |
|
|
# correctly set to the submission time rather than the insertion time.
|
74 |
|
|
# these may differ by several minutes if TNRS is slow.
|
75 |
|
|
sql.with_savepoint(db, process)
|
76 |
5079
|
aaronmk
|
|
77 |
|
|
main()
|