Debian Codegraph @

cluster/cluster.py

raw symbols SCIP
1 #
2 # This is part of "python-cluster". A library to group similar items together.
3 # Copyright (C) 2006 Michel Albert
4 #
5 # This library is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by the
7 # Free Software Foundation; either version 2.1 of the License, or (at your
8 # option) any later version.
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 # for more details.
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this library; if not, write to the Free Software Foundation,
15 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #
17
18 from __future__ import print_function
19
20 from .util import fullyflatten
21
22
23 class Cluster(object):
24 """
25 A collection of items. This is internally used to detect clustered items
26 in the data so we could distinguish other collection types (lists, dicts,
27 ...) from the actual clusters. This means that you could also create
28 clusters of lists with this class.
29 """
30
31 def __repr__(self):
32 return "<Cluster@%s(%s)>" % (self.level, self.items)
33
34 def __str__(self):
35 return self.__str__()
36
37 def __init__(self, level, *args):
38 """
39 Constructor
40
41 :param level: The level of this cluster. This is used in hierarchical
42 clustering to retrieve a specific set of clusters. The higher the
43 level, the smaller the count of clusters returned. The level depends
44 on the difference function used.
45 :param *args: every additional argument passed following the level value
46 will get added as item to the cluster. You could also pass a list as
47 second parameter to initialise the cluster with that list as content
48 """
49 self.level = level
50 if len(args) == 0:
51 self.items = []
52 else:
53 self.items = args
54
55 def __iter__(self):
56 for item in self.items:
57 if isinstance(item, Cluster):
58 for recursed_item in item:
59 yield recursed_item
60 else:
61 yield item
62
63 def display(self, depth=0):
64 """
65 Pretty-prints this cluster. Useful for debuging.
66 """
67 print(depth * " " + "[level %s]" % self.level)
68 for item in self.items:
69 if isinstance(item, Cluster):
70 item.display(depth + 1)
71 else:
72 print(depth * " " + "%s" % item)
73
74 def topology(self):
75 """
76 Returns the structure (topology) of the cluster as tuples.
77
78 Output from cl.data::
79
80 [<[email protected](['CVS',
81 <[email protected](['34.xls',
82 <[email protected]([<[email protected](['0.txt',
83 <[email protected](['ChangeLog', 'ChangeLog.txt'])>])>,
84 <[email protected](['20060730.py',
85 <[email protected](['.cvsignore',
86 <[email protected](['About.py', <[email protected](['.idlerc',
87 '.pylint.d'])>])>])>])>])>])>])>]
88
89 Corresponding output from cl.topo()::
90
91 ('CVS', ('34.xls', (('0.txt', ('ChangeLog', 'ChangeLog.txt')),
92 ('20060730.py', ('.cvsignore', ('About.py',
93 ('.idlerc', '.pylint.d')))))))
94 """
95
96 left = self.items[0]
97 right = self.items[1]
98
99 if isinstance(left, Cluster):
100 first = left.topology()
101 else:
102 first = left
103
104 if isinstance(right, Cluster):
105 second = right.topology()
106 else:
107 second = right
108
109 return first, second
110
111 def getlevel(self, threshold):
112 """
113 Retrieve all clusters up to a specific level threshold. This
114 level-threshold represents the maximum distance between two clusters.
115 So the lower you set this threshold, the more clusters you will
116 receive and the higher you set it, you will receive less but bigger
117 clusters.
118
119 :param threshold: The level threshold:
120
121 .. note::
122 It is debatable whether the value passed into this method should
123 really be as strongly linked to the real cluster-levels as it is
124 right now. The end-user will not know the range of this value
125 unless s/he first inspects the top-level cluster. So instead you
126 might argue that a value ranging from 0 to 1 might be a more
127 useful approach.
128 """
129
130 left = self.items[0]
131 right = self.items[1]
132
133 # if this object itself is below the threshold value we only need to
134 # return it's contents as a list
135 if self.level <= threshold:
136 return [fullyflatten(self.items)]
137
138 # if this cluster's level is higher than the threshold we will
139 # investgate it's left and right part. Their level could be below the
140 # threshold
141 if isinstance(left, Cluster) and left.level <= threshold:
142 if isinstance(right, Cluster):
143 return [fullyflatten(left.items)] + right.getlevel(threshold)
144 else:
145 return [fullyflatten(left.items)] + [[right]]
146 elif isinstance(right, Cluster) and right.level <= threshold:
147 if isinstance(left, Cluster):
148 return left.getlevel(threshold) + [fullyflatten(right.items)]
149 else:
150 return [[left]] + [fullyflatten(right.items)]
151
152 # Alright. We covered the cases where one of the clusters was below
153 # the threshold value. Now we'll deal with the clusters that are above
154 # by recursively applying the previous cases.
155 if isinstance(left, Cluster) and isinstance(right, Cluster):
156 return left.getlevel(threshold) + right.getlevel(threshold)
157 elif isinstance(left, Cluster):
158 return left.getlevel(threshold) + [[right]]
159 elif isinstance(right, Cluster):
160 return [[left]] + right.getlevel(threshold)
161 else:
162 return [[left], [right]]