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 | * AttributeSummarizer.java |
---|
19 | * Copyright (C) 2003 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.beans; |
---|
24 | |
---|
25 | import weka.core.Instances; |
---|
26 | import weka.gui.AttributeVisualizationPanel; |
---|
27 | |
---|
28 | import java.awt.BorderLayout; |
---|
29 | import java.awt.Dimension; |
---|
30 | import java.awt.Font; |
---|
31 | import java.awt.GridLayout; |
---|
32 | import java.beans.beancontext.BeanContext; |
---|
33 | import java.util.Enumeration; |
---|
34 | import java.util.Vector; |
---|
35 | |
---|
36 | import javax.swing.BorderFactory; |
---|
37 | import javax.swing.JPanel; |
---|
38 | import javax.swing.JScrollPane; |
---|
39 | |
---|
40 | /** |
---|
41 | * Bean that encapsulates displays bar graph summaries for attributes in |
---|
42 | * a data set. |
---|
43 | * |
---|
44 | * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a> |
---|
45 | * @version $Revision: 1.11 $ |
---|
46 | */ |
---|
47 | public class AttributeSummarizer |
---|
48 | extends DataVisualizer { |
---|
49 | |
---|
50 | /** for serialization */ |
---|
51 | private static final long serialVersionUID = -294354961169372758L; |
---|
52 | |
---|
53 | /** |
---|
54 | * The number of plots horizontally in the display |
---|
55 | */ |
---|
56 | protected int m_gridWidth = 4; |
---|
57 | |
---|
58 | /** |
---|
59 | * The maximum number of plots to show |
---|
60 | */ |
---|
61 | protected int m_maxPlots = 100; |
---|
62 | |
---|
63 | /** |
---|
64 | * Index on which to color the plots. |
---|
65 | */ |
---|
66 | protected int m_coloringIndex = -1; |
---|
67 | |
---|
68 | /** |
---|
69 | * Creates a new <code>AttributeSummarizer</code> instance. |
---|
70 | */ |
---|
71 | public AttributeSummarizer() { |
---|
72 | java.awt.GraphicsEnvironment ge = |
---|
73 | java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
74 | if (!ge.isHeadless()) { |
---|
75 | appearanceFinal(); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Global info for this bean |
---|
81 | * |
---|
82 | * @return a <code>String</code> value |
---|
83 | */ |
---|
84 | public String globalInfo() { |
---|
85 | return "Plot summary bar charts for incoming data/training/test sets."; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Set the coloring index for the attribute summary plots |
---|
90 | * |
---|
91 | * @param ci an <code>int</code> value |
---|
92 | */ |
---|
93 | public void setColoringIndex(int ci) { |
---|
94 | m_coloringIndex = ci; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Return the coloring index for the attribute summary plots |
---|
99 | * |
---|
100 | * @return an <code>int</code> value |
---|
101 | */ |
---|
102 | public int getColoringIndex() { |
---|
103 | return m_coloringIndex; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Set the width of the grid of plots |
---|
108 | * |
---|
109 | * @param gw the width of the grid |
---|
110 | */ |
---|
111 | public void setGridWidth(int gw) { |
---|
112 | if (gw > 0) { |
---|
113 | m_bcSupport.firePropertyChange("gridWidth", new Integer(m_gridWidth), |
---|
114 | new Integer(gw)); |
---|
115 | m_gridWidth = gw; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Get the width of the grid of plots |
---|
121 | * |
---|
122 | * @return the grid width |
---|
123 | */ |
---|
124 | public int getGridWidth() { |
---|
125 | return m_gridWidth; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Set the maximum number of plots to display |
---|
130 | * |
---|
131 | * @param mp the number of plots to display |
---|
132 | */ |
---|
133 | public void setMaxPlots(int mp) { |
---|
134 | if (mp > 0) { |
---|
135 | m_bcSupport.firePropertyChange("maxPlots", new Integer(m_maxPlots), |
---|
136 | new Integer(mp)); |
---|
137 | m_maxPlots = mp; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Get the number of plots to display |
---|
143 | * |
---|
144 | * @return the number of plots to display |
---|
145 | */ |
---|
146 | public int getMaxPlots() { |
---|
147 | return m_maxPlots; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Set whether the appearance of this bean should be design or |
---|
152 | * application |
---|
153 | * |
---|
154 | * @param design true if bean should appear in design mode |
---|
155 | */ |
---|
156 | public void setDesign(boolean design) { |
---|
157 | m_design = true; |
---|
158 | appearanceDesign(); |
---|
159 | } |
---|
160 | |
---|
161 | protected void appearanceDesign() { |
---|
162 | removeAll(); |
---|
163 | m_visual = |
---|
164 | new BeanVisual("AttributeSummarizer", |
---|
165 | BeanVisual.ICON_PATH+"AttributeSummarizer.gif", |
---|
166 | BeanVisual.ICON_PATH+"AttributeSummarizer_animated.gif"); |
---|
167 | setLayout(new BorderLayout()); |
---|
168 | add(m_visual, BorderLayout.CENTER); |
---|
169 | } |
---|
170 | |
---|
171 | protected void appearanceFinal() { |
---|
172 | removeAll(); |
---|
173 | setLayout(new BorderLayout()); |
---|
174 | } |
---|
175 | |
---|
176 | protected void setUpFinal() { |
---|
177 | removeAll(); |
---|
178 | JScrollPane hp = makePanel(); |
---|
179 | add(hp, BorderLayout.CENTER); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Use the default appearance for this bean |
---|
184 | */ |
---|
185 | public void useDefaultVisual() { |
---|
186 | m_visual.loadIcons(BeanVisual.ICON_PATH+"DefaultDataVisualizer.gif", |
---|
187 | BeanVisual.ICON_PATH+"DefaultDataVisualizer_animated.gif"); |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Return an enumeration of actions that the user can ask this bean to |
---|
192 | * perform |
---|
193 | * |
---|
194 | * @return an <code>Enumeration</code> value |
---|
195 | */ |
---|
196 | public Enumeration enumerateRequests() { |
---|
197 | Vector newVector = new Vector(0); |
---|
198 | if (m_visualizeDataSet != null) { |
---|
199 | newVector.addElement("Show summaries"); |
---|
200 | } |
---|
201 | return newVector.elements(); |
---|
202 | } |
---|
203 | |
---|
204 | private JScrollPane makePanel() { |
---|
205 | String fontFamily = this.getFont().getFamily(); |
---|
206 | Font newFont = new Font(fontFamily, Font.PLAIN, 10); |
---|
207 | JPanel hp = new JPanel(); |
---|
208 | hp.setFont(newFont); |
---|
209 | int numPlots = Math.min(m_visualizeDataSet.numAttributes(), m_maxPlots); |
---|
210 | int gridHeight = numPlots / m_gridWidth; |
---|
211 | |
---|
212 | if (numPlots % m_gridWidth != 0) { |
---|
213 | gridHeight++; |
---|
214 | } |
---|
215 | hp.setLayout(new GridLayout(gridHeight, 4)); |
---|
216 | for (int i = 0; i < numPlots; i++) { |
---|
217 | JPanel temp = new JPanel(); |
---|
218 | temp.setLayout(new BorderLayout()); |
---|
219 | temp.setBorder(BorderFactory.createTitledBorder(m_visualizeDataSet. |
---|
220 | attribute(i).name())); |
---|
221 | |
---|
222 | AttributeVisualizationPanel ap = new AttributeVisualizationPanel(); |
---|
223 | ap.setInstances(m_visualizeDataSet); |
---|
224 | if (m_coloringIndex < 0 && m_visualizeDataSet.classIndex() >= 0) { |
---|
225 | ap.setColoringIndex(m_visualizeDataSet.classIndex()); |
---|
226 | } else { |
---|
227 | ap.setColoringIndex(m_coloringIndex); |
---|
228 | } |
---|
229 | temp.add(ap, BorderLayout.CENTER); |
---|
230 | ap.setAttribute(i); |
---|
231 | hp.add(temp); |
---|
232 | } |
---|
233 | |
---|
234 | Dimension d = new Dimension(830, gridHeight * 100); |
---|
235 | hp.setMinimumSize(d); |
---|
236 | hp.setMaximumSize(d); |
---|
237 | hp.setPreferredSize(d); |
---|
238 | |
---|
239 | JScrollPane scroller = new JScrollPane(hp); |
---|
240 | |
---|
241 | return scroller; |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * Set a bean context for this bean |
---|
246 | * |
---|
247 | * @param bc a <code>BeanContext</code> value |
---|
248 | */ |
---|
249 | /* public void setBeanContext(BeanContext bc) { |
---|
250 | m_beanContext = bc; |
---|
251 | m_design = m_beanContext.isDesignTime(); |
---|
252 | if (m_design) { |
---|
253 | appearanceDesign(); |
---|
254 | } |
---|
255 | } */ |
---|
256 | |
---|
257 | /** |
---|
258 | * Set instances for this bean. This method is a convenience method |
---|
259 | * for clients who use this component programatically |
---|
260 | * |
---|
261 | * @param inst an <code>Instances</code> value |
---|
262 | * @exception Exception if an error occurs |
---|
263 | */ |
---|
264 | public void setInstances(Instances inst) throws Exception { |
---|
265 | if (m_design) { |
---|
266 | throw new Exception("This method is not to be used during design " |
---|
267 | +"time. It is meant to be used if this " |
---|
268 | +"bean is being used programatically as as " |
---|
269 | +"stand alone component."); |
---|
270 | } |
---|
271 | m_visualizeDataSet = inst; |
---|
272 | setUpFinal(); |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * Perform a named user request |
---|
277 | * |
---|
278 | * @param request a string containing the name of the request to perform |
---|
279 | * @exception IllegalArgumentException if request is not supported |
---|
280 | */ |
---|
281 | public void performRequest(String request) { |
---|
282 | if (m_design == false) { |
---|
283 | setUpFinal(); |
---|
284 | return; |
---|
285 | } |
---|
286 | if (request.compareTo("Show summaries") == 0) { |
---|
287 | try { |
---|
288 | // popup matrix panel |
---|
289 | if (!m_framePoppedUp) { |
---|
290 | m_framePoppedUp = true; |
---|
291 | final JScrollPane holderP = makePanel(); |
---|
292 | |
---|
293 | final javax.swing.JFrame jf = |
---|
294 | new javax.swing.JFrame("Visualize"); |
---|
295 | jf.setSize(800,600); |
---|
296 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
297 | jf.getContentPane().add(holderP, BorderLayout.CENTER); |
---|
298 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
299 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
300 | jf.dispose(); |
---|
301 | m_framePoppedUp = false; |
---|
302 | } |
---|
303 | }); |
---|
304 | jf.setVisible(true); |
---|
305 | m_popupFrame = jf; |
---|
306 | } else { |
---|
307 | m_popupFrame.toFront(); |
---|
308 | } |
---|
309 | } catch (Exception ex) { |
---|
310 | ex.printStackTrace(); |
---|
311 | m_framePoppedUp = false; |
---|
312 | } |
---|
313 | } else { |
---|
314 | throw new IllegalArgumentException(request |
---|
315 | + " not supported (AttributeSummarizer)"); |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | public static void main(String [] args) { |
---|
320 | try { |
---|
321 | if (args.length != 1) { |
---|
322 | System.err.println("Usage: AttributeSummarizer <dataset>"); |
---|
323 | System.exit(1); |
---|
324 | } |
---|
325 | java.io.Reader r = new java.io.BufferedReader( |
---|
326 | new java.io.FileReader(args[0])); |
---|
327 | Instances inst = new Instances(r); |
---|
328 | final javax.swing.JFrame jf = new javax.swing.JFrame(); |
---|
329 | jf.getContentPane().setLayout(new java.awt.BorderLayout()); |
---|
330 | final AttributeSummarizer as = new AttributeSummarizer(); |
---|
331 | as.setInstances(inst); |
---|
332 | |
---|
333 | jf.getContentPane().add(as, java.awt.BorderLayout.CENTER); |
---|
334 | jf.addWindowListener(new java.awt.event.WindowAdapter() { |
---|
335 | public void windowClosing(java.awt.event.WindowEvent e) { |
---|
336 | jf.dispose(); |
---|
337 | System.exit(0); |
---|
338 | } |
---|
339 | }); |
---|
340 | jf.setSize(830,600); |
---|
341 | jf.setVisible(true); |
---|
342 | } catch (Exception ex) { |
---|
343 | ex.printStackTrace(); |
---|
344 | System.err.println(ex.getMessage()); |
---|
345 | } |
---|
346 | } |
---|
347 | } |
---|