-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
155 lines (123 loc) · 4.64 KB
/
utils.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
import sys, os, shutil, signal
import json
import arxiv
def _run_format(src: str):
def _format(fpath: str):
pipe = os.popen(f'clang-format --style=file {fpath}')
output = pipe.read()
with open(fpath, 'w') as f:
f.write(output)
for dirpath, dirnames, filenames in os.walk(src):
for fpath in filenames:
if fpath.endswith('.cpp'):
_format(os.path.join(dirpath, fpath))
def _run_clean(src: str):
def _del(fpath: str):
os.popen(f'rm {fpath}')
for dirpath, dirnames, filenames in os.walk(src):
for fpath in filenames:
if fpath == '.DS_Store':
_del(os.path.join(dirpath, fpath))
def _run_compile(debug=""):
if not os.path.exists('build'):
os.mkdir('build')
sh = f'cd build && cmake {"-DCMAKE_BUILD_TYPE=Debug" if debug else ""} .. && make'
for line in os.popen(sh):
print(line, end='')
def _run_get_sources(topicp, out, many="10"):
many = int(many)
with open(topicp) as f:
topics = json.load(f)
toparts = {t: [] for t in topics}
for topic in topics:
res = arxiv.query(search_query=topic)
for p in res[:many]:
toparts[topic].append(p['id'].split('/')[-1])
with open(out, 'w') as f:
json.dump(toparts, f)
def _run_estimate(cdata):
tot = 0
for dirpath, dirnames, filenames in os.walk(cdata):
for fname in filenames:
p = os.path.join(dirpath, fname)
with open(p) as f:
tot += len(f.read()) - 1
print(f'{tot} edges found')
def _run_crawl(cdata: str, refdata, sdata, source):
sh = f'./build/src/executables/crawl {cdata} {refdata} {sdata} {source}'
for line in os.popen(sh):
print(line, end='')
def _run_minhash(cdata, mdata, threshold):
sh = f'./build/src/executables/minhash {cdata} {mdata} {threshold}'
for line in os.popen(sh):
print(line, end='')
def _run_tfidf(sdata, tfidfdata):
sh = f'./build/src/executables/tfidf {sdata} {tfidfdata}'
for line in os.popen(sh):
print(line, end='')
def _run_clustering(mdata, clusterdata):
sh = f'./build/src/executables/cluster {mdata} {clusterdata}'
for line in os.popen(sh):
print(line, end='')
def _run_mf(cdata, vdata, alpha, beta):
sh = f'./build/src/executables/runmf {cdata} {vdata} {alpha} {beta}'
for line in os.popen(sh):
print(line, end='')
def _run_serve(cdata, vdata, clusterdata, keys, k="30"):
sh = f'./build/src/server/serve {cdata} {vdata} {clusterdata} {keys} {k}'
for line in os.popen(sh):
orint(line, end='')
def _run_references_all(data, archives, statep=""):
archs = []
with open(archives) as f:
for line in f:
paper = line.split('/')[-1].rstrip('\n')
if not paper.endswith('tar'):
continue
archs.append(paper)
getyear = lambda a: int(a[-12:-8]) # sort by year
archs = sorted(archs, key=getyear)
archs = [p for p in archs if getyear(p) in range(1000, 2000)]
if os.path.exists(statep):
with open(statep) as f:
state = json.load(f)
else:
state = {
'paper_errs': [],
'temp_err': None, # != None means error deleting
'last': None,
}
def _sig_handler(sig, frame):
print(json.dumps(state))
signal.signal(signal.SIGINT, _sig_handler);
fm = archs.index(state['last']) if state['last'] is not None else -2
for paper in reversed(archs[:fm + 1]):
print(f'at archive {paper}')
sh = f'./build/src/executables/download_references {paper} {data}'
for out in os.popen(sh):
print(out, end='')
if 'what()' in out:
state['paper_errs'].append(paper)
os.remove(paper)
shutil.rmtree('temp', onerror=lambda _, path, __: state.__setitem__('temp', paper))
os.mkdir('temp')
with open(statep, 'w') as f:
json.dump(state, f)
print(state)
os.rmdir('temp')
if __name__ == '__main__':
cmds = {
'fmt': _run_format,
'clean': _run_clean,
'compile': _run_compile,
'estimate': _run_estimate,
'sources': _run_get_sources,
'crawl': _run_crawl,
'ref_all': _run_references_all,
'minhash': _run_minhash,
'tfidf': _run_tfidf,
'cluster': _run_clustering,
'runmf': _run_mf,
'serve': _run_serve,
}
cmds[sys.argv[1]].__call__(*sys.argv[2:])