-
Notifications
You must be signed in to change notification settings - Fork 0
/
HMRF.py
197 lines (123 loc) · 3.56 KB
/
HMRF.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
#!/usr/bin/python
import sys
import incUtils as utils
import random
import numpy as np
from scipy.spatial import distance as Distance
class HMRF:
def __init__(self, distance, aggregate, K = None):
self.distance = distance
self.aggregate = aggregate
self.K = K
self.representatives = None
self.clusters = None
self.maxDist = 0
#Assume M and C are tuples of indexes into data
def cluster(self, data, M, C, neighborhood=None):
self.initialize(data,M,C, neighborhood)
for i in range(100):
self.Estep(data,M,C)
self.Mstep(data,M,C)
return self.clusters
def initialize(self, data, M, C, neighborhood=None):
if(neighborhood == None):
neighborhood, M, C = self.getNeighborhoods(M,C)
'''
print
print "Merge:", sorted(map(sorted,M))
'''
print
print "Neighborhood: (%d)" % (len(neighborhood)), sorted(map(sorted,neighborhood))
n = map(lambda a: map(lambda x: data[x], a), neighborhood)
#print n
centroids = map(self.aggregate,n)
#print centroids
#print self.representatives
L = len(neighborhood)
if(self.K == None or L <= self.K):
self.representatives = centroids
self.clusters = neighborhood
#TODO: Handle K being different from L
return neighborhood
def getNeighborhoods(self, M,C):
sets = map(set, M)
changed = True
while changed:
changed = False
for i,x in enumerate(sets):
for j,y in enumerate(sets):
if i >= j:
continue
if(not x.isdisjoint(y)):
x.update(y)
del sets[j] #double check this. If j doesnt get updated correctly, will cause problem
changed = True
neighborhood = []
for s in sets:
f = list(s)
neighborhood.append(f)
return neighborhood, M, C
#update cluster membership
def Estep(self, data, M, C):
#x and u are indexes
def obj(x,u):
value = self.distance(data[x],self.representatives[u])
for i,j in M:
if x != i and x != j:
continue
y = j
if x == j:
y = i
indicator = 1
if y in self.clusters[u]:
indicator = 0
d = self.distance(data[x],data[y])
self.maxDist = max(self.maxDist, d)
value += d * indicator
for i,j in C:
if x != i and x != j:
continue
y = j
if x == j:
y = i
indicator = 0
if y in self.clusters[u]:
indicator = 1
d = self.distance(data[x],data[y])
self.maxDist = max(self.maxDist, d)
value += (self.maxDist - d) * indicator
return value
unchanged = True
while (unchanged):
unchanged = False
order = range(len(data))
random.shuffle(order)
for o in order:
old = None
for i,c in enumerate(self.clusters):
if o in c:
old = i
c.remove(o)
break
idxs = range(len(self.clusters))
objectives = map(lambda i: obj(o,i), idxs)
new = utils.arg_min(objectives)
self.clusters[new].append(o)
unchanged |= (old != new)
#update cluster representatives
def Mstep(self, data, M,C):
c = map(lambda x: map(lambda i: data[i], x), self.clusters)
centroids = map(self.aggregate, c)
self.representatives = centroids
#TODO: Handle Parameterized distances
def main(args):
data = [[1,0],[0,1],[.2,.3],[.6,.2],[.8,.2],[.5,.5],[.2,.7],[.5,.9],[.2,1]]
M = [(0,4), (1,7), (6,7), (2,5)]
#M = [(0,4), (1,5), (4,5)]
C = [(0,1), (4,8), (3,6)]
data = np.array(data)
aggr = lambda x: np.mean(x, axis=0)
hmrf = HMRF(Distance.euclidean, aggr)
print hmrf.cluster(data, M, C)
if __name__ == "__main__":
main(sys.argv)