-
Notifications
You must be signed in to change notification settings - Fork 3
/
dissimilarity-matrix
executable file
·281 lines (234 loc) · 8.71 KB
/
dissimilarity-matrix
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#! /usr/bin/python3
"""Compute a dissimilarity (cosine distance) matrix between all of the
location matrices in the directory named on the command line. Output
is in CSV format and is written to stdout."""
import argparse
import collections
import csv
import datetime
import glob
import multiprocessing
import os
import sys
import time
import numpy as np
import tables
class ProgressMonitor:
def __init__(self):
self.start_time = time.monotonic()
self.last_message = 0
self.jobsize = None
self.completed = None
self.avg_tick_time = None
self.last_tick = None
self.log = open("pm.log", "wt")
def __del__(self):
self.log.close()
def start_job(self, jobsize, label):
self.jobsize = jobsize
self.completed = 0
self.avg_tick_time = 0
self.last_tick = time.monotonic()
self.message("{} {}", jobsize, label)
def message(self, msg, *args):
self.last_message = time.monotonic()
ts = datetime.timedelta(seconds = self.last_message - self.start_time)
sys.stderr.write(("{}: " + msg + "\n").format(ts, *args))
def tick(self):
now = time.monotonic()
elapsed = now - self.last_tick
self.avg_tick_time = (elapsed if self.avg_tick_time == 0
else elapsed*0.05 + self.avg_tick_time*0.95)
self.completed += 1
self.last_tick = now
self.log.write("{},{},{}\n".format(now,elapsed,self.avg_tick_time))
if now - self.last_message >= 30 or self.completed >= self.jobsize:
remaining = self.jobsize - self.completed
est_rm = datetime.timedelta(seconds=self.avg_tick_time*remaining)
self.message("{}/{} complete, {} remaining",
self.completed, self.jobsize, est_rm)
def check_matching_grid(fi, fj):
ai = fi.root.location.attrs
aj = fj.root.location.attrs
err = ""
def assert_eq(tag):
if getattr(ai, tag) != getattr(aj, tag):
err += " {} {}/{}".format(tag, getattr(ai, tag), getattr(aj, tag))
assert_eq("north")
assert_eq("south")
assert_eq("east")
assert_eq("west")
assert_eq("lat_count")
assert_eq("lat_spacing")
assert_eq("lon_count")
assert_eq("lon_spacing")
assert_eq("fuzz")
assert_eq("resolution")
if err:
return "{} + {}: grid parameter mismatch:{}".format(
fi.filename, fj.filename, err)
return ""
def label_for(f):
ann = f.root.location.attrs.annotations
if 'proxy_alleged_cc2' in ann and 'proxy_provider' in ann:
return ann['proxy_provider'] + '.' + ann['proxy_alleged_cc2']
if 'proxy_label' in ann:
return 'LABEL_' + ann['proxy_label']
return 'CC_' + ann.get('country', 'zz')
def check_pair(args):
i, mi, m0 = args
with tables.open_file(m0) as f0, tables.open_file(mi) as fi:
return i, label_for(fi), check_matching_grid(f0, fi)
def merge_outer_join(R, S, key, val):
"""Perform an outer join on R and S. Abstractly, this pairs up
the elements of R and S by key(x), and yields 3-tuples
(key(rx or sx), val(rx), val(sx)), in sorted order.
If some elements of R lack matching elements in S, or
vice versa, then the missing val() is replaced with a None.
Example:
list(merge_outer_join([(1,'a'), (3,'b')], [(1,'x'), (2,'y')],
key=lambda x: x[0],
val=lambda x: x[1]))
-->
[(1,'a','x'), (2,None,'y'), (3,'b',None)]
Corrected, Py3k-ified, and simplified from
https://code.activestate.com/recipes/492216/
with a bunch of help from
https://codereview.stackexchange.com/questions/141580"""
# We are working with two iterators in parallel but not in lockstep,
# so we must watch out for either one running out at any time.
class StopR(Exception): pass
class StopS(Exception): pass
def advance(itr, exc):
try: return next(itr)
except StopIteration: raise exc
R = iter(sorted((key(r), val(r)) for r in R))
S = iter(sorted((key(s), val(s)) for s in S))
try:
# Whenever we pull values from both R and S, we must first
# set from_S to None in case advance(R) throws.
from_S = None
from_R = advance(R, StopR)
from_S = advance(S, StopS)
while True:
if from_R[0] == from_S[0]:
yield (from_R[0], from_R[1], from_S[1])
from_S = None
from_R = advance(R, StopR)
from_S = advance(S, StopS)
elif from_S[0] < from_R[0]:
yield (from_S[0], None, from_S[1])
from_S = advance(S, StopS)
else:
yield (from_R[0], from_R[1], None)
from_R = advance(R, StopR)
except StopR:
while True:
if from_S is not None:
yield (from_S[0], None, from_S[1])
from_S = next(S)
except StopS:
while True:
yield (from_R[0], from_R[1], None)
from_R = next(R)
def process_pair(args):
from math import sqrt
i, j, mi, mj = args
sij = 0
sii = 0
sjj = 0
with tables.open_file(mi) as fi, \
tables.open_file(mj) as fj:
for _, vi, vj in merge_outer_join(
fi.root.location.iterrows(),
fj.root.location.iterrows(),
key=lambda row: (row['grid_x'], row['grid_y']),
val=lambda row: row['prob_mass']):
if vi:
sii += vi*vi
if vj:
sjj += vj*vj
if vi and vj:
sij += vi*vj
# avoid division by zero
if sii and sjj:
return i, j, 1 - sij/(sqrt(sii)*sqrt(sjj))
return i, j, 1
def enumerate_disjoint_pairs(ms):
N = len(ms)
for i in range(N):
for j in range(i+1, N):
yield (i, j, ms[i], ms[j])
def select_interesting_items(dissim):
"""A dissimilarity-matrix row is _interesting_ if at least one
off-diagonal entry is not equal to 1."""
N = dissim.shape[0]
sel = np.zeros((N,), np.bool)
for i in range(N-1):
if np.any(dissim[i,(i+1):] < 0.99):
sel[i] = True
if np.any(dissim[N-1, :(N-1)] < 0.99):
sel[N-1] = True
return sel
def process(mats, pool, outf):
N = len(mats)
names = [None] * N
error = False
nameu = collections.Counter()
pm = ProgressMonitor()
# Check all the grid parameters once at the beginning, and determine
# all the labels. This is quadratically more efficient than checking
# them every time. If all files' grid parameters are equal to file 0's
# grid parameters, then transitively they are all equal to each other.
pm.start_job(N, "files to check")
for i, li, err in pool.imap_unordered(
check_pair,
((i, mats[i], mats[0]) for i in range(N))):
pm.tick()
if err:
pm.message(err)
error = True
else:
assert names[i] is None
nameu[li] += 1
names[i] = li + str(nameu[li])
if err:
raise SystemExit(1)
# Reorder the matrix by label.
oind = sorted((name, ix) for ix, name in enumerate(names))
names = [None]*N
omats = [None]*N
for i, (name, ix) in enumerate(oind):
names[i] = name
omats[i] = mats[ix]
# A dissimilarity matrix has zeros on the main diagonal, and is
# symmetric about the main diagonal. We could store just the
# upper triangle, but Num/SciPy doesn't seem to have a
# sparse-matrix class that does that.
dissim = np.zeros((N, N))
pm.start_job(N * (N - 1) // 2, "pairs of files to process")
for i, j, dij in pool.imap_unordered(
process_pair, enumerate_disjoint_pairs(omats)):
pm.tick()
dissim[i,j] = dij
dissim[j,i] = dij
pm.message("selecting interesting items...")
sel = select_interesting_items(dissim)
pm.message("{} of {} items selected.", sel.sum(), N)
pm.message("writing matrix...")
wr = csv.writer(outf, dialect='unix', quoting=csv.QUOTE_MINIMAL)
wr.writerow([name for name, isint in zip(names, sel) if isint])
for i in range(N):
if sel[i]:
wr.writerow(dissim[i,sel])
pm.message("done.")
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("input_dir",
help="Directory to read .h5-format location matrices from."
"All such matrices must have the same grid parameters.")
args = ap.parse_args()
mats = sorted(glob.glob(os.path.join(args.input_dir, "*.h5")))
with multiprocessing.Pool() as pool, sys.stdout as outf:
process(mats, pool, outf)
main()