forked from douglasbagnall/bog
-
Notifications
You must be signed in to change notification settings - Fork 2
/
munge.py
260 lines (207 loc) · 6.71 KB
/
munge.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import random
import numpy as np
from meta import makepath
import math
import os
def write_normalised_png(a, fn, verbose=False):
from PIL import Image
hi = np.amax(a)
lo = np.amin(a)
scale = 255.9 / (hi - lo)
b = (a - lo) * scale
c = np.array(b, dtype='uint8')
if verbose:
print fn
print "low %f high %f scale %f" % (lo, hi, scale)
print "b min %f b max %f" % (np.amin(b), np.amax(b))
print "c min %d b max %d" % (np.amin(c), np.amax(c))
im = Image.fromarray(c)
makepath(fn)
im.save(fn)
def clip_range(x, extreme):
x = np.maximum(x, -extreme)
x = np.minimum(x, extreme)
return x
def clipped_neg_exp(raw_affinities):
x = clip_range(raw_affinities, 40)
return np.exp(-x)
def clipped_logistic(raw_affinities):
x = clip_range(raw_affinities, 40)
return 1.0 / (1.0 + np.exp(x * -3.0))
def links_to_clusters(links):
n = links.shape[0]
clusters = [set([x]) for x in range(n)]
for a, b in zip(*np.where(links)):
if clusters[a] is not clusters[b]:
c1, c2 = clusters[a], clusters[b]
c1.update(c2)
for x in c2:
clusters[x] = c1
return set(frozenset(x) for x in clusters)
def data_to_clusters(data, threshold, names=None):
links = data > threshold
clusters = links_to_clusters(links)
if names is None:
return clusters
named_clusters = set()
for c in clusters:
named_clusters.add(frozenset(names[x] for x in c))
return named_clusters
def p_to_affinities(input, samples=3000):
"""Treat the input array as a (somehow) scaled probability that two
nodes are linked. The probability that they're in the same cluster
has a transitive element.
"""
totals = np.zeros_like(input)
scale = 0.1
min_scale = 1e-20
max_scale = 1.0 / (1e-3 + np.min(input))
runs = samples
while runs > 0:
scale = random.uniform(min_scale, max_scale)
r = np.random.random(input.shape)
p = input * scale
links = r < p
clusters = links_to_clusters(links)
if len(clusters) == 1:
# everything is connected, so the scale is too high
# make sure we don't go higher.
max_scale = (scale + max_scale) * 0.5
continue
if len(clusters) == input.shape[0]:
# nothing is connected
min_scale = (scale + 3 * min_scale) * 0.25
continue
for c in clusters:
z = np.zeros(totals.shape[0])
c = np.array(sorted(c), dtype=int)
z[c] = 1
for y in c:
totals[y] += z
runs -= 1
totals *= 1.0 / (np.amax(totals) + 1e-10)
return totals
def shuffle_array(a):
# np.random.shuffle does only one dimension.
shape = a.shape
a = a.flatten()
np.random.shuffle(a)
return a.reshape(shape)
def array_to_link_pairs(a, names, include_self=False):
"""make a sorted list of links from the top triangle of an array."""
links = []
no_self = int(not include_self)
n = a.shape[0]
for i in range(n):
for j in range(i + no_self, n):
links.append((a[i, j], (names[i], names[j])))
return links
def add_to_links(links_out, s, new_p, c):
old_p = c[1].difference(new_p)
for x in new_p:
for y in old_p:
if x > y:
p = (y, x)
else:
p = (x, y)
links_out.append((s, p))
def add_to_cluster(p, linkmap, p2, scores):
c = linkmap[p2]
s, cluster = c
if p in cluster:
print "%s is in 2 sets?" % p
return 0.0
for y in cluster:
s += scores[(p, y)]
c[0] = s
linkmap[p] = c
cluster.add(p)
n = len(cluster)
# scale == 1 / (n! / (r! * (n-r)!)
# == r! * (n-r)! / n!, and here r == 2
# == 2 / (n * (n-1))
scale = 2.0 / (n * (n - 1))
return s * scale
def array_to_link_pairs_cluster_aware(a, names, power=1.0):
"""make a sorted list of links from the top triangle of an array."""
links = array_to_link_pairs(a, names, True)
pairs = [(s ** power, p) for s, p in links if p[0] != p[1]]
pairs.sort(reverse=True)
scores = {}
for s, p in pairs:
scores[p] = s
scores[(p[1], p[0])] = s
links_out = [(s, p) for s, p in links if p[0] == p[1]]
linkmap = {}
inv_power = 1.0 / power
for score, link in pairs:
p1, p2 = link
if p1 in linkmap and p2 in linkmap:
c1 = linkmap[p1]
c2 = linkmap[p2]
if c1 is c2:
continue
s1, x1 = c1 # [score, set]
s2, x2 = c2
for x in x2:
s = add_to_cluster(x, linkmap, p1, scores)
add_to_links(links_out, s ** inv_power, x2, c1)
elif p1 in linkmap:
s = add_to_cluster(p2, linkmap, p1, scores)
add_to_links(links_out, s ** inv_power, [p2], linkmap[p1])
elif p2 in linkmap:
s = add_to_cluster(p1, linkmap, p2, scores)
add_to_links(links_out, s ** inv_power, [p1], linkmap[p2])
else:
c = [score, set(link)]
linkmap[p1] = c
linkmap[p2] = c
links_out.append((score ** inv_power, link))
links_out.sort(reverse=True)
return links_out
def scale_array01(x):
hi = np.amax(x)
lo = np.amin(x)
if lo == hi:
return np.zeros_like(x)
return (x - lo) / (hi - lo)
def cluster_aware_matrix(data, names, power):
links = array_to_link_pairs_cluster_aware(data, names, power)
cdata = links_to_matrix(links, names)
return cdata
def links_to_matrix(links, names):
"""make a symmetric array from a set of links."""
n = len(names)
doc_indices = {d: i for i, d in enumerate(names)}
a = np.zeros((n, n))
for s, p in links:
d1, d2 = p
x = doc_indices[d1]
y = doc_indices[d2]
a[x, y] = s
a[y, x] = s
return a
def find_text_lengths(names, dirname):
"""For when the text length hasn't been saved"""
files = os.listdir(dirname)
if sorted(files) != sorted(names):
print set(files) - set(names)
print set(names) - set(files)
raise ValueError("the files don't match")
lengths = []
for fn in names:
s = os.stat('%s/%s' % (dirname, fn)).st_size
lengths.append(s)
return lengths
def text_length_penalty(data, lengths):
"""reduce the score of short texts and their models (towards zero),
because they can't be certain about anything.
"""
penalty = [1.0 - 1.0 / math.log(s) for s in lengths]
p = np.array(penalty)
p2 = p.reshape(p.shape + (1,))
data *= p
data *= p2
return data