forked from aturanjanin/pvclust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvclust.py
314 lines (263 loc) · 12.3 KB
/
pvclust.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
The original algorithm is implemented in R by Suzuki and Shimodira (2006):
Pvclust: an R package for assessing the uncertanity in hierarchical
clustering. This is its Python reimplementation. The final values produced are
Approximately Unbiased p-value (AU) and Bootstrap Probability (BP) which
are reporting the significance of each cluster in clustering structure.
The AU value is less biased and clusters that have this value greater than
95% are considered significant.
Both values are calculated using Multiscale Bootstrap Resampling.
"""
from math import sqrt
from multiprocessing import Pool, cpu_count
import pandas as pd
import numpy as np
from scipy.stats import norm, chi2
from scipy.cluster.hierarchy import (dendrogram, set_link_color_palette,
leaves_list, to_tree, linkage)
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
class PvClust:
""" Calcuclate AU and BP probabilities for each cluster of the data."""
def __init__(self, data, method='ward', metric='euclidean',
nboot=1000, r=np.array(range(5, 15)), parallel=False):
"""Parameters:
- data : DataFrame
a dataset to which clustering and sampling are applied
- method: a linkage method used in hierarchical clustering
- metric: a distance metric used in hierarchical clustering
- nboot: a number of bootstrap samples
- r: an array of scaling constants
- parallel: boolean value stating should the algorithm run in
parallel
:returns Approximately Unbiased p-value and Bootstrap Probability for
every dendrogram node.
:rtype dict
"""
self.data = data
self.nboot = nboot # number of bootstrap replicates
self.parallel = parallel
self.n = len(self.data)
r = np.array([i/10 for i in r])
self.n_scaled = np.unique([int(i*self.n) for i in r])
self.r = np.unique([i/self.n for i in self.n_scaled])
# apply hierarchical clustering and get clusters
self.metric, self.method = metric, method
hc = HierarchicalClusteringClusters(data.transpose(), method, metric)
self.linkage_matrix = hc.linkage_matrix
self.clusters = hc.find_clusters()
self._result = self._result()
self.result = self._result
def _hc(self, n):
""" Do bootstrap sampling and then apply hierarchical clustering
to the sample """
data = self.data
# we are sampling instances
data = data.sample(n, replace=True, axis=0)
# HC is applied to columns each time (hence transposing)
temp = HierarchicalClusteringClusters(data.transpose(), self.method,
self.metric)
clusters = temp.find_clusters()
return clusters
def _nbootstrap_probability(self, n):
""" Calculate bootstrap probability of each cluster for the dataset of
size n """
# dictionary for counting repetitions of the same clusters throughout
# nboot different clusterings
repetitions = {i: 0 for i in range(len(self.clusters))}
# do HC nboot times for dataset of size n
for _ in range(self.nboot):
sample_clusters = self._hc(n)
# compare obtained clusters with the main ones and
# update repetitions if necessary
for cluster in sample_clusters:
if cluster in self.clusters:
repetitions[self.clusters.index(cluster)] += 1
# calculate BP probability for each cluster for the sample of size n
BP = [repetitions[k]/self.nboot for k in repetitions.keys()]
return np.array(BP)
def _table(self):
""" Make a table of bootstrap probabilities for each sample size"""
# for each sample size in n_scaled calculate BPs of all clusters and
# add it to the table
if self.parallel:
print(f"Calculating using {cpu_count()} cores... ", end="")
with Pool() as pool:
probabilities = pool.map(self._nbootstrap_probability,
self.n_scaled)
table = probabilities[0]
for i in probabilities[1::]:
table = np.column_stack((table, i))
print("Done.")
else:
table = np.empty([len(self.data.transpose())-1, 1])
for i in self.n_scaled:
print(f"Boostrap (r = {round(i/ self.n, 2)}) ... ", end="")
temp = self._nbootstrap_probability(i)
table = np.column_stack((table, temp))
print("Done.")
table = np.delete(table, 0, 1)
return table
def _wls_fit(self):
""" Take all calculated bootstrap probabilities of a single cluster and
fit a curve to it in order to calculate AU and BP for that cluster"""
nboot, r = self.nboot, self.r
r_sq_org = np.array([sqrt(j) for j in r])
r_isq_org = np.array([1/sqrt(j) for j in r])
eps = 0.001
table = self._table()
result = {}
for i in range(len(table)):
BP = table[i]
nboot_list = np.repeat(nboot, len(BP))
use = np.logical_and(np.greater(BP, eps), np.less(BP, 1-eps))
if sum(use) < 3:
au_bp = np.array([0, 0]) if np.mean(BP) < 0.5 else \
np.array([1, 1])
result[i] = np.concatenate((au_bp, np.array([0, 0, 0, 0, 0])))
else:
BP = BP[use]
r_sq = r_sq_org[use]
r_isq = r_isq_org[use]
nboot_list = nboot_list[use]
y = -norm.ppf(BP)
X_model = np.array([[i, j] for i, j in zip(r_sq, r_isq)])
weight = ((1-BP)*BP)/((norm.pdf(y)**2)*nboot_list)
model = LinearRegression(fit_intercept=False)
results_lr = model.fit(X_model, y, sample_weight=1/weight)
z_au = np.array([1, -1])@results_lr.coef_
z_bp = np.array([1, 1])@results_lr.coef_
# AU and BP
au_bp = np.array([1-norm.cdf(z_au), 1-norm.cdf(z_bp)])
Xw = [i/j for i, j in zip(X_model, weight)]
temp = X_model.transpose()@Xw
V = np.linalg.solve(temp, np.identity(len(temp)))
vz_au = np.array([1, -1])@[email protected]([1, -1])
vz_bp = np.array([1, 1])@[email protected]([1, 1])
# estimted standard errors for p-values
se_au = norm.pdf(z_au)*sqrt(vz_au)
se_bp = norm.pdf(z_bp)*sqrt(vz_bp)
# residual sum of squares
y_pred = results_lr.predict(X_model)
rss = sum((y - y_pred)**2/weight)
df = sum(use) - 2 # degrees of freedom
pchi = 1 - chi2.cdf(rss, df) if (df > 0) else 1.0
result[i] = np.concatenate(
(au_bp, np.array([se_au, se_bp, pchi]), results_lr.coef_))
return result
def _result(self):
# calculate AU and BP values
result = pd.DataFrame.from_dict(
self._wls_fit(), orient="index",
columns=['AU', 'BP', 'SE.AU', 'SE.BP', 'pchi', 'v', 'c'])
return result
def plot(self, labels=None):
"""Plot dendrogram with AU BP values for each node"""
plot_dendrogram(self.linkage_matrix,
np.array(self.result[['AU', 'BP']]), labels)
def seplot(self, pvalue='AU', annotate=False):
"""p-values vs Standard error plot"""
x = self.result['AU'] if pvalue == 'AU' else self.result['BP']
y = self. result['SE.AU'] if pvalue == 'AU' else self.result['SE.BP']
clusters = []
plt.scatter(x, y, facecolors='none', edgecolors='r')
plt.title("p-value vs Standard Error plot")
plt.xlabel(pvalue + " p-value")
plt.ylabel("Standard Error")
if annotate:
for i in range(len(y)):
if y[i] > 0.6:
plt.text(x[i], y[i], f"{i}")
clusters.append(i)
plt.show()
if clusters:
return clusters
def print_result(self, which=[], digits=3):
"""Print only desired clusters and/or print values to the desired
decimal point"""
print(" Clustering method:", self.method, "\n", "Distance metric:",
self.metric, "\n", "Number of replicates:", self.nboot, "\n")
results = round(self._result, digits)
if not which:
print(results)
else:
print(results.iloc[which, ])
class HierarchicalClusteringClusters:
"""Apply Hierarhical Clustering on the data and find elements of
each cluster"""
def __init__(self, data, method='ward', metric='euclidean'):
self.linkage_matrix = linkage(data, method, metric)
self.nodes = to_tree(self.linkage_matrix, rd=True)[1]
def l_branch(self, left, node, nodes):
if not node.is_leaf():
if node.left.id > (len(nodes)-1)/2:
self.l_branch(left, nodes[node.left.id], nodes)
self.r_branch(left, nodes[node.left.id], nodes)
else:
left.append(node.left.id)
else:
left.append(node.id)
return list(set(left))
def r_branch(self, right, node, nodes):
if not node.is_leaf():
if node.right.id > (len(nodes)-1)/2:
self.r_branch(right, nodes[node.right.id], nodes)
self.l_branch(right, nodes[node.right.id], nodes)
else:
right.append(node.right.id)
else:
right.append(node.id)
return list(set(right))
# find all clusters produced by HC from leaves to the root node
def find_clusters(self):
nodes = self.nodes
clusters = []
for i in range(len(leaves_list(self.linkage_matrix)), len(nodes)):
left = self.l_branch([], nodes[i], nodes)
right = self.r_branch([], nodes[i], nodes)
node_i = sorted(set(left + right))
if node_i:
clusters.append(node_i)
return clusters
def plot_dendrogram(linkage_matrix, pvalues, labels=None):
""" Plot dendrogram with AU BP values for each node"""
d = dendrogram(linkage_matrix, no_plot=True)
xcoord = d["icoord"]
ycoord = d["dcoord"]
# Obtaining the coordinates of all nodes above leaves
x = {i: (j[1]+j[2])/2 for i, j in enumerate(xcoord)}
y = {i: j[1] for i, j in enumerate(ycoord)}
pos = node_positions(y, x)
plt.figure(figsize=(12, 10))
plt.tight_layout()
set_link_color_palette(['c', 'g'])
d = dendrogram(linkage_matrix, labels=labels, above_threshold_color='c',
color_threshold=0.1)
ax = plt.gca()
maxval = max(y.values())
for node, (x, y) in pos.items():
if node == (len(pos.items())-1):
ax.text(x-6, y+maxval/200, 'AU', fontsize=11, fontweight='bold',
color='purple')
ax.text(x+1, y+maxval/200, 'BP', fontsize=11, fontweight='bold',
color='black')
else:
if pvalues[node][0]*100 == 100:
ax.text(x-5, y+maxval/200, f' {pvalues[node][0]*100:.0f}', fontsize=8,
color='purple', fontweight='bold')
ax.text(x+1, y+maxval/200, f'{pvalues[node][1]*100:.0f}', fontsize=8,
color='black', fontweight='bold')
else:
ax.text(x-5, y+maxval/200, f' {pvalues[node][0]*100:.0f}', fontsize=8,
color='purple')
ax.text(x+1, y+maxval/200, f'{pvalues[node][1]*100:.0f}', fontsize=8,
color='black')
# plt.savefig('dendrogram.pdf')
def node_positions(x, y):
positions = {**x, **y}
for key, value in positions.items():
if key in x and key in y:
positions[key] = (value, x[key])
positions = sorted(positions.items(), key=lambda x: x[1][1])
pos = {i: positions[i][1] for i in range(len(positions))}
return pos