📄 clustererpanel.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * ClustererPanel.java * Copyright (C) 1999 Mark Hall * */package weka.gui.explorer;import weka.core.Instances;import weka.core.Instance;import weka.core.OptionHandler;import weka.core.Attribute;import weka.core.Utils;import weka.core.FastVector;import weka.core.SerializedObject;import weka.core.Drawable;import weka.clusterers.Clusterer;import weka.clusterers.ClusterEvaluation;import weka.gui.Logger;import weka.gui.TaskLogger;import weka.gui.SysErrLog;import weka.gui.GenericObjectEditor;import weka.gui.PropertyPanel;import weka.gui.ResultHistoryPanel;import weka.gui.SetInstancesPanel;import weka.gui.InstancesSummaryPanel;import weka.gui.SaveBuffer;import weka.filters.Filter;import weka.filters.unsupervised.attribute.Remove;import weka.gui.visualize.VisualizePanel;import weka.gui.visualize.PlotData2D;import weka.gui.visualize.Plot2D;import weka.gui.treevisualizer.*;import weka.gui.ListSelectorDialog;import weka.gui.ExtensionFileFilter;import java.util.Random;import java.util.Date;import java.text.SimpleDateFormat;import java.awt.FlowLayout;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.Insets;import java.awt.Font;import java.awt.Dimension;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeSupport;import java.io.File;import java.io.FileWriter;import java.io.Writer;import java.io.BufferedWriter;import java.io.PrintWriter;import java.io.OutputStream;import java.io.ObjectOutputStream;import java.io.FileOutputStream;import java.util.zip.GZIPOutputStream;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.FileInputStream;import java.util.zip.GZIPInputStream;import javax.swing.JFileChooser;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.BorderFactory;import javax.swing.JTextArea;import javax.swing.JScrollPane;import javax.swing.JRadioButton;import javax.swing.ButtonGroup;import javax.swing.JOptionPane;import javax.swing.JComboBox;import javax.swing.DefaultComboBoxModel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.JFrame;import javax.swing.event.ChangeListener;import javax.swing.event.ChangeEvent;import javax.swing.JViewport;import javax.swing.JCheckBox;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.awt.Point;import javax.swing.JPopupMenu;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.DefaultListModel;import javax.swing.JList;import javax.swing.filechooser.FileFilter;/** * This panel allows the user to select and configure a clusterer, and evaluate * the clusterer using a number of testing modes (test on the training data, * train/test on a percentage split, test on a * separate split). The results of clustering runs are stored in a result * history so that previous results are accessible. * * @author Mark Hall (mhall@cs.waikato.ac.nz) * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public class ClustererPanel extends JPanel { /** Lets the user configure the clusterer */ protected GenericObjectEditor m_ClustererEditor = new GenericObjectEditor(); /** The panel showing the current clusterer selection */ protected PropertyPanel m_CLPanel = new PropertyPanel(m_ClustererEditor); /** The output area for classification results */ protected JTextArea m_OutText = new JTextArea(20, 40); /** The destination for log/status messages */ protected Logger m_Log = new SysErrLog(); /** The buffer saving object for saving output */ SaveBuffer m_SaveOut = new SaveBuffer(m_Log, this); /** A panel controlling results viewing */ protected ResultHistoryPanel m_History = new ResultHistoryPanel(m_OutText); /** Click to set test mode to generate a % split */ protected JRadioButton m_PercentBut = new JRadioButton("Percentage split"); /** Click to set test mode to test on training data */ protected JRadioButton m_TrainBut = new JRadioButton("Use training set"); /** Click to set test mode to a user-specified test set */ protected JRadioButton m_TestSplitBut = new JRadioButton("Supplied test set"); /** Click to set test mode to classes to clusters based evaluation */ protected JRadioButton m_ClassesToClustersBut = new JRadioButton("Classes to clusters evaluation"); /** Lets the user select the class column for classes to clusters based evaluation */ protected JComboBox m_ClassCombo = new JComboBox(); /** Label by where the % split is entered */ protected JLabel m_PercentLab = new JLabel("%", SwingConstants.RIGHT); /** The field where the % split is entered */ protected JTextField m_PercentText = new JTextField("66"); /** The button used to open a separate test dataset */ protected JButton m_SetTestBut = new JButton("Set..."); /** The frame used to show the test set selection panel */ protected JFrame m_SetTestFrame; /** The button used to popup a list for choosing attributes to ignore while clustering */ protected JButton m_ignoreBut = new JButton("Ignore attributes"); protected DefaultListModel m_ignoreKeyModel = new DefaultListModel(); protected JList m_ignoreKeyList = new JList(m_ignoreKeyModel); // protected Remove m_ignoreFilter = null; /** * Alters the enabled/disabled status of elements associated with each * radio button */ ActionListener m_RadioListener = new ActionListener() { public void actionPerformed(ActionEvent e) { updateRadioLinks(); } }; /** Click to start running the clusterer */ protected JButton m_StartBut = new JButton("Start"); /** Stop the class combo from taking up to much space */ private Dimension COMBO_SIZE = new Dimension(250, m_StartBut .getPreferredSize().height); /** Click to stop a running clusterer */ protected JButton m_StopBut = new JButton("Stop"); /** The main set of instances we're playing with */ protected Instances m_Instances; /** The user-supplied test set (if any) */ protected Instances m_TestInstances; /** The user supplied test set after preprocess filters have been applied */ protected Instances m_TestInstancesCopy; /** The current visualization object */ protected VisualizePanel m_CurrentVis = null; /** default x index for visualizing */ protected int m_visXIndex; /** default y index for visualizing */ protected int m_visYIndex; /** Check to save the predictions in the results list for visualizing later on */ protected JCheckBox m_StorePredictionsBut = new JCheckBox("Store clusters for visualization"); /** A thread that clustering runs in */ protected Thread m_RunThread; /** The instances summary panel displayed by m_SetTestFrame */ protected InstancesSummaryPanel m_Summary; /** Filter to ensure only model files are selected */ protected FileFilter m_ModelFilter = new ExtensionFileFilter("model", "Model object files"); /** The file chooser for selecting model files */ protected JFileChooser m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); /* Register the property editors we need */ static { java.beans.PropertyEditorManager .registerEditor(weka.core.SelectedTag.class, weka.gui.SelectedTagEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.clusterers.Clusterer.class, weka.gui.GenericObjectEditor.class); } /** * Creates the clusterer panel */ public ClustererPanel() { // Connect / configure the components m_OutText.setEditable(false); m_OutText.setFont(new Font("Monospaced", Font.PLAIN, 12)); m_OutText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); m_OutText.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) { m_OutText.selectAll(); } } }); m_History.setBorder(BorderFactory.createTitledBorder("Result list (right-click for options)")); m_ClustererEditor.setClassType(Clusterer.class); m_ClustererEditor.setValue(new weka.clusterers.EM()); m_ClustererEditor.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { repaint(); } }); m_TrainBut.setToolTipText("Cluster the same set that the clusterer" + " is trained on"); m_PercentBut.setToolTipText("Train on a percentage of the data and" + " cluster the remainder"); m_TestSplitBut.setToolTipText("Cluster a user-specified dataset"); m_ClassesToClustersBut.setToolTipText("Evaluate clusters with respect to a" +" class"); m_ClassCombo.setToolTipText("Select the class attribute for class based" +" evaluation"); m_StartBut.setToolTipText("Starts the clustering"); m_StopBut.setToolTipText("Stops a running clusterer"); m_StorePredictionsBut. setToolTipText("Store predictions in the result list for later " +"visualization"); m_ignoreBut.setToolTipText("Ignore attributes during clustering"); m_FileChooser.setFileFilter(m_ModelFilter); m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); m_ClassCombo.setPreferredSize(COMBO_SIZE); m_ClassCombo.setMaximumSize(COMBO_SIZE); m_ClassCombo.setMinimumSize(COMBO_SIZE); m_ClassCombo.setEnabled(false); m_TrainBut.setSelected(true); m_StorePredictionsBut.setSelected(true); updateRadioLinks(); ButtonGroup bg = new ButtonGroup(); bg.add(m_TrainBut); bg.add(m_PercentBut); bg.add(m_TestSplitBut); bg.add(m_ClassesToClustersBut); m_TrainBut.addActionListener(m_RadioListener); m_PercentBut.addActionListener(m_RadioListener); m_TestSplitBut.addActionListener(m_RadioListener); m_ClassesToClustersBut.addActionListener(m_RadioListener); m_SetTestBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setTestSet(); } }); m_StartBut.setEnabled(false); m_StopBut.setEnabled(false); m_ignoreBut.setEnabled(false); m_StartBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { startClusterer(); } }); m_StopBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stopClusterer(); } }); m_ignoreBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setIgnoreColumns(); } }); m_History.setHandleRightClicks(false); // see if we can popup a menu for the selected result m_History.getList().addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { } else { int index = m_History.getList().locationToIndex(e.getPoint()); if (index != -1) { String name = m_History.getNameAtIndex(index); visualizeClusterer(name, e.getX(), e.getY()); } else { visualizeClusterer(null, e.getX(), e.getY()); } } } }); // Layout the GUI JPanel p1 = new JPanel(); p1.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Clusterer"), BorderFactory.createEmptyBorder(0, 5, 5, 5) )); p1.setLayout(new BorderLayout()); p1.add(m_CLPanel, BorderLayout.NORTH); JPanel p2 = new JPanel(); GridBagLayout gbL = new GridBagLayout(); p2.setLayout(gbL); p2.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Cluster mode"), BorderFactory.createEmptyBorder(0, 5, 5, 5) )); GridBagConstraints gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 0; gbC.gridx = 0; gbL.setConstraints(m_TrainBut, gbC); p2.add(m_TrainBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 1; gbC.gridx = 0; gbL.setConstraints(m_TestSplitBut, gbC); p2.add(m_TestSplitBut);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -