| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 134 |
|
| 135 |
if self.level <= threshold: |
| 136 |
return [fullyflatten(self.items)] |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 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]] |