001/* 002 AUTOMATICALLY GENERATED BY jTemp FROM 003 /Users/jon/Work/openimaj/tags/openimaj-1.3.1/machine-learning/clustering/src/main/jtemp/org/openimaj/ml/clustering/#T#CentroidsResult.jtemp 004*/ 005/** 006 * Copyright (c) 2011, The University of Southampton and the individual contributors. 007 * All rights reserved. 008 * 009 * Redistribution and use in source and binary forms, with or without modification, 010 * are permitted provided that the following conditions are met: 011 * 012 * * Redistributions of source code must retain the above copyright notice, 013 * this list of conditions and the following disclaimer. 014 * 015 * * Redistributions in binary form must reproduce the above copyright notice, 016 * this list of conditions and the following disclaimer in the documentation 017 * and/or other materials provided with the distribution. 018 * 019 * * Neither the name of the University of Southampton nor the names of its 020 * contributors may be used to endorse or promote products derived from this 021 * software without specific prior written permission. 022 * 023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 027 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 030 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 033 */ 034 035package org.openimaj.ml.clustering; 036 037import java.io.DataInput; 038import java.io.DataOutput; 039import java.io.IOException; 040import java.io.PrintWriter; 041import java.util.Arrays; 042import java.util.Scanner; 043 044import org.openimaj.ml.clustering.CentroidsProvider; 045import org.openimaj.ml.clustering.Clusters; 046import org.openimaj.ml.clustering.SpatialClusters; 047import org.openimaj.ml.clustering.SpatialClusterer; 048import org.openimaj.ml.clustering.assignment.HardAssigner; 049import org.openimaj.ml.clustering.assignment.hard.ExactDoubleAssigner; 050import org.openimaj.util.pair.IntDoublePair; 051 052/** 053 * The result of a {@link SpatialClusterer} that just produces a flat set of centroids. 054 * 055 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 056 */ 057public class DoubleCentroidsResult implements SpatialClusters<double[]>, CentroidsProvider<double[]> { 058 final static String HEADER = Clusters.CLUSTER_HEADER + "Double".charAt(0) + "Cen"; 059 060 /** The centroids of the clusters */ 061 public double[][] centroids; 062 063 @Override 064 public boolean equals(Object obj){ 065 if(!(obj instanceof DoubleCentroidsResult)) 066 return false; 067 068 DoubleCentroidsResult other = (DoubleCentroidsResult)obj; 069 for (int i = 0; i < this.centroids.length; i++) { 070 if (!Arrays.equals(this.centroids[i], other.centroids[i])) 071 return false; 072 } 073 return true; 074 } 075 076 @Override 077 public String asciiHeader() { 078 return "ASCII"+HEADER ; 079 } 080 081 @Override 082 public byte[] binaryHeader() { 083 return HEADER.getBytes(); 084 } 085 086 @Override 087 public void readASCII(Scanner br) throws IOException { 088 // Read Header 089 final int K = Integer.parseInt(br.nextLine().trim()); 090 final int M = Integer.parseInt(br.nextLine().trim()); 091 092 centroids = new double[K][M]; 093 for (int k=0; k<K; k++) { 094 String [] parts = br.nextLine().split(","); 095 096 for (int d=0; d<M; d++) { 097 centroids[k][d] = Double.parseDouble(parts[d]); 098 } 099 } 100 } 101 102 @Override 103 public void readBinary(DataInput in) throws IOException { 104 final int K = in.readInt(); 105 final int M = in.readInt(); 106 107 centroids = new double[K][M]; 108 109 for (int k=0; k<K; k++) { 110 for (int d=0; d<M; d++) { 111 centroids[k][d] = in.readDouble(); 112 } 113 } 114 } 115 116 @Override 117 public void writeASCII(PrintWriter writer) throws IOException { 118 writer.println(centroids.length); 119 writer.println(centroids[0].length); 120 121 for (int k=0; k<centroids.length; k++) { 122 for (int d=0; d<centroids[0].length; d++) { 123 writer.print(centroids[k][d] + ","); 124 } 125 writer.println(); 126 } 127 } 128 129 @Override 130 public void writeBinary(DataOutput out) throws IOException { 131 out.writeInt(centroids.length); 132 out.writeInt(centroids[0].length); 133 134 for (int k=0; k<centroids.length; k++) { 135 for (int d=0; d<centroids[0].length; d++) { 136 out.writeDouble(centroids[k][d]); 137 } 138 } 139 } 140 141 @Override 142 public String toString() { 143 String str = ""; 144 str += "DoubleCentroidsResult" + "\n"; 145 str += "No. of Clusters: " + centroids.length + "\n"; 146 str += "No. of Dimensions: " + centroids[0].length + "\n"; 147 return str; 148 } 149 150 @Override 151 public double[][] getCentroids() { 152 return this.centroids; 153 } 154 155 @Override 156 public HardAssigner<double[], double[], IntDoublePair> defaultHardAssigner() { 157 return new ExactDoubleAssigner(this); 158 } 159 160 @Override 161 public int numDimensions() { 162 return centroids[0].length; 163 } 164 165 @Override 166 public int numClusters() { 167 return centroids.length; 168 } 169}