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 package org.openimaj.experiment.evaluation.cluster;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Map.Entry;
37
38 import org.openimaj.experiment.evaluation.AnalysisResult;
39 import org.openimaj.experiment.evaluation.Evaluator;
40 import org.openimaj.experiment.evaluation.cluster.analyser.ClusterAnalyser;
41 import org.openimaj.ml.clustering.dbscan.SparseMatrixDBSCAN;
42 import org.openimaj.util.function.Function;
43 import org.openimaj.util.iterator.UniformDoubleRangeIterable;
44
45 import ch.akuhn.matrix.SparseMatrix;
46
47
48
49
50
51
52
53
54
55 public class RangedDBSCANClusterEvaluator<D, T extends AnalysisResult>
56 implements
57 Evaluator<Map<Double, int[][]>, RangedAnalysisResult<Double, T>>
58 {
59
60 private int[][] correct;
61 private ClusterAnalyser<T> analyser;
62 private SparseMatrixDBSCAN gen;
63 private SparseMatrix data;
64 UniformDoubleRangeIterable r;
65
66
67
68
69
70
71
72
73
74
75 public RangedDBSCANClusterEvaluator(UniformDoubleRangeIterable r, SparseMatrixDBSCAN gen, SparseMatrix data,
76 int[][] clusters, ClusterAnalyser<T> analyser)
77 {
78 this.gen = gen;
79 this.correct = clusters;
80 this.analyser = analyser;
81 this.data = data;
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95 public <A, B> RangedDBSCANClusterEvaluator(UniformDoubleRangeIterable r, SparseMatrixDBSCAN gen, SparseMatrix data,
96 Map<A, ? extends List<B>> dataset, ClusterAnalyser<T> analyser)
97 {
98 this.r = r;
99 this.gen = gen;
100 this.correct = new int[dataset.size()][];
101 int j = 0;
102 int k = 0;
103 for (final Entry<A, ? extends List<B>> es : dataset.entrySet()) {
104 this.correct[j] = new int[es.getValue().size()];
105 int i = 0;
106 final List<B> value = es.getValue();
107 for (int l = 0; l < value.size(); l++) {
108 this.correct[j][i++] = k;
109 k++;
110 }
111 j++;
112 }
113 this.analyser = analyser;
114 this.data = data;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 public <A, B> RangedDBSCANClusterEvaluator(
129 UniformDoubleRangeIterable r,
130 SparseMatrixDBSCAN gen,
131 SparseMatrix data,
132 Function<B, Integer> indexFunc,
133 Map<A, ? extends List<B>> dataset,
134 ClusterAnalyser<T> analyser)
135 {
136 this.r = r;
137 this.gen = gen;
138 this.correct = new int[dataset.size()][];
139 int j = 0;
140 for (final Entry<A, ? extends List<B>> es : dataset.entrySet()) {
141 this.correct[j] = new int[es.getValue().size()];
142 int i = 0;
143 final List<B> value = es.getValue();
144 for (final B b : value) {
145 this.correct[j][i++] = indexFunc.apply(b);
146 }
147 j++;
148 }
149 this.analyser = analyser;
150 this.data = data;
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164 public <A, B> RangedDBSCANClusterEvaluator(
165 UniformDoubleRangeIterable r,
166 SparseMatrixDBSCAN gen,
167 Map<A, ? extends List<B>> dataset,
168 Function<List<B>, SparseMatrix> transform,
169 ClusterAnalyser<T> analyser)
170 {
171 this.r = r;
172 this.gen = gen;
173 this.analyser = analyser;
174 this.correct = new int[dataset.size()][];
175 int j = 0;
176 final List<B> flattened = new ArrayList<B>();
177 for (final Entry<A, ? extends List<B>> es : dataset.entrySet()) {
178 this.correct[j] = new int[es.getValue().size()];
179 int i = 0;
180 for (final B b : es.getValue()) {
181 this.correct[j][i++] = flattened.size();
182 flattened.add(b);
183 }
184 j++;
185 }
186 this.data = transform.apply(flattened);
187 }
188
189 @Override
190 public Map<Double, int[][]> evaluate() {
191 final Map<Double, int[][]> ret = new HashMap<Double, int[][]>();
192 for (final Double eps : this.r) {
193 this.gen.setEps(eps);
194 ret.put(eps, new ClusterEvaluator<SparseMatrix, T>(gen, data, correct, analyser).evaluate());
195 }
196 return ret;
197 }
198
199 @Override
200 public RangedAnalysisResult<Double, T> analyse(Map<Double, int[][]> estimated) {
201 final RangedAnalysisResult<Double, T> ret = new RangedAnalysisResult<Double, T>();
202 for (final Entry<Double, int[][]> ent : estimated.entrySet()) {
203 ret.put(ent.getKey(), this.analyser.analyse(correct, ent.getValue()));
204 }
205 return ret;
206 }
207
208 }