/** * In this situation there are not enough unique points which can act as unique seeds */ @Test public void notEnoughUniquePoints() { int DOF = 20; List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,30,true); for (int i = 1; i < points.size(); i += 2) { System.arraycopy(points.get(i-1),0,points.get(i),0,DOF); } List<double[]> seeds = TestStandardKMeans_F64.createPoints(DOF,20,false); InitializeKMeans_F64 alg = createAlg(); alg.init(DOF,0xBEEF); alg.selectSeeds(points, seeds); // just make sure it found a match in the input set for( double[] a : seeds ) { findMatch( a , points ); } }
List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,numPoints,true); List<double[]> seeds = TestStandardKMeans_F64.createPoints(DOF,numSeeds,false);
@Test public void selectSeeds() { int DOF = 20; InitializeKMeans_F64 alg = createAlg(); alg.init(DOF,0xBEEF); List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,100,true); List<double[]> seeds = TestStandardKMeans_F64.createPoints(DOF,20,false); alg.selectSeeds(points,seeds); // make sure nothing was added to the list assertEquals(20,seeds.size()); for (int i = 0; i < seeds.size(); i++) { double[] s = seeds.get(i); // make sure the seed was written to for (int j = 0; j < DOF; j++) { assertTrue(s[j]!=0); } // make sure it wasn't swapped with one of the points for (int j = 0; j < points.size(); j++) { assertTrue(points.get(j) != s); } } }
/** * In this situation there are not enough unique points which can act as unique seeds. * * This is a stricter version of generic test */ @Test public void notEnoughUniquePoints_strict() { int DOF = 20; List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,30,true); for (int i = 1; i < points.size(); i += 2) { System.arraycopy(points.get(i-1),0,points.get(i),0,DOF); } List<double[]> seeds = TestStandardKMeans_F64.createPoints(DOF,20,false); InitializeKMeans_F64 alg = createAlg(); alg.init(DOF,0xBEEF); alg.selectSeeds(points, seeds); int hits[] = new int[15]; for( double[] a : seeds ) { int match = findMatch( a , points )/2; hits[match]++; } // make sure each one was selected at least once for (int i = 0; i < hits.length; i++) { assertTrue(hits[i] > 0); } }
/** * Request more seeds than there are points. This is impossible to do and ensure the seeds are * unique. */ @Test public void impossible() { try { int DOF = 20; InitializeKMeans_F64 alg = createAlg(); alg.init(DOF,0xBEEF); // 4 points and 4 seeds. Each point must be a seed List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,3,true); List<double[]> seeds = TestStandardKMeans_F64.createPoints(DOF,4,false); alg.selectSeeds(points,seeds); fail("Should have thrown an exception!"); } catch( Exception e ) { } }
@Test public void computeDistance() { int DOF = 5; List<double[]> points = TestStandardKMeans_F64.createPoints(DOF,200,true); ComputeClusters<double[]> alg = createClustersAlg(false); alg.init(DOF,243234); alg.process(points,3); double first = alg.getDistanceMeasure(); alg.process(points,10); double second = alg.getDistanceMeasure(); // it's actually difficult to come up with meaningful tests for distance which don't make // assumptions about the algorithm. So there's only these really simple tests assertTrue(first!=second); assertFalse(Double.isNaN(first)); assertFalse(Double.isNaN(second)); assertFalse(Double.isInfinite(first)); assertFalse(Double.isInfinite(second)); } }