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 | * ModelPerformanceChart.java |
---|
19 | * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.beans; |
---|
24 | |
---|
25 | import weka.core.Instances; |
---|
26 | import weka.gui.visualize.PlotData2D; |
---|
27 | import weka.gui.visualize.VisualizePanel; |
---|
28 | |
---|
29 | import java.awt.BorderLayout; |
---|
30 | import java.beans.PropertyChangeListener; |
---|
31 | import java.beans.VetoableChangeListener; |
---|
32 | import java.beans.beancontext.BeanContext; |
---|
33 | import java.beans.beancontext.BeanContextChild; |
---|
34 | import java.beans.beancontext.BeanContextChildSupport; |
---|
35 | import java.io.Serializable; |
---|
36 | import java.util.Enumeration; |
---|
37 | import java.util.Vector; |
---|
38 | |
---|
39 | import javax.swing.JFrame; |
---|
40 | import javax.swing.JPanel; |
---|
41 | |
---|
42 | /** |
---|
43 | * Bean that can be used for displaying threshold curves (e.g. ROC |
---|
44 | * curves) and scheme error plots |
---|
45 | * |
---|
46 | * @author Mark Hall |
---|
47 | * @version $Revision: 4762 $ |
---|
48 | */ |
---|
49 | public class ModelPerformanceChart |
---|
50 | extends JPanel |
---|
51 | implements ThresholdDataListener, VisualizableErrorListener, |
---|
52 | Visible, UserRequestAcceptor, |
---|
53 | Serializable, BeanContextChild { |
---|
54 | |
---|
55 | /** for serialization */ |
---|
56 | private static final long serialVersionUID = -4602034200071195924L; |
---|
57 | |
---|
58 | protected BeanVisual m_visual; |
---|
59 | |
---|
60 | protected transient PlotData2D m_masterPlot; |
---|
61 | |
---|
62 | protected transient JFrame m_popupFrame; |
---|
63 | |
---|
64 | protected boolean m_framePoppedUp = false; |
---|
65 | |
---|
66 | /** |
---|
67 | * True if this bean's appearance is the design mode appearance |
---|
68 | */ |
---|
69 | protected boolean m_design; |
---|
70 | |
---|
71 | /** |
---|
72 | * BeanContex that this bean might be contained within |
---|
73 | */ |
---|
74 | protected transient BeanContext m_beanContext = null; |
---|
75 | |
---|
76 | private transient VisualizePanel m_visPanel; |
---|
77 | |
---|
78 | /** |
---|
79 | * BeanContextChild support |
---|
80 | */ |
---|
81 | protected BeanContextChildSupport m_bcSupport = |
---|
82 | new BeanContextChildSupport(this); |
---|
83 | |
---|
84 | public ModelPerformanceChart() { |
---|
85 | java.awt.GraphicsEnvironment ge = |
---|
86 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
87 | if (!ge.isHeadless()) { |
---|
88 | appearanceFinal(); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Global info for this bean |
---|
94 | * |
---|
95 | * @return a <code>String</code> value |
---|
96 | */ |
---|
97 | public String globalInfo() { |
---|
98 | return "Visualize performance charts (such as ROC)."; |
---|
99 | } |
---|
100 | |
---|
101 | protected void appearanceDesign() { |
---|
102 | removeAll(); |
---|
103 | m_visual = new BeanVisual("ModelPerformanceChart", |
---|
104 | BeanVisual.ICON_PATH+"ModelPerformanceChart.gif", |
---|
105 | BeanVisual.ICON_PATH |
---|
106 | +"ModelPerformanceChart_animated.gif"); |
---|
107 | setLayout(new BorderLayout()); |
---|
108 | add(m_visual, BorderLayout.CENTER); |
---|
109 | } |
---|
110 | |
---|
111 | protected void appearanceFinal() { |
---|
112 | removeAll(); |
---|
113 | setLayout(new BorderLayout()); |
---|
114 | setUpFinal(); |
---|
115 | } |
---|
116 | |
---|
117 | protected void setUpFinal() { |
---|
118 | if (m_visPanel == null) { |
---|
119 | m_visPanel = new VisualizePanel(); |
---|
120 | } |
---|
121 | add(m_visPanel, BorderLayout.CENTER); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Display a threshold curve. |
---|
126 | * |
---|
127 | * @param e a ThresholdDataEvent |
---|
128 | */ |
---|
129 | public synchronized void acceptDataSet(ThresholdDataEvent e) { |
---|
130 | if (m_visPanel == null) { |
---|
131 | m_visPanel = new VisualizePanel(); |
---|
132 | } |
---|
133 | if (m_masterPlot == null) { |
---|
134 | m_masterPlot = e.getDataSet(); |
---|
135 | } |
---|
136 | try { |
---|
137 | // check for compatable data sets |
---|
138 | if (!m_masterPlot.getPlotInstances().relationName(). |
---|
139 | equals(e.getDataSet().getPlotInstances().relationName())) { |
---|
140 | |
---|
141 | // if not equal then remove all plots and set as new master plot |
---|
142 | m_masterPlot = e.getDataSet(); |
---|
143 | m_visPanel.setMasterPlot(m_masterPlot); |
---|
144 | m_visPanel.validate(); m_visPanel.repaint(); |
---|
145 | } else { |
---|
146 | // add as new plot |
---|
147 | m_visPanel.addPlot(e.getDataSet()); |
---|
148 | m_visPanel.validate(); m_visPanel.repaint(); |
---|
149 | } |
---|
150 | m_visPanel.setXIndex(4); m_visPanel.setYIndex(5); |
---|
151 | } catch (Exception ex) { |
---|
152 | System.err.println("Problem setting up visualization (ModelPerformanceChart)"); |
---|
153 | ex.printStackTrace(); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Display a scheme error plot. |
---|
159 | * |
---|
160 | * @param e a VisualizableErrorEvent |
---|
161 | */ |
---|
162 | public synchronized void acceptDataSet(VisualizableErrorEvent e) { |
---|
163 | if (m_visPanel == null) { |
---|
164 | m_visPanel = new VisualizePanel(); |
---|
165 | } |
---|
166 | if (m_masterPlot == null) { |
---|
167 | m_masterPlot = e.getDataSet(); |
---|
168 | } |
---|
169 | try { |
---|
170 | m_visPanel.setMasterPlot(m_masterPlot); |
---|
171 | } catch (Exception ex) { |
---|
172 | System.err.println("Problem setting up visualization (ModelPerformanceChart)"); |
---|
173 | ex.printStackTrace(); |
---|
174 | } |
---|
175 | m_visPanel.validate(); |
---|
176 | m_visPanel.repaint(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Set the visual appearance of this bean |
---|
181 | * |
---|
182 | * @param newVisual a <code>BeanVisual</code> value |
---|
183 | */ |
---|
184 | public void setVisual(BeanVisual newVisual) { |
---|
185 | m_visual = newVisual; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Return the visual appearance of this bean |
---|
190 | */ |
---|
191 | public BeanVisual getVisual() { |
---|
192 | return m_visual; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * Use the default appearance for this bean |
---|
197 | */ |
---|
198 | public void useDefaultVisual() { |
---|
199 | m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultDataVisualizer.gif", |
---|
200 | BeanVisual.ICON_PATH+"DefaultDataVisualizer_animated.gif"); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Describe <code>enumerateRequests</code> method here. |
---|
205 | * |
---|
206 | * @return an <code>Enumeration</code> value |
---|
207 | */ |
---|
208 | public Enumeration enumerateRequests() { |
---|
209 | Vector newVector = new Vector(0); |
---|
210 | if (m_masterPlot != null) { |
---|
211 | newVector.addElement("Show chart"); |
---|
212 | newVector.addElement("?Clear all plots"); |
---|
213 | } |
---|
214 | return newVector.elements(); |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Add a property change listener to this bean |
---|
219 | * |
---|
220 | * @param name the name of the property of interest |
---|
221 | * @param pcl a <code>PropertyChangeListener</code> value |
---|
222 | */ |
---|
223 | public void addPropertyChangeListener(String name, |
---|
224 | PropertyChangeListener pcl) { |
---|
225 | m_bcSupport.addPropertyChangeListener(name, pcl); |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Remove a property change listener from this bean |
---|
230 | * |
---|
231 | * @param name the name of the property of interest |
---|
232 | * @param pcl a <code>PropertyChangeListener</code> value |
---|
233 | */ |
---|
234 | public void removePropertyChangeListener(String name, |
---|
235 | PropertyChangeListener pcl) { |
---|
236 | m_bcSupport.removePropertyChangeListener(name, pcl); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Add a vetoable change listener to this bean |
---|
241 | * |
---|
242 | * @param name the name of the property of interest |
---|
243 | * @param vcl a <code>VetoableChangeListener</code> value |
---|
244 | */ |
---|
245 | public void addVetoableChangeListener(String name, |
---|
246 | VetoableChangeListener vcl) { |
---|
247 | m_bcSupport.addVetoableChangeListener(name, vcl); |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Remove a vetoable change listener from this bean |
---|
252 | * |
---|
253 | * @param name the name of the property of interest |
---|
254 | * @param vcl a <code>VetoableChangeListener</code> value |
---|
255 | */ |
---|
256 | public void removeVetoableChangeListener(String name, |
---|
257 | VetoableChangeListener vcl) { |
---|
258 | m_bcSupport.removeVetoableChangeListener(name, vcl); |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * Set a bean context for this bean |
---|
263 | * |
---|
264 | * @param bc a <code>BeanContext</code> value |
---|
265 | */ |
---|
266 | public void setBeanContext(BeanContext bc) { |
---|
267 | m_beanContext = bc; |
---|
268 | m_design = m_beanContext.isDesignTime(); |
---|
269 | if (m_design) { |
---|
270 | appearanceDesign(); |
---|
271 | } else { |
---|
272 | java.awt.GraphicsEnvironment ge = |
---|
273 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
274 | if (!ge.isHeadless()) { |
---|
275 | appearanceFinal(); |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Return the bean context (if any) that this bean is embedded in |
---|
282 | * |
---|
283 | * @return a <code>BeanContext</code> value |
---|
284 | */ |
---|
285 | public BeanContext getBeanContext() { |
---|
286 | return m_beanContext; |
---|
287 | } |
---|
288 | |
---|
289 | /** |
---|
290 | * Describe <code>performRequest</code> method here. |
---|
291 | * |
---|
292 | * @param request a <code>String</code> value |
---|
293 | * @exception IllegalArgumentException if an error occurs |
---|
294 | */ |
---|
295 | public void performRequest(String request) { |
---|
296 | if (request.compareTo("Show chart") == 0) { |
---|
297 | try { |
---|
298 | // popup visualize panel |
---|
299 | if (!m_framePoppedUp) { |
---|
300 | m_framePoppedUp = true; |
---|
301 | |
---|
302 | final javax.swing.JFrame jf = |
---|
303 | new javax.swing.JFrame("Model Performance Chart"); |
---|
304 | jf.setSize(800,600); |
---|
305 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
306 | jf.getContentPane().add(m_visPanel, BorderLayout.CENTER); |
---|
307 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
308 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
309 | jf.dispose(); |
---|
310 | m_framePoppedUp = false; |
---|
311 | } |
---|
312 | }); |
---|
313 | jf.setVisible(true); |
---|
314 | m_popupFrame = jf; |
---|
315 | } else { |
---|
316 | m_popupFrame.toFront(); |
---|
317 | } |
---|
318 | } catch (Exception ex) { |
---|
319 | ex.printStackTrace(); |
---|
320 | m_framePoppedUp = false; |
---|
321 | } |
---|
322 | } else if (request.equals("Clear all plots")) { |
---|
323 | m_visPanel.removeAllPlots(); |
---|
324 | m_visPanel.validate(); m_visPanel.repaint(); |
---|
325 | m_visPanel = null; |
---|
326 | m_masterPlot = null; |
---|
327 | } else { |
---|
328 | throw new IllegalArgumentException(request |
---|
329 | + " not supported (Model Performance Chart)"); |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | public static void main(String [] args) { |
---|
334 | try { |
---|
335 | if (args.length != 1) { |
---|
336 | System.err.println("Usage: ModelPerformanceChart <dataset>"); |
---|
337 | System.exit(1); |
---|
338 | } |
---|
339 | java.io.Reader r = new java.io.BufferedReader( |
---|
340 | new java.io.FileReader(args[0])); |
---|
341 | Instances inst = new Instances(r); |
---|
342 | final javax.swing.JFrame jf = new javax.swing.JFrame(); |
---|
343 | jf.getContentPane().setLayout(new java.awt.BorderLayout()); |
---|
344 | final ModelPerformanceChart as = new ModelPerformanceChart(); |
---|
345 | PlotData2D pd = new PlotData2D(inst); |
---|
346 | pd.setPlotName(inst.relationName()); |
---|
347 | ThresholdDataEvent roc = new ThresholdDataEvent(as, pd); |
---|
348 | as.acceptDataSet(roc); |
---|
349 | |
---|
350 | jf.getContentPane().add(as, java.awt.BorderLayout.CENTER); |
---|
351 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
352 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
353 | jf.dispose(); |
---|
354 | System.exit(0); |
---|
355 | } |
---|
356 | }); |
---|
357 | jf.setSize(800,600); |
---|
358 | jf.setVisible(true); |
---|
359 | } catch (Exception ex) { |
---|
360 | ex.printStackTrace(); |
---|
361 | System.err.println(ex.getMessage()); |
---|
362 | } |
---|
363 | } |
---|
364 | } |
---|