📄 preprocesspanel.java
字号:
m_Log.statusMessage("Passing through filter " + (i + 1) + ": " + filters[i].getClass().getName()); filters[i].setInputFormat(temp); temp = Filter.useFilter(temp, filters[i]); } if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskFinished(); } // try to save a copy of the filters using serialization try { m_FiltersCopy = new SerializedObject(filters); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Could not create copy of filters", null, JOptionPane.ERROR_MESSAGE); } return temp; } catch (UnassignedClassException ex) { // Pop up an error optionpane JOptionPane.showMessageDialog(this, "Problem filtering instances:\n" + ex.getMessage() + "\n\n" +"You should not use filters that require " +"a class attribute during preprocessing,\n" +"as this will bias any classification " +"results you obtain. Consider using a\n" +"FilteredClassifier instead.", "Instance Filter", JOptionPane.ERROR_MESSAGE); return null; } catch (Exception ex) { // Pop up an error optionpane JOptionPane.showMessageDialog(this, "Problem filtering instances:\n" + ex.getMessage(), "Instance Filter", JOptionPane.ERROR_MESSAGE); ex.printStackTrace(); m_Log.logMessage(ex.getMessage()); return null; } } /** * Applies the current filters and attribute selection settings and * sets the result as the working dataset. This is done in the IO * thread, and an error message is popped up if the IO thread is busy. */ public void setWorkingInstancesFromFilters() { if (m_IOThread == null) { int res = 0; if (m_AttPanel.getSelectedAttributes().length == 0) { Object[] options = { "OK", "CANCEL" }; res = JOptionPane.showOptionDialog(null, "Unselecting all attributes will result " +"in an empty dataset.\nDo you want to " +"continue?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]); } if (res == 0) { m_IOThread = new Thread() { public void run() { Instances f = filterInstances(m_BaseInstances); if (f != null) { if ((f == m_BaseInstances) && (f == m_WorkingInstances)) { m_Log.logMessage("No changes from base relation"); } else { setWorkingInstances(f); } } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } } else { JOptionPane.showMessageDialog(this, "Can't apply filters at this time,\n" + "currently busy with other IO", "Apply filters", 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) { int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File sFile = m_FileChooser.getSelectedFile(); if (!sFile.getName().toLowerCase().endsWith(Instances.FILE_EXTENSION)) { sFile = new File(sFile.getParent(), sFile.getName() + Instances.FILE_EXTENSION); } File selected = sFile; saveInstancesToFile(selected, m_WorkingInstances); } } 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 setBaseInstancesFromFileQ() { if (m_IOThread == null) { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File selected = m_FileChooser.getSelectedFile(); setBaseInstancesFromFile(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 setBaseInstancesFromDBQ() { if (m_IOThread == null) { try { InstanceQuery InstQ = (InstanceQuery)m_DatabaseQueryEditor.getValue(); InstQ.connectToDatabase(); setBaseInstancesFromDB(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 setBaseInstancesFromURLQ() { 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); setBaseInstancesFromURL(url); } } catch (Exception ex) { 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 filtered instances to the supplied file. * * @param f a value of type 'File' * @param inst the instances to save */ protected void saveInstancesToFile(final File f, final Instances inst) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Saving to file..."); Writer w = new BufferedWriter(new FileWriter(f)); Instances h = new Instances(inst, 0); w.write(h.toString()); w.write("\n"); for (int i = 0; i < inst.numInstances(); i++) { w.write(inst.instance(i).toString()); w.write("\n"); } w.close(); m_Log.statusMessage("OK"); } catch (Exception ex) { ex.printStackTrace(); m_Log.logMessage(ex.getMessage()); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't save at this time,\n" + "currently busy with other IO", "Save Instances", JOptionPane.WARNING_MESSAGE); } } /** * Pops up generic object editor with list of conversion filters * * @param f the File */ private void converterQuery(final File f) { final GenericObjectEditor convEd = new GenericObjectEditor(); try { convEd.setClassType(weka.core.converters.Loader.class); convEd.setValue(new weka.core.converters.CSVLoader()); ((GenericObjectEditor.GOEPanel)convEd.getCustomEditor()) .addOkListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tryConverter((Loader)convEd.getValue(), f); } }); } catch (Exception ex) { } PropertyDialog pd = new PropertyDialog(convEd, 100, 100); } /** * Applies the selected converter * * @param cnv the converter to apply to the input file * @param f the input file */ private void tryConverter(final Loader cnv, final File f) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { cnv.setSource(f); Instances inst = cnv.getDataSet(); setBaseInstances(inst); } catch (Exception ex) { m_Log.statusMessage(cnv.getClass().getName()+" failed to load " +f.getName()); m_IOThread = null; converterQuery(f); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } } /** * Loads results from a set of instances contained in the supplied * file. This is started in the IO thread, and a dialog is popped up * if there's a problem. * * @param f a value of type 'File' */ public void setBaseInstancesFromFile(final File f) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Reading from file..."); Reader r = new BufferedReader(new FileReader(f)); setBaseInstances(new Instances(r)); r.close(); } catch (Exception ex) { m_Log.statusMessage("Problem reading " + f.getName() + " as an arff file."); m_IOThread = null; converterQuery(f); /* m_Log.statusMessage("Problem reading " + f.getName()); JOptionPane.showMessageDialog(PreprocessPanel.this, "Couldn't read from file:\n" + f.getName() + "\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); */ } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Loads instances from a database * * @param iq the InstanceQuery object to load from (this is assumed * to have been already connected to a valid database). */ public void setBaseInstancesFromDB(final InstanceQuery iq) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Reading from database..."); final Instances i = iq.retrieveInstances(); SwingUtilities.invokeAndWait(new Runnable() { public void run() { setBaseInstances(new Instances(i)); } }); iq.disconnectFromDatabase(); } catch (Exception ex) { m_Log.statusMessage("Probelm executing DB query "+m_SQLQ); JOptionPane.showMessageDialog(PreprocessPanel.this, "Couldn't read from database:\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Loads instances from a URL. * * @param u the URL to load from. */ public void setBaseInstancesFromURL(final URL u) { if (m_IOThread == null) { m_IOThread = new Thread() { public void run() { try { m_Log.statusMessage("Reading from URL..."); Reader r = new BufferedReader( new InputStreamReader(u.openStream())); setBaseInstances(new Instances(r)); r.close(); } catch (Exception ex) { m_Log.statusMessage("Problem reading " + u); JOptionPane.showMessageDialog(PreprocessPanel.this, "Couldn't read from URL:\n" + u + "\n" + ex.getMessage(), "Load Instances", JOptionPane.ERROR_MESSAGE); } m_IOThread = null; } }; m_IOThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority m_IOThread.start(); } else { JOptionPane.showMessageDialog(this, "Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); } } /** * Tests out the instance-preprocessing panel from the command line. * * @param args ignored */ public static void main(String [] args) { try { final JFrame jf = new JFrame("Weka Knowledge Explorer: Preprocess"); jf.getContentPane().setLayout(new BorderLayout()); final PreprocessPanel sp = new PreprocessPanel(); jf.getContentPane().add(sp, BorderLayout.CENTER); weka.gui.LogPanel lp = new weka.gui.LogPanel(); sp.setLog(lp); jf.getContentPane().add(lp, BorderLayout.SOUTH); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); jf.setSize(800, 600); jf.setVisible(true); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -