All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.classifiers.timeseries.gui.CustomPeriodicTestEditor Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.1.27
Show newest version
/*
 *   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 .
 */


/*
 *    CustomPeriodicTestEditor.java
 *    Copyright (C) 2010-2016 University of Waikato, Hamilton, New Zealand
 */

package weka.classifiers.timeseries.gui;

import weka.classifiers.timeseries.core.CustomPeriodicTest;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Vector;

/**
 * Provides an editor for a single interval from a custom periodic test. See
 * the javadoc for CustomPeriodicTest for information on the format that a
 * single test interval takes.
 * 
 * @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
 * @version $Revision: 45163 $
 */
public class CustomPeriodicTestEditor extends JPanel {
  
  /** For serialization */
  private static final long serialVersionUID = 8515802184515862677L;
  
  /** Combo box for the operator */
  protected JComboBox m_operator = new JComboBox();
  
  /** Combo box for the year field */
  protected JComboBox m_year = new JComboBox();
  
  /** Combo box for the month field */
  protected JComboBox m_month = new JComboBox();
  
  /** Combo box for the week of the year field */
  protected JComboBox m_week_of_yr = new JComboBox();
  
  /** Combo box for the week of the month field */
  protected JComboBox m_week_of_month = new JComboBox();
  
  /** Combo box for the day of the year field */
  protected JComboBox m_day_of_yr = new JComboBox();
  
  /** Combo box for the day of the month field */
  protected JComboBox m_day_of_month = new JComboBox();
  
  /** Combo box for the day of the week field */
  protected JComboBox m_day_of_week = new JComboBox();
  
  /** Combo box for the hour of the day field */
  protected JComboBox m_hour_of_day = new JComboBox();
  
  /** Combo box for the minute field */
  protected JComboBox m_min_of_hour = new JComboBox();
  
  /** Combo box for the second field */
  protected JComboBox m_second = new JComboBox();

  /** true if we are editing the upper bound */
  protected boolean m_right;
  
  /** The test to edit */
  protected CustomPeriodicTest m_testToEdit = 
    new CustomPeriodicTest(">*:*:*:*:*:*:*:*:*:*");
  
  /** The bound that we are editing */
  protected CustomPeriodicTest.TestPart m_partToEdit;
  
  /** Support for property change events */
  protected PropertyChangeSupport m_support = new PropertyChangeSupport(this);
  
