📄 simplesetuppanel.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. *//* * SimpleSetupPanel.java * Copyright (C) 2002 Richard Kirkby * */package weka.gui.experiment;import weka.classifiers.Classifier;import weka.core.xml.KOML;import weka.experiment.CSVResultListener;import weka.experiment.ClassifierSplitEvaluator;import weka.experiment.CrossValidationResultProducer;import weka.experiment.DatabaseResultListener;import weka.experiment.Experiment;import weka.experiment.InstancesResultListener;import weka.experiment.PropertyNode;import weka.experiment.RandomSplitResultProducer;import weka.experiment.RegressionSplitEvaluator;import weka.experiment.SplitEvaluator;import weka.experiment.xml.XMLExperiment;import weka.gui.DatabaseConnectionDialog;import weka.gui.ExtensionFileFilter;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusAdapter;import java.awt.event.FocusEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.beans.IntrospectionException;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.beans.PropertyDescriptor;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;import javax.swing.filechooser.FileFilter;/** * This panel controls the configuration of an experiment. * <p> * If <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a> * is in the classpath the experiments can also be serialized to XML instead of a * binary format.* * @author Richard kirkby (rkirkby@cs.waikato.ac.nz) * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.12 $ */public class SimpleSetupPanel extends JPanel { /** The experiment being configured */ protected Experiment m_Exp; /** The panel which switched between simple and advanced setup modes */ protected SetupModePanel m_modePanel = null; /** The database destination URL to store results into */ protected String m_destinationDatabaseURL; /** The filename to store results into */ protected String m_destinationFilename = ""; /** The number of folds for a cross-validation experiment */ protected int m_numFolds = 10; /** The training percentage for a train/test split experiment */ protected double m_trainPercent = 66; /** The number of times to repeat the sub-experiment */ protected int m_numRepetitions = 10; /** Whether or not the user has consented for the experiment to be simplified */ protected boolean m_userHasBeenAskedAboutConversion; /** Filter for choosing CSV files */ protected ExtensionFileFilter m_csvFileFilter = new ExtensionFileFilter(".csv", "Comma separated value files"); /** FIlter for choosing ARFF files */ protected ExtensionFileFilter m_arffFileFilter = new ExtensionFileFilter(".arff", "ARFF files"); /** Click to load an experiment */ protected JButton m_OpenBut = new JButton("Open..."); /** Click to save an experiment */ protected JButton m_SaveBut = new JButton("Save..."); /** Click to create a new experiment with default settings */ protected JButton m_NewBut = new JButton("New"); /** A filter to ensure only experiment files get shown in the chooser */ protected FileFilter m_ExpFilter = new ExtensionFileFilter(Experiment.FILE_EXTENSION, "Experiment configuration files (*" + Experiment.FILE_EXTENSION + ")"); /** A filter to ensure only experiment (in KOML format) files get shown in the chooser */ protected FileFilter m_KOMLFilter = new ExtensionFileFilter(KOML.FILE_EXTENSION, "Experiment configuration files (*" + KOML.FILE_EXTENSION + ")"); /** A filter to ensure only experiment (in XML format) files get shown in the chooser */ protected FileFilter m_XMLFilter = new ExtensionFileFilter(".xml", "Experiment configuration files (*.xml)"); /** The file chooser for selecting experiments */ protected JFileChooser m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); /** The file chooser for selecting result destinations */ protected JFileChooser m_DestFileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); /** Combo box for choosing experiment destination type */ protected JComboBox m_ResultsDestinationCBox = new JComboBox(); /** Label for destination field */ protected JLabel m_ResultsDestinationPathLabel = new JLabel("Filename:"); /** Input field for result destination path */ protected JTextField m_ResultsDestinationPathTField = new JTextField(); /** Button for browsing destination files */ protected JButton m_BrowseDestinationButton = new JButton("Browse..."); /** Combo box for choosing experiment type */ protected JComboBox m_ExperimentTypeCBox = new JComboBox(); /** Label for parameter field */ protected JLabel m_ExperimentParameterLabel = new JLabel("Number of folds:"); /** Input field for experiment parameter */ protected JTextField m_ExperimentParameterTField = new JTextField(); /** Radio button for choosing classification experiment */ protected JRadioButton m_ExpClassificationRBut = new JRadioButton("Classification"); /** Radio button for choosing regression experiment */ protected JRadioButton m_ExpRegressionRBut = new JRadioButton("Regression"); /** Input field for number of repetitions */ protected JTextField m_NumberOfRepetitionsTField = new JTextField(); /** Radio button for choosing datasets first in order of execution */ protected JRadioButton m_OrderDatasetsFirstRBut = new JRadioButton("Data sets first"); /** Radio button for choosing algorithms first in order of execution */ protected JRadioButton m_OrderAlgorithmsFirstRBut = new JRadioButton("Algorithms first"); /** The strings used to identify the combo box choices */ protected static String DEST_DATABASE_TEXT = ("JDBC database"); protected static String DEST_ARFF_TEXT = ("ARFF file"); protected static String DEST_CSV_TEXT = ("CSV file"); protected static String TYPE_CROSSVALIDATION_TEXT = ("Cross-validation"); protected static String TYPE_RANDOMSPLIT_TEXT = ("Train/Test Percentage Split (data randomized)"); protected static String TYPE_FIXEDSPLIT_TEXT = ("Train/Test Percentage Split (order preserved)"); /** The panel for configuring selected datasets */ protected DatasetListPanel m_DatasetListPanel = new DatasetListPanel(); /** The panel for configuring selected algorithms */ protected AlgorithmListPanel m_AlgorithmListPanel = new AlgorithmListPanel(); /** A button for bringing up the notes */ protected JButton m_NotesButton = new JButton("Notes"); /** Frame for the notes */ protected JFrame m_NotesFrame = new JFrame("Notes"); /** Area for user notes Default of 10 rows */ protected JTextArea m_NotesText = new JTextArea(null, 10, 0); /** * Manages sending notifications to people when we change the experiment, * at this stage, only the resultlistener so the resultpanel can update. */ protected PropertyChangeSupport m_Support = new PropertyChangeSupport(this); /** * Creates the setup panel with the supplied initial experiment. * * @param exp a value of type 'Experiment' */ public SimpleSetupPanel(Experiment exp) { this(); setExperiment(exp); } /** * Creates the setup panel with no initial experiment. */ public SimpleSetupPanel() { // everything disabled on startup m_ResultsDestinationCBox.setEnabled(false); m_ResultsDestinationPathLabel.setEnabled(false); m_ResultsDestinationPathTField.setEnabled(false); m_BrowseDestinationButton.setEnabled(false); m_ExperimentTypeCBox.setEnabled(false); m_ExperimentParameterLabel.setEnabled(false); m_ExperimentParameterTField.setEnabled(false); m_ExpClassificationRBut.setEnabled(false); m_ExpRegressionRBut.setEnabled(false); m_NumberOfRepetitionsTField.setEnabled(false); m_OrderDatasetsFirstRBut.setEnabled(false); m_OrderAlgorithmsFirstRBut.setEnabled(false); // get sensible default database address try { m_destinationDatabaseURL = (new DatabaseResultListener()).getDatabaseURL(); } catch (Exception e) {} // create action listeners m_NewBut.setMnemonic('N'); m_NewBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Experiment newExp = new Experiment(); CrossValidationResultProducer cvrp = new CrossValidationResultProducer(); cvrp.setNumFolds(10); cvrp.setSplitEvaluator(new ClassifierSplitEvaluator()); newExp.setResultProducer(cvrp); newExp.setPropertyArray(new Classifier[0]); newExp.setUsePropertyIterator(true); setExperiment(newExp); // defaults if (ExperimenterDefaults.getUseClassification()) m_ExpClassificationRBut.setSelected(true); else m_ExpRegressionRBut.setSelected(true); setSelectedItem( m_ResultsDestinationCBox, ExperimenterDefaults.getDestination()); destinationTypeChanged(); setSelectedItem( m_ExperimentTypeCBox, ExperimenterDefaults.getExperimentType()); m_numRepetitions = ExperimenterDefaults.getRepetitions(); m_NumberOfRepetitionsTField.setText( "" + m_numRepetitions); if (ExperimenterDefaults.getExperimentType().equals( TYPE_CROSSVALIDATION_TEXT)) { m_numFolds = ExperimenterDefaults.getFolds(); m_ExperimentParameterTField.setText( "" + m_numFolds); } else { m_trainPercent = ExperimenterDefaults.getTrainPercentage(); m_ExperimentParameterTField.setText( "" + m_trainPercent); } if (ExperimenterDefaults.getDatasetsFirst()) m_OrderDatasetsFirstRBut.setSelected(true); else m_OrderAlgorithmsFirstRBut.setSelected(true); expTypeChanged(); } }); m_SaveBut.setEnabled(false); m_SaveBut.setMnemonic('S'); m_SaveBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveExperiment(); } }); m_OpenBut.setMnemonic('O'); m_OpenBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openExperiment(); } }); m_FileChooser.addChoosableFileFilter(m_ExpFilter); if (KOML.isPresent()) m_FileChooser.addChoosableFileFilter(m_KOMLFilter); m_FileChooser.addChoosableFileFilter(m_XMLFilter); if (ExperimenterDefaults.getExtension().equals(".xml")) m_FileChooser.setFileFilter(m_XMLFilter); else if (KOML.isPresent() && ExperimenterDefaults.getExtension().equals(KOML.FILE_EXTENSION)) m_FileChooser.setFileFilter(m_KOMLFilter); else m_FileChooser.setFileFilter(m_ExpFilter); m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); m_DestFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); m_BrowseDestinationButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //using this button for both browsing file & setting username/password if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT){ chooseURLUsername(); } else { chooseDestinationFile(); } } }); m_ExpClassificationRBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { expTypeChanged(); } }); m_ExpRegressionRBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { expTypeChanged(); } }); m_OrderDatasetsFirstRBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (m_Exp != null) { m_Exp.setAdvanceDataSetFirst(true); m_Support.firePropertyChange("", null, null); } } }); m_OrderAlgorithmsFirstRBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (m_Exp != null) { m_Exp.setAdvanceDataSetFirst(false); m_Support.firePropertyChange("", null, null); } } }); m_ResultsDestinationPathTField.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) {destinationAddressChanged();} public void removeUpdate(DocumentEvent e) {destinationAddressChanged();} public void changedUpdate(DocumentEvent e) {destinationAddressChanged();} }); m_ExperimentParameterTField.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) {expParamChanged();} public void removeUpdate(DocumentEvent e) {expParamChanged();} public void changedUpdate(DocumentEvent e) {expParamChanged();} }); m_NumberOfRepetitionsTField.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) {numRepetitionsChanged();} public void removeUpdate(DocumentEvent e) {numRepetitionsChanged();} public void changedUpdate(DocumentEvent e) {numRepetitionsChanged();} }); m_NotesFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { m_NotesButton.setEnabled(true); } }); m_NotesFrame.getContentPane().add(new JScrollPane(m_NotesText)); m_NotesFrame.setSize(600, 400); m_NotesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { m_NotesButton.setEnabled(false); m_NotesFrame.setVisible(true); } }); m_NotesButton.setEnabled(false); m_NotesText.setEditable(true); //m_NotesText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); m_NotesText.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { m_Exp.setNotes(m_NotesText.getText()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -