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.dbscan; 031 032import java.util.ArrayList; 033import java.util.Iterator; 034import java.util.List; 035 036import org.openimaj.data.DataSource; 037import org.openimaj.knn.DoubleNearestNeighbours; 038import org.openimaj.knn.DoubleNearestNeighboursExact; 039import org.openimaj.knn.NearestNeighboursFactory; 040import org.openimaj.ml.clustering.DataClusterer; 041import org.openimaj.ml.clustering.SpatialClusterer; 042import org.openimaj.ml.clustering.dbscan.neighbourhood.RegionMode; 043import org.openimaj.util.pair.IntDoublePair; 044 045/** 046 * Implementation of DBSCAN (http://en.wikipedia.org/wiki/DBSCAN) using 047 * a 048 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 049 * 050 */ 051public class DoubleNNDBSCAN extends DBSCAN implements SpatialClusterer<DoubleDBSCANClusters, double[]>, DataClusterer<double[][], DoubleDBSCANClusters>{ 052 053 private NearestNeighboursFactory<? extends DoubleNearestNeighbours, double[]> nnf; 054 private double eps; 055 private int minPts; 056 057 058 /** 059 * Perform a DBScane with this configuration 060 * @param eps 061 * @param minPts 062 * @param nnf 063 */ 064 public DoubleNNDBSCAN(double eps, int minPts, NearestNeighboursFactory<? extends DoubleNearestNeighbours, double[]> nnf) { 065 this.eps = eps; 066 this.nnf = nnf; 067 this.minPts = minPts; 068 } 069 /** 070 * @param eps 071 * @param minPts 072 */ 073 public DoubleNNDBSCAN(double eps, int minPts) { 074 this(eps,minPts,new DoubleNearestNeighboursExact.Factory()); 075 } 076 class NNRegionMode implements RegionMode<IntDoublePair>{ 077 double[][] data; 078 DoubleNearestNeighbours nn; 079 public NNRegionMode(double[][] data) { 080 this.data = data; 081 this.nn = nnf.create(data); 082 } 083 @Override 084 public List<IntDoublePair> regionQuery(int index) { 085 List<IntDoublePair> res = nn.searchKNN(data[index], data.length); 086 List<IntDoublePair> ret = new ArrayList<IntDoublePair>(); 087 for (IntDoublePair intFloatPair : res) { 088 if(intFloatPair.second<eps)ret.add(intFloatPair); 089 else break; 090 } 091 return ret; 092 } 093 094 @Override 095 public boolean validRegion(List<IntDoublePair> region) { 096 return region.size() >= minPts; 097 } 098 099 } 100 101 102 103 @Override 104 public DoubleDBSCANClusters cluster(double[][] data) { 105 State state = new State(data.length,new NNRegionMode(data),this.noiseAsClusters); 106 return dbscan(state); 107 } 108 109 @Override 110 public DoubleDBSCANClusters cluster(DataSource<double[]> data) { 111 double[][] allData = new double[data.size()][]; 112 Iterator<double[]> iterator = data.iterator(); 113 for (int i = 0; i < allData.length; i++) { 114 allData[i] = iterator.next(); 115 } 116 return this.cluster(allData); 117 } 118 119 @Override 120 public int[][] performClustering(double[][] data) { 121 return cluster(data).clusters(); 122 } 123 124 /** 125 * @return the epse parameter 126 */ 127 public double getEps() { 128 return this.eps; 129 } 130 131 @Override 132 public String toString() { 133 return String.format("%s: eps=%2.2f, minpts=%d, NN=%s",this.getClass().getSimpleName(),eps,minPts,this.nnf.getClass().getSimpleName()); 134 } 135 136}