-
Notifications
You must be signed in to change notification settings - Fork 1
/
binarytree.py
executable file
·157 lines (117 loc) · 4.29 KB
/
binarytree.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename: support.py
# Shell and config manipulation
# Author: Pedro H. A. Hasselmann
# Escrevendo em python3 e usando python2.6:
from __future__ import print_function, unicode_literals, absolute_import, division
from numpy import array, arange, concatenate, all, insert, vectorize, vstack
from collections import deque
from itertools import izip
# Much faster look up than with lists, for larger lists
def filtering(X, crt, i):
criteria = set(crt)
@vectorize
def verify(elem): return elem not in criteria
return X[verify(X[:,i])]
def select(X, crt, i):
criteria = set(crt)
@vectorize
def verify(elem): return elem in criteria
return X[verify(X[:,i])]
class BTree:
''' Convert scipy Linkage matrix to Binary Tree for easily cutting nodes'''
def __init__(self, D):
self.Y = self.linkage(D)
self.tree, self.height = self.tree()
#self.order = self.ordering_by_size()
#print(self.Y)
def linkage(self, D):
''' linkage matrix with derivative node n+i'''
import scipy.cluster.hierarchy as sch
Y = sch.median(D)
#Z = sch.to_tree(Y, rd=True)
N = D.shape[0]
Y = concatenate((Y, arange(N,2*N-1).reshape((Y.shape[0], 1))), 1)
return Y
def tree(self):
''' Get all leaves over a group of nodes.
Nodes are dictionaries inside lists.'''
Y = self.Y
N = Y.shape[0]
def get_sequence(node, bseq, height, n=0):
''' Recursively kernel.'''
# b = {a:[{c:[]},{d:[]}], b:[{e:[]},{f:[]}]}
if node > N:
n += 1
index = node - N -1
if n not in height: height[n] = deque()
bseq[Y[index][-1]] = deque()
for branch in [0,1]:
if Y[index][branch] > N:
bseq[Y[index][-1]].append({Y[index][branch]:None})
height[n].append(Y[index][branch])
get_sequence(Y[index][branch], bseq[Y[index][-1]][branch], height, n)
if Y[index][branch] < N:
bseq[Y[index][-1]].append(Y[index][branch])
n -= 1
t = {}
h = {0: Y[-1][-1]}
get_sequence(Y[-1][-1], t, h)
return t, h
def ordering_by_size(self):
''' Dictionary of nodes by order'''
Y = self.Y
sizes = iter(sorted(set(Y[:, -2])))
order = dict()
# Pairs' nodes:
n = 0
order[n] = list(Y[Y[:,-2] == 2e0][:, -1])
s = sizes.next()
# Nodes of higher order, where order is defined by total number of leaves:
while True:
try:
#print(n, s)
s = sizes.next()
n += 1
order[n] = list(Y[Y[:,-2] == s][:,-1])
Y = filtering(Y, order[n], -1)
#print(order[n].size)
except StopIteration:
break
return order
def cutting(self, branches):
''' Get all leaves over a group of nodes.'''
Y = self.Y
N = Y.shape[0]
#opp = {1:0,0:1}
def get_leaves(node, leaves):
''' Recursively kernel.'''
if node > N:
index = node - N -1
for branch in [0,1]:
if Y[index][branch] > N:
get_leaves(Y[index][branch], leaves)
if Y[index][branch] < N:
leaves.append(int(Y[index][branch]))
#get_leaves(Y[index][opp[branch]], leaves)
##################################################
leaves = dict()
for node in branches:
leaves[node] = deque()
get_leaves(node, leaves[node])
leaves[node] = list(set(leaves[node]))
return leaves
def plot(self, group, stats, x = [0.36, 0.47, 0.62, 0.75, 0.89]):
''' Plot all leaves over a node'''
import matplotlib.pyplot as plt
Y = array(map(lambda x: x[0], stats))
e = array(map(lambda x: x[1], stats))
Y = insert(Y, 1, 1e0, axis=1)[group]
e = insert(e, 1, 0e0, axis=1)[group]
[plt.errorbar(x, y[0], yerr=y[1], color='k', fmt='o-') for y in izip(Y,e)]
plt.show()
if __name__ == "__main__":
from file_module import unpickle
test = "q1.2_u0.7_m0.3_MOC3qnum"
bt = BTree(unpickle(test,"D2"))