Skip to content

Commit

Permalink
Added unit-tests for kmeans algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
annoviko committed Mar 26, 2014
1 parent 55efa79 commit 874ac51
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions kmeans/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest

from kmeans import kmeans;

from support import read_sample;

from samples.definitions import SIMPLE_SAMPLES;

class Test(unittest.TestCase):
def templateLengthProcessData(self, path_to_file, start_centers, expected_cluster_length):
sample = read_sample(path_to_file);
(clusters, centers) = kmeans(sample, start_centers);

obtained_cluster_sizes = [len(cluster) for cluster in clusters];
assert len(sample) == sum(obtained_cluster_sizes);

obtained_cluster_sizes.sort();
expected_cluster_length.sort();
assert obtained_cluster_sizes == expected_cluster_length;

def testClusterAllocationSampleSimple1(self):
self.templateLengthProcessData(SIMPLE_SAMPLES.SAMPLE_SIMPLE1, [[3.7, 5.5], [6.7, 7.5]], [5, 5]);

def testClusterAllocationSampleSimple2(self):
self.templateLengthProcessData(SIMPLE_SAMPLES.SAMPLE_SIMPLE2, [[3.5, 4.8], [6.9, 7], [7.5, 0.5]], [10, 5, 8]);

def testClusterAllocationSampleSimple3(self):
self.templateLengthProcessData(SIMPLE_SAMPLES.SAMPLE_SIMPLE3, [[0.2, 0.1], [4.0, 1.0], [2.0, 2.0], [2.3, 3.9]], [10, 10, 10, 30]);

def testClusterAllocationSampleSimple4(self):
self.templateLengthProcessData(SIMPLE_SAMPLES.SAMPLE_SIMPLE4, [[1.5, 0.0], [1.5, 2.0], [1.5, 4.0], [1.5, 6.0], [1.5, 8.0]], [15, 15, 15, 15, 15]);

def testClusterAllocationSampleSimple5(self):
self.templateLengthProcessData(SIMPLE_SAMPLES.SAMPLE_SIMPLE5, [[0.0, 1.0], [0.0, 0.0], [1.0, 1.0], [1.0, 0.0]], [15, 15, 15, 15]);


if __name__ == "__main__":
unittest.main();
2 changes: 2 additions & 0 deletions ut/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dbscan import tests as dbscan_unit_tests;
from hierarchical import tests as hierarchical_unit_tests;
from hsyncnet import tests as hsyncnet_unit_tests;
from kmeans import tests as kmeans_unit_tests;
from nnet.som import tests as nnet_som_unit_tests;
from nnet.sync import tests as nnet_sync_unit_tests;
from support import tests as support_unit_tests;
Expand All @@ -23,6 +24,7 @@
suite.addTests(unittest.TestLoader().loadTestsFromModule(nnet_sync_unit_tests));
suite.addTests(unittest.TestLoader().loadTestsFromModule(support_unit_tests));
suite.addTests(unittest.TestLoader().loadTestsFromModule(syncnet_unit_tests));
suite.addTests(unittest.TestLoader().loadTestsFromModule(kmeans_unit_tests));

unittest.TextTestRunner(verbosity = 2).run(suite);

0 comments on commit 874ac51

Please sign in to comment.