📄 boundaryvisualizer.java
字号:
m_yAxisPanel.repaint(0,0,0,m_yAxisPanel.getWidth(), m_yAxisPanel.getHeight()); } /** * Get the training instances * * @return the training instances */ public Instances getInstances() { return m_trainingInstances; } /** * Set the training instances * * @param inst the instances to use */ public void setInstances(Instances inst) throws Exception { if (inst == null) { m_trainingInstances = inst; m_classPanel.setInstances(m_trainingInstances); return; } if (inst.numAttributes() < 3) { throw new Exception("Not enough attributes in the data to visualize!"); } m_trainingInstances = inst; m_classPanel.setInstances(m_trainingInstances); // setup combo boxes String [] classAttNames = new String [m_trainingInstances.numAttributes()]; final Vector xAttNames = new Vector(); Vector yAttNames = new Vector(); for (int i = 0; i < m_trainingInstances.numAttributes(); i++) { classAttNames[i] = m_trainingInstances.attribute(i).name(); String type = ""; switch (m_trainingInstances.attribute(i).type()) { case Attribute.NOMINAL: type = " (Nom)"; break; case Attribute.NUMERIC: type = " (Num)"; break; case Attribute.STRING: type = " (Str)"; break; case Attribute.DATE: type = " (Dat)"; break; case Attribute.RELATIONAL: type = " (Rel)"; break; default: type = " (???)"; } classAttNames[i] += type; if (m_trainingInstances.attribute(i).isNumeric()) { xAttNames.addElement("X: "+classAttNames[i]); yAttNames.addElement("Y: "+classAttNames[i]); } } m_classAttBox.setModel(new DefaultComboBoxModel(classAttNames)); m_xAttBox.setModel(new DefaultComboBoxModel(xAttNames)); m_yAttBox.setModel(new DefaultComboBoxModel(yAttNames)); if (xAttNames.size() > 1) { m_yAttBox.setSelectedIndex(1); } if (classAttNames.length > 0) m_classAttBox.setSelectedIndex(classAttNames.length - 1); //select last attribute as class by default. -jimmy m_classAttBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { configureForClassAttribute(); } }); m_xAttBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { if (xAttNames.size() > 1) { if (m_xAttBox.getSelectedIndex() == m_yAttBox.getSelectedIndex()) { m_xAttBox.setSelectedIndex((m_xAttBox.getSelectedIndex() + 1) % xAttNames.size()); } } computeBounds(); repaint(); try{ plotTrainingData(); } catch (Exception ex) {} //jimmy } } }); m_yAttBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { if (xAttNames.size() > 1) { if (m_yAttBox.getSelectedIndex() == m_xAttBox.getSelectedIndex()) { m_yAttBox.setSelectedIndex((m_yAttBox.getSelectedIndex() + 1) % xAttNames.size()); } } computeBounds(); repaint(); try{ plotTrainingData(); } catch (Exception ex) {} } } }); //set up the add points selector combo box setUpClassValueSelectorCB(); configureForClassAttribute(); computeBounds(); revalidate(); repaint(); } /** Set up the combo box that chooses which class values to use when adding data points. */ private void setUpClassValueSelectorCB() { classValueSelector.removeAllItems(); int classAttribute = m_classAttBox.getSelectedIndex(); //System.err.println(m_trainingInstances.numClasses() + " classes"); m_trainingInstances.setClassIndex(classAttribute); if (m_trainingInstances.attribute(classAttribute).isNominal()) { classValueSelector.setEditable(false); for (int i = 0; i < /*m_trainingInstances.numDistinctValues(classAttribute)*/m_trainingInstances.numClasses(); i++) classValueSelector.insertItemAt(m_trainingInstances.attribute(classAttribute).value(i) , i); classValueSelector.setSelectedIndex(0); } else { classValueSelector.setEditable(true); } } /** * Set up the class values combo boxes */ private void configureForClassAttribute() { int classIndex = m_classAttBox.getSelectedIndex(); if (classIndex >= 0) { // see if this is a nominal attribute if (!m_trainingInstances.attribute(classIndex).isNominal() || m_classifier == null) { m_startBut.setEnabled(false); } else { m_startBut.setEnabled(true); } // set up class colours FastVector colors = new FastVector(); if (!m_trainingInstances.attribute(m_classAttBox.getSelectedIndex()).isNominal()) //this if by jimmy { for (int i = 0; i < BoundaryPanel.DEFAULT_COLORS.length; i++) colors.addElement(BoundaryPanel.DEFAULT_COLORS[i]); } else { for (int i = 0; i < m_trainingInstances.attribute(classIndex).numValues(); i++) { colors.addElement(BoundaryPanel. DEFAULT_COLORS[i % BoundaryPanel.DEFAULT_COLORS.length]);// m_classPanel.setColours(colors); // m_boundaryPanel.setColors(colors); } } m_classPanel.setColours(colors); //jimmy m_boundaryPanel.setColors(colors); } } /** * 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 { java.io.Reader r = new java.io.BufferedReader( new java.io.FileReader(selected)); Instances i = new Instances(r); setInstances(i); //dataFileLabel.setText(selected.getName()); dataFileLabel.setText(i.relationName()); } catch (Exception e) { JOptionPane.showMessageDialog(this,"Can't load at this time,\n" + "currently busy with other IO", "Load Instances", JOptionPane.WARNING_MESSAGE); e.printStackTrace(); } } } /** Sets up the BoundaryPanel object so that it is ready for plotting. * @return an error code:<br/> * 0 - SUCCESS<br/> * 1 - ERROR - Kernel bandwidth < 0<br/> * 2 - ERROR - Kernel bandwidth >= number of training instances. */ public int setUpBoundaryPanel() throws Exception { int returner = 0; //OK code. int tempSamples = m_numberOfSamplesFromEachRegion; try { tempSamples = Integer.parseInt(m_regionSamplesText.getText().trim()); } catch (Exception ex) { m_regionSamplesText.setText(""+tempSamples); } m_numberOfSamplesFromEachRegion = tempSamples; m_boundaryPanel. setNumSamplesPerRegion(tempSamples); tempSamples = m_generatorSamplesBase; try { tempSamples = Integer.parseInt(m_generatorSamplesText.getText().trim()); } catch (Exception ex) { m_generatorSamplesText.setText(""+tempSamples); } m_generatorSamplesBase = tempSamples; m_boundaryPanel.setGeneratorSamplesBase((double)tempSamples); tempSamples = m_kernelBandwidth; try { tempSamples = Integer.parseInt(m_kernelBandwidthText.getText().trim()); } catch (Exception ex) { m_kernelBandwidthText.setText(""+tempSamples); } m_kernelBandwidth = tempSamples; m_dataGenerator.setKernelBandwidth(tempSamples); if (m_kernelBandwidth < 0) returner = 1; if (m_kernelBandwidth >= m_trainingInstances.numInstances()) returner = 2; m_trainingInstances. setClassIndex(m_classAttBox.getSelectedIndex()); m_boundaryPanel.setClassifier(m_classifier); m_boundaryPanel.setTrainingData(m_trainingInstances); m_boundaryPanel.setXAttribute(m_xIndex); m_boundaryPanel.setYAttribute(m_yIndex); m_boundaryPanel. setPlotTrainingData(m_plotTrainingData.isSelected()); return returner; } /** Plots the training data on-screen. Also does all of the setup required * for this to work. */ public void plotTrainingData() throws Exception { m_boundaryPanel.initialize(); setUpBoundaryPanel(); computeBounds(); m_boundaryPanel.plotTrainingData(); } /** Stops the plotting thread. */ public void stopPlotting() { m_boundaryPanel.stopPlotting(); } /** * Sets whether System.exit gets called when no more windows are open. * * @param value if TRUE then a System.exit call is ossued after the * last window gets closed. */ public static void setExitIfNoWindowsOpen(boolean value) { m_ExitIfNoWindowsOpen = value; } /** * Gets whether System.exit gets called after the last window gets closed * * @return TRUE if System.exit gets called after last window * got closed. */ public static boolean getExitIfNoWindowsOpen() { return m_ExitIfNoWindowsOpen; } /** Creates a new GUI window with all of the BoundaryVisualizer trappings, * @param classifier The classifier to use in the new window. May be null. * @param instances The dataset to visualize on in the new window. May be null. */ public static void createNewVisualizerWindow(Classifier classifier, Instances instances) throws Exception { m_WindowCount++; final javax.swing.JFrame jf = new javax.swing.JFrame("Weka classification boundary visualizer"); jf.getContentPane().setLayout(new BorderLayout()); final BoundaryVisualizer bv = new BoundaryVisualizer(); jf.getContentPane().add(bv, BorderLayout.CENTER); jf.setSize(bv.getMinimumSize()); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { m_WindowCount--; bv.stopPlotting(); jf.dispose(); if ((m_WindowCount == 0) && m_ExitIfNoWindowsOpen) { System.exit(0); } } }); jf.pack(); jf.setVisible(true); jf.setResizable(false); Dimension t = jf.getSize(); if (classifier == null) bv.setClassifier(null); else { bv.setClassifier(classifier); bv.m_classifierEditor.setValue(classifier); } if (instances == null) bv.setInstances(null); else { bv.setInstances(instances); try{ bv.dataFileLabel.setText(instances.relationName()); bv.plotTrainingData(); bv.m_classPanel.setCindex(bv.m_classAttBox.getSelectedIndex()); bv.repaint(0,0,0,bv.getWidth(), bv.getHeight()); } catch (Exception ex) {} } } /** * Main method for testing this class * * @param args a <code>String[]</code> value */ public static void main(String [] args) { try { if (args.length < 2) { createNewVisualizerWindow(null, null); } else { String [] argsR = null; if (args.length > 2) { argsR = new String [args.length-2]; for (int j = 2; j < args.length; j++) { argsR[j-2] = args[j]; } } Classifier c = Classifier.forName(args[1], argsR); System.err.println("Loading instances from : "+args[0]); java.io.Reader r = new java.io.BufferedReader( new java.io.FileReader(args[0])); Instances i = new Instances(r); createNewVisualizerWindow(c, i); } } catch (Exception ex) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -