}
// copy the current state of things
Instances inst = new Instances(m_instances);
TSEvaluation eval = new TSEvaluation(inst,
m_advancedConfigPanel.getHoldoutSetSize());
if (m_configAndBuild) {
// configure the WekaForecaster with the base
// learner
m_threadForecaster.setBaseForecaster(m_advancedConfigPanel.getBaseClassifier());
m_simpleConfigPanel.applyToForecaster(m_threadForecaster);
m_advancedConfigPanel.applyToForecaster(m_threadForecaster);
}
m_simpleConfigPanel.applyToEvaluation(eval, m_threadForecaster);
m_advancedConfigPanel.applyToEvaluation(eval, m_threadForecaster);
eval.setForecastFuture(m_advancedConfigPanel.getOutputFuturePredictions() ||
m_advancedConfigPanel.getGraphFuturePredictions());
if (m_threadForecaster instanceof OverlayForecaster &&
((OverlayForecaster)m_threadForecaster).isUsingOverlayData()) {
if (!eval.getEvaluateOnTestData() &&
(m_advancedConfigPanel.m_outputFutureCheckBox.isSelected() ||
m_advancedConfigPanel.m_graphFutureCheckBox.isSelected())) {
// warn the user that future forecast can't be produced for the training data
dontShowMessageDialog("weka.classifiers.timeseries.gui.CantFutureForecastTraining",
"Unable to generate a future forecast beyond the end of the training\n" +
"data because there is no future overlay data available. Use a holdout\n" +
"set for evaluation in order to simulate having \"future\" overlay\n" +
"data available.\n\n",
"ForecastingPanel");
}
if (eval.getEvaluateOnTestData() &&
(m_advancedConfigPanel.m_outputFutureCheckBox.isSelected() ||
m_advancedConfigPanel.m_graphFutureCheckBox.isSelected())) {
// warn the user that future forecast can't be produced for the test data
dontShowMessageDialog("weka.classifiers.timeseries.gui.CantFutureForecastTesting",
"Unable to generate a future forecast beyond the end of the test\n" +
"data because there is no future overlay data available.\n\n",
"ForecastingPanel");
}
}
fname = m_threadForecaster.getAlgorithmName();
if (m_name == null) {
String algoName = fname.substring(0, fname.indexOf(' '));
if (algoName.startsWith("weka.classifiers.")) {
name += algoName.substring("weka.classifiers.".length());
} else {
name += algoName;
}
}
String lagOptions = "";
if (m_threadForecaster instanceof TSLagUser) {
TSLagMaker lagMaker = ((TSLagUser)m_threadForecaster).getTSLagMaker();
lagOptions = Utils.joinOptions(lagMaker.getOptions());
}
if (lagOptions.length() > 0 && m_name == null) {
name += " [" + lagOptions + "]";
}
if (m_name == null) {
outBuff.append("=== Run information ===\n\n");
} else {
outBuff = m_history.getNamedBuffer(name);
outBuff.append("\n=== Model re-evaluation===\n\n");
}
outBuff.append("Scheme:\n\t" + fname).append("\n\n");
if (lagOptions.length() > 0) {
outBuff.append("Lagged and derived variable options:\n\t").
append(lagOptions + "\n\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");
}
if (m_configAndBuild) {
m_history.addResult(name, outBuff);
}
m_history.setSingle(name);
if (m_log != null) {
m_log.logMessage("Started " + fname);
if (m_configAndBuild) {
logger.println("Training forecaster...");
}
if (m_log instanceof TaskLogger) {
((TaskLogger)m_log).taskStarted();
}
}
Instances trainInst = eval.getTrainingData();
if (m_configAndBuild) {
m_threadForecaster.buildForecaster(trainInst, logger);
outBuff.append("\n" + m_threadForecaster.toString());
m_history.updateResult(name);
}
if (eval.getEvaluateOnTrainingData() ||
eval.getEvaluateOnTestData()) {
logger.println("Evaluating...");
}
// evaluate the forecaster
eval.evaluateForecaster(m_threadForecaster, false, logger);
// output any predictions
if (m_advancedConfigPanel.getOutputPredictionsAtStep() > 0) {
int step = m_advancedConfigPanel.getOutputPredictionsAtStep();
String targetName = m_advancedConfigPanel.getOutputPredictionsTarget();
String fieldsToForecast = m_threadForecaster.getFieldsToForecast();
if (!fieldsToForecast.contains(targetName)) {
throw new Exception("Cannot output predictions for \""
+ targetName + "\" because that field is not being predicted.");
}
if (eval.getTrainingData() != null &&
eval.getEvaluateOnTrainingData()) {
String predString = eval.printPredictionsForTrainingData("=== Predictions " +
"for training data: " + targetName + " ("
+ step + (step > 1 ? "-steps ahead)" : "-step ahead)") + " ===",
targetName, step, eval.getPrimeWindowSize());
outBuff.append("\n").append(predString);
}
if (eval.getTestData() != null) {
int instanceNumOffset =
(eval.getTrainingData() != null &&
m_advancedConfigPanel.getHoldoutSetSize() > 0)
? eval.getTrainingData().numInstances()
: 0;
String predString = eval.printPredictionsForTestData("=== Predictions " +
"for test data: " + targetName + " ("
+ step + (step > 1 ? "-steps ahead)" : "-step ahead)") + " ===",
targetName, step, instanceNumOffset);
outBuff.append("\n").append(predString);
}
m_history.updateResult(name);
}
// output any future predictions
if (m_advancedConfigPanel.getOutputFuturePredictions()) {
if (eval.getTrainingData() != null /*&&
eval.getEvaluateOnTrainingData()*/) {
outBuff.append("\n=== Future predictions from end of training data ===\n");
outBuff.append(eval.printFutureTrainingForecast(m_threadForecaster));
}
if (eval.getTestData() != null &&
eval.getEvaluateOnTestData()) {
outBuff.append("\n=== Future predictions from end of test data ===\n");
outBuff.append(eval.printFutureTestForecast(m_threadForecaster));
}
m_history.updateResult(name);
}
// evaluation summary
if (eval.getEvaluateOnTrainingData() || eval.getEvaluateOnTestData()) {
outBuff.append("\n" + eval.toSummaryString());
m_history.updateResult(name);
}
// result object list
List<Object> resultList = (m_configAndBuild)
? new ArrayList<Object>()
: (List<Object>)m_history.getNamedObject(name);
if (!m_configAndBuild) {
// go through and remove any JPanels
List<Object> newResultList = new ArrayList<Object>();
for (int z = 0; z < resultList.size(); z++) {
if (resultList.get(z) instanceof TSForecaster ||
resultList.get(z) instanceof Instances) {
newResultList.add(resultList.get(z));
}
}
resultList = newResultList;
}
// handle graphs
List<JPanel> graphList = new ArrayList<JPanel>();
// graph predictions for targets at specific step
if (m_advancedConfigPanel.getGraphPredictionsAtStep() > 0) {
int stepNum = m_advancedConfigPanel.getGraphPredictionsAtStep();
List<String> targets =
AbstractForecaster.stringToList(m_threadForecaster.getFieldsToForecast());
if (eval.getTrainingData() != null && eval.getEvaluateOnTrainingData()) {
JPanel trainTargetsAtStep =
eval.graphPredictionsForTargetsOnTraining(GraphDriver.getDefaultDriver(),
m_threadForecaster, targets, stepNum, eval.getPrimeWindowSize());
trainTargetsAtStep.setToolTipText("Train pred. for targets");
graphList.add(trainTargetsAtStep);
}
if (eval.getTestData() != null && eval.getEvaluateOnTestData()) {
int instanceOffset = (eval.getPrimeForTestDataWithTestData())
? eval.getPrimeWindowSize()
: 0;
JPanel testTargetsAtStep =
eval.graphPredictionsForTargetsOnTesting(GraphDriver.getDefaultDriver(),
m_threadForecaster, targets, stepNum, instanceOffset);
testTargetsAtStep.setToolTipText("Test pred. for targets");
graphList.add(testTargetsAtStep);
}
}
// graph predictions for specific target at selected steps
if (m_advancedConfigPanel.getGraphTargetForSteps()) {
String fieldsToForecast = m_threadForecaster.getFieldsToForecast();
String selectedTarget =
m_advancedConfigPanel.getGraphTargetForStepsTarget();
if (!fieldsToForecast.contains(selectedTarget)) {
throw new Exception("Cannot graph predictions for \""
+ selectedTarget + "\" because that field is not being predicted.");
}
List<Integer> stepList =
m_advancedConfigPanel.getGraphTargetForStepsStepList();
if (eval.getTrainingData() != null && eval.getEvaluateOnTrainingData()) {
JPanel trainStepsForTarget =
eval.graphPredictionsForStepsOnTraining(GraphDriver.getDefaultDriver(),
m_threadForecaster, selectedTarget, stepList, eval.getPrimeWindowSize());
trainStepsForTarget.setToolTipText("Train pred. at steps");
graphList.add(trainStepsForTarget);
}
if (eval.getTestData() != null && eval.getEvaluateOnTestData()) {
int instanceOffset = (eval.getPrimeForTestDataWithTestData())
? eval.getPrimeWindowSize()
: 0;
JPanel testStepsForTarget =
eval.graphPredictionsForStepsOnTesting(GraphDriver.getDefaultDriver(),
m_threadForecaster, selectedTarget, stepList, instanceOffset);
testStepsForTarget.setToolTipText("Test pred. at steps");
graphList.add(testStepsForTarget);
}
}
// graph future predictions
if (m_advancedConfigPanel.getGraphFuturePredictions()) {
if (eval.getTrainingData() != null /*&& eval.getEvaluateOnTrainingData()*/) {
try {
JPanel trainFuture = eval.
graphFutureForecastOnTraining(GraphDriver.getDefaultDriver(),
m_threadForecaster, AbstractForecaster.
stringToList(m_threadForecaster.getFieldsToForecast()));
trainFuture.setToolTipText("Train future pred.");
graphList.add(trainFuture);
} catch (Exception ex) {
if (m_threadForecaster instanceof OverlayForecaster &&
((OverlayForecaster)m_threadForecaster).isUsingOverlayData()) {
if (m_log != null) {
m_log.logMessage("Unable to graph future forecast for training " +
"data because no future overlay data is available");
}
} else {
if (m_log != null) {
m_log.logMessage("Unable to graph future forecast for " +
"training data: " + ex.getMessage());
}
}
}
}
if (eval.getTestData() != null && eval.getEvaluateOnTestData()) {
try {
JPanel testFuture = eval.
graphFutureForecastOnTesting(GraphDriver.getDefaultDriver(),
m_threadForecaster, AbstractForecaster.
stringToList(m_threadForecaster.getFieldsToForecast()));
testFuture.setToolTipText("Test future pred.");