1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * IncrementalClassifierEvaluator.java |
---|
19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.beans; |
---|
24 | |
---|
25 | import weka.classifiers.Classifier; |
---|
26 | import weka.classifiers.AbstractClassifier; |
---|
27 | import weka.classifiers.Evaluation; |
---|
28 | import weka.core.Instance; |
---|
29 | import weka.core.Instances; |
---|
30 | import weka.core.Utils; |
---|
31 | |
---|
32 | import java.util.Vector; |
---|
33 | |
---|
34 | /** |
---|
35 | * Bean that evaluates incremental classifiers |
---|
36 | * |
---|
37 | * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a> |
---|
38 | * @version $Revision: 5928 $ |
---|
39 | */ |
---|
40 | public class IncrementalClassifierEvaluator |
---|
41 | extends AbstractEvaluator |
---|
42 | implements IncrementalClassifierListener, |
---|
43 | EventConstraints { |
---|
44 | |
---|
45 | /** for serialization */ |
---|
46 | private static final long serialVersionUID = -3105419818939541291L; |
---|
47 | |
---|
48 | private transient Evaluation m_eval; |
---|
49 | |
---|
50 | private transient Classifier m_classifier; |
---|
51 | |
---|
52 | private Vector m_listeners = new Vector(); |
---|
53 | private Vector m_textListeners = new Vector(); |
---|
54 | |
---|
55 | private Vector m_dataLegend = new Vector(); |
---|
56 | |
---|
57 | private ChartEvent m_ce = new ChartEvent(this); |
---|
58 | private double [] m_dataPoint = new double[1]; |
---|
59 | private boolean m_reset = false; |
---|
60 | |
---|
61 | private double m_min = Double.MAX_VALUE; |
---|
62 | private double m_max = Double.MIN_VALUE; |
---|
63 | |
---|
64 | // how often to report # instances processed to the log |
---|
65 | private int m_statusFrequency = 100; |
---|
66 | private int m_instanceCount = 0; |
---|
67 | |
---|
68 | // output info retrieval and auc stats for each class (if class is nominal) |
---|
69 | private boolean m_outputInfoRetrievalStats = false; |
---|
70 | |
---|
71 | public IncrementalClassifierEvaluator() { |
---|
72 | m_visual.loadIcons(BeanVisual.ICON_PATH |
---|
73 | +"IncrementalClassifierEvaluator.gif", |
---|
74 | BeanVisual.ICON_PATH |
---|
75 | +"IncrementalClassifierEvaluator_animated.gif"); |
---|
76 | m_visual.setText("IncrementalClassifierEvaluator"); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Set a custom (descriptive) name for this bean |
---|
81 | * |
---|
82 | * @param name the name to use |
---|
83 | */ |
---|
84 | public void setCustomName(String name) { |
---|
85 | m_visual.setText(name); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Get the custom (descriptive) name for this bean (if one has been set) |
---|
90 | * |
---|
91 | * @return the custom name (or the default name) |
---|
92 | */ |
---|
93 | public String getCustomName() { |
---|
94 | return m_visual.getText(); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Global info for this bean |
---|
99 | * |
---|
100 | * @return a <code>String</code> value |
---|
101 | */ |
---|
102 | public String globalInfo() { |
---|
103 | return "Evaluate the performance of incrementally trained classifiers."; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Accepts and processes a classifier encapsulated in an incremental |
---|
108 | * classifier event |
---|
109 | * |
---|
110 | * @param ce an <code>IncrementalClassifierEvent</code> value |
---|
111 | */ |
---|
112 | public void acceptClassifier(final IncrementalClassifierEvent ce) { |
---|
113 | try { |
---|
114 | if (ce.getStatus() == IncrementalClassifierEvent.NEW_BATCH) { |
---|
115 | // m_eval = new Evaluation(ce.getCurrentInstance().dataset()); |
---|
116 | m_eval = new Evaluation(ce.getStructure()); |
---|
117 | m_eval.useNoPriors(); |
---|
118 | |
---|
119 | m_dataLegend = new Vector(); |
---|
120 | m_reset = true; |
---|
121 | m_dataPoint = new double[0]; |
---|
122 | Instances inst = ce.getStructure(); |
---|
123 | System.err.println("NEW BATCH"); |
---|
124 | m_instanceCount = 0; |
---|
125 | if (m_logger != null) { |
---|
126 | m_logger.statusMessage(statusMessagePrefix() |
---|
127 | + "IncrementalClassifierEvaluator: started processing..."); |
---|
128 | m_logger.logMessage("[IncrementalClassifierEvaluator]" + |
---|
129 | statusMessagePrefix() + " started processing..."); |
---|
130 | } |
---|
131 | /* if (inst.classIndex() >= 0) { |
---|
132 | if (inst.attribute(inst.classIndex()).isNominal()) { |
---|
133 | if (inst.isMissing(inst.classIndex())) { |
---|
134 | m_dataLegend.addElement("Confidence"); |
---|
135 | } else { |
---|
136 | m_dataLegend.addElement("Accuracy"); |
---|
137 | } |
---|
138 | } else { |
---|
139 | if (inst.isMissing(inst.classIndex())) { |
---|
140 | m_dataLegend.addElement("Prediction"); |
---|
141 | } else { |
---|
142 | m_dataLegend.addElement("RRSE"); |
---|
143 | } |
---|
144 | } |
---|
145 | } */ |
---|
146 | } else { |
---|
147 | if (m_instanceCount > 0 && m_instanceCount % m_statusFrequency == 0) { |
---|
148 | if (m_logger != null) { |
---|
149 | m_logger.statusMessage(statusMessagePrefix() + "Processed " |
---|
150 | + m_instanceCount + " instances."); |
---|
151 | } |
---|
152 | } |
---|
153 | m_instanceCount++; |
---|
154 | Instance inst = ce.getCurrentInstance(); |
---|
155 | // if (inst.attribute(inst.classIndex()).isNominal()) { |
---|
156 | double [] dist = ce.getClassifier().distributionForInstance(inst); |
---|
157 | double pred = 0; |
---|
158 | if (!inst.isMissing(inst.classIndex())) { |
---|
159 | if (m_outputInfoRetrievalStats) { |
---|
160 | // store predictions so AUC etc can be output. |
---|
161 | m_eval.evaluateModelOnceAndRecordPrediction(dist, inst); |
---|
162 | } else { |
---|
163 | m_eval.evaluateModelOnce(dist, inst); |
---|
164 | } |
---|
165 | } else { |
---|
166 | pred = ce.getClassifier().classifyInstance(inst); |
---|
167 | } |
---|
168 | if (inst.classIndex() >= 0) { |
---|
169 | // need to check that the class is not missing |
---|
170 | if (inst.attribute(inst.classIndex()).isNominal()) { |
---|
171 | if (!inst.isMissing(inst.classIndex())) { |
---|
172 | if (m_dataPoint.length < 2) { |
---|
173 | m_dataPoint = new double[2]; |
---|
174 | m_dataLegend.addElement("Accuracy"); |
---|
175 | m_dataLegend.addElement("RMSE (prob)"); |
---|
176 | } |
---|
177 | // int classV = (int) inst.value(inst.classIndex()); |
---|
178 | m_dataPoint[1] = m_eval.rootMeanSquaredError(); |
---|
179 | // int maxO = Utils.maxIndex(dist); |
---|
180 | // if (maxO == classV) { |
---|
181 | // dist[classV] = -1; |
---|
182 | // maxO = Utils.maxIndex(dist); |
---|
183 | // } |
---|
184 | // m_dataPoint[1] -= dist[maxO]; |
---|
185 | } else { |
---|
186 | if (m_dataPoint.length < 1) { |
---|
187 | m_dataPoint = new double[1]; |
---|
188 | m_dataLegend.addElement("Confidence"); |
---|
189 | } |
---|
190 | } |
---|
191 | double primaryMeasure = 0; |
---|
192 | if (!inst.isMissing(inst.classIndex())) { |
---|
193 | primaryMeasure = 1.0 - m_eval.errorRate(); |
---|
194 | } else { |
---|
195 | // record confidence as the primary measure |
---|
196 | // (another possibility would be entropy of |
---|
197 | // the distribution, or perhaps average |
---|
198 | // confidence) |
---|
199 | primaryMeasure = dist[Utils.maxIndex(dist)]; |
---|
200 | } |
---|
201 | // double [] dataPoint = new double[1]; |
---|
202 | m_dataPoint[0] = primaryMeasure; |
---|
203 | // double min = 0; double max = 100; |
---|
204 | /* ChartEvent e = |
---|
205 | new ChartEvent(IncrementalClassifierEvaluator.this, |
---|
206 | m_dataLegend, min, max, dataPoint); */ |
---|
207 | m_ce.setLegendText(m_dataLegend); |
---|
208 | m_ce.setMin(0); m_ce.setMax(1); |
---|
209 | m_ce.setDataPoint(m_dataPoint); |
---|
210 | m_ce.setReset(m_reset); |
---|
211 | m_reset = false; |
---|
212 | } else { |
---|
213 | // numeric class |
---|
214 | if (m_dataPoint.length < 1) { |
---|
215 | m_dataPoint = new double[1]; |
---|
216 | if (inst.isMissing(inst.classIndex())) { |
---|
217 | m_dataLegend.addElement("Prediction"); |
---|
218 | } else { |
---|
219 | m_dataLegend.addElement("RMSE"); |
---|
220 | } |
---|
221 | } |
---|
222 | if (!inst.isMissing(inst.classIndex())) { |
---|
223 | double update; |
---|
224 | if (!inst.isMissing(inst.classIndex())) { |
---|
225 | update = m_eval.rootMeanSquaredError(); |
---|
226 | } else { |
---|
227 | update = pred; |
---|
228 | } |
---|
229 | m_dataPoint[0] = update; |
---|
230 | if (update > m_max) { |
---|
231 | m_max = update; |
---|
232 | } |
---|
233 | if (update < m_min) { |
---|
234 | m_min = update; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | m_ce.setLegendText(m_dataLegend); |
---|
239 | m_ce.setMin((inst.isMissing(inst.classIndex()) |
---|
240 | ? m_min |
---|
241 | : 0)); |
---|
242 | m_ce.setMax(m_max); |
---|
243 | m_ce.setDataPoint(m_dataPoint); |
---|
244 | m_ce.setReset(m_reset); |
---|
245 | m_reset = false; |
---|
246 | } |
---|
247 | notifyChartListeners(m_ce); |
---|
248 | |
---|
249 | if (ce.getStatus() == IncrementalClassifierEvent.BATCH_FINISHED) { |
---|
250 | if (m_logger != null) { |
---|
251 | m_logger.logMessage("[IncrementalClassifierEvaluator]" |
---|
252 | + statusMessagePrefix() + " Finished processing."); |
---|
253 | m_logger.statusMessage(statusMessagePrefix() + "Done."); |
---|
254 | } |
---|
255 | if (m_textListeners.size() > 0) { |
---|
256 | String textTitle = ce.getClassifier().getClass().getName(); |
---|
257 | textTitle = |
---|
258 | textTitle.substring(textTitle.lastIndexOf('.')+1, |
---|
259 | textTitle.length()); |
---|
260 | String results = "=== Performance information ===\n\n" |
---|
261 | + "Scheme: " + textTitle + "\n" |
---|
262 | + "Relation: "+ inst.dataset().relationName() + "\n\n" |
---|
263 | + m_eval.toSummaryString(); |
---|
264 | if (inst.classIndex() >= 0 && |
---|
265 | inst.classAttribute().isNominal() && |
---|
266 | (m_outputInfoRetrievalStats)) { |
---|
267 | results += "\n" + m_eval.toClassDetailsString(); |
---|
268 | } |
---|
269 | |
---|
270 | if (inst.classIndex() >= 0 && |
---|
271 | inst.classAttribute().isNominal()) { |
---|
272 | results += "\n" + m_eval.toMatrixString(); |
---|
273 | } |
---|
274 | textTitle = "Results: " + textTitle; |
---|
275 | TextEvent te = |
---|
276 | new TextEvent(this, |
---|
277 | results, |
---|
278 | textTitle); |
---|
279 | notifyTextListeners(te); |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | } catch (Exception ex) { |
---|
285 | if (m_logger != null) { |
---|
286 | m_logger.logMessage("[IncrementalClassifierEvaluator]" |
---|
287 | + statusMessagePrefix() + " Error processing prediction " |
---|
288 | + ex.getMessage()); |
---|
289 | m_logger.statusMessage(statusMessagePrefix() |
---|
290 | + "ERROR: problem processing prediction (see log for details)"); |
---|
291 | } |
---|
292 | ex.printStackTrace(); |
---|
293 | stop(); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * Returns true, if at the current time, the named event could |
---|
299 | * be generated. Assumes that supplied event names are names of |
---|
300 | * events that could be generated by this bean. |
---|
301 | * |
---|
302 | * @param eventName the name of the event in question |
---|
303 | * @return true if the named event could be generated at this point in |
---|
304 | * time |
---|
305 | */ |
---|
306 | public boolean eventGeneratable(String eventName) { |
---|
307 | if (m_listenee == null) { |
---|
308 | return false; |
---|
309 | } |
---|
310 | |
---|
311 | if (m_listenee instanceof EventConstraints) { |
---|
312 | if (!((EventConstraints)m_listenee). |
---|
313 | eventGeneratable("incrementalClassifier")) { |
---|
314 | return false; |
---|
315 | } |
---|
316 | } |
---|
317 | return true; |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | * Stop all action |
---|
322 | */ |
---|
323 | public void stop() { |
---|
324 | // tell the listenee (upstream bean) to stop |
---|
325 | if (m_listenee instanceof BeanCommon) { |
---|
326 | // System.err.println("Listener is BeanCommon"); |
---|
327 | ((BeanCommon)m_listenee).stop(); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * Returns true if. at this time, the bean is busy with some |
---|
333 | * (i.e. perhaps a worker thread is performing some calculation). |
---|
334 | * |
---|
335 | * @return true if the bean is busy. |
---|
336 | */ |
---|
337 | public boolean isBusy() { |
---|
338 | return false; |
---|
339 | } |
---|
340 | |
---|
341 | private void notifyChartListeners(ChartEvent ce) { |
---|
342 | Vector l; |
---|
343 | synchronized (this) { |
---|
344 | l = (Vector)m_listeners.clone(); |
---|
345 | } |
---|
346 | if (l.size() > 0) { |
---|
347 | for(int i = 0; i < l.size(); i++) { |
---|
348 | ((ChartListener)l.elementAt(i)).acceptDataPoint(ce); |
---|
349 | } |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * Notify all text listeners of a TextEvent |
---|
355 | * |
---|
356 | * @param te a <code>TextEvent</code> value |
---|
357 | */ |
---|
358 | private void notifyTextListeners(TextEvent te) { |
---|
359 | Vector l; |
---|
360 | synchronized (this) { |
---|
361 | l = (Vector)m_textListeners.clone(); |
---|
362 | } |
---|
363 | if (l.size() > 0) { |
---|
364 | for(int i = 0; i < l.size(); i++) { |
---|
365 | // System.err.println("Notifying text listeners " |
---|
366 | // +"(ClassifierPerformanceEvaluator)"); |
---|
367 | ((TextListener)l.elementAt(i)).acceptText(te); |
---|
368 | } |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | /** |
---|
373 | * Set how often progress is reported to the status bar. |
---|
374 | * |
---|
375 | * @param s report progress every s instances |
---|
376 | */ |
---|
377 | public void setStatusFrequency(int s) { |
---|
378 | m_statusFrequency = s; |
---|
379 | } |
---|
380 | |
---|
381 | /** |
---|
382 | * Get how often progress is reported to the status bar. |
---|
383 | * |
---|
384 | * @return after how many instances, progress is reported to the |
---|
385 | * status bar |
---|
386 | */ |
---|
387 | public int getStatusFrequency() { |
---|
388 | return m_statusFrequency; |
---|
389 | } |
---|
390 | |
---|
391 | /** |
---|
392 | * Return a tip text string for this property |
---|
393 | * |
---|
394 | * @return a string for the tip text |
---|
395 | */ |
---|
396 | public String statusFrequencyTipText() { |
---|
397 | return "How often to report progress to the status bar."; |
---|
398 | } |
---|
399 | |
---|
400 | /** |
---|
401 | * Set whether to output per-class information retrieval |
---|
402 | * statistics (nominal class only). |
---|
403 | * |
---|
404 | * @param i true if info retrieval stats are to be output |
---|
405 | */ |
---|
406 | public void setOutputPerClassInfoRetrievalStats(boolean i) { |
---|
407 | m_outputInfoRetrievalStats = i; |
---|
408 | } |
---|
409 | |
---|
410 | /** |
---|
411 | * Get whether per-class information retrieval stats are to be output. |
---|
412 | * |
---|
413 | * @return true if info retrieval stats are to be output |
---|
414 | */ |
---|
415 | public boolean getOutputPerClassInfoRetrievalStats() { |
---|
416 | return m_outputInfoRetrievalStats; |
---|
417 | } |
---|
418 | |
---|
419 | /** |
---|
420 | * Return a tip text string for this property |
---|
421 | * |
---|
422 | * @return a string for the tip text |
---|
423 | */ |
---|
424 | public String outputPerClassInfoRetrievalStatsTipText() { |
---|
425 | return "Output per-class info retrieval stats. If set to true, predictions get " |
---|
426 | +"stored so that stats such as AUC can be computed. Note: this consumes some memory."; |
---|
427 | } |
---|
428 | |
---|
429 | /** |
---|
430 | * Add a chart listener |
---|
431 | * |
---|
432 | * @param cl a <code>ChartListener</code> value |
---|
433 | */ |
---|
434 | public synchronized void addChartListener(ChartListener cl) { |
---|
435 | m_listeners.addElement(cl); |
---|
436 | } |
---|
437 | |
---|
438 | /** |
---|
439 | * Remove a chart listener |
---|
440 | * |
---|
441 | * @param cl a <code>ChartListener</code> value |
---|
442 | */ |
---|
443 | public synchronized void removeChartListener(ChartListener cl) { |
---|
444 | m_listeners.remove(cl); |
---|
445 | } |
---|
446 | |
---|
447 | /** |
---|
448 | * Add a text listener |
---|
449 | * |
---|
450 | * @param cl a <code>TextListener</code> value |
---|
451 | */ |
---|
452 | public synchronized void addTextListener(TextListener cl) { |
---|
453 | m_textListeners.addElement(cl); |
---|
454 | } |
---|
455 | |
---|
456 | /** |
---|
457 | * Remove a text listener |
---|
458 | * |
---|
459 | * @param cl a <code>TextListener</code> value |
---|
460 | */ |
---|
461 | public synchronized void removeTextListener(TextListener cl) { |
---|
462 | m_textListeners.remove(cl); |
---|
463 | } |
---|
464 | |
---|
465 | private String statusMessagePrefix() { |
---|
466 | return getCustomName() + "$" + hashCode() + "|"; |
---|
467 | } |
---|
468 | } |
---|