-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_tree2.py
More file actions
53 lines (36 loc) · 1.36 KB
/
local_tree2.py
File metadata and controls
53 lines (36 loc) · 1.36 KB
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
import numpy as np
import tree
import bintree_cut
import collections
def make_tree(affinity):
#start with initial affinity
#threshold initial affinity
#normalize to MC
#apply MC to each point.
#construct new affinity on
return bintree_cut.make_markov(affinity)
def threshold(affinity,threshold):
aff = affinity.copy()
aff[affinity<threshold] = 0.0
return aff
def bfs(affinity, start):
queue, enqueued = collections.deque([(None, start)]), set([start])
while queue:
parent, n = queue.popleft()
yield parent, n
new = set(np.where(affinity[n,:]>0)[0].tolist()) - enqueued
enqueued |= new
queue.extend([(n, child) for child in new])
def find_components(affinity,thres):
t_affinity = threshold(affinity,thres)
t_affinity[t_affinity > 0] = 1.0
assign_components = np.zeros(np.shape(t_affinity)[0], np.int)
component_no = 1
while len(np.where(assign_components == 0)[0]) > 0:
init_pos = np.where(assign_components == 0)[0][0]
component = [x[1] for x in bfs(t_affinity,init_pos)]
#_proc_step(t_affinity,init_pos,component)
assign_components[component] = component_no
component_no += 1
#print thres, -np.sort(-np.bincount(assign_components))[0:3]
return assign_components