⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preprocesspanel.java

📁 Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	  m_EditBut.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());	      // if class was not set before, reset it again after use of filter	      if (m_Instances.classIndex() < 0)		newInstances.setClassIndex(-1);	      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);    }  }  /**   * Loads instances from an SQL query the user provided with the   * SqlViewerDialog, 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.   * @param url           the database URL   * @param user          the user to connect as   * @param pw            the password of the user   * @param query         the query for retrieving instances from   */  public void setInstancesFromDBQ(String url, String user,                                   String pw, String query) {    if (m_IOThread == null) {      try {	InstanceQuery InstQ = new InstanceQuery();        InstQ.setDatabaseURL(url);        InstQ.setUsername(user);        InstQ.setPassword(pw);        InstQ.setQuery(query);	        // we have to disconnect, otherwise we can't change the DB!        if (InstQ.isConnected())          InstQ.disconnectFromDatabase();	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);    }  }    /**   * sets Instances generated via DataGenerators (pops up a Dialog)   */  public void generateInstances() {    if (m_IOThread == null) {      m_IOThread = new Thread() {	  public void run() {	    try {              // create dialog              final DataGeneratorPanel generatorPanel = new DataGeneratorPanel();              final JDialog dialog = new JDialog();              final JButton generateButton = new JButton("Generate");              final JCheckBox showOutputCheckBox =                                   new JCheckBox("Show generated data as text, incl. comments");              showOutputCheckBox.setMnemonic('S');              generatorPanel.setGenerator(m_DataGenerator);              generatorPanel.setPreferredSize(                  new Dimension(                        300,                         (int) generatorPanel.getPreferredSize().getHeight()));              generateButton.setMnemonic('G');              generateButton.setToolTipText("Generates the dataset according the settings.");              generateButton.addActionListener(new ActionListener(){                  public void actionPerformed(ActionEvent evt){                    // generate                    generatorPanel.execute();                    boolean generated = (generatorPanel.getInstances() != null);                    if (generated)                      setInstances(generatorPanel.getInstances());                    // close dialog                    dialog.dispose();                    // get last generator                    m_DataGenerator = generatorPanel.getGenerator();                    // display output?                    if ( (generated) && (showOutputCheckBox.isSelected()) )                      showGeneratedInstances(generatorPanel.getOutput());                }              });              dialog.setTitle("DataGenerator");              dialog.getContentPane().add(generatorPanel, BorderLayout.CENTER);              dialog.getContentPane().add(generateButton, BorderLayout.EAST);              dialog.getContentPane().add(showOutputCheckBox, BorderLayout.SOUTH);              dialog.pack();                            // display dialog              dialog.setVisible(true);	    }             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 generate data at this time,\n"				    + "currently busy with other IO",				    "Generate Data",				    JOptionPane.WARNING_MESSAGE);    }  }    /**   * displays a dialog with the generated instances from the DataGenerator   */  protected void showGeneratedInstances(String data) {    final JDialog dialog = new JDialog();    final JButton saveButton = new JButton("Save");    final JButton closeButton = new JButton("Close");    final JTextArea textData = new JTextArea(data);    final JPanel panel = new JPanel();    panel.setLayout(new FlowLayout(FlowLayout.RIGHT));    textData.setEditable(false);    textData.setFont(        new Font("Monospaced", Font.PLAIN, textData.getFont().getSize()));    saveButton.setMnemonic('S');    saveButton.setToolTipText("Saves the output to a file");    saveButton.addActionListener(new ActionListener(){      public void actionPerformed(ActionEvent evt){        JFileChooser filechooser = new JFileChooser();        int result = filechooser.showSaveDialog(dialog);        if (result == JFileChooser.APPROVE_OPTION) {          try {            BufferedWriter writer = new BufferedWriter(                                      new FileWriter(                                        filechooser.getSelectedFile()));            writer.write(textData.getText());            writer.flush();            writer.close();            JOptionPane.showMessageDialog(              dialog,               "Output successfully saved to file '"               + filechooser.getSelectedFile() + "'!",              "Information",              JOptionPane.INFORMATION_MESSAGE);          }          catch (Exception e) {            e.printStackTrace();          }          dialog.dispose();        }      }    });    closeButton.setMnemonic('C');    closeButton.setToolTipText("Closes the dialog");    closeButton.addActionListener(new ActionListener(){      public void actionPerformed(ActionEvent evt){        dialog.dispose();      }    });    panel.add(saveButton);    panel.add(closeButton);    dialog.setTitle("Generated Instances (incl. comments)");    dialog.getContentPane().add(new JScrollPane(textData), BorderLayout.CENTER);    dialog.getContentPane().add(panel, BorderLayout.SOUTH);    dialog.pack();    // make sure, it's not bigger than 80% of the screen    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();    int width  = dialog.getWidth() > screen.getWidth()*0.8                    ? (int) (screen.getWidth()*0.8) : dialog.getWidth();    int height = dialog.getHeight() > screen.getHeight()*0.8                     ? (int) (screen.getHeight()*0.8) : dialog.getHeight();    dialog.setSize(width, height);        // display dialog    dialog.setVisible(true);  }  /**   * 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();	      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -