📄 clustererpanel.java
字号:
gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 1; gbC.gridx = 1; gbC.gridwidth = 2; gbC.insets = new Insets(2, 10, 2, 0); gbL.setConstraints(m_SetTestBut, gbC); p2.add(m_SetTestBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 2; gbC.gridx = 0; gbL.setConstraints(m_PercentBut, gbC); p2.add(m_PercentBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 2; gbC.gridx = 1; gbC.insets = new Insets(2, 10, 2, 10); gbL.setConstraints(m_PercentLab, gbC); p2.add(m_PercentLab); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 2; gbC.gridx = 2; gbC.weightx = 100; gbC.ipadx = 20; gbL.setConstraints(m_PercentText, gbC); p2.add(m_PercentText); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 3; gbC.gridx = 0; gbC.gridwidth = 2; gbL.setConstraints(m_ClassesToClustersBut, gbC); p2.add(m_ClassesToClustersBut); m_ClassCombo.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0)); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 4; gbC.gridx = 0; gbC.gridwidth = 2; gbL.setConstraints(m_ClassCombo, gbC); p2.add(m_ClassCombo); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 5; gbC.gridx = 0; gbC.gridwidth = 2; gbL.setConstraints(m_StorePredictionsBut, gbC); p2.add(m_StorePredictionsBut); JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(2, 1)); JPanel ssButs = new JPanel(); ssButs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); ssButs.setLayout(new GridLayout(1, 2, 5, 5)); ssButs.add(m_StartBut); ssButs.add(m_StopBut); JPanel ib = new JPanel(); ib.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); ib.setLayout(new GridLayout(1, 1, 5, 5)); ib.add(m_ignoreBut); buttons.add(ib); buttons.add(ssButs); JPanel p3 = new JPanel(); p3.setBorder(BorderFactory.createTitledBorder("Clusterer output")); p3.setLayout(new BorderLayout()); final JScrollPane js = new JScrollPane(m_OutText); p3.add(js, BorderLayout.CENTER); js.getViewport().addChangeListener(new ChangeListener() { private int lastHeight; public void stateChanged(ChangeEvent e) { JViewport vp = (JViewport)e.getSource(); int h = vp.getViewSize().height; if (h != lastHeight) { // i.e. an addition not just a user scrolling lastHeight = h; int x = h - vp.getExtentSize().height; vp.setViewPosition(new Point(0, x)); } } }); JPanel mondo = new JPanel(); gbL = new GridBagLayout(); mondo.setLayout(gbL); gbC = new GridBagConstraints(); // gbC.anchor = GridBagConstraints.WEST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 0; gbC.gridx = 0; gbL.setConstraints(p2, gbC); mondo.add(p2); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.NORTH; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 1; gbC.gridx = 0; gbL.setConstraints(buttons, gbC); mondo.add(buttons); gbC = new GridBagConstraints(); //gbC.anchor = GridBagConstraints.NORTH; gbC.fill = GridBagConstraints.BOTH; gbC.gridy = 2; gbC.gridx = 0; gbC.weightx = 0; gbL.setConstraints(m_History, gbC); mondo.add(m_History); gbC = new GridBagConstraints(); gbC.fill = GridBagConstraints.BOTH; gbC.gridy = 0; gbC.gridx = 1; gbC.gridheight = 3; gbC.weightx = 100; gbC.weighty = 100; gbL.setConstraints(p3, gbC); mondo.add(p3); setLayout(new BorderLayout()); add(p1, BorderLayout.NORTH); add(mondo, BorderLayout.CENTER); } /** * Updates the enabled status of the input fields and labels. */ protected void updateRadioLinks() { m_SetTestBut.setEnabled(m_TestSplitBut.isSelected()); if ((m_SetTestFrame != null) && (!m_TestSplitBut.isSelected())) { m_SetTestFrame.setVisible(false); } m_PercentText.setEnabled(m_PercentBut.isSelected()); m_PercentLab.setEnabled(m_PercentBut.isSelected()); m_ClassCombo.setEnabled(m_ClassesToClustersBut.isSelected()); } /** * 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; } /** * Set the default attributes to use on the x and y axis * of a new visualization object. * @param x the index of the attribute to use on the x axis * @param y the index of the attribute to use on the y axis */ public void setXY_VisualizeIndexes(int x, int y) { m_visXIndex = x; m_visYIndex = y; } /** * Tells the panel to use a new set of instances. * * @param inst a set of Instances */ public void setInstances(Instances inst) { setXY_VisualizeIndexes(0,0); m_Instances = inst; m_ignoreKeyModel.removeAllElements(); String [] attribNames = new String [m_Instances.numAttributes()]; for (int i = 0; i < m_Instances.numAttributes(); i++) { String name = m_Instances.attribute(i).name(); m_ignoreKeyModel.addElement(name); String type = ""; switch (m_Instances.attribute(i).type()) { case Attribute.NOMINAL: type = "(Nom) "; break; case Attribute.NUMERIC: type = "(Num) "; break; case Attribute.STRING: type = "(Str) "; break; default: type = "(???) "; } String attnm = m_Instances.attribute(i).name(); attribNames[i] = type + attnm; } m_StartBut.setEnabled(m_RunThread == null); m_StopBut.setEnabled(m_RunThread != null); m_ignoreBut.setEnabled(true); m_ClassCombo.setModel(new DefaultComboBoxModel(attribNames)); m_ClassCombo.setSelectedIndex(attribNames.length - 1); updateRadioLinks(); } /** * Sets the user test set. Information about the current test set * is displayed in an InstanceSummaryPanel and the user is given the * ability to load another set from a file or url. * */ protected void setTestSet() { if (m_SetTestFrame == null) { final SetInstancesPanel sp = new SetInstancesPanel(); m_Summary = sp.getSummary(); if (m_TestInstancesCopy != null) { sp.setInstances(m_TestInstances); } sp.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { m_TestInstances = sp.getInstances(); } }); // Add propertychangelistener to update m_TestInstances whenever // it changes in the settestframe m_SetTestFrame = new JFrame("Test Instances"); m_SetTestFrame.getContentPane().setLayout(new BorderLayout()); m_SetTestFrame.getContentPane().add(sp, BorderLayout.CENTER); m_SetTestFrame.pack(); } m_SetTestFrame.setVisible(true); } /** * Sets up the structure for the visualizable instances. This dataset * contains the original attributes plus the clusterer's cluster assignments * @param testInstancs the instances that the clusterer has clustered * @return a PlotData2D object encapsulating the visualizable instances. The * instances contain one more attribute (predicted * cluster) than the testInstances */ private PlotData2D setUpVisualizableInstances(Instances testInstances, ClusterEvaluation eval) throws Exception { int numClusters = eval.getNumClusters(); double [] clusterAssignments = eval.getClusterAssignments(); FastVector hv = new FastVector(); Instances newInsts; Attribute predictedCluster; FastVector clustVals = new FastVector(); for (int i = 0; i < numClusters; i++) { clustVals.addElement("cluster"+i); } predictedCluster = new Attribute("Cluster", clustVals); for (int i = 0; i < testInstances.numAttributes(); i++) { hv.addElement(testInstances.attribute(i).copy()); } hv.addElement(predictedCluster); newInsts = new Instances(testInstances.relationName()+"_clustered", hv, testInstances.numInstances()); double [] values; int j; int [] pointShapes = null; int [] classAssignments = null; if (testInstances.classIndex() >= 0) { classAssignments = eval.getClassesToClusters(); pointShapes = new int[testInstances.numInstances()]; for (int i = 0; i < testInstances.numInstances(); i++) { pointShapes[i] = Plot2D.CONST_AUTOMATIC_SHAPE; } } for (int i = 0; i < testInstances.numInstances(); i++) { values = new double[newInsts.numAttributes()]; for (j = 0; j < testInstances.numAttributes(); j++) { values[j] = testInstances.instance(i).value(j); } values[j] = clusterAssignments[i]; newInsts.add(new Instance(1.0, values)); if (pointShapes != null) { if ((int)testInstances.instance(i).classValue() != classAssignments[(int)clusterAssignments[i]]) { pointShapes[i] = Plot2D.ERROR_SHAPE; } } } PlotData2D plotData = new PlotData2D(newInsts); if (pointShapes != null) { plotData.setShapeType(pointShapes); } plotData.addInstanceNumberAttribute(); return plotData; } /** * Starts running the currently configured clusterer with the current * settings. This is run in a separate thread, and will only start if * there is no clusterer already running. The clusterer output is sent * to the results history panel. */ protected void startClusterer() { if (m_RunThread == null) { m_StartBut.setEnabled(false); m_StopBut.setEnabled(true); m_ignoreBut.setEnabled(false); m_RunThread = new Thread() { public void run() { // Copy the current state of things m_Log.statusMessage("Setting up..."); Instances inst = new Instances(m_Instances); Instances userTest = null; PlotData2D predData = null; if (m_TestInstances != null) { userTest = new Instances(m_TestInstancesCopy); } boolean saveVis = m_StorePredictionsBut.isSelected(); String grph = null; int[] ignoredAtts = null; int testMode = 0; int percent = 66; Clusterer clusterer = (Clusterer) m_ClustererEditor.getValue(); StringBuffer outBuff = new StringBuffer(); String name = (new SimpleDateFormat("HH:mm:ss - ")) .format(new Date()); String cname = clusterer.getClass().getName(); if (cname.startsWith("weka.clusterers.")) { name += cname.substring("weka.clusterers.".length()); } else { name += cname; } try { m_Log.logMessage("Started " + cname); if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskStarted(); } if (m_PercentBut.isSelected()) { testMode = 2; percent = Integer.parseInt(m_PercentText.getText()); if ((percent <= 0) || (percent >= 100)) { throw new Exception("Percentage must be between 0 and 100"); } } else if (m_TrainBut.isSelected()) { testMode = 3; } else if (m_TestSplitBut.isSelected()) { testMode = 4; // Check the test instance compatibility if (userTest == null) { throw new Exception("No user test set has been opened"); } if (!inst.equalHeaders(userTest)) { throw new Exception("Train and test set are not compatible"); } } else if (m_ClassesToClustersBut.isSelected()) { testMode = 5; } else { throw new Exception("Unknown test mode"); } Instances trainInst = new Instances(inst); if (m_ClassesToClustersBut.isSelected()) { trainInst.setClassIndex(m_ClassCombo.getSelectedIndex()); inst.setClassIndex(m_ClassCombo.getSelectedIndex()); if (inst.classAttribute().isNumeric()) { throw new Exception("Class must be nominal for class based " +"evaluation!"); } } if (!m_ignoreKeyList.isSelectionEmpty()) { trainInst = removeIgnoreCols(trainInst); } // Output some header information outBuff.append("=== Run information ===\n\n"); outBuff.append("Scheme: " + cname); if (clusterer instanceof OptionHandler) { String [] o = ((OptionHandler) clusterer).getOptions(); outBuff.append(" " + Utils.joinOptions(o)); } outBuff.append("\n"); outBuff.append("Relation: " + inst.relationName() + '\n'); outBuff.append("Instances: " + inst.numInstances() + '\n'); outBuff.append("Attributes: " + inst.numAttributes() + '\n'); if (inst.numAttributes() < 100) { boolean [] selected = new boolean [inst.numAttributes()]; for (int i = 0; i < inst.numAttributes(); i++) { selected[i] = true; } if (!m_ignoreKeyList.isSelectionEmpty()) { int [] indices = m_ignoreKeyList.getSelectedIndices(); for (int i = 0; i < indices.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -