source: src/main/java/weka/gui/InstancesSummaryPanel.java @ 18

Last change on this file since 18 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 7.9 KB
RevLine 
[4]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 *    InstancesSummaryPanel.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui;
24
25import weka.core.Instances;
26import weka.core.Utils;
27
28import java.awt.BorderLayout;
29import java.awt.GridBagConstraints;
30import java.awt.GridBagLayout;
31
32import javax.swing.BorderFactory;
33import javax.swing.JLabel;
34import javax.swing.JPanel;
35import javax.swing.SwingConstants;
36
37/**
38 * This panel just displays relation name, number of instances, and number of
39 * attributes.
40 *
41 * @author Len Trigg (trigg@cs.waikato.ac.nz)
42 * @version $Revision: 5297 $
43 */
44public class InstancesSummaryPanel
45  extends JPanel {
46
47  /** for serialization */
48  private static final long serialVersionUID = -5243579535296681063L;
49
50  /** Message shown when no instances have been loaded */
51  protected static final String NO_SOURCE = "None";
52
53  /** Displays the name of the relation */
54  protected JLabel m_RelationNameLab = new JLabel(NO_SOURCE);
55 
56  /** Displays the number of instances */
57  protected JLabel m_NumInstancesLab = new JLabel(NO_SOURCE);
58 
59  /** Displays the number of attributes */
60  protected JLabel m_NumAttributesLab = new JLabel(NO_SOURCE);
61 
62  /** Displays the sum of instance weights */
63  protected JLabel m_sumOfWeightsLab = new JLabel(NO_SOURCE);
64   
65  /** The instances we're playing with */
66  protected Instances m_Instances;
67 
68  /**
69   * Whether to display 0 or ? for the number of instances
70   * in cases where a dataset has only structure. Depending
71   * on where this panel is used from, the user may have
72   * loaded a dataset with no instances or a Loader that
73   * can read incrementally may be being used (in which case
74   * we don't know how many instances are in the dataset...
75   * yet).
76   */
77  protected boolean m_showZeroInstancesAsUnknown = false;
78
79  /**
80   * Creates the instances panel with no initial instances.
81   */
82  public InstancesSummaryPanel() {
83
84    GridBagLayout gbLayout = new GridBagLayout();
85    setLayout(gbLayout);
86    JLabel lab = new JLabel("Relation:", SwingConstants.RIGHT);
87    lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
88    GridBagConstraints gbConstraints = new GridBagConstraints();
89    gbConstraints.anchor = GridBagConstraints.EAST;
90    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
91    gbConstraints.gridy = 0;     gbConstraints.gridx = 0;
92    gbLayout.setConstraints(lab, gbConstraints);
93    add(lab);
94    gbConstraints = new GridBagConstraints();
95    gbConstraints.anchor = GridBagConstraints.WEST;
96    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
97    gbConstraints.gridy = 0;     gbConstraints.gridx = 1;
98    gbConstraints.weightx = 100; gbConstraints.gridwidth = 3;
99    gbLayout.setConstraints(m_RelationNameLab, gbConstraints);
100    add(m_RelationNameLab);
101    m_RelationNameLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 10));
102   
103    lab = new JLabel("Instances:", SwingConstants.RIGHT);
104    lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
105    gbConstraints = new GridBagConstraints();
106    gbConstraints.anchor = GridBagConstraints.EAST;
107    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
108    gbConstraints.gridy = 1;     gbConstraints.gridx = 0;
109    gbLayout.setConstraints(lab, gbConstraints);
110    add(lab);
111    gbConstraints = new GridBagConstraints();
112    gbConstraints.anchor = GridBagConstraints.WEST;
113    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
114    gbConstraints.gridy = 1;     gbConstraints.gridx = 1;
115    gbConstraints.weightx = 100;
116    gbLayout.setConstraints(m_NumInstancesLab, gbConstraints);
117    add(m_NumInstancesLab);
118    m_NumInstancesLab.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 10));
119
120    lab = new JLabel("Attributes:", SwingConstants.RIGHT);
121    lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
122    gbConstraints = new GridBagConstraints();
123    gbConstraints.anchor = GridBagConstraints.EAST;
124    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
125    gbConstraints.gridy = 0;     gbConstraints.gridx = 2;
126    gbLayout.setConstraints(lab, gbConstraints);
127    add(lab);
128    gbConstraints = new GridBagConstraints();
129    gbConstraints.anchor = GridBagConstraints.WEST;
130    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
131    gbConstraints.gridy = 0;     gbConstraints.gridx = 3;
132    gbConstraints.weightx = 100;
133    gbLayout.setConstraints(m_NumAttributesLab, gbConstraints);
134    add(m_NumAttributesLab);
135    m_NumAttributesLab.setBorder(BorderFactory.createEmptyBorder(0, 5,
136                                                                 0, 10));
137   
138    lab = new JLabel("Sum of weights:", SwingConstants.RIGHT);
139    lab.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
140    gbConstraints = new GridBagConstraints();
141    gbConstraints.anchor = GridBagConstraints.EAST;
142    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
143    gbConstraints.gridy = 1;     gbConstraints.gridx = 2;
144    gbLayout.setConstraints(lab, gbConstraints);
145    add(lab);
146    gbConstraints = new GridBagConstraints();
147    gbConstraints.anchor = GridBagConstraints.WEST;
148    gbConstraints.fill = GridBagConstraints.HORIZONTAL;
149    gbConstraints.gridy = 1;     gbConstraints.gridx = 3;
150    gbConstraints.weightx = 100;
151    gbLayout.setConstraints(m_sumOfWeightsLab, gbConstraints);
152    add(m_sumOfWeightsLab);
153    m_sumOfWeightsLab.setBorder(BorderFactory.createEmptyBorder(0, 5,
154                                                                 0, 10));
155   
156  }
157 
158  public void setShowZeroInstancesAsUnknown(boolean zeroAsUnknown) {
159    m_showZeroInstancesAsUnknown = zeroAsUnknown;
160  }
161 
162  public boolean getShowZeroInstancesAsUnknown() {
163    return m_showZeroInstancesAsUnknown;
164  }
165
166  /**
167   * Tells the panel to use a new set of instances.
168   *
169   * @param inst a set of Instances
170   */
171  public void setInstances(Instances inst) {
172    m_Instances = inst;
173    m_RelationNameLab.setText(m_Instances.relationName());
174    m_NumInstancesLab.setText("" + 
175        ((m_showZeroInstancesAsUnknown && m_Instances.numInstances() == 0) 
176            ? "?" 
177            : "" + m_Instances.numInstances()));
178    m_NumAttributesLab.setText("" + m_Instances.numAttributes());
179    m_sumOfWeightsLab.setText("" + 
180        ((m_showZeroInstancesAsUnknown && m_Instances.numInstances() == 0) 
181            ? "?" 
182            : "" + Utils.doubleToString(m_Instances.sumOfWeights(), 3)));
183  }
184
185  /**
186   * Tests out the instance summary panel from the command line.
187   *
188   * @param args optional name of dataset to load
189   */
190  public static void main(String [] args) {
191
192    try {
193      final javax.swing.JFrame jf = new javax.swing.JFrame("Instances Panel");
194      jf.getContentPane().setLayout(new BorderLayout());
195      final InstancesSummaryPanel p = new InstancesSummaryPanel();
196      p.setBorder(BorderFactory.createTitledBorder("Relation"));
197      jf.getContentPane().add(p, BorderLayout.CENTER);
198      jf.addWindowListener(new java.awt.event.WindowAdapter() {
199        public void windowClosing(java.awt.event.WindowEvent e) {
200          jf.dispose();
201          System.exit(0);
202        }
203      });
204      jf.pack();
205      jf.setVisible(true);
206      if (args.length == 1) {
207        java.io.Reader r = new java.io.BufferedReader(
208                           new java.io.FileReader(args[0]));
209        Instances i = new Instances(r);
210        p.setInstances(i);
211      }
212    } catch (Exception ex) {
213      ex.printStackTrace();
214      System.err.println(ex.getMessage());
215    }
216  }
217 
218}
Note: See TracBrowser for help on using the repository browser.