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 org.openimaj.data.DataSource; 033import org.openimaj.knn.DoubleNearestNeighbours; 034import org.openimaj.knn.DoubleNearestNeighboursExact; 035import org.openimaj.ml.clustering.SpatialClusterer; 036import org.openimaj.ml.clustering.SpatialClusters; 037import org.openimaj.ml.clustering.dbscan.DoubleNNDBSCAN; 038import org.openimaj.util.function.Function; 039import org.openimaj.util.pair.IndependentPair; 040 041import ch.akuhn.matrix.eigenvalues.Eigenvalues; 042 043/** 044 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 045 * @param <DATATYPE> 046 * 047 */ 048public class SpectralClusteringConf<DATATYPE>{ 049 /** 050 * A function which can represent itself as a string 051 * 052 * @param <DATATYPE> 053 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 054 */ 055 public static interface ClustererProvider<DATATYPE> extends Function<IndependentPair<double[], double[][]>, SpatialClusterer<? extends SpatialClusters<DATATYPE>,DATATYPE>>{ 056 public String toString(); 057 } 058 protected static class DefaultClustererFunction<DATATYPE> implements ClustererProvider<DATATYPE>{ 059 060 061 private SpatialClusterer<? extends SpatialClusters<DATATYPE>, DATATYPE> internal; 062 063 public DefaultClustererFunction( SpatialClusterer<? extends SpatialClusters<DATATYPE>,DATATYPE> internal) { 064 this.internal = internal; 065 } 066 067 @Override 068 public SpatialClusterer<? extends SpatialClusters<DATATYPE>, DATATYPE> apply(IndependentPair<double[], double[][]> in) { 069 return internal; 070 } 071 072 @Override 073 public String toString() { 074 return internal.toString(); 075 } 076 077 } 078 079 /** 080 * The internal clusterer 081 */ 082 083 ClustererProvider<DATATYPE> internal; 084 085 /** 086 * The graph laplacian creator 087 */ 088 public GraphLaplacian laplacian; 089 090 /** 091 * The method used to select the number of eigen vectors from the lower valued eigenvalues 092 */ 093 public EigenChooser eigenChooser; 094 095 public int skipEigenVectors = 0; 096 097 public boolean eigenValueScale = false; 098 099 /** 100 * @param internal the internal clusterer 101 * @param eigK the value handed to {@link HardCodedEigenChooser} 102 * 103 */ 104 public SpectralClusteringConf(SpatialClusterer<? extends SpatialClusters<DATATYPE>,DATATYPE> internal, int eigK) { 105 this.internal = new DefaultClustererFunction<DATATYPE>(internal); 106 this.laplacian = new GraphLaplacian.Normalised(); 107 this.eigenChooser = new HardCodedEigenChooser(eigK); 108 109 } 110 111 /** 112 * The underlying {@link EigenChooser} is set to an {@link ChangeDetectingEigenChooser} which 113 * looks for a 100x gap between eigen vectors to select number of clusters. It also insists upon 114 * a maximum of 0.1 * number of data items (so 10 items per cluster) 115 * 116 * @param internal the internal clusterer 117 * 118 */ 119 public SpectralClusteringConf(SpatialClusterer<? extends SpatialClusters<DATATYPE>,DATATYPE> internal) { 120 this.internal = new DefaultClustererFunction<DATATYPE>(internal); 121 this.laplacian = new GraphLaplacian.Normalised(); 122 this.eigenChooser = new ChangeDetectingEigenChooser(100,0.1); 123 124 } 125 126 /** 127 * @param internal an internal clusterer 128 * @param lap the laplacian 129 * @param top the top eigen vectors 130 */ 131 public SpectralClusteringConf(SpatialClusterer<? extends SpatialClusters<DATATYPE>, DATATYPE> internal, GraphLaplacian lap, int top) { 132 this.internal = new DefaultClustererFunction<DATATYPE>(internal); 133 this.laplacian = lap; 134 this.eigenChooser = new HardCodedEigenChooser(top); 135 } 136 137 /** 138 * The underlying {@link EigenChooser} is set to an {@link ChangeDetectingEigenChooser} which 139 * looks for a 100x gap between eigen vectors to select number of clusters. It also insists upon 140 * a maximum of 0.1 * number of data items (so 10 items per cluster) 141 * 142 * @param internal the internal clusterer 143 * @param laplacian the graph laplacian 144 * 145 */ 146 public SpectralClusteringConf(SpatialClusterer<? extends SpatialClusters<DATATYPE>,DATATYPE> internal, GraphLaplacian laplacian) { 147 this.internal = new DefaultClustererFunction<DATATYPE>(internal); 148 this.laplacian = laplacian; 149 this.eigenChooser = new ChangeDetectingEigenChooser(100,0.1); 150 151 } 152 153 /** 154 * The underlying {@link EigenChooser} is set to an {@link ChangeDetectingEigenChooser} which 155 * looks for a 100x gap between eigen vectors to select number of clusters. It also insists upon 156 * a maximum of 0.1 * number of data items (so 10 items per cluster) 157 * @param internal the internal clusterer 158 * 159 */ 160 public SpectralClusteringConf(ClustererProvider<DATATYPE> internal) { 161 this.internal = internal; 162 this.laplacian = new GraphLaplacian.Normalised(); 163 this.eigenChooser = new ChangeDetectingEigenChooser(100,0.1); 164 165 } 166 167 168}