Skip to content

Commit c5c548a

Browse files
committed
If items are iterable, clustering breaks.
This commit will package tuples into lists before creating the matrix. Fixes #20
1 parent 3b69777 commit c5c548a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

cluster/matrix.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def worker(self):
5757
tasks_completed = 0
5858
for task in iter(self.task_queue.get, 'STOP'):
5959
col_index, item, item2 = task
60-
if not hasattr(item, '__iter__'):
60+
if not hasattr(item, '__iter__') or isinstance(item, tuple):
6161
item = [item]
62-
if not hasattr(item2, '__iter__'):
62+
if not hasattr(item2, '__iter__') or isinstance(item2, tuple):
6363
item2 = [item2]
6464
result = (col_index, self.combinfunc(item, item2))
6565
self.done_queue.put(result)
@@ -123,9 +123,9 @@ def genmatrix(self, num_processes=1):
123123
num_tasks_completed += 1
124124
else:
125125
# Otherwise do it here, in line
126-
if not hasattr(item, '__iter__'):
126+
if not hasattr(item, '__iter__') or isinstance(item, tuple):
127127
item = [item]
128-
if not hasattr(item2, '__iter__'):
128+
if not hasattr(item2, '__iter__') or isinstance(item2, tuple):
129129
item2 = [item2]
130130
row[col_index] = self.combinfunc(item, item2)
131131

cluster/test/test_hierarchical.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"""
2727

2828
from difflib import SequenceMatcher
29+
from math import sqrt
2930
from sys import hexversion
3031
import unittest
3132

@@ -219,6 +220,25 @@ def testUnmodifiedData(self):
219220
self.assertEqual(sorted(new_data), sorted(self.__data))
220221

221222

223+
class HClusterTuplesTestCase(Py23TestCase):
224+
'''
225+
Test case to cover the case where the data contains tuple-items
226+
227+
See Github issue #20
228+
'''
229+
230+
def testSingleLinkage(self):
231+
"Basic Hierarchical Clustering test with integers"
232+
233+
def euclidian_distance(a, b):
234+
return sqrt(sum([pow(z[0] - z[1], 2) for z in zip(a, b)]))
235+
236+
self.__data = [(1, 1), (1, 2), (1, 3)]
237+
cl = HierarchicalClustering(self.__data, euclidian_distance)
238+
result = cl.getlevel(40)
239+
self.assertIsNotNone(result)
240+
241+
222242
if __name__ == '__main__':
223243
suite = unittest.TestSuite((
224244
unittest.makeSuite(HClusterIntegerTestCase),

0 commit comments

Comments
 (0)