View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.ml.clustering.spectral;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import org.apache.log4j.Logger;
36  import org.openimaj.experiment.evaluation.cluster.ClusterEvaluator;
37  import org.openimaj.experiment.evaluation.cluster.analyser.FullMEAnalysis;
38  import org.openimaj.experiment.evaluation.cluster.analyser.FullMEClusterAnalyser;
39  import org.openimaj.experiment.evaluation.cluster.processor.Clusterer;
40  import org.openimaj.feature.DoubleFVComparison;
41  import org.openimaj.knn.DoubleNearestNeighboursExact;
42  import org.openimaj.ml.clustering.SpatialClusterer;
43  import org.openimaj.ml.clustering.SpatialClusters;
44  import org.openimaj.ml.clustering.dbscan.DistanceDBSCAN;
45  import org.openimaj.ml.clustering.dbscan.DoubleDBSCANClusters;
46  import org.openimaj.ml.clustering.dbscan.DoubleNNDBSCAN;
47  import org.openimaj.ml.clustering.dbscan.SparseMatrixDBSCAN;
48  import org.openimaj.ml.dataset.WineDataset;
49  import org.openimaj.util.function.Function;
50  import org.openimaj.util.pair.DoubleObjectPair;
51  
52  import ch.akuhn.matrix.SparseMatrix;
53  import ch.akuhn.matrix.Vector;
54  import ch.akuhn.matrix.eigenvalues.AllEigenvalues;
55  import ch.akuhn.matrix.eigenvalues.Eigenvalues;
56  
57  /**
58   * Perform spectral clustering experiments using the Wine Dataset
59   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
60   *
61   */
62  public class WineDatasetExperiment {
63  	private static final int MAXIMUM_DISTANCE = 300;
64  	private static Logger logger = Logger.getLogger(WineDatasetExperiment.class);
65  
66  	/**
67  	 * @param args
68  	 */
69  	public static void main(String[] args) {
70  		WineDataset ds = new WineDataset(2,3);
71  		
72  //		logger.info("Clustering using spectral clustering");
73  //		DoubleSpectralClustering clust = prepareSpectralClustering();
74  //		ClustererWrapper spectralWrapper = new NormalisedSimilarityDoubleClustererWrapper<double[]>(
75  //			ds, 
76  //			new WrapperExtractor(), 
77  //			clust, 
78  //			MAXIMUM_DISTANCE
79  //		);
80  //		evaluate(ds, clust);
81  //		logger.info("Clustering using DBScan");
82  //		DoubleDBSCAN dbScan = prepareDBScane();
83  //		ClustererWrapper dbScanWrapper = new NormalisedSimilarityDoubleClustererWrapper<double[]>(
84  //			ds, 
85  //			new WrapperExtractor(), 
86  //			dbScan, 
87  //			MAXIMUM_DISTANCE
88  //		);
89  //		evaluate(ds, dbScan);
90  		
91  		logger.info("Clustering using modified spectral clustering");
92  		DoubleSpectralClustering clustCSP = prepareCSPSpectralClustering(ds);
93  		Function<List<double[]>,SparseMatrix> func = new RBFSimilarityDoubleClustererWrapper<double[]>(new DummyExtractor());
94  		evaluate(ds, clustCSP, func);
95  	}
96  
97  	private static DoubleSpectralClustering prepareCSPSpectralClustering(WineDataset ds) {
98  		SpatialClusterer<? extends SpatialClusters<double[]>, double[]> cl = null;
99  		// 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 }