m_History.addVis(name, m_CurrentVis); }
m_History.addObject(name, m_CurrentVis); m_VisualizeBut.setEnabled(true); } else { m_VisualizeBut.setEnabled(false); }
	public void run() {	  // Copy the current state of things	  m_Log.statusMessage("Setting up...");	  Instances inst = new Instances(m_Instances);	  Instances userTest = null;	  double [] predictions = null;	  Instances predInstances = null;	  if (m_TestInstances != null) {	    userTest = new Instances(m_TestInstances);	  }	  	  boolean saveVis = m_StorePredictionsBut.isSelected();	  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 {	    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 {	      throw new Exception("Unknown test mode");	    }	    // Output some header information	    m_Log.logMessage("Started " + cname);	    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) {	      for (int i = 0; i < inst.numAttributes(); i++) {		outBuff.append("              " + inst.attribute(i).name()			       + '\n');	      }	    } else {	      outBuff.append("              [list of attributes omitted]\n");	    }	    outBuff.append("Test mode:    ");	    switch (testMode) {	      case 3: // Test on training	      outBuff.append("evaluate on training data\n");	      break;	      case 2: // Percent split	      outBuff.append("split " + percent			       + "% train, remainder test\n");	      break;	      case 4: // Test on user split	      outBuff.append("user supplied test set: "			     + userTest.numInstances() + " instances\n");	      break;	    }	    outBuff.append("\n");	    m_History.addResult(name, outBuff);	    m_History.setSingle(name);	    	    // Build the model and output it.	    m_Log.statusMessage("Building model on training data...");	    clusterer.buildClusterer(inst);	    outBuff.append("=== Clustering model (full training set) ===\n\n");	    outBuff.append(clusterer.toString() + '\n');	    m_History.updateResult(name);	    	    ClusterEvaluation eval = new ClusterEvaluation();	    eval.setClusterer(clusterer);	    switch (testMode) {	      case 3: // Test on training	      m_Log.statusMessage("Clustering training data...");	      eval.evaluateClusterer(inst);	      predInstances = inst;	      predictions = eval.getClusterAssignments();	      outBuff.append("=== Evaluation on training set ===\n");	      break;	      case 2: // Percent split	      m_Log.statusMessage("Randomizing instances...");	      inst.randomize(new Random(42));	      int trainSize = inst.numInstances() * percent / 100;	      int testSize = inst.numInstances() - trainSize;	      Instances train = new Instances(inst, 0, trainSize);	      Instances test = new Instances(inst, trainSize, testSize);	      m_Log.statusMessage("Building model on training split...");	      clusterer.buildClusterer(train);	      m_Log.statusMessage("Evaluating on test split...");	      eval.evaluateClusterer(test);	      predInstances = test;	      predictions = eval.getClusterAssignments();	      outBuff.append("=== Evaluation on test split ===\n");	      break;			      case 4: // Test on user split	      m_Log.statusMessage("Evaluating on test data...");	      eval.evaluateClusterer(userTest);	      predInstances = userTest;	      predictions = eval.getClusterAssignments();	      outBuff.append("=== Evaluation on test set ===\n");	      break;	      default:	      throw new Exception("Test mode not implemented");	    }	    outBuff.append(eval.clusterResultsToString());	    outBuff.append("\n");	    m_History.updateResult(name);	    m_Log.logMessage("Finished " + cname);	    m_Log.statusMessage("OK");	  } catch (Exception ex) {	    m_Log.logMessage(ex.getMessage());	    m_Log.statusMessage("See error log");	    ex.printStackTrace();	  } finally {	    if (isInterrupted()) {	      m_Log.logMessage("Interrupted " + cname);	      m_Log.statusMessage("See error log");	    }	    m_RunThread = null;	    m_StartBut.setEnabled(true);	    m_StopBut.setEnabled(false);	    if (predInstances != null) {	      m_CurrentVis = new VisualizePanel();	      m_CurrentVis.setPredictions(predictions);	      m_CurrentVis.setPredictionsNumeric(false);	      m_CurrentVis.setName(name+" ("+inst.relationName()+")");	      m_CurrentVis.setColourIndex(-1);	      m_CurrentVis.setInstances(predInstances);	      try {		m_CurrentVis.setXIndex(m_visXIndex); m_CurrentVis.setYIndex(m_visYIndex);	      } catch (Exception ex) {		System.err.println(ex);	      }	      m_CurrentVis.addActionListener(new ActionListener() {		  public void actionPerformed(ActionEvent e) {		    if (m_CurrentVis.getInstances().			relationName().			compareTo(m_Instances.relationName()) == 0) {		      setXY_VisualizeIndexes(m_CurrentVis.getXIndex(), 					     m_CurrentVis.getYIndex());		    }		  }		});	      if (saveVis) {		m_History.addVis(name, m_CurrentVis);	      }  	    }	    m_VisualizeBut.setEnabled(true);	  }	}