-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathclustering.py
More file actions
1628 lines (1305 loc) · 62 KB
/
clustering.py
File metadata and controls
1628 lines (1305 loc) · 62 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# vim:ts=4:sw=4:sts=4:et
# -*- coding: utf-8 -*-
"""Classes related to graph clustering."""
from copy import deepcopy
from io import StringIO
from igraph._igraph import GraphBase, community_to_membership, _compare_communities
from igraph.configuration import Configuration
from igraph.datatypes import UniqueIdGenerator
from igraph.drawing.colors import ClusterColoringPalette
from igraph.drawing.cairo.dendrogram import CairoDendrogramDrawer
from igraph.drawing.matplotlib.dendrogram import MatplotlibDendrogramDrawer
from igraph.statistics import Histogram
from igraph.summary import _get_wrapper_for_width
from igraph.utils import deprecated
class Clustering:
"""Class representing a clustering of an arbitrary ordered set.
This is now used as a base for L{VertexClustering}, but it might be
useful for other purposes as well.
Members of an individual cluster can be accessed by the C{[]} operator:
>>> cl = Clustering([0,0,0,0,1,1,1,2,2,2,2])
>>> cl[0]
[0, 1, 2, 3]
The membership vector can be accessed by the C{membership} property:
>>> cl.membership
[0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2]
The number of clusters can be retrieved by the C{len} function:
>>> len(cl)
3
You can iterate over the clustering object as if it were a regular list
of clusters:
>>> for cluster in cl:
... print(" ".join(str(idx) for idx in cluster))
...
0 1 2 3
4 5 6
7 8 9 10
If you need all the clusters at once as lists, you can simply convert
the clustering object to a list:
>>> cluster_list = list(cl)
>>> print(cluster_list)
[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
"""
def __init__(self, membership, params=None):
"""Constructor.
@param membership: the membership list -- that is, the cluster
index in which each element of the set belongs to.
@param params: additional parameters to be stored in this
object's dictionary.
"""
self._membership = list(membership)
if len(self._membership) > 0:
self._len = max(m for m in self._membership if m is not None) + 1
else:
self._len = 0
if params:
self.__dict__.update(params)
def __getitem__(self, idx):
"""Returns the members of the specified cluster.
@param idx: the index of the cluster
@return: the members of the specified cluster as a list
@raise IndexError: if the index is out of bounds"""
if idx < 0 or idx >= self._len:
raise IndexError("cluster index out of range")
return [i for i, e in enumerate(self._membership) if e == idx]
def __iter__(self):
"""Iterates over the clusters in this clustering.
This method will return a generator that generates the clusters
one by one."""
clusters = [[] for _ in range(self._len)]
for idx, cluster in enumerate(self._membership):
clusters[cluster].append(idx)
return iter(clusters)
def __len__(self):
"""Returns the number of clusters.
@return: the number of clusters
"""
return self._len
def __str__(self):
return self.summary(verbosity=1, width=78)
def as_cover(self):
"""Returns a L{Cover} that contains the same clusters as this clustering."""
return Cover(self._graph, self)
def compare_to(self, other, *args, **kwds):
"""Compares this clustering to another one using some similarity or
distance metric.
This is a convenience method that simply calls L{compare_communities}
with the two clusterings as arguments. Any extra positional or keyword
argument is also forwarded to L{compare_communities}."""
return compare_communities(self, other, *args, **kwds)
@property
def membership(self):
"""Returns the membership vector."""
return self._membership[:]
@property
def n(self):
"""Returns the number of elements covered by this clustering."""
return len(self._membership)
def size(self, idx):
"""Returns the size of a given cluster.
@param idx: the cluster in which we are interested.
"""
return len(self[idx])
def sizes(self, *args):
"""Returns the size of given clusters.
The indices are given as positional arguments. If there are no
positional arguments, the function will return the sizes of all clusters.
"""
counts = [0] * len(self)
for x in self._membership:
if x is not None:
counts[x] += 1
if args:
return [counts[idx] for idx in args]
return counts
def size_histogram(self, bin_width=1):
"""Returns the histogram of cluster sizes.
@param bin_width: the bin width of the histogram
@return: a L{Histogram} object
"""
return Histogram(bin_width, self.sizes())
def summary(self, verbosity=0, width=None):
"""Returns the summary of the clustering.
The summary includes the number of items and clusters, and also the
list of members for each of the clusters if the verbosity is nonzero.
@param verbosity: determines whether the cluster members should be
printed. Zero verbosity prints the number of items and clusters only.
@return: the summary of the clustering as a string.
"""
out = StringIO()
print(
"Clustering with %d elements and %d clusters"
% (
len(self._membership),
len(self),
),
file=out,
)
if verbosity < 1:
return out.getvalue().strip()
ndigits = len(str(len(self)))
wrapper = _get_wrapper_for_width(width, subsequent_indent=" " * (ndigits + 3))
for idx, cluster in enumerate(self._formatted_cluster_iterator()):
wrapper.initial_indent = "[%*d] " % (ndigits, idx)
print("\n".join(wrapper.wrap(cluster)), file=out)
return out.getvalue().strip()
def _formatted_cluster_iterator(self):
"""Iterates over the clusters and formats them into a string to be
presented in the summary."""
for cluster in self:
yield ", ".join(str(member) for member in cluster)
class VertexClustering(Clustering):
"""The clustering of the vertex set of a graph.
This class extends L{Clustering} by linking it to a specific L{Graph} object
and by optionally storing the modularity score of the clustering.
It also provides some handy methods like getting the subgraph corresponding
to a cluster and such.
@note: since this class is linked to a L{Graph}, destroying the graph by the
C{del} operator does not free the memory occupied by the graph if there
exists a L{VertexClustering} that references the L{Graph}.
"""
# Allow None to be passed to __plot__ as the "palette" keyword argument
_default_palette = None
def __init__(
self,
graph,
membership=None,
modularity=None,
params=None,
modularity_params=None,
):
"""Creates a clustering object for a given graph.
@param graph: the graph that will be associated to the clustering
@param membership: the membership list. The length of the list must
be equal to the number of vertices in the graph. If C{None}, every
vertex is assumed to belong to the same cluster.
@param modularity: the modularity score of the clustering. If C{None},
it will be calculated when needed.
@param params: additional parameters to be stored in this object.
@param modularity_params: arguments that should be passed to
L{Graph.modularity} when the modularity is (re)calculated. If the
original graph was weighted, you should pass a dictionary
containing a C{weight} key with the appropriate value here.
"""
if membership is None:
super().__init__([0] * graph.vcount(), params)
else:
if len(membership) != graph.vcount():
raise ValueError("membership list has invalid length")
super().__init__(membership, params)
self._graph = graph
self._modularity = modularity
self._modularity_dirty = modularity is None
if modularity_params is None:
self._modularity_params = {}
else:
self._modularity_params = dict(modularity_params)
@classmethod
def FromAttribute(cls, graph, attribute, intervals=None, params=None):
"""Creates a vertex clustering based on the value of a vertex attribute.
Vertices having the same attribute will correspond to the same cluster.
@param graph: the graph on which we are working
@param attribute: name of the attribute on which the clustering
is based.
@param intervals: for numeric attributes, you can either pass a single
number or a list of numbers here. A single number means that the
vertices will be put in bins of that width and vertices ending up
in the same bin will be in the same cluster. A list of numbers
specify the bin positions explicitly; e.g., C{[10, 20, 30]} means
that there will be four categories: vertices with the attribute
value less than 10, between 10 and 20, between 20 and 30 and over 30.
Intervals are closed from the left and open from the right.
@param params: additional parameters to be stored in this object.
@return: a new VertexClustering object
"""
from bisect import bisect
def safeintdiv(x, y):
"""Safe integer division that handles None gracefully"""
if x is None:
return None
return int(x / y)
def safebisect(intervals, x):
"""Safe list bisection that handles None gracefully"""
if x is None:
return None
return bisect(intervals, x)
try:
_ = iter(intervals)
iterable = True
except TypeError:
iterable = False
if intervals is None:
vec = graph.vs[attribute]
elif iterable:
intervals = list(intervals)
vec = [safebisect(intervals, x) for x in graph.vs[attribute]]
else:
intervals = float(intervals)
vec = [safeintdiv(x, intervals) for x in graph.vs[attribute]]
idgen = UniqueIdGenerator()
idgen[None] = None
vec = [idgen[i] for i in vec]
return cls(graph, vec, None, params)
def as_cover(self):
"""Returns a L{VertexCover} that contains the same clusters as this
clustering."""
return VertexCover(self._graph, self)
def cluster_graph(self, combine_vertices=None, combine_edges=None):
"""Returns a graph where each cluster is contracted into a single
vertex.
In the resulting graph, vertex M{i} represents cluster M{i} in this
clustering. Vertex M{i} and M{j} will be connected if there was
at least one connected vertex pair M{(a, b)} in the original graph such
that vertex M{a} was in cluster M{i} and vertex M{b} was in cluster
M{j}.
@param combine_vertices: specifies how to derive the attributes of
the vertices in the new graph from the attributes of the old ones.
See L{Graph.contract_vertices()<igraph._igraph.GraphBase.contract_vertices>}
for more details.
@param combine_edges: specifies how to derive the attributes of the
edges in the new graph from the attributes of the old ones. See
L{Graph.simplify()<igraph._igraph.GraphBase.simplify>} for more details.
If you specify C{False} here, edges will not be combined, and the
number of edges between the vertices representing the original
clusters will be equal to the number of edges between the members of
those clusters in the original graph.
@return: the new graph.
"""
result = self.graph.copy()
result.contract_vertices(self.membership, combine_vertices)
if combine_edges is not False:
result.simplify(combine_edges=combine_edges)
return result
def crossing(self):
"""Returns a boolean vector where element M{i} is C{True} iff edge
M{i} lies between clusters, C{False} otherwise."""
membership = self.membership
return [
membership[v1] != membership[v2] for v1, v2 in self.graph.get_edgelist()
]
@property
def modularity(self):
"""Returns the modularity score"""
if self._modularity_dirty:
return self._recalculate_modularity_safe()
return self._modularity
q = modularity
@property
def graph(self):
"""Returns the graph belonging to this object"""
return self._graph
def recalculate_modularity(self):
"""Recalculates the stored modularity value.
This method must be called before querying the modularity score of the
clustering through the class member C{modularity} or C{q} if the
graph has been modified (edges have been added or removed) since the
creation of the L{VertexClustering} object.
@return: the new modularity score
"""
self._modularity = self._graph.modularity(
self._membership, **self._modularity_params
)
self._modularity_dirty = False
return self._modularity
def _recalculate_modularity_safe(self):
"""Recalculates the stored modularity value and swallows all exceptions
raised by the modularity function (if any).
@return: the new modularity score or C{None} if the modularity function
could not be calculated.
"""
try:
return self.recalculate_modularity()
except Exception:
return None
finally:
self._modularity_dirty = False
def subgraph(self, idx):
"""Get the subgraph belonging to a given cluster.
Precondition: the vertex set of the graph hasn't been modified since the
moment the cover was constructed.
@param idx: the cluster index
@return: a copy of the subgraph
"""
return self._graph.subgraph(self[idx])
def subgraphs(self):
"""Gets all the subgraphs belonging to each of the clusters.
Precondition: the vertex set of the graph hasn't been modified since the
moment the cover was constructed.
@return: a list containing copies of the subgraphs
"""
return [self._graph.subgraph(cl) for cl in self]
def giant(self):
"""Returns the largest cluster of the clustered graph.
The largest cluster is a cluster for which no larger cluster exists in
the clustering. It may also be known as the I{giant community} if the
clustering represents the result of a community detection function.
Precondition: the vertex set of the graph hasn't been modified since the
moment the cover was constructed.
@note: there can be multiple largest clusters, this method will return
the copy of an arbitrary one if there are multiple largest clusters.
@return: a copy of the largest cluster.
"""
ss = self.sizes()
if ss:
max_size = max(ss)
return self.subgraph(ss.index(max_size))
else:
return self._graph.copy()
def __plot__(self, backend, context, *args, **kwds):
"""Plots the clustering to the given Cairo context or matplotlib Axes.
This is done by calling L{Graph.__plot__()} with the same arguments, but
coloring the graph vertices according to the current clustering (unless
overridden by the C{vertex_color} argument explicitly).
This method understands all the positional and keyword arguments that
are understood by L{Graph.__plot__()}, only the differences will be
highlighted here:
- C{mark_groups}: whether to highlight some of the vertex groups by
colored polygons. Besides the values accepted by L{Graph.__plot__}
(i.e., a dict mapping colors to vertex indices, a list containing
lists of vertex indices, or C{False}), the following are also
accepted:
- C{True}: all the groups will be highlighted, the colors matching
the corresponding color indices from the current palette
(see the C{palette} keyword argument of L{Graph.__plot__}).
- A dict mapping cluster indices or tuples of vertex indices to
color names. The given clusters or vertex groups will be
highlighted by the given colors.
- A list of cluster indices. This is equivalent to passing a
dict mapping numeric color indices from the current palette
to cluster indices; therefore, the cluster referred to by element
I{i} of the list will be highlighted by color I{i} from the
palette.
The value of the C{plotting.mark_groups} configuration key is also
taken into account here; if that configuration key is C{True} and
C{mark_groups} is not given explicitly, it will automatically be set
to C{True}.
In place of lists of vertex indices, you may also use L{VertexSeq}
instances.
In place of color names, you may also use color indices into the
current palette. C{None} as a color name will mean that the
corresponding group is ignored.
- C{palette}: the palette used to resolve numeric color indices to RGBA
values. By default, this is an instance of L{ClusterColoringPalette}.
@see: L{Graph.__plot__()} for more supported keyword arguments.
"""
from igraph.drawing.colors import default_edge_colors
if "edge_color" not in kwds and "color" not in self.graph.edge_attributes():
# Set up a default edge coloring based on internal vs external edges
colors = default_edge_colors[backend]
kwds["edge_color"] = [
colors[is_crossing] for is_crossing in self.crossing()
]
palette = kwds.get("palette", None)
if palette is None:
kwds["palette"] = ClusterColoringPalette(len(self))
if "mark_groups" not in kwds:
if Configuration.instance()["plotting.mark_groups"]:
kwds["mark_groups"] = self
else:
kwds["mark_groups"] = _handle_mark_groups_arg_for_clustering(
kwds["mark_groups"], self
)
if "vertex_color" not in kwds:
kwds["vertex_color"] = self.membership
result = self._graph.__plot__(backend, context, *args, **kwds)
return result
def _formatted_cluster_iterator(self):
"""Iterates over the clusters and formats them into a string to be
presented in the summary."""
if self._graph.is_named():
names = self._graph.vs["name"]
for cluster in self:
yield ", ".join(str(names[member]) for member in cluster)
else:
for cluster in self:
yield ", ".join(str(member) for member in cluster)
###############################################################################
class Dendrogram:
"""The hierarchical clustering (dendrogram) of some dataset.
A hierarchical clustering means that we know not only the way the
elements are separated into groups, but also the exact history of
how individual elements were joined into larger subgroups.
This class internally represents the hierarchy by a matrix with n rows
and 2 columns -- or more precisely, a list of lists of size 2. This is
exactly the same as the original format used by C{igraph}'s C core.
The M{i}th row of the matrix contains the indices of the two clusters
being joined in time step M{i}. The joint group will be represented by
the ID M{n+i}, with M{i} starting from one. The ID of the joint group
will be referenced in the upcoming steps instead of any of its individual
members. So, IDs less than or equal to M{n} (where M{n} is the number of
rows in the matrix) mean the original members of the dataset (with ID
from 0 to M{n}), while IDs up from M{n+1} mean joint groups. As an
example, take a look at the dendrogram and the internal representation of
a given clustering of five nodes::
0 -+
|
1 -+-+
|
2 ---+-+ <====> [[0, 1], [3, 4], [2, 5], [6, 7]]
|
3 -+ |
| |
4 -+---+---
"""
def __init__(self, merges):
"""Creates a hierarchical clustering.
@param merges: the merge history either in matrix or tuple format"""
self._merges = [tuple(pair) for pair in merges]
self._nmerges = len(self._merges)
if self._nmerges:
self._nitems = max(self._merges[-1]) - self._nmerges + 2
else:
self._nitems = 0
self._names = None
@staticmethod
def _convert_matrix_to_tuple_repr(merges, n=None):
"""Converts the matrix representation of a clustering to a tuple
representation.
@param merges: the matrix representation of the clustering
@return: the tuple representation of the clustering
"""
if n is None:
n = len(merges) + 1
tuple_repr = range(n)
idxs = list(range(n))
for rowidx, row in enumerate(merges):
i, j = row
try:
idxi, idxj = idxs[i], idxs[j]
tuple_repr[idxi] = (tuple_repr[idxi], tuple_repr[idxj])
tuple_repr[idxj] = None
except IndexError:
raise ValueError(
"malformed matrix, subgroup referenced "
+ "before being created in step %d" % rowidx
) from None
idxs.append(j)
return [x for x in tuple_repr if x is not None]
def _traverse_inorder(self):
"""Conducts an inorder traversal of the merge tree.
The inorder traversal returns the nodes on the last level in the order
they should be drawn so that no edges cross each other.
@return: the result of the inorder traversal in a list."""
result = []
seen_nodes = set()
for node_index in reversed(range(self._nitems + self._nmerges)):
if node_index in seen_nodes:
continue
stack = [node_index]
while stack:
last = stack.pop()
seen_nodes.add(last)
if last < self._nitems:
# 'last' is a regular node so the traversal ends here, we
# can append it to the results
result.append(last)
else:
# 'last' is a merge node, so let us proceed with the entry
# where this merge node was created
stack.extend(self._merges[last - self._nitems])
return result
def __str__(self):
return self.summary(verbosity=1)
def format(self, format="newick"):
"""Formats the dendrogram in a foreign format.
Currently only the Newick format is supported.
Example:
>>> d = Dendrogram([(2, 3), (0, 1), (4, 5)])
>>> d.format()
'((2,3)4,(0,1)5)6;'
>>> d.names = list("ABCDEFG")
>>> d.format()
'((C,D)E,(A,B)F)G;'
"""
if format == "newick":
n = self._nitems + self._nmerges
if self._names is None:
nodes = list(range(n))
else:
nodes = list(self._names)
if len(nodes) < n:
nodes.extend("" for _ in range(n - len(nodes)))
for k, (i, j) in enumerate(self._merges, self._nitems):
nodes[k] = "(%s,%s)%s" % (nodes[i], nodes[j], nodes[k])
nodes[i] = nodes[j] = None
return nodes[-1] + ";"
raise ValueError("unsupported format: %r" % format)
def summary(self, verbosity=0, max_leaf_count=40):
"""Returns the summary of the dendrogram.
The summary includes the number of leafs and branches, and also an
ASCII art representation of the dendrogram unless it is too large.
@param verbosity: determines whether the ASCII representation of the
dendrogram should be printed. Zero verbosity prints only the number
of leafs and branches.
@param max_leaf_count: the maximal number of leafs to print in the
ASCII representation. If the dendrogram has more leafs than this
limit, the ASCII representation will not be printed even if the
verbosity is larger than or equal to 1.
@return: the summary of the dendrogram as a string.
"""
out = StringIO()
print(
"Dendrogram, %d elements, %d merges"
% (
self._nitems,
self._nmerges,
),
file=out,
)
if self._nitems == 0 or verbosity < 1 or self._nitems > max_leaf_count:
return out.getvalue().strip()
print("", file=out)
positions = [None] * self._nitems
inorder = self._traverse_inorder()
distance = 2
level_distance = 2
nextp = 0
for idx, element in enumerate(inorder):
positions[element] = nextp
inorder[idx] = str(element)
nextp += max(distance, len(inorder[idx]) + 1)
width = max(positions) + 1
# Print the nodes on the lowest level
print((" " * (distance - 1)).join(inorder), file=out)
midx = 0
max_community_idx = self._nitems
while midx < self._nmerges:
char_array = [" "] * width
for position in positions:
if position >= 0:
char_array[position] = "|"
char_str = "".join(char_array)
for _ in range(level_distance - 1):
print(char_str, file=out) # Print the lines
cidx_incr = 0
while midx < self._nmerges:
id1, id2 = self._merges[midx]
if id1 >= max_community_idx or id2 >= max_community_idx:
break
midx += 1
pos1, pos2 = positions[id1], positions[id2]
positions[id1], positions[id2] = -1, -1
if pos1 > pos2:
pos1, pos2 = pos2, pos1
positions.append((pos1 + pos2) // 2)
dashes = "-" * (pos2 - pos1 - 1)
char_array[pos1 : (pos2 + 1)] = "`%s'" % dashes
cidx_incr += 1
max_community_idx += cidx_incr
print("".join(char_array), file=out)
return out.getvalue().strip()
def __plot__(self, backend, context, *args, **kwds):
"""Draws the dendrogram on the given Cairo context or matplotlib Axes.
Supported keyword arguments are:
- C{orientation}: the orientation of the dendrogram. Must be one of
the following values: C{left-right}, C{bottom-top}, C{right-left}
or C{top-bottom}. Individual elements are always placed at the
former edge and merges are performed towards the latter edge.
Possible aliases: C{horizontal} = C{left-right},
C{vertical} = C{bottom-top}, C{lr} = C{left-right},
C{rl} = C{right-left}, C{tb} = C{top-bottom}, C{bt} = C{bottom-top}.
The default is C{left-right}.
"""
if backend == "matplotlib":
drawer = MatplotlibDendrogramDrawer(context)
else:
bbox = kwds.pop("bbox", None)
palette = kwds.pop("palette", None)
if bbox is None:
raise ValueError("bbox is required for Cairo plots")
if palette is None:
raise ValueError("palette is required for Cairo plots")
drawer = CairoDendrogramDrawer(context, bbox, palette)
drawer.draw(self, **kwds)
@property
def merges(self):
"""Returns the performed merges in matrix format"""
return deepcopy(self._merges)
@property
def names(self):
"""Returns the names of the nodes in the dendrogram"""
return self._names
@names.setter
def names(self, items):
"""Sets the names of the nodes in the dendrogram"""
if items is None:
self._names = None
return
items = list(items)
if len(items) < self._nitems:
raise ValueError("must specify at least %d names" % self._nitems)
n = self._nitems + self._nmerges
self._names = items[:n]
if len(self._names) < n:
self._names.extend("" for _ in range(n - len(self._names)))
class VertexDendrogram(Dendrogram):
"""The dendrogram resulting from the hierarchical clustering of the
vertex set of a graph."""
def __init__(self, graph, merges, optimal_count=None, modularity_params=None):
"""Creates a dendrogram object for a given graph.
@param graph: the graph that will be associated to the clustering
@param merges: the merges performed given in matrix form.
@param optimal_count: the optimal number of clusters where the
dendrogram should be cut. This is a hint usually provided by the
clustering algorithm that produces the dendrogram. C{None} means
that such a hint is not available; the optimal count will then be
selected based on the modularity in such a case.
@param modularity_params: arguments that should be passed to
L{Graph.modularity} when the modularity is (re)calculated. If the
original graph was weighted, you should pass a dictionary
containing a C{weight} key with the appropriate value here.
"""
super().__init__(merges)
self._graph = graph
self._optimal_count = optimal_count
if modularity_params is None:
self._modularity_params = {}
else:
self._modularity_params = dict(modularity_params)
def as_clustering(self, n=None):
"""Cuts the dendrogram at the given level and returns a corresponding
L{VertexClustering} object.
@param n: the desired number of clusters. Merges are replayed from the
beginning until the membership vector has exactly M{n} distinct elements
or until there are no more recorded merges, whichever happens first.
If C{None}, the optimal count hint given by the clustering algorithm
will be used If the optimal count was not given either, it will be
calculated by selecting the level where the modularity is maximal.
@return: a new L{VertexClustering} object.
"""
if n is None:
n = self.optimal_count
num_elts = self._graph.vcount()
idgen = UniqueIdGenerator()
membership = community_to_membership(self._merges, num_elts, num_elts - n)
membership = [idgen[m] for m in membership]
return VertexClustering(
self._graph, membership, modularity_params=self._modularity_params
)
@property
def optimal_count(self):
"""Returns the optimal number of clusters for this dendrogram.
If an optimal count hint was given at construction time, this
property simply returns the hint. If such a count was not given,
this method calculates the optimal number of clusters by maximizing
the modularity along all the possible cuts in the dendrogram.
"""
if self._optimal_count is not None:
return self._optimal_count
n = self._graph.vcount()
if n == 0:
return 0
max_q, optimal_count = 0, n - len(self._merges)
for step in range(min(n - 1, len(self._merges) + 1)):
membs = community_to_membership(self._merges, n, step)
q = self._graph.modularity(membs, **self._modularity_params)
if q > max_q:
optimal_count = n - step
max_q = q
self._optimal_count = optimal_count
return optimal_count
@optimal_count.setter
def optimal_count(self, value):
self._optimal_count = max(int(value), 1)
def __plot__(self, backend, context, *args, **kwds):
"""Draws the vertex dendrogram on the given Cairo context or matplotlib Axes
See L{Dendrogram.__plot__} for the list of supported keyword
arguments."""
from igraph.drawing.metamagic import AttributeCollectorBase
class VisualVertexBuilder(AttributeCollectorBase):
_kwds_prefix = "vertex_"
label = None
builder = VisualVertexBuilder(self._graph.vs, kwds)
self._names = [vertex.label for vertex in builder]
self._names = [
name if name is not None else str(idx)
for idx, name in enumerate(self._names)
]
result = Dendrogram.__plot__(self, backend, context, *args, **kwds)
del self._names
return result
###############################################################################
class Cover:
"""Class representing a cover of an arbitrary ordered set.
Covers are similar to clusterings, but each element of the set may
belong to more than one cluster in a cover, and elements not belonging
to any cluster are also allowed.
L{Cover} instances provide a similar API as L{Clustering} instances;
for instance, iterating over a L{Cover} will iterate over the clusters
just like with a regular L{Clustering} instance. However, they are not
derived from each other or from a common superclass, and there might
be functions that exist only in one of them or the other.
Clusters of an individual cover can be accessed by the C{[]} operator:
>>> cl = Cover([[0,1,2,3], [2,3,4], [0,1,6]])
>>> cl[0]
[0, 1, 2, 3]
The membership vector can be accessed by the C{membership} property.
Note that contrary to L{Clustering} instances, the membership vector
will contain lists that contain the cluster indices each item belongs
to:
>>> cl.membership
[[0, 2], [0, 2], [0, 1], [0, 1], [1], [], [2]]
The number of clusters can be retrieved by the C{len} function:
>>> len(cl)
3
You can iterate over the cover as if it were a regular list of
clusters:
>>> for cluster in cl:
... print(" ".join(str(idx) for idx in cluster))
...
0 1 2 3
2 3 4
0 1 6
If you need all the clusters at once as lists, you can simply convert
the cover to a list:
>>> cluster_list = list(cl)
>>> print(cluster_list)
[[0, 1, 2, 3], [2, 3, 4], [0, 1, 6]]
L{Clustering} objects can readily be converted to L{Cover} objects
using the constructor:
>>> clustering = Clustering([0, 0, 0, 0, 1, 1, 1, 2, 2, 2])
>>> cover = Cover(clustering)
>>> list(clustering) == list(cover)
True
"""
def __init__(self, clusters, n=0):
"""Constructs a cover with the given clusters.
@param clusters: the clusters in this cover, as a list or iterable.
Each cluster is specified by a list or tuple that contains the
IDs of the items in this cluster. IDs start from zero.
@param n: the total number of elements in the set that is covered
by this cover. If it is less than the number of unique elements
found in all the clusters, we will simply use the number of unique
elements, so it is safe to leave this at zero. You only have to
specify this parameter if there are some elements that are covered
by none of the clusters.
"""
self._clusters = [list(cluster) for cluster in clusters]
try:
self._n = max(max(cluster) + 1 for cluster in self._clusters if cluster)
except ValueError:
self._n = 0
self._n = max(n, self._n)
def __getitem__(self, index):
"""Returns the cluster with the given index."""
return self._clusters[index]
def __iter__(self):
"""Iterates over the clusters in this cover."""
return iter(self._clusters)
def __len__(self):
"""Returns the number of clusters in this cover."""
return len(self._clusters)
def __str__(self):
"""Returns a string representation of the cover."""
return self.summary(verbosity=1, width=78)
@property
def membership(self):
"""Returns the membership vector of this cover.
The membership vector of a cover covering I{n} elements is a list of
length I{n}, where element I{i} contains the cluster indices of the
I{i}th item.
"""
result = [[] for _ in range(self._n)]