// Copy the current state of things
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Text_First"));
Instances inst = new Instances(m_Instances);
inst.setClassIndex(-1);
Instances userTest = null;
PlotData2D predData = null;
if (m_TestInstances != null) {
userTest = new Instances(m_TestInstances);
}
boolean saveVis = m_StorePredictionsBut.isSelected();
String grph = null;
int[] ignoredAtts = null;
int testMode = 0;
int percent = 66;
Clusterer clusterer = (Clusterer) m_ClustererEditor.getValue();
Clusterer fullClusterer = null;
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;
}
String cmd = m_ClustererEditor.getValue().getClass().getName();
if (m_ClustererEditor.getValue() instanceof OptionHandler)
cmd += " " + Utils.joinOptions(((OptionHandler) m_ClustererEditor.getValue()).getOptions());
try {
m_Log.logMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_LogMessage_Text_First") + cname);
m_Log.logMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_LogMessage_Text_Second") + cmd);
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(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Exception_Text_First"));
}
} 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(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Exception_Text_Second"));
}
if (!inst.equalHeaders(userTest)) {
throw new Exception(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Exception_Text_Third"));
}
} else if (m_ClassesToClustersBut.isSelected()) {
testMode = 5;
} else {
throw new Exception(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Exception_Text_Fourth"));
}
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(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Exception_Text_Fifth"));
}
}
if (!m_ignoreKeyList.isSelectionEmpty()) {
trainInst = removeIgnoreCols(trainInst);
}
// Output some header information
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_First"));
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Second") + cname);
if (clusterer instanceof OptionHandler) {
String [] o = ((OptionHandler) clusterer).getOptions();
outBuff.append(" " + Utils.joinOptions(o));
}
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Third"));
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Fourth") + inst.relationName() + '\n');
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Sixth") + inst.numInstances() + '\n');
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Eighth") + 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++) {
selected[indices[i]] = false;
}
}
if (m_ClassesToClustersBut.isSelected()) {
selected[m_ClassCombo.getSelectedIndex()] = false;
}
for (int i = 0; i < inst.numAttributes(); i++) {
if (selected[i]) {
outBuff.append(" " + inst.attribute(i).name()
+ '\n');
}
}
if (!m_ignoreKeyList.isSelectionEmpty()
|| m_ClassesToClustersBut.isSelected()) {
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Eleventh"));
for (int i = 0; i < inst.numAttributes(); i++) {
if (!selected[i]) {
outBuff.append(" " + inst.attribute(i).name()
+ '\n');
}
}
}
} else {
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Thirteenth"));
}
if (!m_ignoreKeyList.isSelectionEmpty()) {
ignoredAtts = m_ignoreKeyList.getSelectedIndices();
}
if (m_ClassesToClustersBut.isSelected()) {
// add class to ignored list
if (ignoredAtts == null) {
ignoredAtts = new int[1];
ignoredAtts[0] = m_ClassCombo.getSelectedIndex();
} else {
int[] newIgnoredAtts = new int[ignoredAtts.length+1];
System.arraycopy(ignoredAtts, 0, newIgnoredAtts, 0, ignoredAtts.length);
newIgnoredAtts[ignoredAtts.length] = m_ClassCombo.getSelectedIndex();
ignoredAtts = newIgnoredAtts;
}
}
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Fourteenth"));
switch (testMode) {
case 3: // Test on training
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Fifteenth"));
break;
case 2: // Percent split
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Sixteenth") + percent
+ Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Seventeenth"));
break;
case 4: // Test on user split
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Eighteenth")
+ userTest.numInstances() + Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Nineteenth"));
break;
case 5: // Classes to clusters evaluation on training
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_Twentyth"));
break;
}
outBuff.append("\n");
m_History.addResult(name, outBuff);
m_History.setSingle(name);
// Build the model and output it.
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Second"));
trainTimeStart = System.currentTimeMillis();
// remove the class attribute (if set) and build the clusterer
clusterer.buildClusterer(removeClass(trainInst));
trainTimeElapsed = System.currentTimeMillis() - trainTimeStart;
// if (testMode == 2) {
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_TwentySecond"));
outBuff.append(clusterer.toString() + '\n');
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_TimeTakenFull") +
Utils.doubleToString(trainTimeElapsed / 1000.0,2)
+ " " + Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentyNineth"));
// }
m_History.updateResult(name);
if (clusterer instanceof Drawable) {
try {
grph = ((Drawable)clusterer).graph();
} catch (Exception ex) {
}
}
// copy full model for output
SerializedObject so = new SerializedObject(clusterer);
fullClusterer = (Clusterer) so.getObject();
ClusterEvaluation eval = new ClusterEvaluation();
eval.setClusterer(clusterer);
switch (testMode) {
case 3: case 5: // Test on training
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Third"));
eval.evaluateClusterer(trainInst, "", false);
predData = setUpVisualizableInstances(inst,eval);
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_TwentySecond"));
break;
case 2: // Percent split
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Fourth"));
inst.randomize(new Random(1));
trainInst.randomize(new Random(1));
int trainSize = trainInst.numInstances() * percent / 100;
int testSize = trainInst.numInstances() - trainSize;
Instances train = new Instances(trainInst, 0, trainSize);
Instances test = new Instances(trainInst, trainSize, testSize);
Instances testVis = new Instances(inst, trainSize, testSize);
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Fifth"));
trainTimeStart = System.currentTimeMillis();
clusterer.buildClusterer(train);
trainTimeElapsed = System.currentTimeMillis() - trainTimeStart;
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Sixth"));
eval.evaluateClusterer(test, "", false);
predData = setUpVisualizableInstances(testVis, eval);
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_TwentyThird"));
outBuff.append(clusterer.toString() + '\n');
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_TimeTakenPercentage") +
Utils.doubleToString(trainTimeElapsed / 1000.0,2)
+ " " + Messages.getInstance().getString("ClassifierPanel_StartClassifier_OutBuffer_Text_TwentyNineth"));
break;
case 4: // Test on user split
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Seventh"));
Instances userTestT = new Instances(userTest);
if (!m_ignoreKeyList.isSelectionEmpty()) {
userTestT = removeIgnoreCols(userTestT);
}
eval.evaluateClusterer(userTestT, "", false);
predData = setUpVisualizableInstances(userTest, eval);
outBuff.append(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_OutBuffer_Text_TwentyFourth"));
break;
default:
throw new Exception(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Exception_Text_Sixth"));
}
outBuff.append(eval.clusterResultsToString());
outBuff.append("\n");
m_History.updateResult(name);
m_Log.logMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_LogMessage_Text_Third") + cname);
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Eighth"));
} catch (Exception ex) {
ex.printStackTrace();
m_Log.logMessage(ex.getMessage());
JOptionPane.showMessageDialog(ClustererPanel.this,
Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_JOptionPaneShowMessageDialog_Text_First")
+ ex.getMessage(),
Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_JOptionPaneShowMessageDialog_Text_Second"),
JOptionPane.ERROR_MESSAGE);
m_Log.statusMessage(Messages.getInstance().getString("ClustererPanel_StartClusterer_Run_Log_StatusMessage_Nineth"));
} finally {
if (predData != null) {
m_CurrentVis = new VisualizePanel();
m_CurrentVis.setName(name+" ("+inst.relationName()+")");
m_CurrentVis.setLog(m_Log);
predData.setPlotName(name+" ("+inst.relationName()+")");
try {
m_CurrentVis.addPlot(predData);
} catch (Exception ex) {
System.err.println(ex);