Package weka.classifiers.timeseries.core

Examples of weka.classifiers.timeseries.core.TSLagMaker$PeriodicityHandler


  protected JFreeChart getPredictedTargetsChart(TSForecaster forecaster,
      ErrorModule preds, List<String> targetNames, int stepNumber,
      int instanceNumOffset, Instances data) {

    if (forecaster instanceof TSLagUser && data != null) {
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      if (lagMaker.getAdjustForTrends()
          && !lagMaker.isUsingAnArtificialTimeIndex()) {

        // fill in any missing time stamps only
        data = new Instances(data);
        data = weka.classifiers.timeseries.core.Utils.replaceMissing(data,
            null, lagMaker.getTimeStampField(), true,
            lagMaker.getPeriodicity(), lagMaker.getSkipEntries());
      }
    }

    // set up a collection of predicted and actual series
    XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection();
    for (String target : targetNames) {
      XYIntervalSeries targetSeries = new XYIntervalSeries(target + "-actual",
          false, false);
      xyDataset.addSeries(targetSeries);
      targetSeries = new XYIntervalSeries(target + "-predicted", false, false);
      xyDataset.addSeries(targetSeries);

    }

    ValueAxis timeAxis = null;
    NumberAxis valueAxis = new NumberAxis("");
    valueAxis.setAutoRangeIncludesZero(false);
    int timeIndex = -1;
    boolean timeAxisIsDate = false;
    if (forecaster instanceof TSLagUser && data != null) {
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      if (!lagMaker.isUsingAnArtificialTimeIndex()
          && lagMaker.getAdjustForTrends()) {
        String timeName = lagMaker.getTimeStampField();
        if (data.attribute(timeName).isDate()) {
          timeAxis = new DateAxis("");
          timeAxisIsDate = true;
          timeIndex = data.attribute(timeName).index();
        }
View Full Code Here


  protected JFreeChart getPredictedStepsChart(TSForecaster forecaster,
      List<ErrorModule> preds, String targetName, List<Integer> stepsToPlot,
      int instanceNumOffset, Instances data) {

    if (forecaster instanceof TSLagUser && data != null) {
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      if (lagMaker.getAdjustForTrends()
          && !lagMaker.isUsingAnArtificialTimeIndex()) {

        // fill in any missing time stamps only
        data = new Instances(data);
        data = weka.classifiers.timeseries.core.Utils.replaceMissing(data,
            null, lagMaker.getTimeStampField(), true,
            lagMaker.getPeriodicity(), lagMaker.getSkipEntries());
      }
    }

    // set up a collection of predicted series
    XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection();

    XYIntervalSeries targetSeries = new XYIntervalSeries(targetName, false,
        false);
    xyDataset.addSeries(targetSeries);
    // for (int i = 0; i < preds.size(); i++) {
    for (int z = 0; z < stepsToPlot.size(); z++) {
      int i = stepsToPlot.get(z);
      i--;
      // ignore out of range steps
      if (i < 0 || i >= preds.size()) {
        continue;
      }

      String step = "-steps";
      if (i == 0) {
        step = "-step";
      }
      targetSeries = new XYIntervalSeries(targetName + "_" + (i + 1) + step
          + "-ahead", false, false);
      xyDataset.addSeries(targetSeries);
    }

    ValueAxis timeAxis = null;
    NumberAxis valueAxis = new NumberAxis("");
    valueAxis.setAutoRangeIncludesZero(false);
    int timeIndex = -1;
    boolean timeAxisIsDate = false;
    if (forecaster instanceof TSLagUser && data != null) {
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      if (!lagMaker.isUsingAnArtificialTimeIndex()
          && lagMaker.getAdjustForTrends()) {
        String timeName = lagMaker.getTimeStampField();
        if (data.attribute(timeName).isDate()) {
          timeAxis = new DateAxis("");
          timeAxisIsDate = true;
          timeIndex = data.attribute(timeName).index();
        }
View Full Code Here

  protected JFreeChart getFutureForecastChart(TSForecaster forecaster,
      List<List<NumericPrediction>> preds, List<String> targetNames,
      Instances history) {

    if (forecaster instanceof TSLagUser && history != null) {
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      if (lagMaker.getAdjustForTrends()
          && !lagMaker.isUsingAnArtificialTimeIndex()) {

        // fill in any missing time stamps only
        history = new Instances(history);
        history = weka.classifiers.timeseries.core.Utils.replaceMissing(
            history, null, lagMaker.getTimeStampField(), true,
            lagMaker.getPeriodicity(), lagMaker.getSkipEntries());
      }
    }

    // set up a collection of series
    XYIntervalSeriesCollection xyDataset = new XYIntervalSeriesCollection();

    if (history != null) {
      // add actual historical data values
      for (String targetName : targetNames) {
        XYIntervalSeries targetSeries = new XYIntervalSeries(targetName, false,
            false);
        xyDataset.addSeries(targetSeries);
      }
    }

    // add predicted series
    for (String targetName : targetNames) {
      XYIntervalSeries targetSeries = new XYIntervalSeries(targetName
          + "-predicted", false, false);
      xyDataset.addSeries(targetSeries);
    }

    ValueAxis timeAxis = null;
    NumberAxis valueAxis = new NumberAxis("");
    valueAxis.setAutoRangeIncludesZero(false);
    int timeIndex = -1;
    boolean timeAxisIsDate = false;
    double artificialTimeStart = 0;
    double lastRealTimeValue = Utils.missingValue();
    if (forecaster instanceof TSLagUser && history != null) {
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      if (!lagMaker.isUsingAnArtificialTimeIndex()
          && lagMaker.getAdjustForTrends()) {
        String timeName = lagMaker.getTimeStampField();
        if (history.attribute(timeName).isDate()) {
          timeAxis = new DateAxis("");
          timeAxisIsDate = true;
          timeIndex = history.attribute(timeName).index();
        }
      } else {
        try {
          artificialTimeStart = (history != null) ? 1 : lagMaker
              .getArtificialTimeStartValue() + 1;
        } catch (Exception ex) {
        }
      }
    }
View Full Code Here

      // Check training data even if there is no evaluation on the
      // training data as output for a future forecast beyond the
      // end of the training data will require updated training data
      // too
      TSLagMaker lagMaker = ((TSLagUser) forecaster).getTSLagMaker();
      Instances trainMod = new Instances(m_trainingData);
      trainMod = weka.classifiers.timeseries.core.Utils.replaceMissing(
          trainMod, lagMaker.getFieldsToLag(), lagMaker.getTimeStampField(),
          false, lagMaker.getPeriodicity(), lagMaker.getSkipEntries());

      if (trainMod.numInstances() != m_trainingData.numInstances()) {
        m_trainingData = trainMod;
      }

      if (m_evaluateTestData) {
        m_missingTimeStampTestSetRows = new ArrayList<String>();
        m_missingTargetListTestSet = new ArrayList<Integer>();
        m_missingTimeStampListTestSet = new ArrayList<Integer>();

        Instances testMod = new Instances(m_testData);
        testMod = weka.classifiers.timeseries.core.Utils.replaceMissing(
            testMod, lagMaker.getFieldsToLag(), lagMaker.getTimeStampField(),
            false, lagMaker.getPeriodicity(), lagMaker.getSkipEntries(),
            m_missingTargetListTestSet, m_missingTimeStampListTestSet,
            m_missingTimeStampTestSetRows);

        if (testMod.numInstances() != m_testData.numInstances()) {
          m_testData = testMod;
View Full Code Here

    boolean timeIsInstanceNum = true;
    double timeStart = 1;
    double timeDelta = 1;
    String dateFormat = null;
    TSLagMaker lagMaker = null;
    if (forecaster instanceof TSLagUser) {
      lagMaker = ((TSLagUser) forecaster).getTSLagMaker();

      if (!lagMaker.isUsingAnArtificialTimeIndex()
          && lagMaker.getAdjustForTrends()) {
        // suss out the time stamp
        String timeStampName = lagMaker.getTimeStampField();
        if (insts.attribute(timeStampName).isDate()) {
          dateFormat = insts.attribute(timeStampName).getDateFormat();
        }
        timeStart = insts.instance(0).value(insts.attribute(timeStampName));
        timeDelta = lagMaker.getDeltaTime();
        timeIsInstanceNum = false;
      }
    }

    List<String> targets = AbstractForecaster.stringToList(forecaster
        .getFieldsToForecast());
    int maxColWidth = 1;
    for (String t : targets) {
      if (t.length() > maxColWidth) {
        maxColWidth = t.length();
      }
    }

    int maxTimeColWidth = "inst#".length();
    SimpleDateFormat format = null;
    if (dateFormat != null) {
      // timestamp needs to be interpreted as a date
      maxTimeColWidth = "Time".length();
      format = new SimpleDateFormat(dateFormat);
      String formatted = format.format(new Date((long) timeStart));
      if (formatted.length() > maxTimeColWidth) {
        maxTimeColWidth = formatted.length();
      }
    } else {
      // covers instance numbers and numeric timestamps
      double maxTime = timeStart
          + ((insts.numInstances() + m_horizon) * timeDelta);
      maxTimeColWidth = maxWidth(maxTimeColWidth, maxTime);
    }

    for (int i = 0; i < insts.numInstances(); i++) {
      Instance current = insts.instance(i);
      for (String t : targets) {
        if (!current.isMissing(insts.attribute(t))) {
          maxColWidth = maxWidth(maxColWidth, current.value(insts.attribute(t)));
        }
      }
    }

    StringBuffer result = new StringBuffer();
    if (dateFormat != null) {
      result.append(pad("Time", " ", maxTimeColWidth, false)).append("  ");
    } else {
      result.append(pad("inst#", " ", maxTimeColWidth, false)).append("  ");
    }

    for (String t : targets) {
      result.append(pad(t, " ", maxColWidth, true)).append(" ");
    }
    result.append("\n");

    int instNum = (int) timeStart;
    long dateTime = (long) timeStart;
    for (int i = 0; i < insts.numInstances() + futureForecast.size(); i++) {
      String predMarker = (i < insts.numInstances()) ? "" : "*";
      if (timeIsInstanceNum) {
        result.append(
            pad("" + instNum + predMarker, " ", maxTimeColWidth, false))
            .append("  ");
        instNum += (int) timeDelta;
      } else if (dateFormat != null) {
        result.append(
            pad(format.format(new Date(dateTime)) + predMarker, " ",
                maxTimeColWidth, false)).append("  ");
        if (lagMaker != null) {
          dateTime = (long) lagMaker.advanceSuppliedTimeValue(dateTime);
        } else {
          dateTime += (long) timeDelta;
        }
      } else {
        result.append(
View Full Code Here

   * @param forecaster the forecaster to apply the configuration to
   * @throws Exception if a problem occurs
   */
  public void applyToForecaster(WekaForecaster forecaster) throws Exception {
    if (forecaster != null) {
      TSLagMaker lagMaker = forecaster.getTSLagMaker();
     
      // Set variance adjustment
      lagMaker.setAdjustForVariance(m_adjustForVarianceCheckBox.isSelected());
     
      int minLag = ((SpinnerNumberModel)m_minLagSpinner.getModel()).
        getNumber().intValue();
      int maxLag = ((SpinnerNumberModel)m_maxLagSpinner.getModel()).
        getNumber().intValue();
      if (m_useCustomLags.isSelected()) {
       
        // set lag lengths from spinners               
        if (maxLag < minLag) {
          throw new Exception("Maximum lag value must be greater than or equal to" +
              " the minimum lag value");
        }
       
        if (m_instances != null && maxLag > m_instances.numInstances()) {
          throw new Exception("The maximum lag can't exceed the number instances (" +
              m_instances.numInstances() + ") in the data!");
        }
       
        lagMaker.setMinLag(minLag); lagMaker.setMaxLag(maxLag);
       
        if (m_fineTuneLagsField.getText() != null &&
            m_fineTuneLagsField.getText().length() > 0) {
          lagMaker.setLagRange(m_fineTuneLagsField.getText());
        }
       
        // set up averaging from spinners
        if (m_averageLongLags.isSelected()) {
          lagMaker.setAverageConsecutiveLongLags(true);
         
          int avLagsAfter = ((SpinnerNumberModel)m_averageLagsAfter.getModel()).
            getNumber().intValue();
          int numToAv = ((SpinnerNumberModel)m_numConsecutiveToAverage.getModel()).
            getNumber().intValue();
         
          if (avLagsAfter < minLag || avLagsAfter > maxLag) {
            throw new Exception("Point at which to start lag averaging must " +
                "lie between the minimum and maximum lag value.");
          }
         
          lagMaker.setAverageLagsAfter(avLagsAfter);
          lagMaker.setNumConsecutiveLongLagsToAverage(numToAv);
        } else {
          lagMaker.setAverageConsecutiveLongLags(false);
        }
      } else {
        lagMaker.setAverageConsecutiveLongLags(false);
        lagMaker.setLagRange("");
      }
     
      // date-derived periodic customization
      if (getCustomizeDateDerivedPeriodics()) {
        // reset all to false
        lagMaker.setAddAMIndicator(false); lagMaker.setAddDayOfWeek(false);
        lagMaker.setAddMonthOfYear(false); lagMaker.setAddQuarterOfYear(false);
        lagMaker.setAddWeekendIndicator(false); lagMaker.setAddDayOfMonth(false);
        lagMaker.setAddNumDaysInMonth(false);
       
        int[] selected = m_dateDerivedPeriodicSelector.getSelectedAttributes();
        if (m_dateDerivedPeriodicsHeader != null) {
          Map<String, ArrayList<CustomPeriodicTest>> custom =
            new HashMap<String, ArrayList<CustomPeriodicTest>>();
          for (int i = 0; i < selected.length; i++) {
            int s = selected[i];
            String name = m_dateDerivedPeriodicsHeader.attribute(s).name();
            if (s < NUM_PREDEFINED_PERIODICS) {
              if (name.equals("AM")) {
                lagMaker.setAddAMIndicator(true);             
              }
              if (name.equals("DayOfWeek")) {
                lagMaker.setAddDayOfWeek(true);
              }
              if (name.equals("DayOfMonth")) {
                lagMaker.setAddDayOfMonth(true);
              }
              if (name.equals("NumDaysInMonth")) {
                lagMaker.setAddNumDaysInMonth(true);
              }
              if (name.equals("Weekend")) {
                lagMaker.setAddWeekendIndicator(true);
              }
              if (name.equals("Month")) {
                lagMaker.setAddMonthOfYear(true);
              }
              if (name.equals("Quarter")) {
                lagMaker.setAddQuarterOfYear(true);
              }
            } else {
              // remove the "c_"
              name = name.replaceFirst("c_", "");
              ArrayList<CustomPeriodicTest> selectedP =
                m_customPeriodics.get(name);
              custom.put(name, selectedP);
               
            }
          }
          lagMaker.clearCustomPeriodics();
          if (custom.size() > 0) {           
            lagMaker.setCustomPeriodics(custom);
          }
        }
      }
     
      // non-date primary periodic attribute
      String ppfn = m_primaryPeriodicCombo.getSelectedItem().toString();
      if (!ppfn.equals("<None>")) {
        lagMaker.setPrimaryPeriodicFieldName(ppfn);
      } else {
        lagMaker.setPrimaryPeriodicFieldName("");
      }
     
      if (m_useOverlayData.isSelected() && m_overlayHeader != null &&
          forecaster instanceof OverlayForecaster) {
        int[] selected = m_overlaySelector.getSelectedAttributes();
View Full Code Here

          }
        }       
       
        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 + "]";
        }
View Full Code Here

        StringBuffer outBuff = new StringBuffer();
        WekaForecaster wf = (WekaForecaster)f;       
       
        String lagOptions = "";
        if (wf instanceof TSLagUser) {
          TSLagMaker lagMaker = ((TSLagUser)wf).getTSLagMaker();
          lagOptions = Utils.joinOptions(lagMaker.getOptions());
        }
       
        String fname = wf.getAlgorithmName();
        String algoName = fname.substring(0, fname.indexOf(' '));
        if (algoName.startsWith("weka.classifiers.")) {
View Full Code Here

   * @param forecaster the WekaForecaster to configure
   * @throws Exception if there is a problem configuring.
   */
  public void applyToForecaster(WekaForecaster forecaster) throws Exception {
    if (forecaster != null) {
      TSLagMaker lagMaker = forecaster.getTSLagMaker();

      String selected = m_timeStampCombo.getSelectedItem().toString();
      if (!selected.equals("<Use an artificial time index>")
          && !selected.equals("<None>")) {
        lagMaker.setTimeStampField(selected);
      } else {
        lagMaker.setTimeStampField("");
      }

      if (selected.equals("<None>")) {
        lagMaker.setAdjustForTrends(false);
      } else {
        lagMaker.setAdjustForTrends(true);
      }

      if (m_periodicityCombo.getSelectedItem().toString()
          .equals("<Detect automatically>")
          && (selected.equals("<Use an artificial time index>") && !m_advancedConfig
              .isUsingCustomLags())) {

        /*
         * JOptionPane.showConfirmDialog(this,
         * "Cannot automatically detect periodicity" +
         * " when using an artificial time index.", "Forecasting",
         * JOptionPane.OK_OPTION);
         */
        throw new Exception(
            "Cannot automatically detect periodicity when using "
                + "an artificial time index.");
      }

      if (m_periodicityCombo.getSelectedItem().toString()
          .equals("<Detect automatically>")
          && !selected.equals("<Use an artificial time index>")
          && !selected.equals("<None>")
          && (!m_instances.attribute(selected).isDate() && !m_advancedConfig
              .isUsingCustomLags())) {
        throw new Exception(
            "Cannot automatically detect periodicity when using "
                + "a non-date time stamp (select manually or use custom lags.");
      }

      // reset any date-derived periodics to false
      lagMaker.setAddAMIndicator(false);
      lagMaker.setAddDayOfWeek(false);
      lagMaker.setAddMonthOfYear(false);
      lagMaker.setAddQuarterOfYear(false);
      lagMaker.setAddWeekendIndicator(false);
      if (!selected.equals("<None>")) {
        checkPeriodicity(forecaster);
      }

      if (m_computeConfidence.isSelected()) {
View Full Code Here

TOP

Related Classes of weka.classifiers.timeseries.core.TSLagMaker$PeriodicityHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.