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 .
*/
/*
* AdvancedConfigPanel.java
* Copyright (C) 2010-2016 University of Waikato, Hamilton, New Zealand
*/
package weka.classifiers.timeseries.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import weka.classifiers.Classifier;
import weka.classifiers.timeseries.PrimingDataLearner;
import weka.classifiers.timeseries.WekaForecaster;
import weka.classifiers.timeseries.core.CustomPeriodicTest;
import weka.classifiers.timeseries.core.OverlayForecaster;
import weka.filters.supervised.attribute.TSLagMaker;
import weka.classifiers.timeseries.eval.TSEvalModule;
import weka.classifiers.timeseries.eval.TSEvaluation;
import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.Instances;
import weka.core.Range;
import weka.core.SerializedObject;
import weka.gui.AttributeSelectionPanel;
import weka.gui.ExtensionFileFilter;
import weka.gui.GenericObjectEditor;
import weka.gui.PropertyDialog;
import weka.gui.PropertyPanel;
public class AdvancedConfigPanel extends JPanel {
protected static final int NUM_PREDEFINED_PERIODICS = 7;
/**
* For serialization
*/
private static final long serialVersionUID = 5465960083615138964L;
/** The training instances to operate on */
protected Instances m_instances;
/** A reference to the simple config panel */
protected SimpleConfigPanel m_simpleConfig;
/** Editor for selecting and configuring the base algorithm */
protected GenericObjectEditor m_baseLearnerEditor = new GenericObjectEditor();
/** Property panel for editing base algorithm */
protected PropertyPanel m_baseLearnerPanel = new PropertyPanel(
m_baseLearnerEditor);
/** Custom lags checkbox */
protected JCheckBox m_useCustomLags = new JCheckBox("Use custom lag lengths");
/** Min lag spinner */
protected JSpinner m_minLagSpinner;
/** Max lag spinner */
protected JSpinner m_maxLagSpinner;
/** Remove the initial instances where lag values are unknown/missing */
protected JCheckBox m_removeLeadingInstancesWithMissingLags = new JCheckBox(
"Remove leading instances with unknown lag values");
/** Include powers of time attributes */
protected JCheckBox m_powersOfTime = new JCheckBox("Include powers of time");
/** Include products of time and lagged variables as attributes */
protected JCheckBox m_timeLagProducts = new JCheckBox(
"Included products of time and lagged variables");
protected JButton m_moreOptsBut = new JButton("More options...");
/** Adjust for variance check box */
protected JCheckBox m_adjustForVarianceCheckBox = new JCheckBox(
"Adjust for variance");
/** Field for fine tuning the lags that are created by specifying a range */
protected JTextField m_fineTuneLagsField = new JTextField();
/** Check box for averaging consecutive long lags */
protected JCheckBox m_averageLongLags = new JCheckBox(
"Average consecutive long lags");
/** Spinner for selecting which lag to start averaging from */
protected JSpinner m_averageLagsAfter;
/**
* Spinner for selecting how many consecutive long lags to average into one
* new field
*/
protected JSpinner m_numConsecutiveToAverage;
/** Data structure for holding the field names for date-derived fields */
protected Instances m_dateDerivedPeriodicsHeader;
/** Custom date-derived fields check box */
protected JCheckBox m_customizeDateDerivedPeriodics = new JCheckBox(
"Customize");
/** Edit button for custom date-derived fields */
protected JButton m_editCustomPeriodicBut = new JButton("Edit");
/** Add button for custom date-derived fields */
protected JButton m_addCustomPeriodicBut = new JButton("New");
/** Delete button for custom date-derived fields */
protected JButton m_deleteCustomPeriodicBut = new JButton("Delete");
/** Save button for saving date-derived periodics to a file */
protected JButton m_savePeriodicBut = new JButton("Save");
/** Load button for loading pre-defined date-derived periodics from a file */
protected JButton m_loadPeriodicBut = new JButton("Load");
/** File chooser for saving/loading date-derived periodics */
protected JFileChooser m_fileChooser;
/** Map of custom date-derived fields */
protected Map> m_customPeriodics =
new HashMap>();
/** Panel for holding and selecting date-derived periodic attributes */
protected AttributeSelectionPanelExtended m_dateDerivedPeriodicSelector =
new AttributeSelectionPanelExtended(false, false, false, false);
/** Non date-based primary periodic stuff */
protected JComboBox m_primaryPeriodicCombo = new JComboBox();
/** Panel for holding and selecting overlay fields */
protected AttributeSelectionPanelExtended m_overlaySelector =
new AttributeSelectionPanelExtended(true, true, true, true);
/** Check box for overlay fields */
protected JCheckBox m_useOverlayData = new JCheckBox("Use overlay data");
/** Holds the structure of overlay fields */
protected Instances m_overlayHeader;
/** Holds the names of the currently selected evaluation modules */
protected Instances m_evaluationModsHeader;
/** Panel for holding and selecting evaluation modules */
protected AttributeSelectionPanel m_evaluationMetrics =
new AttributeSelectionPanel(false, false, false, false);
/** Perform training set evaluation check box */
protected JCheckBox m_trainingCheckBox =
new JCheckBox("Evaluate on training");
/** Perform hold-out evaluation check box */
protected JCheckBox m_holdoutCheckBox = new JCheckBox(
"Evaluate on held out training");
/** Text field for setting the size of the hold-out set */
protected JTextField m_holdoutSize = new JTextField();
/** show the separate test set checkbox and button? */
protected boolean m_allowSeparateTestSet = true;
/** Separate test set check box */
protected JCheckBox m_separateTestSetCheckBox = new JCheckBox(
"Evaluate on a separate test set");
/** Button for selecting a separate test set */
protected JButton m_testSetBut = new JButton("Separate test set");
/** Check box for outputting predictions */
protected JCheckBox m_outputPredsCheckBox = new JCheckBox(
"Output predictions at step");
/** Combo box for selecting which target to output predictions for */
protected JComboBox m_outputPredsCombo = new JComboBox();
protected JLabel m_outputPredsComboLabel = new JLabel("Target to output",
JLabel.RIGHT);
/** Spinner for selecting which step to output */
protected JSpinner m_outputStepSpinner;
protected JLabel m_outputStepLabel = new JLabel("Step to output",
JLabel.RIGHT);
/** Output future predictions check box */
protected JCheckBox m_outputFutureCheckBox = new JCheckBox(
"Output future predictions beyond end of series ");
/** Graph predictions at step check box */
protected JCheckBox m_graphPredsAtStepCheckBox = new JCheckBox(
"Graph predictions at step");
/** Spinner for selecting which step to graph predictions at */
protected JSpinner m_graphPredsAtStepSpinner;
protected JLabel m_stepLab = new JLabel("Steps to graph");
/** Graph targets for specific step */
protected JCheckBox m_graphTargetForStepsCheckBox = new JCheckBox(
"Graph target at steps:");
/** Graph target at steps combo box */
protected JComboBox m_graphTargetAtStepsCombo = new JComboBox();
protected JLabel m_targetComboLabel = new JLabel("Target to graph",
JLabel.RIGHT);
/**
* Text field for specifying a range of steps to graph for the selected target
*/
protected JTextField m_stepRange = new JTextField("1");
/** Graph future predictions check box */
protected JCheckBox m_graphFutureCheckBox = new JCheckBox(
"Graph future predictions beyond end of series");
static {
GenericObjectEditor.registerEditors();
}
/** A tabbed pane to hold the individual advanced configuration panels */
JTabbedPane m_configHolder = new JTabbedPane();
/**
* Constructor
*
* @param s a reference to the simple configuration panel
* @param allowSeparateTestSet true if the separate test set button is to be
* displayed
*/
public AdvancedConfigPanel(SimpleConfigPanel s, boolean allowSeparateTestSet) {
m_allowSeparateTestSet = allowSeparateTestSet;
m_simpleConfig = s;
setLayout(new BorderLayout());
layoutLearnerPanel();
layoutLagPanel();
layoutDateDerivedPeriodicPanel();
layoutOverlayPanel();
layoutEvaluationPanel();
layoutOutputPanel();
add(m_configHolder, BorderLayout.CENTER);
}
/**
* Constructor
*
* @param s a reference to the simple configuration panel
*/
public AdvancedConfigPanel(SimpleConfigPanel s) {
this(s, true);
}
/**
* Tests the Weka advanced config panel from the command line.
*
* @param args must contain the name of an arff file to load.
*/
public static void main(String[] args) {
try {
if (args.length == 0) {
throw new Exception("supply the name of an arff file");
}
Instances i =
new Instances(new java.io.BufferedReader(
new java.io.FileReader(args[0])));
SimpleConfigPanel scp = new SimpleConfigPanel(null);
scp.setInstances(i);
AdvancedConfigPanel acp = new AdvancedConfigPanel(scp);
// acp.setInstances(i);
final javax.swing.JFrame jf = new javax.swing.JFrame("Weka Forecasting");
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(acp, BorderLayout.CENTER);
jf.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
jf.dispose();
System.exit(0);
}
});
jf.pack();
jf.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
}
/**
* Get a title for displaying in the tab that will hold this panel
*
* @return a title for this configuration panel
*/
public String getTabTitle() {
return "Advanced configuration";
}
/**
* Get the tool tip for this configuration panel
*
* @return the tool tip for this configuration panel
*/
public String getTabTitleToolTip() {
return "Advanced configuration";
}
/**
* Get the underlying Weka classifier that will be used to make the
* predictions
*
* @return the underlying Weka classifier
*/
public Classifier getBaseClassifier() {
return (Classifier) m_baseLearnerEditor.getValue();
}
/**
* Set the instances that will be used in the training and evaluation of the
* forecaster
*
* @param train the instances to use in the training and evaluation process
*/
public void setInstances(Instances train) {
m_instances = train;
updatePanel();
}
/**
* Set the enabled/disabled status of date-derived periodic panel and its
* associated widgets.
*
* @param s true if date-derived periodicis is to be enabled
*/
public void enableDateDerivedPeriodics(boolean s) {
m_customizeDateDerivedPeriodics.setEnabled(s);
if (!s) {
m_customizeDateDerivedPeriodics.setSelected(false);
m_addCustomPeriodicBut.setEnabled(false);
m_editCustomPeriodicBut.setEnabled(false);
m_deleteCustomPeriodicBut.setEnabled(false);
m_savePeriodicBut.setEnabled(false);
m_loadPeriodicBut.setEnabled(false);
}
}
/**
* Returns true if date-derived periodics is enabled
*
* @return if date-derived periodics is enabled.
*/
public boolean isEnabledCustomizeDateDerivedPeriodics() {
return m_customizeDateDerivedPeriodics.isEnabled();
}
/**
* Returns true if the date-derived periodics check box is selected.
*
* @return true if the date-derived periodics check box is selected.
*/
public boolean getCustomizeDateDerivedPeriodics() {
return m_customizeDateDerivedPeriodics.isSelected();
}
/**
* Returns true if the user has opted to customize the lags.
*
* @return true if custom lags are in use.
*/
public boolean isUsingCustomLags() {
return m_useCustomLags.isSelected();
}
/**
* Gets the size of the holdout set.
*
* @return the size of the holdout set or 0 if no holdout set is being used.
*/
public double getHoldoutSetSize() {
double result = 0;
if (m_holdoutCheckBox.isSelected()) {
try {
result = Double.parseDouble(m_holdoutSize.getText());
} catch (NumberFormatException ex) {
}
}
return result;
}
/**
* Returns true if the user has opted to output future predictions
*
* @return if the user has opted to output future predictions
*/
public boolean getOutputFuturePredictions() {
return m_outputFutureCheckBox.isSelected();
}
/**
* Returns at which step to output predictions. Returns 0 if the user has not
* opted to output predictions.
*
* @return step at which to output predictions or 0 if no predictions are to
* be output.
*/
public int getOutputPredictionsAtStep() {
if (!m_outputPredsCheckBox.isSelected()) {
return 0;
}
return ((SpinnerNumberModel) m_outputStepSpinner.getModel()).getNumber()
.intValue();
}
/**
* Returns true if the user has opted to graph a target at specified steps
*
* @return true if the user has opted to graph a target at specified steps
*/
public boolean getGraphTargetForSteps() {
return m_graphTargetForStepsCheckBox.isSelected();
}
/**
* Return the target that is to be graphed at various steps
*
* @return the target that is to be graphed at specified steps or null if the
* user has not opted to graph a targert at specified steps
*/
public String getGraphTargetForStepsTarget() {
if (!getGraphTargetForSteps()) {
return null;
}
return m_graphTargetAtStepsCombo.getSelectedItem().toString();
}
/**
* If the user has opted to graph a target a various steps, then this method
* returns the list of steps that they have selected.
*
* @return the list of steps at which to graph a target, or null if the user
* has not opted to graph a target at various steps.
*/
public List getGraphTargetForStepsStepList() {
String rng = m_stepRange.getText();
if (rng == null || rng.length() == 0) {
return null;
}
Range range = new Range(rng);
range.setUpper(m_simpleConfig.getHorizonValue());
int[] indices = range.getSelection();
List rangeList = new ArrayList();
for (int i : indices) {
rangeList.add((i + 1));
}
return rangeList;
}
/**
* Get the selected target to output predictions for. Returns null if the user
* has not opted to output predictions.
*
* @return the name of the target to output predictions for or null if no
* predictions are to be output.
*/
public String getOutputPredictionsTarget() {
if (!m_outputPredsCheckBox.isSelected()) {
return null;
}
return m_outputPredsCombo.getSelectedItem().toString();
}
/**
* Get the step number to graph all the targets at.
*
* @return the step number to graph all the targets at, or 0 if the user has
* not opted to graph all the targets.
*/
public int getGraphPredictionsAtStep() {
if (!m_graphPredsAtStepCheckBox.isSelected()) {
return 0;
}
return ((SpinnerNumberModel) m_graphPredsAtStepSpinner.getModel())
.getNumber().intValue();
}
/**
* Returns true if the user has opted to graph future predictions
*
* @return true if the user has opted graph future predictions
*/
public boolean getGraphFuturePredictions() {
return m_graphFutureCheckBox.isSelected();
}
/**
* Updates various enabled/selected status of widgets based on the current
* configuration
*/
public void updatePanel() {
updateDateDerivedPanel();
updatePrimaryPeriodic();
updateOutputPanel();
updateOverlayPanel();
}
/**
* Updates the status/selection of widgets on the overlay panel
*/
public void updateOverlayPanel() {
Instances newI = createAvailableOverlayList();
if (newI == null) {
m_useOverlayData.setSelected(false);
m_useOverlayData.setEnabled(false);
m_overlaySelector.clearTableModel();
} else {
m_useOverlayData.setEnabled(true);
if (m_useOverlayData.isSelected()) {
if (m_overlayHeader == null || !newI.equalHeaders(m_overlayHeader)) {
m_overlayHeader = newI;
m_overlaySelector.setInstances(newI);
}
}
}
}
private Instances createAvailableOverlayList() {
String ppfn = m_primaryPeriodicCombo.getSelectedItem().toString();
Instances result = null;
if (m_simpleConfig.m_targetHeader != null) {
ArrayList availableAtts = new ArrayList();
int[] selectedTargets =
m_simpleConfig.m_targetPanel.getSelectedAttributes();
for (int i = 0; i < m_instances.numAttributes(); i++) {
// skip all date attributes
if (m_instances.attribute(i).isDate()) {
continue;
}
// skip any primary periodic
if (m_instances.attribute(i).name().equals(ppfn)) {
continue;
}
if (m_simpleConfig.m_targetHeader.attribute(m_instances.attribute(i)
.name()) != null) {
// now need to check whether it's been selected as a target
int indexToCheck =
m_simpleConfig.m_targetHeader.attribute(
m_instances.attribute(i).name()).index();
boolean ok = true;
for (int selectedTarget : selectedTargets) {
if (indexToCheck == selectedTarget) {
ok = false; // can't use this attribute
break;
}
}
if (ok) {
availableAtts.add(new Attribute(m_instances.attribute(i).name()));
}
} else {
// this is available
availableAtts.add(new Attribute(m_instances.attribute(i).name()));
}
}
if (availableAtts.size() > 0) {
result = new Instances("Overlay", availableAtts, 1);
}
}
return result;
}
/**
* Updates the status/selection of various widgets on the output panel
*/
public void updateOutputPanel() {
if (m_simpleConfig.m_targetHeader != null) {
// configure the target combos
Vector candidates = new Vector();
for (int i = 0; i < m_simpleConfig.m_targetHeader.numAttributes(); i++) {
candidates.add(m_simpleConfig.m_targetHeader.attribute(i).name());
}
m_graphTargetAtStepsCombo.setModel(new DefaultComboBoxModel(candidates));
m_outputPredsCombo.setModel(new DefaultComboBoxModel(candidates));
}
}
/**
* Updates the status/selection of various widgets on the date-derived
* periodics panel
*/
protected void updateDateDerivedPanel() {
if (m_simpleConfig.m_timeStampCombo.getSelectedItem() == null) {
return;
}
String selectedTimeStamp =
m_simpleConfig.m_timeStampCombo.getSelectedItem().toString();
if (selectedTimeStamp.equals("")
|| selectedTimeStamp.equals("