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.kdtree;
31  
32  import java.io.DataInput;
33  import java.io.DataOutput;
34  import java.io.IOException;
35  import java.io.PrintWriter;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Scanner;
40  
41  import org.openimaj.ml.clustering.IndexClusters;
42  import org.openimaj.ml.clustering.SpatialClusters;
43  import org.openimaj.ml.clustering.assignment.HardAssigner;
44  import org.openimaj.util.pair.IntDoublePair;
45  import org.openimaj.util.tree.DoubleKDTree;
46  import org.openimaj.util.tree.DoubleKDTree.KDTreeNode;
47  
48  /**
49   *
50   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
51   */
52  public class KDTreeClusters extends IndexClusters implements SpatialClusters<double[]> {
53  
54  
55  	private final class KDHardTreeAssigner implements HardAssigner<double[], double[], IntDoublePair> {
56  		
57  		private Map<int[], Integer> clusterToIndex;
58  
59  		public KDHardTreeAssigner() {
60  			this.clusterToIndex = new HashMap<int[],Integer>();
61  			for (int i = 0; i < KDTreeClusters.this.leaves.size(); i++) {
62  				this.clusterToIndex.put(leaves.get(i), i);
63  			}
64  		}
65  		
66  		@Override
67  		public int numDimensions() {
68  			return dims;
69  		}
70  
71  		@Override
72  		public int[] assign(double[][] data) {
73  			int[] ret = new int[data.length];
74  			for (int i = 0; i < ret.length; i++) {
75  				ret[i] = assign(data[i]);
76  			}
77  			return ret;
78  		}
79  
80  		@Override
81  		public int assign(double[] data) {
82  			KDTreeNode toFollow = tree.root;
83  			while(toFollow.indices == null){
84  				if(data[toFollow.discriminantDimension] < toFollow.discriminant){
85  					toFollow = toFollow.left;
86  				}
87  				else{
88  					toFollow = toFollow.right;
89  				}
90  			}
91  			return this.clusterToIndex.get(toFollow.indices);
92  		}
93  
94  		@Override
95  		public void assignDistance(double[][] data, int[] indices, double[] distances) {
96  			throw new UnsupportedOperationException();
97  		}
98  
99  		@Override
100 		public IntDoublePair assignDistance(double[] data) {
101 			throw new UnsupportedOperationException();
102 		}
103 
104 		@Override
105 		public int size() {
106 			return numClusters();
107 		}
108 	}
109 
110 	private List<int[]> leaves;
111 	private int dims;
112 	private DoubleKDTree tree;
113 
114 	/**
115 	 * @param tree the KDTree which represents the clusters
116 	 * @param dims 
117 	 */
118 	public KDTreeClusters(DoubleKDTree tree, int dims) {
119 		this.tree = tree;
120 		this.leaves = tree.leafIndices();
121 		this.dims = dims;
122 		this.clusters = new int[leaves.size()][];
123 		for (int i = 0; i < clusters.length; i++) {
124 			clusters[i] = leaves.get(i);
125 			this.nEntries += this.clusters[i].length;
126 		}
127 		
128 	}
129 
130 	@Override
131 	public int numDimensions() {
132 		return dims;
133 	}
134 
135 	@Override
136 	public int numClusters() {
137 		return leaves.size();
138 	}
139 
140 	@Override
141 	public HardAssigner<double[], ?, ?> defaultHardAssigner() {
142 		return new KDHardTreeAssigner();
143 	}
144 	@Override
145 	public void readASCII(Scanner in) throws IOException { throw new UnsupportedOperationException(); }
146 	
147 	@Override
148 	public String asciiHeader() { throw new UnsupportedOperationException(); }
149 	
150 	@Override
151 	public void readBinary(DataInput in) throws IOException{ throw new UnsupportedOperationException(); }
152 	
153 	@Override
154 	public byte[] binaryHeader() { throw new UnsupportedOperationException(); }
155 	
156 	@Override
157 	public void writeASCII(PrintWriter out) throws IOException { throw new UnsupportedOperationException(); }
158 	
159 	@Override
160 	public void writeBinary(DataOutput out) throws IOException { throw new UnsupportedOperationException(); }
161 
162 }