📄 attributeselectionpanel.java
字号:
package chen.macroweka.gui.explorer;
import weka.core.Instances;
import weka.core.OptionHandler;
import weka.core.Attribute;
import weka.core.Utils;
import weka.core.Range;
import weka.core.FastVector;
import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.ASSearch;
import weka.attributeSelection.AttributeTransformer;
import weka.attributeSelection.AttributeSelection;
import weka.attributeSelection.Ranker;
import weka.attributeSelection.AttributeEvaluator;
import weka.filters.Filter;
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.SaveBuffer;
import weka.gui.FileEditor;
import weka.gui.visualize.*;
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.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 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.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Point;
import java.awt.Dimension;
import javax.swing.JPopupMenu;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
/**
* This panel allows the user to select and configure an attribute
* evaluator and a search method, set the
* attribute of the current dataset to be used as the class, and perform
* attribute selection using one of two selection modes (select using all the
* training data or perform a n-fold cross validation---on each trial
* selecting features using n-1 folds of the data).
* The results of attribute selection runs are stored in a results history
* so that previous results are accessible.
*
* @author Mark Hall (mhall@cs.waikato.ac.nz)
* @version $Revision: 1.1 $
*/
public class AttributeSelectionPanel extends JPanel {
/** Lets the user configure the attribute evaluator */
protected GenericObjectEditor m_AttributeEvaluatorEditor =
new GenericObjectEditor();
/** Lets the user configure the search method */
protected GenericObjectEditor m_AttributeSearchEditor =
new GenericObjectEditor();
/** The panel showing the current attribute evaluation method */
protected PropertyPanel m_AEEPanel =
new PropertyPanel(m_AttributeEvaluatorEditor);
/** The panel showing the current search method */
protected PropertyPanel m_ASEPanel =
new PropertyPanel(m_AttributeSearchEditor);
/** The output area for attribute selection 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);
/** Lets the user select the class column */
protected JComboBox m_ClassCombo = new JComboBox();
/** Click to set evaluation mode to cross-validation */
protected JRadioButton m_CVBut = new JRadioButton("Cross-validation");
/** Click to set test mode to test on training data */
protected JRadioButton m_TrainBut =
new JRadioButton("Use full training set");
/** Label by where the cv folds are entered */
protected JLabel m_CVLab = new JLabel("Folds", SwingConstants.RIGHT);
/** The field where the cv folds are entered */
protected JTextField m_CVText = new JTextField("10");
/** Label by where cv random seed is entered */
protected JLabel m_SeedLab = new JLabel("Seed",SwingConstants.RIGHT);
/** The field where the seed value is entered */
protected JTextField m_SeedText = new JTextField("1");
/**
* 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 attribute selector */
protected JButton m_StartBut = new JButton("Start");
/** Click to stop a running classifier */
protected JButton m_StopBut = new JButton("Stop");
/** Stop the class combo from taking up to much space */
private Dimension COMBO_SIZE = new Dimension(150, m_StartBut
.getPreferredSize().height);
/** The main set of instances we're playing with */
protected Instances m_Instances;
/** A thread that attribute selection runs in */
protected Thread m_RunThread;
/* Register the property editors we need */
static {
GenericObjectEditor.registerEditors();
}
/**
* Creates the classifier panel
*/
public AttributeSelectionPanel() {
// 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_AttributeEvaluatorEditor.setClassType(ASEvaluation.class);
m_AttributeEvaluatorEditor.setValue(new weka.attributeSelection.
CfsSubsetEval());
m_AttributeEvaluatorEditor.
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if (m_AttributeEvaluatorEditor.getValue() instanceof AttributeEvaluator) {
if (!(m_AttributeSearchEditor.getValue() instanceof Ranker)) {
Object backup = m_AttributeEvaluatorEditor.getBackup();
int result =
JOptionPane.showConfirmDialog(null, "You must use use the Ranker search method "
+"in order to use\n"
+m_AttributeEvaluatorEditor.getValue().getClass().getName()
+".\nShould I select the Ranker search method for you?",
"Alert!", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
m_AttributeSearchEditor.setValue(new Ranker());
} else {
// restore to what was there previously (if possible)
if (backup != null) {
m_AttributeEvaluatorEditor.setValue(backup);
}
}
}
} else {
if (m_AttributeSearchEditor.getValue() instanceof Ranker) {
Object backup = m_AttributeEvaluatorEditor.getBackup();
int result =
JOptionPane.showConfirmDialog(null, "You must use use a search method that explores \n"
+"the space of attribute subsets (such as GreedyStepwise) in "
+"order to use\n"
+m_AttributeEvaluatorEditor.getValue().getClass().getName()
+".\nShould I select the GreedyStepwise search method for "
+"you?\n(you can always switch to a different method afterwards)",
"Alert!", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
m_AttributeSearchEditor.setValue(new weka.attributeSelection.GreedyStepwise());
} else {
// restore to what was there previously (if possible)
if (backup != null) {
m_AttributeEvaluatorEditor.setValue(backup);
}
}
}
}
updateRadioLinks();
repaint();
}
});
m_AttributeSearchEditor.setClassType(ASSearch.class);
m_AttributeSearchEditor.setValue(new weka.attributeSelection.BestFirst());
m_AttributeSearchEditor.
addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if (m_AttributeSearchEditor.getValue() instanceof Ranker) {
if (!(m_AttributeEvaluatorEditor.getValue() instanceof AttributeEvaluator)) {
Object backup = m_AttributeSearchEditor.getBackup();
int result =
JOptionPane.showConfirmDialog(null, "You must use use an evaluator that evaluates\n"
+"single attributes (such as InfoGain) in order to use\n"
+"the Ranker. Should I select the InfoGain evaluator "
+"for you?\n"
+"(You can always switch to a different method afterwards)" ,
"Alert!", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
m_AttributeEvaluatorEditor.setValue(new weka.attributeSelection.InfoGainAttributeEval());
} else {
// restore to what was there previously (if possible)
if (backup != null) {
m_AttributeSearchEditor.setValue(backup);
}
}
}
} else {
if (m_AttributeEvaluatorEditor.getValue() instanceof AttributeEvaluator) {
Object backup = m_AttributeSearchEditor.getBackup();
int result =
JOptionPane.showConfirmDialog(null, "You must use use an evaluator that evaluates\n"
+"subsets of attributes (such as CFS) in order to use\n"
+m_AttributeEvaluatorEditor.getValue().getClass().getName()
+".\nShould I select the CFS subset evaluator for you?"
+"\n(you can always switch to a different method afterwards)",
"Alert!", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
m_AttributeEvaluatorEditor.setValue(new weka.attributeSelection.CfsSubsetEval());
} else {
// restore to what was there previously (if possible)
if (backup != null) {
m_AttributeSearchEditor.setValue(backup);
}
}
}
}
repaint();
}
});
m_ClassCombo.setToolTipText("Select the attribute to use as the class");
m_TrainBut.setToolTipText("select attributes using the full training "
+ "dataset");
m_CVBut.setToolTipText("Perform a n-fold cross-validation");
m_StartBut.setToolTipText("Starts attribute selection");
m_StopBut.setToolTipText("Stops a attribute selection task");
m_ClassCombo.setPreferredSize(COMBO_SIZE);
m_ClassCombo.setMaximumSize(COMBO_SIZE);
m_ClassCombo.setMinimumSize(COMBO_SIZE);
m_History.setPreferredSize(COMBO_SIZE);
m_History.setMaximumSize(COMBO_SIZE);
m_History.setMinimumSize(COMBO_SIZE);
m_ClassCombo.setEnabled(false);
m_TrainBut.setSelected(true);
updateRadioLinks();
ButtonGroup bg = new ButtonGroup();
bg.add(m_TrainBut);
bg.add(m_CVBut);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -