001/* 002 AUTOMATICALLY GENERATED BY jTemp FROM 003 /Users/jsh2/Work/openimaj/target/checkout/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.assignment.HardAssigner; 048import org.openimaj.ml.clustering.assignment.hard.ExactShortAssigner; 049import org.openimaj.util.pair.IntFloatPair; 050 051/** 052 * The result of a {@link SpatialClusterer} that just produces a flat set of centroids. 053 * 054 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 055 */ 056public class ShortCentroidsResult implements SpatialClusters<short[]>, CentroidsProvider<short[]> { 057 final static String HEADER = Clusters.CLUSTER_HEADER + "Short".charAt(0) + "Cen"; 058 059 /** The centroids of the clusters */ 060 public short[][] centroids; 061 062 @Override 063 public boolean equals(Object obj){ 064 if(!(obj instanceof ShortCentroidsResult)) 065 return false; 066 067 ShortCentroidsResult other = (ShortCentroidsResult)obj; 068 for (int i = 0; i < this.centroids.length; i++) { 069 if (!Arrays.equals(this.centroids[i], other.centroids[i])) 070 return false; 071 } 072 return true; 073 } 074 075 @Override 076 public String asciiHeader() { 077 return "ASCII"+HEADER ; 078 } 079 080 @Override 081 public byte[] binaryHeader() { 082 return HEADER.getBytes(); 083 } 084 085 @Override 086 public void readASCII(Scanner br) throws IOException { 087 // Read Header 088 final int K = Integer.parseInt(br.nextLine().trim()); 089 final int M = Integer.parseInt(br.nextLine().trim()); 090 091 centroids = new short[K][M]; 092 for (int k=0; k<K; k++) { 093 String [] parts = br.nextLine().split(","); 094 095 for (int d=0; d<M; d++) { 096 centroids[k][d] = Short.parseShort(parts[d]); 097 } 098 } 099 } 100 101 @Override 102 public void readBinary(DataInput in) throws IOException { 103 final int K = in.readInt(); 104 final int M = in.readInt(); 105 106 centroids = new short[K][M]; 107 108 for (int k=0; k<K; k++) { 109 for (int d=0; d<M; d++) { 110 centroids[k][d] = in.readShort(); 111 } 112 } 113 } 114 115 @Override 116 public void writeASCII(PrintWriter writer) throws IOException { 117 writer.println(centroids.length); 118 writer.println(centroids[0].length); 119 120 for (int k=0; k<centroids.length; k++) { 121 for (int d=0; d<centroids[0].length; d++) { 122 writer.print(centroids[k][d] + ","); 123 } 124 writer.println(); 125 } 126 } 127 128 @Override 129 public void writeBinary(DataOutput out) throws IOException { 130 out.writeInt(centroids.length); 131 out.writeInt(centroids[0].length); 132 133 for (int k=0; k<centroids.length; k++) { 134 for (int d=0; d<centroids[0].length; d++) { 135 out.writeShort(centroids[k][d]); 136 } 137 } 138 } 139 140 @Override 141 public String toString() { 142 String str = ""; 143 str += "ShortCentroidsResult" + "\n"; 144 str += "No. of Clusters: " + centroids.length + "\n"; 145 str += "No. of Dimensions: " + centroids[0].length + "\n"; 146 return str; 147 } 148 149 @Override 150 public short[][] getCentroids() { 151 return this.centroids; 152 } 153 154 @Override 155 public HardAssigner<short[], float[], IntFloatPair> defaultHardAssigner() { 156 return new ExactShortAssigner(this); 157 } 158 159 @Override 160 public int numDimensions() { 161 return centroids[0].length; 162 } 163 164 @Override 165 public int numClusters() { 166 return centroids.length; 167 } 168}