  /**
   * Constructor
   * 
   * @param right true if we should edit the upper bound of the test
   */
  public CustomPeriodicTestEditor(boolean right) {
    setLayout(new GridLayout(1,10));
    
    /*SpinnerNumberModel snm = new SpinnerNumberModel();
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(52);
    m_week_of_yr = new JSpinner(snm);
    snm = new SpinnerNumberModel();
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(5);
    m_week_of_month = new JSpinner(snm);
    snm = new SpinnerNumberModel();
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(365);
    m_day_of_yr = new JSpinner(snm);
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(31);
    m_day_of_month = new JSpinner(snm);
    snm = new SpinnerNumberModel();
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(23);
    m_hour_of_day = new JSpinner(snm);
    snm = new SpinnerNumberModel();
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(59);
    m_min_of_hour = new JSpinner(snm);
    snm = new SpinnerNumberModel();
    snm.setValue(1); snm.setMinimum(1); snm.setMaximum(59);
    m_second = new JSpinner(snm);*/
    
    add(m_operator);
    add(m_year); add(m_month); add(m_week_of_yr); add(m_week_of_month);
    add(m_day_of_yr); add(m_day_of_month); add(m_day_of_week);
    add(m_hour_of_day); add(m_min_of_hour); add(m_second);
    
    m_right = right;
    if (m_right) {
      m_testToEdit = new CustomPeriodicTest("<*:*:*:*:*:*:*:*:*:*");
    }
    m_partToEdit = (m_right) ? m_testToEdit.getUpperTest() : m_testToEdit.getLowerTest();
    
    setupCombos();
    
    
    m_operator.setToolTipText("Operator");
    m_operator.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setOperator(m_operator.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_year.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setYear(m_year.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    }); 
    m_year.setToolTipText("Year");
    m_month.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setMonth(m_month.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });        
    m_month.setToolTipText("Month");
    m_week_of_yr.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setWeekOfYear(m_week_of_yr.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_week_of_yr.setToolTipText("Week of the year");
    m_week_of_month.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setWeekOfMonth(m_week_of_month.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_week_of_month.setToolTipText("Week of the month");
    m_day_of_yr.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setDayOfYear(m_day_of_yr.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_day_of_yr.setToolTipText("Day of the year");
    m_day_of_month.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setDayOfMonth(m_day_of_month.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_day_of_month.setToolTipText("Day of the month");
    m_day_of_week.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setDayOfWeek(m_day_of_week.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_day_of_week.setToolTipText("Day of the week");
    m_hour_of_day.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setHourOfDay(m_hour_of_day.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_hour_of_day.setToolTipText("Hour of the day");
    m_min_of_hour.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setMinuteOfHour(m_min_of_hour.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_min_of_hour.setToolTipText("Minute of the hour");
    m_second.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (m_partToEdit != null){
          m_partToEdit.setSecond(m_second.getSelectedItem().toString());
          m_support.firePropertyChange("", null, null);
        }
      }
    });
    m_second.setToolTipText("Second");
  }
  
  /**
   * Set the test to edit
   * 
   * @param t the CustomPeriodicTest to edit
   */
  public void setTestToEdit(CustomPeriodicTest t) {
    m_testToEdit = t;
    m_partToEdit = (m_right) ? m_testToEdit.getUpperTest() : m_testToEdit.getLowerTest();
    fillGUIElements();
  }
  
  /**
   * Get the test being edited
   * 
   * @return the CustomPeriodicTest being edited
   */
  public CustomPeriodicTest getTestBeingEdited() {
    return m_testToEdit;
  }
  
  /**
   * Initialize GUI elements from the test being edited
   */
  protected void fillGUIElements() {      

    if (m_partToEdit == null) {
      setEnabled(false);
      return;
    }
    
    m_operator.setSelectedItem(m_partToEdit.m_boundOperator.toString());
    m_year.setSelectedItem(numericValueToString(m_partToEdit.m_year));    
    m_month.setSelectedItem(m_partToEdit.getMonthString());        
    m_week_of_yr.setSelectedItem(numericValueToString(m_partToEdit.m_week_of_yr));        
    m_week_of_month.setSelectedItem(numericValueToString(m_partToEdit.m_week_of_month));        
    m_day_of_yr.setSelectedItem(numericValueToString(m_partToEdit.m_day_of_yr));        
    m_day_of_month.setSelectedItem(numericValueToString(m_partToEdit.m_day_of_month));    
    m_day_of_week.setSelectedItem(m_partToEdit.getDayString());    
    m_hour_of_day.setSelectedItem(numericValueToString(m_partToEdit.m_hour_of_day));        
    m_min_of_hour.setSelectedItem(numericValueToString(m_partToEdit.m_min_of_hour));        
    m_second.setSelectedItem(numericValueToString(m_partToEdit.m_second));
    
    /*((SpinnerNumberModel)m_week_of_yr.getModel()).setValue(partToEdit.m_week_of_yr);
    ((SpinnerNumberModel)m_week_of_month.getModel()).setValue(partToEdit.m_week_of_month);
    ((SpinnerNumberModel)m_day_of_yr.getModel()).setValue(partToEdit.m_day_of_yr);
    ((SpinnerNumberModel)m_day_of_month.getModel()).setValue(partToEdit.m_day_of_month); */
    
    /*((SpinnerNumberModel)m_hour_of_day.getModel()).setValue(partToEdit.m_hour_of_day);
    ((SpinnerNumberModel)m_min_of_hour.getModel()).setValue(partToEdit.m_min_of_hour);
    ((SpinnerNumberModel)m_second.getModel()).setValue(partToEdit.m_second); */
  }
  
  /**
   * Converts an integer field value to a string. Values equal to -100
   * indicate match anything "wildcards", represented as "*"
   * 
   * @param val the integer value to convert to a string
   * @return the value converted to a string.
   */
  protected String numericValueToString(int val) {
    if (val == -100) {
      return "*";
    }
    return "" + val;
  }
  
  /**
   * Set up combo boxes
   */
  protected void setupCombos() {
    Vector inequalities = new Vector();
    
    if (!m_right) {
      inequalities.add(">"); inequalities.add(">="); inequalities.add("=");
    } else {
      inequalities.add("<");
      inequalities.add("<=");
    }
    
    m_operator.setModel(new DefaultComboBoxModel(inequalities));
    
    Vector years = new Vector();
    years.add("*");
    for (int i = 1900; i <=2100; i++) {
      years.add("" + i);
    }
    m_year.setModel(new DefaultComboBoxModel(years));
    
    Vector months = new Vector();
    months.add("*");
    months.add("jan"); months.add("feb"); months.add("mar"); months.add("apr");
    months.add("may"); months.add("jun"); months.add("jul"); months.add("aug");
    months.add("sep"); months.add("oct"); months.add("nov"); months.add("dec");
    m_month.setModel(new DefaultComboBoxModel(months));
    
    Vector days = new Vector();
    days.add("*");
    days.add("sun"); days.add("mon"); days.add("tue"); days.add("wed"); 
    days.add("thu"); days.add("fri"); days.add("sat");
    m_day_of_week.setModel(new DefaultComboBoxModel(days));
    
    Vector week = new Vector();
    week.add("*");
    for (int i = 1; i <= 52; i++) {
      week.add("" + i);
    }
    m_week_of_yr.setModel(new DefaultComboBoxModel(week));
    week = new Vector();
    week.add("*");
    for (int i = 0; i <= 6; i++) {
      week.add("" + i);
    }
    m_week_of_month.setModel(new DefaultComboBoxModel(week));
    days = new Vector();
    days.add("*");
    for (int i = 1; i <= 365; i++) {
      days.add("" + i);
    }
    m_day_of_yr.setModel(new DefaultComboBoxModel(days));
    days = new Vector();
    days.add("*");
    for (int i = 1; i <= 31; i++) {
      days.add("" + i);
    }
    m_day_of_month.setModel(new DefaultComboBoxModel(days));
    Vector hour = new Vector();
    hour.add("*");
    for (int i = 0; i < 24; i++) {
      hour.add("" + i);
    }
    m_hour_of_day.setModel(new DefaultComboBoxModel(hour));
    Vector minute = new Vector();
    minute.add("*");
    for (int i = 0; i < 60; i++) {
      minute.add("" + i);
    }
    m_min_of_hour.setModel(new DefaultComboBoxModel(minute));
    Vector second = new Vector();
    second.add("*");
    for (int i = 0; i < 60; i++) {
      second.add("" + i);
    }
    m_second.setModel(new DefaultComboBoxModel(second));
  }
  
  /**
   * Set the enabled status of all the GUI elements
   * 
   * @param enabled true if all elements should be enabled
   */
  public void setEnabled(boolean enabled) {
    m_operator.setEnabled(enabled);
    m_year.setEnabled(enabled);
    m_month.setEnabled(enabled);
    m_week_of_yr.setEnabled(enabled);
    m_week_of_month.setEnabled(enabled);
    m_day_of_yr.setEnabled(enabled);
    m_day_of_month.setEnabled(enabled);
    m_day_of_week.setEnabled(enabled);
    m_hour_of_day.setEnabled(enabled);
    m_min_of_hour.setEnabled(enabled);
    m_second.setEnabled(enabled);    
  }
  
  /**
   * Adds a PropertyChangeListener who will be notified of value changes.
   *
   * @param l a value of type 'PropertyChangeListener'
   */
  @Override
  public void addPropertyChangeListener(PropertyChangeListener l) {
    if (m_support != null && l != null) {
      m_support.addPropertyChangeListener(l);
    }
  }

  /**
   * Removes a PropertyChangeListener.
   *
   * @param l a value of type 'PropertyChangeListener'
   */
  @Override
  public void removePropertyChangeListener(PropertyChangeListener l) {
    if (m_support != null && l != null) {
      m_support.removePropertyChangeListener(l);
    }
  }
  
  /**
   * Main method for testing this class. Expects a single quoted argument containing
   * the textual definition of one bound of a periodic test.
   * 
   * @param args
   */
  public static void main(String[] args) {
    try {
      CustomPeriodicTestEditor ed = new CustomPeriodicTestEditor(false);
      final CustomPeriodicTest test = new CustomPeriodicTest(">1965:aug:*:*:*:28:*:*:*:*");
      ed.setTestToEdit(test);
      final javax.swing.JFrame jf =
        new javax.swing.JFrame("Periodic test editor");
      jf.getContentPane().setLayout(new BorderLayout());
      jf.getContentPane().add(ed, BorderLayout.CENTER);
      jf.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(java.awt.event.WindowEvent e) {
          System.out.println(test.toString());
          jf.dispose();
          System.exit(0);
        }
      });
      jf.pack();
      jf.setVisible(true);
    } catch (Exception ex) {
      ex.printStackTrace();      
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy