📄 simplesetuppanel.java
字号:
* Prompts the user to select an experiment file and loads it.
*/
private void openExperiment() {
int returnVal = m_FileChooser.showOpenDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
File expFile = m_FileChooser.getSelectedFile();
try {
FileInputStream fi = new FileInputStream(expFile);
ObjectInputStream oi = new ObjectInputStream(
new BufferedInputStream(fi));
Experiment exp = (Experiment)oi.readObject();
oi.close();
if (!setExperiment(exp)) {
if (m_modePanel != null) m_modePanel.switchToAdvanced(exp);
}
System.err.println("Opened experiment:\n" + exp);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Couldn't open experiment file:\n"
+ expFile
+ "\nReason:\n" + ex.getMessage(),
"Open Experiment",
JOptionPane.ERROR_MESSAGE);
// Pop up error dialog
}
}
/**
* Prompts the user for a filename to save the experiment to, then saves
* the experiment.
*/
private void saveExperiment() {
int returnVal = m_FileChooser.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
File expFile = m_FileChooser.getSelectedFile();
if (!expFile.getName().toLowerCase().endsWith(Experiment.FILE_EXTENSION)) {
expFile = new File(expFile.getParent(), expFile.getName()
+ Experiment.FILE_EXTENSION);
}
try {
FileOutputStream fo = new FileOutputStream(expFile);
ObjectOutputStream oo = new ObjectOutputStream(
new BufferedOutputStream(fo));
oo.writeObject(m_Exp);
oo.close();
System.err.println("Saved experiment:\n" + m_Exp);
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Couldn't save experiment file:\n"
+ expFile
+ "\nReason:\n" + ex.getMessage(),
"Save Experiment",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* Adds a PropertyChangeListener who will be notified of value changes.
*
* @param l a value of type 'PropertyChangeListener'
*/
public void addPropertyChangeListener(PropertyChangeListener l) {
m_Support.addPropertyChangeListener(l);
}
/**
* Removes a PropertyChangeListener.
*
* @param l a value of type 'PropertyChangeListener'
*/
public void removePropertyChangeListener(PropertyChangeListener l) {
m_Support.removePropertyChangeListener(l);
}
/**
* Responds to a change in the destination type.
*/
private void destinationTypeChanged() {
if (m_Exp == null) return;
if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT) {
m_ResultsDestinationPathLabel.setText("URL:");
m_ResultsDestinationPathTField.setText(m_destinationDatabaseURL);
m_BrowseDestinationButton.setEnabled(true); //!!!
m_BrowseDestinationButton.setText("User...");
} else {
m_ResultsDestinationPathLabel.setText("Filename:");
m_ResultsDestinationPathTField.setText(m_destinationFilename);
m_BrowseDestinationButton.setEnabled(true);
m_BrowseDestinationButton.setText("Browse...");
}
if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT) {
DatabaseResultListener drl = null;
try {
drl = new DatabaseResultListener();
} catch (Exception e) {
e.printStackTrace();
}
drl.setDatabaseURL(m_destinationDatabaseURL);
m_Exp.setResultListener(drl);
} else if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
InstancesResultListener irl = new InstancesResultListener();
irl.setOutputFile(new File(m_destinationFilename));
m_Exp.setResultListener(irl);
} else if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
CSVResultListener crl = new CSVResultListener();
crl.setOutputFile(new File(m_destinationFilename));
m_Exp.setResultListener(crl);
}
m_Support.firePropertyChange("", null, null);
}
/**
* Responds to a change in the destination address.
*/
private void destinationAddressChanged() {
if (m_Exp == null) return;
if (m_ResultsDestinationCBox.getSelectedItem() == DEST_DATABASE_TEXT) {
m_destinationDatabaseURL = m_ResultsDestinationPathTField.getText();
if (m_Exp.getResultListener() instanceof DatabaseResultListener) {
((DatabaseResultListener)m_Exp.getResultListener()).setDatabaseURL(m_destinationDatabaseURL);
}
} else if (m_ResultsDestinationCBox.getSelectedItem() == DEST_ARFF_TEXT) {
m_destinationFilename = m_ResultsDestinationPathTField.getText();
if (m_Exp.getResultListener() instanceof InstancesResultListener) {
((InstancesResultListener)m_Exp.getResultListener()).setOutputFile(new File(m_destinationFilename));
}
} else if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
m_destinationFilename = m_ResultsDestinationPathTField.getText();
if (m_Exp.getResultListener() instanceof CSVResultListener) {
((CSVResultListener)m_Exp.getResultListener()).setOutputFile(new File(m_destinationFilename));
}
}
m_Support.firePropertyChange("", null, null);
}
/**
* Responds to a change in the experiment type.
*/
private void expTypeChanged() {
if (m_Exp == null) return;
// update parameter ui
if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
m_ExperimentParameterLabel.setText("Number of folds:");
m_ExperimentParameterTField.setText("" + m_numFolds);
} else {
m_ExperimentParameterLabel.setText("Train percentage:");
m_ExperimentParameterTField.setText("" + m_trainPercent);
}
// update iteration ui
if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_FIXEDSPLIT_TEXT) {
m_NumberOfRepetitionsTField.setEnabled(false);
m_NumberOfRepetitionsTField.setText("1");
m_Exp.setRunLower(1);
m_Exp.setRunUpper(1);
} else {
m_NumberOfRepetitionsTField.setText("" + m_numRepetitions);
m_NumberOfRepetitionsTField.setEnabled(true);
m_Exp.setRunLower(1);
m_Exp.setRunUpper(m_numRepetitions);
}
SplitEvaluator se = null;
Classifier sec = null;
if (m_ExpClassificationRBut.isSelected()) {
se = new ClassifierSplitEvaluator();
sec = ((ClassifierSplitEvaluator)se).getClassifier();
} else {
se = new RegressionSplitEvaluator();
sec = ((RegressionSplitEvaluator)se).getClassifier();
}
// build new ResultProducer
if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
CrossValidationResultProducer cvrp = new CrossValidationResultProducer();
cvrp.setNumFolds(m_numFolds);
cvrp.setSplitEvaluator(se);
PropertyNode[] propertyPath = new PropertyNode[2];
try {
propertyPath[0] = new PropertyNode(se, new PropertyDescriptor("splitEvaluator",
CrossValidationResultProducer.class),
CrossValidationResultProducer.class);
propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier",
se.getClass()),
se.getClass());
} catch (IntrospectionException e) {
e.printStackTrace();
}
m_Exp.setResultProducer(cvrp);
m_Exp.setPropertyPath(propertyPath);
} else {
RandomSplitResultProducer rsrp = new RandomSplitResultProducer();
rsrp.setRandomizeData(m_ExperimentTypeCBox.getSelectedItem() == TYPE_RANDOMSPLIT_TEXT);
rsrp.setTrainPercent(m_trainPercent);
rsrp.setSplitEvaluator(se);
PropertyNode[] propertyPath = new PropertyNode[2];
try {
propertyPath[0] = new PropertyNode(se, new PropertyDescriptor("splitEvaluator",
RandomSplitResultProducer.class),
RandomSplitResultProducer.class);
propertyPath[1] = new PropertyNode(sec, new PropertyDescriptor("classifier",
se.getClass()),
se.getClass());
} catch (IntrospectionException e) {
e.printStackTrace();
}
m_Exp.setResultProducer(rsrp);
m_Exp.setPropertyPath(propertyPath);
}
m_Exp.setUsePropertyIterator(true);
m_Support.firePropertyChange("", null, null);
}
/**
* Responds to a change in the experiment parameter.
*/
private void expParamChanged() {
if (m_Exp == null) return;
if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
try {
m_numFolds = Integer.parseInt(m_ExperimentParameterTField.getText());
} catch (NumberFormatException e) {
return;
}
} else {
try {
m_trainPercent = Double.parseDouble(m_ExperimentParameterTField.getText());
} catch (NumberFormatException e) {
return;
}
}
if (m_ExperimentTypeCBox.getSelectedItem() == TYPE_CROSSVALIDATION_TEXT) {
if (m_Exp.getResultProducer() instanceof CrossValidationResultProducer) {
CrossValidationResultProducer cvrp = (CrossValidationResultProducer) m_Exp.getResultProducer();
cvrp.setNumFolds(m_numFolds);
} else {
return;
}
} else {
if (m_Exp.getResultProducer() instanceof RandomSplitResultProducer) {
RandomSplitResultProducer rsrp = (RandomSplitResultProducer) m_Exp.getResultProducer();
rsrp.setRandomizeData(m_ExperimentTypeCBox.getSelectedItem() == TYPE_RANDOMSPLIT_TEXT);
rsrp.setTrainPercent(m_trainPercent);
} else {
//System.err.println("not rsrp");
return;
}
}
m_Support.firePropertyChange("", null, null);
}
/**
* Responds to a change in the number of repetitions.
*/
private void numRepetitionsChanged() {
if (m_Exp == null || !m_NumberOfRepetitionsTField.isEnabled()) return;
try {
m_numRepetitions = Integer.parseInt(m_NumberOfRepetitionsTField.getText());
} catch (NumberFormatException e) {
return;
}
m_Exp.setRunLower(1);
m_Exp.setRunUpper(m_numRepetitions);
m_Support.firePropertyChange("", null, null);
}
/**
* Lets user enter username/password/URL.
*/
private void chooseURLUsername() {
String dbaseURL=((DatabaseResultListener)m_Exp.getResultListener()).getDatabaseURL();
String username=((DatabaseResultListener)m_Exp.getResultListener()).getUsername();
DatabaseConnectionDialog dbd= new DatabaseConnectionDialog(null,dbaseURL,username);
dbd.setVisible(true);
//if (dbaseURL == null) {
if (dbd.getReturnValue()==JOptionPane.CLOSED_OPTION) {
return;
}
((DatabaseResultListener)m_Exp.getResultListener()).setUsername(dbd.getUsername());
((DatabaseResultListener)m_Exp.getResultListener()).setPassword(dbd.getPassword());
((DatabaseResultListener)m_Exp.getResultListener()).setDatabaseURL(dbd.getURL());
}
/**
* Lets user browse for a destination file..
*/
private void chooseDestinationFile() {
FileFilter fileFilter = null;
if (m_ResultsDestinationCBox.getSelectedItem() == DEST_CSV_TEXT) {
fileFilter = m_csvFileFilter;
} else {
fileFilter = m_arffFileFilter;
}
m_DestFileChooser.setFileFilter(fileFilter);
int returnVal = m_DestFileChooser.showSaveDialog(this);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
m_ResultsDestinationPathTField.setText(m_DestFileChooser.getSelectedFile().toString());
}
/**
* Tests out the experiment setup from the command line.
*
* @param args arguments to the program.
*/
public static void main(String [] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -