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 */
030
031package org.openimaj.ml.clustering;
032
033import java.io.DataInput;
034import java.io.DataOutput;
035import java.io.IOException;
036import java.io.PrintWriter;
037import java.util.Scanner;
038
039import org.openimaj.feature.FeatureVector;
040import org.openimaj.ml.clustering.assignment.HardAssigner;
041import org.openimaj.ml.clustering.assignment.hard.ExactFeatureVectorAssigner;
042import org.openimaj.util.pair.IntFloatPair;
043
044/**
045 * The result of a {@link SpatialClusterer} that just produces a flat set of
046 * centroids in the form of {@link FeatureVector}s.
047 *
048 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
049 * @param <T>
050 *            Type of features
051 */
052public class FeatureVectorCentroidsResult<T extends FeatureVector> implements SpatialClusters<T>, CentroidsProvider<T> {
053        final static String HEADER = Clusters.CLUSTER_HEADER + "Byte".charAt(0) + "Cen";
054
055        /** The centroids of the clusters */
056        public T[] centroids;
057
058        @Override
059        public boolean equals(Object obj) {
060                if (!(obj instanceof ByteCentroidsResult))
061                        return false;
062
063                final FeatureVectorCentroidsResult<?> other = (FeatureVectorCentroidsResult<?>) obj;
064                for (int i = 0; i < this.centroids.length; i++) {
065                        if (!this.centroids[i].equals(other.centroids[i]))
066                                return false;
067                }
068                return true;
069        }
070
071        @Override
072        public String asciiHeader() {
073                return "ASCII" + HEADER;
074        }
075
076        @Override
077        public byte[] binaryHeader() {
078                return HEADER.getBytes();
079        }
080
081        @Override
082        public void readASCII(Scanner br) throws IOException {
083                // // Read Header
084                // final int K = Integer.parseInt(br.nextLine().trim());
085                // final int M = Integer.parseInt(br.nextLine().trim());
086                //
087                // centroids = new byte[K][M];
088                // for (int k = 0; k < K; k++) {
089                // final String[] parts = br.nextLine().split(",");
090                //
091                // for (int d = 0; d < M; d++) {
092                // centroids[k][d] = Byte.parseByte(parts[d]);
093                // }
094                // }
095                throw new UnsupportedOperationException();
096        }
097
098        @Override
099        public void readBinary(DataInput in) throws IOException {
100                // final int K = in.readInt();
101                // final int M = in.readInt();
102                //
103                // centroids = new byte[K][M];
104                //
105                // for (int k = 0; k < K; k++) {
106                // for (int d = 0; d < M; d++) {
107                // centroids[k][d] = in.readByte();
108                // }
109                // }
110                throw new UnsupportedOperationException();
111        }
112
113        @Override
114        public void writeASCII(PrintWriter writer) throws IOException {
115                // writer.println(centroids.length);
116                // writer.println(centroids[0].length);
117                //
118                // for (int k = 0; k < centroids.length; k++) {
119                // for (int d = 0; d < centroids[0].length; d++) {
120                // writer.print(centroids[k][d] + ",");
121                // }
122                // writer.println();
123                // }
124                throw new UnsupportedOperationException();
125        }
126
127        @Override
128        public void writeBinary(DataOutput out) throws IOException {
129                // out.writeInt(centroids.length);
130                // out.writeInt(centroids[0].length);
131                //
132                // for (int k = 0; k < centroids.length; k++) {
133                // for (int d = 0; d < centroids[0].length; d++) {
134                // out.writeByte(centroids[k][d]);
135                // }
136                // }
137                throw new UnsupportedOperationException();
138        }
139
140        @Override
141        public String toString() {
142                String str = "";
143                str += "ByteCentroidsResult" + "\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 T[] getCentroids() {
151                return this.centroids;
152        }
153
154        @Override
155        public HardAssigner<T, float[], IntFloatPair> defaultHardAssigner() {
156                return new ExactFeatureVectorAssigner<T>(this, null);
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}