📄 preprocesspanel.java
字号:
JPanel lhs = new JPanel();
lhs.setLayout(new BorderLayout());
lhs.add(m_InstSummaryPanel, BorderLayout.NORTH);
lhs.add(attInfo, BorderLayout.CENTER);
JPanel rhs = new JPanel();
rhs.setLayout(new BorderLayout());
rhs.add(attVis, BorderLayout.CENTER);
JPanel relation = new JPanel();
relation.setLayout(new GridLayout(1, 2));
relation.add(lhs);
relation.add(rhs);
JPanel middle = new JPanel();
middle.setLayout(new BorderLayout());
middle.add(filter, BorderLayout.NORTH);
middle.add(relation, BorderLayout.CENTER);
setLayout(new BorderLayout());
add(buttons, BorderLayout.NORTH);
add(middle, BorderLayout.CENTER);
}
/**
* Sets the Logger to receive informational messages
*
* @param newLog the Logger that will now get info messages
*/
public void setLog(Logger newLog) {
m_Log = newLog;
}
/**
* Tells the panel to use a new base set of instances.
*
* @param inst a set of Instances
*/
public void setInstances(Instances inst) {
m_Instances = inst;
try {
Runnable r = new Runnable() {
public void run() {
m_InstSummaryPanel.setInstances(m_Instances);
m_AttPanel.setInstances(m_Instances);
m_RemoveButton.setEnabled(true);
m_AttSummaryPanel.setInstances(m_Instances);
m_AttVisualizePanel.setInstances(m_Instances);
// select the first attribute in the list
m_AttPanel.getSelectionModel().setSelectionInterval(0, 0);
m_AttSummaryPanel.setAttribute(0);
m_AttVisualizePanel.setAttribute(0);
m_ApplyFilterBut.setEnabled(true);
m_Log.logMessage("Base relation is now "
+ m_Instances.relationName()
+ " (" + m_Instances.numInstances()
+ " instances)");
m_SaveBut.setEnabled(true);
m_Log.statusMessage("OK");
// Fire a propertychange event
m_Support.firePropertyChange("", null, null);
}
};
if (SwingUtilities.isEventDispatchThread()) {
r.run();
} else {
SwingUtilities.invokeAndWait(r);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Problem setting base instances:\n"
+ ex.getMessage(),
"Instances",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* Gets the working set of instances.
*
* @return the working instances
*/
public Instances getInstances() {
return m_Instances;
}
/**
* 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);
}
/**
* Passes the dataset through the filter that has been configured for use.
*/
protected void applyFilter(final Filter filter) {
if (m_IOThread == null) {
m_IOThread = new Thread() {
public void run() {
try {
if (filter != null) {
if (m_Log instanceof TaskLogger) {
((TaskLogger)m_Log).taskStarted();
}
m_Log.statusMessage("Passing dataset through filter "
+ filter.getClass().getName());
int classIndex = m_AttVisualizePanel.getColoringIndex();
if ((classIndex < 0) && (filter instanceof SupervisedFilter)) {
throw new IllegalArgumentException("Class (colour) needs to " +
"be set for supervised " +
"filter.");
}
Instances copy = new Instances(m_Instances);
copy.setClassIndex(classIndex);
filter.setInputFormat(copy);
Instances newInstances = filter.useFilter(copy, filter);
if (newInstances == null || newInstances.numAttributes() < 1) {
throw new Exception("Dataset is empty.");
}
m_Log.statusMessage("Saving undo information");
addUndoPoint();
m_AttVisualizePanel.setColoringIndex(copy.classIndex());
m_Instances = newInstances;
setInstances(m_Instances);
if (m_Log instanceof TaskLogger) {
((TaskLogger)m_Log).taskFinished();
}
}
} catch (Exception ex) {
if (m_Log instanceof TaskLogger) {
((TaskLogger)m_Log).taskFinished();
}
// Pop up an error optionpane
JOptionPane.showMessageDialog(PreprocessPanel.this,
"Problem filtering instances:\n"
+ ex.getMessage(),
"Apply Filter",
JOptionPane.ERROR_MESSAGE);
m_Log.logMessage("Problem filtering instances: " + ex.getMessage());
m_Log.statusMessage("Problem filtering instances");
}
m_IOThread = null;
}
};
m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority
m_IOThread.start();
} else {
JOptionPane.showMessageDialog(this,
"Can't apply filter at this time,\n"
+ "currently busy with other IO",
"Apply Filter",
JOptionPane.WARNING_MESSAGE);
}
}
/**
* Queries the user for a file to save instances as, then saves the
* instances in a background process. This is done in the IO
* thread, and an error message is popped up if the IO thread is busy.
*/
public void saveWorkingInstancesToFileQ() {
if (m_IOThread == null) {
m_FileChooser.setAcceptAllFileFilterUsed(false);
int returnVal = m_FileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File sFile = m_FileChooser.getSelectedFile();
if (m_FileChooser.getFileFilter() == m_arffFileFilter) {
if (!sFile.getName().toLowerCase().endsWith(Instances.FILE_EXTENSION)) {
sFile = new File(sFile.getParent(), sFile.getName()
+ Instances.FILE_EXTENSION);
}
File selected = sFile;
saveInstancesToFile(selected, m_Instances, true);
} else if (m_FileChooser.getFileFilter() == m_csvFileFilter) {
if (!sFile.getName().toLowerCase().endsWith(CSVLoader.FILE_EXTENSION)) {
sFile = new File(sFile.getParent(), sFile.getName()
+ CSVLoader.FILE_EXTENSION);
}
File selected = sFile;
saveInstancesToFile(selected, m_Instances, false);
} else if (m_FileChooser.getFileFilter() == m_c45FileFilter) {
File selected = sFile;
saveInstancesToC45File(selected, m_Instances);
} else if (m_FileChooser.getFileFilter() == m_bsiFileFilter) {
if (!sFile.getName().toLowerCase().
endsWith(Instances.SERIALIZED_OBJ_FILE_EXTENSION)) {
sFile = new File(sFile.getParent(), sFile.getName()
+ Instances.SERIALIZED_OBJ_FILE_EXTENSION);
}
File selected = sFile;
saveSerializedInstancesToFile(selected, m_Instances);
}
}
FileFilter temp = m_FileChooser.getFileFilter();
m_FileChooser.setAcceptAllFileFilterUsed(true);
m_FileChooser.setFileFilter(temp);
} else {
JOptionPane.showMessageDialog(this,
"Can't save at this time,\n"
+ "currently busy with other IO",
"Save Instances",
JOptionPane.WARNING_MESSAGE);
}
}
/**
* Queries the user for a file to load instances from, then loads the
* instances in a background process. This is done in the IO
* thread, and an error message is popped up if the IO thread is busy.
*/
public void setInstancesFromFileQ() {
if (m_IOThread == null) {
int returnVal = m_FileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selected = m_FileChooser.getSelectedFile();
try {
addUndoPoint();
} catch (Exception ignored) {}
setInstancesFromFile(selected);
}
} else {
JOptionPane.showMessageDialog(this,
"Can't load at this time,\n"
+ "currently busy with other IO",
"Load Instances",
JOptionPane.WARNING_MESSAGE);
}
}
/**
* Queries the user for a URL to a database to load instances from,
* then loads the instances in a background process. This is done in the IO
* thread, and an error message is popped up if the IO thread is busy.
*/
public void setInstancesFromDBQ() {
if (m_IOThread == null) {
try {
InstanceQuery InstQ =
(InstanceQuery)m_DatabaseQueryEditor.getValue();
InstQ.connectToDatabase();
try {
addUndoPoint();
} catch (Exception ignored) {}
setInstancesFromDB(InstQ);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Problem connecting to database:\n"
+ ex.getMessage(),
"Load Instances",
JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this,
"Can't load at this time,\n"
+ "currently busy with other IO",
"Load Instances",
JOptionPane.WARNING_MESSAGE);
}
}
/**
* Queries the user for a URL to load instances from, then loads the
* instances in a background process. This is done in the IO
* thread, and an error message is popped up if the IO thread is busy.
*/
public void setInstancesFromURLQ() {
if (m_IOThread == null) {
try {
String urlName = (String) JOptionPane.showInputDialog(this,
"Enter the source URL",
"Load Instances",
JOptionPane.QUESTION_MESSAGE,
null,
null,
m_LastURL);
if (urlName != null) {
m_LastURL = urlName;
URL url = new URL(urlName);
try {
addUndoPoint();
} catch (Exception ignored) {}
setInstancesFromURL(url);
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Problem with URL:\n"
+ ex.getMessage(),
"Load Instances",
JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this,
"Can't load at this time,\n"
+ "currently busy with other IO",
"Load Instances",
JOptionPane.WARNING_MESSAGE);
}
}
/**
* Saves the current instances in C45 names and data file format
*
* @param f a value of type 'File'
* @param inst the instances to save
*/
protected void saveInstancesToC45File(final File f, final Instances inst) {
if (m_IOThread == null) {
final int classIndex = m_AttVisualizePanel.getColoringIndex();
if (inst.attribute(classIndex).isNumeric()) {
JOptionPane.showMessageDialog(this,
"Can't save in C45 format,\n"
+ "as the selected class is numeric.",
"Save Instances",
JOptionPane.ERROR_MESSAGE);
return;
}
m_IOThread = new Thread() {
public void run() {
try {
m_Log.statusMessage("Saving to file...");
String name = f.getAbsolutePath();
if (name.lastIndexOf('.') != -1) {
name = name.substring(0, name.lastIndexOf('.'));
}
File fData = new File(name+".data");
File fNames = new File(name+".names");
Writer w = new BufferedWriter(new FileWriter(fNames));
Writer w2 = new BufferedWriter(new FileWriter(fData));
// write the names file
for (int i = 0; i < inst.attribute(classIndex).numValues(); i++) {
w.write(inst.attribute(classIndex).value(i));
if (i < inst.attribute(classIndex).numValues()-1) {
w.write(",");
} else {
w.write(".\n");
}
}
for (int i = 0; i < inst.numAttributes(); i++) {
if (i != classIndex) {
w.write(inst.attribute(i).name()+": ");
if (inst.attribute(i).isNumeric() || inst.attribute(i).isDate()) {
w.write("continuous.\n");
} else {
Attribute temp = inst.attribute(i);
for (int j = 0; j < temp.numValues(); j++) {
w.write(temp.value(j));
if (j < temp.numValues()-1) {
w.write(",");
} else {
w.write(".\n");
}
}
}
}
}
w.close();
// write the data file
for (int i = 0; i < inst.numInstances(); i++) {
Instance tempI = inst.instance(i);
for (int j = 0; j < inst.numAttributes(); j++) {
if (j != classIndex) {
if (tempI.isMissing(j)) {
w2.write("?,");
} else if (inst.attribute(j).isNominal() ||
inst.attribute(j).isString()) {
w2.write(inst.attribute(j).value((int)tempI.value(j))+",");
} else {
w2.write(""+tempI.value(j)+",");
}
}
}
// w2.write(inst.instance(i).toString());
// write the class value
if (tempI.isMissing(classIndex)) {
w2.write("?");
} else {
w2.write(inst.attribute(classIndex).
value((int)tempI.value(classIndex)));
}
w2.write("\n");
}
w2.close();
m_Log.statusMessage("OK");
} catch (Exception ex) {
ex.printStackTrace();
m_Log.logMessage(ex.getMessage());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -