}
weka.classifiers.Classifier classifierToUse = m_Classifier;
Instances testSet = e.getTestSet();
if (testSet != null) {
if (testSet.classIndex() < 0) {
// testSet.setClassIndex(testSet.numAttributes() - 1);
// stop all processing
stop();
String errorMessage = statusMessagePrefix()
+ "ERROR: no class attribute set in test data!";
if (m_log != null) {
m_log.statusMessage(errorMessage);
m_log.logMessage("[Classifier] " + errorMessage);
} else {
System.err.println("[Classifier] " + errorMessage);
}
return;
}
}
// If we just have a test set connection or
// there is just one run involving one set (and we are not
// currently building a model), then use the
// last saved model
if (classifierToUse != null && m_state == IDLE &&
(!m_listenees.containsKey("trainingSet") ||
(e.getMaxRunNumber() == 1 && e.getMaxSetNumber() == 1))) {
// if this is structure only then just return at this point
if (e.getTestSet() != null && e.isStructureOnly()) {
return;
}
if (classifierToUse instanceof EnvironmentHandler && m_env != null) {
((EnvironmentHandler)classifierToUse).setEnvironment(m_env);
}
if (classifierToUse instanceof weka.classifiers.misc.InputMappedClassifier) {
// make sure that we have the correct training header (if InputMappedClassifier
// is loading a model from a file).
try {
m_trainingSet =
((weka.classifiers.misc.InputMappedClassifier)classifierToUse).
getModelHeader(m_trainingSet); // this returns the argument if a model is not being loaded
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
// check that we have a training set/header (if we don't,
// then it means that no model has been loaded
if (m_trainingSet == null) {
stop();
String errorMessage = statusMessagePrefix()
+ "ERROR: no trained/loaded classifier to use for prediction!";
if (m_log != null) {
m_log.statusMessage(errorMessage);
m_log.logMessage("[Classifier] " + errorMessage);
} else {
System.err.println("[Classifier] " + errorMessage);
}
return;
}
testSet = e.getTestSet();
if (e.getRunNumber() == 1 && e.getSetNumber() == 1) {
m_currentBatchIdentifier = new Date();
}
if (testSet != null) {
if (!m_trainingSet.equalHeaders(testSet) &&
!(classifierToUse instanceof weka.classifiers.misc.InputMappedClassifier)) {
boolean wrapClassifier = false;
if (!Utils.
getDontShowDialog("weka.gui.beans.Classifier.AutoWrapInInputMappedClassifier")) {
java.awt.GraphicsEnvironment ge =
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment();
if (!ge.isHeadless()) {
JCheckBox dontShow = new JCheckBox("Do not show this message again");
Object[] stuff = new Object[2];
stuff[0] = "Data used to train model and test set are not compatible.\n" +
"Would you like to automatically wrap the classifier in\n" +
"an \"InputMappedClassifier\" before proceeding?.\n";
stuff[1] = dontShow;
int result = JOptionPane.showConfirmDialog(this, stuff,
"KnowledgeFlow:Classifier", JOptionPane.YES_OPTION);
if (result == JOptionPane.YES_OPTION) {
wrapClassifier = true;
}
if (dontShow.isSelected()) {
String response = (wrapClassifier) ? "yes" : "no";
try {
Utils.
setDontShowDialogResponse("weka.gui.explorer.ClassifierPanel.AutoWrapInInputMappedClassifier",
response);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} else {
// running headless, so just go ahead and wrap anyway
wrapClassifier = true;
}
} else {
// What did the user say - do they want to autowrap or not?
String response;
try {
response = Utils.getDontShowDialogResponse("weka.gui.explorer.ClassifierPanel.AutoWrapInInputMappedClassifier");
if (response != null && response.equalsIgnoreCase("yes")) {
wrapClassifier = true;
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (wrapClassifier) {
weka.classifiers.misc.InputMappedClassifier temp =
new weka.classifiers.misc.InputMappedClassifier();
temp.setClassifier(classifierToUse);
temp.setModelHeader(new Instances(m_trainingSet, 0));
classifierToUse = temp;
}
}
if (m_trainingSet.equalHeaders(testSet) ||
(classifierToUse instanceof weka.classifiers.misc.InputMappedClassifier)) {
BatchClassifierEvent ce =
new BatchClassifierEvent(this, classifierToUse,
new DataSetEvent(this, m_trainingSet),
new DataSetEvent(this, e.getTestSet()),
e.getRunNumber(), e.getMaxRunNumber(),
e.getSetNumber(), e.getMaxSetNumber());
ce.setGroupIdentifier(m_currentBatchIdentifier.getTime());
if (m_log != null && !e.isStructureOnly()) {
m_log.statusMessage(statusMessagePrefix() + "Finished.");
}
notifyBatchClassifierListeners(ce);
} else {
// if headers do not match check to see if it's
// just the class that is different and that
// all class values are missing
if (testSet.numInstances() > 0) {
if (testSet.classIndex() == m_trainingSet.classIndex() &&
testSet.attributeStats(testSet.classIndex()).missingCount ==
testSet.numInstances()) {
// now check the other attributes against the training
// structure
boolean ok = true;
for (int i = 0; i < testSet.numAttributes(); i++) {
if (i != testSet.classIndex()) {
ok = testSet.attribute(i).equals(m_trainingSet.attribute(i));
if (!ok) {
break;
}
}
}