1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
53
54
55
56
57
58 public class RandomShortClustererTest {
59
60
61
62 @Rule
63 public TemporaryFolder folder = new TemporaryFolder();
64
65
66
67
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
115
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 }