Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Provides a time series forecasting environment for Weka. Includes a wrapper for Weka regression schemes that automates the process of creating lagged variables and date-derived periodic variables and provides the ability to do closed-loop forecasting. New evaluation routines are provided by a special evaluation module and graphing of predictions/forecasts are provided via the JFreeChart library. Includes both command-line and GUI user interfaces. Sample time series data can be found in ${WEKA_HOME}/packages/timeseriesForecasting/sample-data.
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
/*
* ForecastingPanel.java
* Copyright (C) 2010-2016 University of Waikato, Hamilton, New Zealand
*/
package weka.classifiers.timeseries.gui;
import weka.classifiers.timeseries.AbstractForecaster;
import weka.classifiers.timeseries.TSForecaster;
import weka.classifiers.timeseries.WekaForecaster;
import weka.classifiers.timeseries.core.OverlayForecaster;
import weka.core.SerializationHelper;
import weka.filters.supervised.attribute.TSLagMaker;
import weka.classifiers.timeseries.core.TSLagUser;
import weka.classifiers.timeseries.eval.TSEvaluation;
import weka.classifiers.timeseries.eval.graph.GraphDriver;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;
import weka.gui.BrowserHelper;
import weka.gui.LogPanel;
import weka.gui.Logger;
import weka.gui.ResultHistoryPanel;
import weka.gui.TaskLogger;
import weka.gui.WekaTaskMonitor;
import weka.gui.beans.KnowledgeFlowApp;
import weka.gui.beans.TimeSeriesForecasting;
import weka.gui.beans.TimeSeriesForecastingKFPerspective;
import weka.gui.knowledgeflow.TimeSeriesPerspective;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
/**
* Main GUI panel for the forecasting environment.
*
* @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
* @version $Revision: 52593 $
*/
public class ForecastingPanel extends JPanel {
/** For serialization */
private static final long serialVersionUID = -8415151090793037265L;
/** The training instances to operate on */
protected Instances m_instances;
/** Panel for logging */
protected Logger m_log;// = new LogPanel(new WekaTaskMonitor());
/** The simple configuration panel */
protected SimpleConfigPanel m_simpleConfigPanel;
/** The advanced configuration panel */
protected AdvancedConfigPanel m_advancedConfigPanel;
/** The current forecaster */
protected WekaForecaster m_forecaster = new WekaForecaster();
/** Tabbed pane to hold the simple and advanced config panels */
protected JTabbedPane m_configPane = new JTabbedPane();
/** The main textual output area */
protected JTextArea m_outText = new JTextArea(20, 50);
/** A panel holding and controlling the results for viewing */
protected ResultHistoryPanel m_history = new ResultHistoryPanel(m_outText);
/** Button to launch the forecaster */
protected JButton m_startBut = new JButton("Start");
/** Button to stop processing */
protected JButton m_stopBut = new JButton("Stop");
/** Button to visit help docs in browser */
protected JButton m_helpBut = new JButton("Help");
/**
* The split panel to divided the history/results area from the configuration
*/
protected JSplitPane m_splitP;
protected Thread m_runThread;
/** True if we are running in the old KnowledgeFlow as a perspective */
protected boolean m_isRunningAsPerspective = false;
/** Holds a listener for when we are running in the new KnowledgeFlow */
protected TimeSeriesPerspective.TimeSeriesModelListener m_forecasterListener;
/** The file chooser for selecting model files. */
protected JFileChooser m_fileChooser = new JFileChooser(new File(
System.getProperty("user.dir")));
/**
* For each dataset, perform a check (if a timestamp is specified) just once
* to see if it is in ascending order
*/
protected boolean m_sortedCheck;
/**
* Tabbed pane that holds the main text output plus tabs for any generated
* graphs
*/
JTabbedPane m_outputPane = new JTabbedPane();
/**
* Inner class extending Thread. Executes a forecasting task
*/
protected class ForecastingThread extends Thread {
protected boolean m_configAndBuild = true;
protected WekaForecaster m_threadForecaster = null;
protected String m_name;
public ForecastingThread(WekaForecaster forecaster, String name) {
m_threadForecaster = forecaster;
m_name = name;
}
public void setConfigureAndBuild(boolean configAndBuild) {
m_configAndBuild = configAndBuild;
}
@Override
public void run() {
LogPrintStream logger = new LogPrintStream();
logger.println("Setting up...");
String name = m_name;
StringBuffer outBuff = null;
if (name == null) {
name = (new SimpleDateFormat("HH:mm:ss - ")).format(new Date());
outBuff = new StringBuffer();
}
String fname = "";
try {
if (!m_sortedCheck) {
sortCheck();
}
// 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