View Javadoc

1   /*
2   	AUTOMATICALLY GENERATED BY jTemp FROM
3   	/Users/jsh2/Work/openimaj/target/checkout/machine-learning/clustering/src/test/jtemp/org/openimaj/ml/clustering/random/Random#T#ClustererTest.jtemp
4   */
5   /**
6    * Copyright (c) 2011, The University of Southampton and the individual contributors.
7    * All rights reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without modification,
10   * are permitted provided that the following conditions are met:
11   *
12   *   * 	Redistributions of source code must retain the above copyright notice,
13   * 	this list of conditions and the following disclaimer.
14   *
15   *   *	Redistributions in binary form must reproduce the above copyright notice,
16   * 	this list of conditions and the following disclaimer in the documentation
17   * 	and/or other materials provided with the distribution.
18   *
19   *   *	Neither the name of the University of Southampton nor the names of its
20   * 	contributors may be used to endorse or promote products derived from this
21   * 	software without specific prior written permission.
22   *
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   */
34  package org.openimaj.ml.clustering.random;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.Random;
39  
40  import static org.junit.Assert.*;
41  
42  import org.junit.Rule;
43  import org.junit.Test;
44  import org.junit.rules.TemporaryFolder;
45  
46  import org.openimaj.io.IOUtils;
47  import org.openimaj.ml.clustering.assignment.hard.ExactShortAssigner;
48  import org.openimaj.ml.clustering.ShortCentroidsResult;
49  import org.openimaj.data.RandomData;
50  
51  /**
52   * Set of tests for the random cluster implementations
53   * 
54   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
55   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
56   *
57   */
58  public class RandomShortClustererTest {
59  	/**
60  	 * Temporary directory for IO tests
61  	 */
62      @Rule
63      public TemporaryFolder folder = new TemporaryFolder();
64          
65  	/**
66  	 * Test the simple cluster algorithm
67  	 * @throws IOException
68  	 */
69  	@Test public void testRandomShortClusterer() throws IOException{
70  		short [][] data = {
71  				{0,0,0},
72  				{0,0,0},
73  				{0,0,-1},
74  				{-1,0,-1},
75  				{1,0,1},
76  				{2,2,2},
77  				{2,2,2},
78  				{3,2,2},
79  				{3,2,3},
80  				{1,2,1},
81  		};
82  		
83  		RandomShortClusterer s = new RandomShortClusterer(data[0].length);
84  		s.setSeed(2);
85  		int[] trainDataIndex = RandomData.getUniqueRandomIntsA(2, 0, data.length, new Random(2));
86  		short[][] trainData = new short[trainDataIndex.length][];
87  		for(int i = 0; i < trainDataIndex.length; i++){
88  			trainData[i] = data[trainDataIndex[i]];
89  		}
90  		ShortCentroidsResult cluster = s.cluster(trainData);
91  		
92  		ExactShortAssigner sass = new ExactShortAssigner(cluster);
93  		
94  		int[] pushed = sass.assign(data);
95  		
96  		File outFile = folder.newFile("RandomShort.cluster");
97  		File outSFile = folder.newFile("RandomShortSaveable.cluster");
98  		IOUtils.writeASCII(outFile, cluster);
99  		IOUtils.writeBinary(outSFile, cluster);
100 		
101 		ShortCentroidsResult sLoaded = IOUtils.read(outFile,new ShortCentroidsResult());
102 		ExactShortAssigner sLoadedAss = new ExactShortAssigner(sLoaded);
103 		ShortCentroidsResult ssaveLoaded = IOUtils.read(outSFile,new ShortCentroidsResult());
104 		ExactShortAssigner ssaveLoadedAss = new ExactShortAssigner(ssaveLoaded);
105 		
106 		int[] loadedPushed = sLoadedAss.assign(data);
107 		int[] loadedSaveablePushed = ssaveLoadedAss.assign(data);
108 		
109 		assertArrayEquals(loadedPushed, pushed);
110 		assertArrayEquals(loadedSaveablePushed, pushed);		
111 	}
112 
113 	/**
114 	 * tests the random set cluster algorithm
115 	 * @throws IOException
116 	 */
117 	@Test public void testRandomSetShortCluster() throws IOException{
118 		short [][] data = {
119 				{0,0,0},
120 				{0,0,0},
121 				{0,0,-1},
122 				{-1,0,-1},
123 				{1,0,1},
124 				{2,2,2},
125 				{2,2,2},
126 				{3,2,2},
127 				{3,2,3},
128 				{1,2,1},
129 		};
130 		RandomShortClusterer s = new RandomSetShortClusterer(data[0].length);
131 		s.setSeed(2);
132 		int[] trainDataIndex = RandomData.getUniqueRandomIntsA(2, 0, data.length, new Random(2));
133 		short[][] trainData = new short[trainDataIndex.length][];
134 		for(int i = 0; i < trainDataIndex.length; i++){
135 			trainData[i] = data[trainDataIndex[i]];
136 		}
137 		
138 		ShortCentroidsResult cluster = s.cluster(trainData);
139 		
140 		ExactShortAssigner sass = new ExactShortAssigner(cluster);
141 		
142 		int[] pushed = sass.assign(data);
143 		
144 		File outFile = folder.newFile("RandomShort2.cluster");
145 		File outSFile = folder.newFile("RandomShortSaveable2.cluster");
146 		IOUtils.writeASCII(outFile, cluster);
147 		IOUtils.writeBinary(outSFile, cluster);
148 		
149 		ShortCentroidsResult sLoaded = IOUtils.read(outFile,new ShortCentroidsResult());
150 		ExactShortAssigner sLoadedAss = new ExactShortAssigner(sLoaded);
151 		ShortCentroidsResult ssLoaded = IOUtils.read(outSFile,new ShortCentroidsResult());
152 		ExactShortAssigner ssLoadedAss = new ExactShortAssigner(ssLoaded);
153 		
154 		int[] loadedPushed = sLoadedAss.assign(data);
155 		int[] loadedSPushed = ssLoadedAss.assign(data);
156 		
157 		assertArrayEquals(loadedPushed,pushed);
158 		assertArrayEquals(loadedSPushed,pushed);		
159 	}
160 }