Package me.uits.aiphial.general.basic

Source Code of me.uits.aiphial.general.basic.SimpleBandwidthSelectorTest

/*
*
* This file is part of Aiphial.
*
* Copyright (c) 2010 Nicolay Mitropolsky <NicolayMitropolsky@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package me.uits.aiphial.general.basic;

import me.uits.aiphial.general.basic.SimpleBandwidthSelector;
import me.uits.aiphial.general.basic.MeanShiftClusterer;
import me.uits.aiphial.general.basic.IMeanShiftClusterer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import me.uits.aiphial.general.CollectionUtls;
import me.uits.aiphial.general.dataStore.DataStore;
import me.uits.aiphial.general.dataStore.MultiDimMapDataStore;
import me.uits.aiphial.general.dataStore.NDimPoint;
import me.uits.aiphial.general.dataStore.SimpleNDimPoint;
import me.uits.aiphial.general.datagenerator.DataGenerator;
import static org.junit.Assert.*;

/**
*
* @author Nicolay Mitropolsky <NicolayMitropolsky@gmail.com>
*/
public class SimpleBandwidthSelectorTest
{

    public SimpleBandwidthSelectorTest()
    {
    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {
    }

    @AfterClass
    public static void tearDownClass() throws Exception
    {
    }

    //@Test
    public void testGenerared()
    {
        final int dim = 3;
        final int clusterscount = 3;
        final int deviation = 10;
        final int pointsCount = 100;

        DataGenerator dg = new DataGenerator(dim);

        dg.setDeviation(deviation);
        dg.setMaxValue(300);


        List<NDimPoint> generated = dg.generate(clusterscount, pointsCount);

        DataStore<NDimPoint> ds = new MultiDimMapDataStore<NDimPoint>(dim);
        ds.addAll(generated);

        /*
        Float[] bandwidth = new Float[dim];
       
        final double pow = Math.pow(Math.pow(dg.getMaxValue(), dim) / pointsCount, 1.0F / dim);

        for (int i = 0; i < bandwidth.length; i++)
        {
        bandwidth[i] =(float)pow; //(float) deviation * 3;
        }
         */




        Float[] bandwidth0 = new SimpleBandwidthSelector().getBandwidth(ds);

        ds.setOptimalWindow(bandwidth0);
      
        System.out.println();

        IMeanShiftClusterer<NDimPoint> instance = new MeanShiftClusterer<NDimPoint>();
        instance.setDataStore(ds);
        instance.doClustering();

        System.out.println("clusters=" + instance.getClusters().size());

        assertTrue(instance.getClusters().size() == clusterscount);

    }

   @Test
    public void testDoClustering1()
    {
        System.out.println("doClustering");

        DataStore<SimpleNDimPoint> ds = new MultiDimMapDataStore<SimpleNDimPoint>(2);

        Set<SimpleNDimPoint> firstCulster = new HashSet<SimpleNDimPoint>();
        firstCulster.add(new SimpleNDimPoint(1f, 2f));
        firstCulster.add(new SimpleNDimPoint(1.1f, 2.1f));
        firstCulster.add(new SimpleNDimPoint(1.2f, 2.2f));
        firstCulster.add(new SimpleNDimPoint(1.3f, 2.3f));
        firstCulster.add(new SimpleNDimPoint(1.4f, 1.9f));
        firstCulster.add(new SimpleNDimPoint(1f, 1.8f));


        ds.addAll(firstCulster);


        Set<SimpleNDimPoint> secondCulster = new HashSet<SimpleNDimPoint>();
        secondCulster.add(new SimpleNDimPoint(5f, 4f));
        secondCulster.add(new SimpleNDimPoint(5.1f, 4f));
        secondCulster.add(new SimpleNDimPoint(5.2f, 4f));
        secondCulster.add(new SimpleNDimPoint(5.3f, 4f));
        secondCulster.add(new SimpleNDimPoint(5.4f, 4f));

        ds.addAll(secondCulster);
        Float[] bandwidth = new SimpleBandwidthSelector().getBandwidth(ds);


        System.out.println();
      

        IMeanShiftClusterer<SimpleNDimPoint> instance = new MeanShiftClusterer<SimpleNDimPoint>();
        instance.setWindow(bandwidth);
        instance.setDataStore(ds);
        instance.doClustering();

        assertTrue(CollectionUtls.checkInCollectionEquality(instance.getClusters(), Arrays.asList(firstCulster, secondCulster)));

    }
}
TOP

Related Classes of me.uits.aiphial.general.basic.SimpleBandwidthSelectorTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.