From 11bb5ac591b6c0c0b50cfd8c2f4a43bbe6ad4f87 Mon Sep 17 00:00:00 2001 From: Jeff Heaton Date: Sat, 11 Jan 2014 08:31:18 -0600 Subject: [PATCH] Fixed issue with TemporalMLData set's index. --- .../ml/data/temporal/TemporalMLDataSet.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/encog/ml/data/temporal/TemporalMLDataSet.java b/src/main/java/org/encog/ml/data/temporal/TemporalMLDataSet.java index 250c09b0b..435660311 100644 --- a/src/main/java/org/encog/ml/data/temporal/TemporalMLDataSet.java +++ b/src/main/java/org/encog/ml/data/temporal/TemporalMLDataSet.java @@ -33,6 +33,7 @@ import org.encog.ml.data.MLDataPair; import org.encog.ml.data.basic.BasicMLData; import org.encog.ml.data.basic.BasicMLDataPair; +import org.encog.ml.data.temporal.TemporalDataDescription.Type; import org.encog.neural.data.basic.BasicNeuralData; import org.encog.neural.data.basic.BasicNeuralDataSet; import org.encog.util.time.TimeSpan; @@ -329,7 +330,14 @@ public TemporalPoint createPoint(final int sequence) { private double formatData(final TemporalDataDescription desc, final int index) { final double[] result = new double[1]; - + + if( desc.getType()==Type.DELTA_CHANGE || desc.getType()==Type.PERCENT_CHANGE ) { + if (index + this.inputWindowSize > this.points.size()) { + throw new TemporalError("Can't generate input temporal data " + + "beyond the end of provided data."); + } + } + switch (desc.getType()) { case DELTA_CHANGE: result[0] = getDataDeltaChange(desc, index); @@ -356,6 +364,7 @@ private double formatData(final TemporalDataDescription desc, */ public void generate() { sortPoints(); + // add one to the start index so we are "one ahead", needed to calculate DELTA, if that encoding is chosen. final int start = calculateStartIndex() + 1; final int setSize = calculateActualSetSize(); final int range = start + setSize - this.predictWindowSize @@ -379,11 +388,6 @@ public void generate() { * @return The input neural data generated. */ public BasicNeuralData generateInputNeuralData(final int index) { - if (index + this.inputWindowSize > this.points.size()) { - throw new TemporalError("Can't generate input temporal data " - + "beyond the end of provided data."); - } - final BasicNeuralData result = new BasicNeuralData( this.inputNeuronCount); int resultIndex = 0; @@ -487,7 +491,10 @@ private double getDataPercentChange(final TemporalDataDescription desc, */ private double getDataRAW(final TemporalDataDescription desc, final int index) { - final TemporalPoint point = this.points.get(index); + // Note: The reason that we subtract 1 from the index is because we are always one ahead. + // This allows the DELTA change formatter to work. DELTA change requires two timeslices, + // so we have to be one ahead. RAW only requires one, so we shift backwards. + final TemporalPoint point = this.points.get(index-1); return point.getData(desc.getIndex()); }