001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.ml.clustering.spectral;
031
032import java.util.Iterator;
033import java.util.List;
034
035import org.apache.log4j.Logger;
036import org.openimaj.experiment.evaluation.cluster.ClusterEvaluator;
037import org.openimaj.experiment.evaluation.cluster.analyser.FullMEAnalysis;
038import org.openimaj.experiment.evaluation.cluster.analyser.FullMEClusterAnalyser;
039import org.openimaj.experiment.evaluation.cluster.processor.Clusterer;
040import org.openimaj.feature.DoubleFVComparison;
041import org.openimaj.knn.DoubleNearestNeighboursExact;
042import org.openimaj.ml.clustering.SpatialClusterer;
043import org.openimaj.ml.clustering.SpatialClusters;
044import org.openimaj.ml.clustering.dbscan.DistanceDBSCAN;
045import org.openimaj.ml.clustering.dbscan.DoubleDBSCANClusters;
046import org.openimaj.ml.clustering.dbscan.DoubleNNDBSCAN;
047import org.openimaj.ml.clustering.dbscan.SparseMatrixDBSCAN;
048import org.openimaj.ml.dataset.WineDataset;
049import org.openimaj.util.function.Function;
050import org.openimaj.util.pair.DoubleObjectPair;
051
052import ch.akuhn.matrix.SparseMatrix;
053import ch.akuhn.matrix.Vector;
054import ch.akuhn.matrix.eigenvalues.AllEigenvalues;
055import ch.akuhn.matrix.eigenvalues.Eigenvalues;
056
057/**
058 * Perform spectral clustering experiments using the Wine Dataset
059 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
060 *
061 */
062public class WineDatasetExperiment {
063        private static final int MAXIMUM_DISTANCE = 300;
064        private static Logger logger = Logger.getLogger(WineDatasetExperiment.class);
065
066        /**
067         * @param args
068         */
069        public static void main(String[] args) {
070                WineDataset ds = new WineDataset(2,3);
071                
072//              logger.info("Clustering using spectral clustering");
073//              DoubleSpectralClustering clust = prepareSpectralClustering();
074//              ClustererWrapper spectralWrapper = new NormalisedSimilarityDoubleClustererWrapper<double[]>(
075//                      ds, 
076//                      new WrapperExtractor(), 
077//                      clust, 
078//                      MAXIMUM_DISTANCE
079//              );
080//              evaluate(ds, clust);
081//              logger.info("Clustering using DBScan");
082//              DoubleDBSCAN dbScan = prepareDBScane();
083//              ClustererWrapper dbScanWrapper = new NormalisedSimilarityDoubleClustererWrapper<double[]>(
084//                      ds, 
085//                      new WrapperExtractor(), 
086//                      dbScan, 
087//                      MAXIMUM_DISTANCE
088//              );
089//              evaluate(ds, dbScan);
090                
091                logger.info("Clustering using modified spectral clustering");
092                DoubleSpectralClustering clustCSP = prepareCSPSpectralClustering(ds);
093                Function<List<double[]>,SparseMatrix> func = new RBFSimilarityDoubleClustererWrapper<double[]>(new DummyExtractor());
094                evaluate(ds, clustCSP, func);
095        }
096
097        private static DoubleSpectralClustering prepareCSPSpectralClustering(WineDataset ds) {
098                SpatialClusterer<? extends SpatialClusters<double[]>, double[]> cl = null;
099                // Creater the spectral clustering
100                SpectralClusteringConf<double[]> conf = new SpectralClusteringConf<double[]>(cl );
101                conf.eigenChooser = new EigenChooser() {
102                        
103                        @Override
104                        public Eigenvalues prepare(SparseMatrix laplacian) {
105                                Eigenvalues eig = new AllEigenvalues(laplacian);
106                                return eig;
107                        }
108                        
109                        @Override
110                        public int nEigenVectors(Iterator<DoubleObjectPair<Vector>> vals, int totalEigenVectors) {
111                                // TODO Auto-generated method stub
112                                return 0;
113                        }
114                };
115                DoubleSpectralClustering clust = new DoubleSpectralClustering(conf);
116                return clust;
117        }
118
119        private static SparseMatrixDBSCAN prepareDBScane() {
120                // Creater the spectral clustering
121                double epss = 0.5;
122                SparseMatrixDBSCAN inner = new DistanceDBSCAN(epss, 1);
123                return inner;
124        }
125
126        private static DoubleSpectralClustering prepareSpectralClustering() {
127                // Creater the spectral clustering
128                double epss = 0.6;
129                SpatialClusterer<DoubleDBSCANClusters,double[]> inner = new DoubleNNDBSCAN(epss, 2,new DoubleNearestNeighboursExact.Factory(DoubleFVComparison.EUCLIDEAN));
130                SpectralClusteringConf<double[]> conf = new SpectralClusteringConf<double[]>(
131                        inner
132                );
133//              conf.eigenChooser = new AutoSelectingEigenChooser(100, 1.0);
134                conf.eigenChooser = new HardCodedEigenChooser(10);
135                DoubleSpectralClustering clust = new DoubleSpectralClustering(conf);
136                return clust;
137        }
138
139        private static void evaluate(WineDataset ds, Clusterer<SparseMatrix> clust, Function<List<double[]>, SparseMatrix> func) {
140                ClusterEvaluator<SparseMatrix, FullMEAnalysis> eval = new ClusterEvaluator<SparseMatrix, FullMEAnalysis>(clust,ds,func,new FullMEClusterAnalyser());
141                int[][] evaluate = eval.evaluate();
142                logger.info("Expected Classes: " + ds.size());
143                logger.info("Detected Classes: " + evaluate.length);
144                FullMEAnalysis res = eval.analyse(evaluate);
145                System.out.println(res.getSummaryReport());
146        }
147}