TimeSeriesTranslate.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    This program is free software; you can redistribute it and/or modify
  3.  *    it under the terms of the GNU General Public License as published by
  4.  *    the Free Software Foundation; either version 2 of the License, or
  5.  *    (at your option) any later version.
  6.  *
  7.  *    This program is distributed in the hope that it will be useful,
  8.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  *    GNU General Public License for more details.
  11.  *
  12.  *    You should have received a copy of the GNU General Public License
  13.  *    along with this program; if not, write to the Free Software
  14.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15.  */
  16. /*
  17.  *    TimeSeriesTranslate.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.filters.unsupervised.attribute;
  22. import weka.filters.*;
  23. import java.io.*;
  24. import java.util.*;
  25. import weka.core.*;
  26. /** 
  27.  * An instance filter that assumes instances form time-series data and
  28.  * replaces attribute values in the current instance with the equivalent
  29.  * attribute attribute values of some previous (or future) instance. For
  30.  * instances where the desired value is unknown either the instance may
  31.  * be dropped, or missing values used.<p>
  32.  *
  33.  * Valid filter-specific options are:<p>
  34.  *
  35.  * -R index1,index2-index4,...<br>
  36.  * Specify list of columns to calculate new values for.
  37.  * First and last are valid indexes.
  38.  * (default none)<p>
  39.  *
  40.  * -V <br>
  41.  * Invert matching sense (i.e. calculate for all non-specified columns)<p>
  42.  *
  43.  * -I num <br>
  44.  * The number of instances forward to translate values between.
  45.  * A negative number indicates taking values from a past instance.
  46.  * (default -1) <p>
  47.  *
  48.  * -M <br>
  49.  * For instances at the beginning or end of the dataset where the translated
  50.  * values are not known, use missing values (default is to remove those
  51.  * instances). <p>
  52.  *
  53.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  54.  * @version $Revision: 1.1 $
  55.  */
  56. public class TimeSeriesTranslate extends AbstractTimeSeries {
  57.   /**
  58.    * Sets the format of the input instances.
  59.    *
  60.    * @param instanceInfo an Instances object containing the input instance
  61.    * structure (any instances contained in the object are ignored - only the
  62.    * structure is required).
  63.    * @return true if the outputFormat may be collected immediately
  64.    * @exception UnsupportedAttributeTypeException if selected
  65.    * attributes are not numeric or nominal.
  66.    */
  67.   public boolean setInputFormat(Instances instanceInfo) throws Exception {
  68.     super.setInputFormat(instanceInfo);
  69.     // Create the output buffer
  70.     Instances outputFormat = new Instances(instanceInfo, 0); 
  71.     for(int i = 0; i < instanceInfo.numAttributes(); i++) {
  72.       if (m_SelectedCols.isInRange(i)) {
  73. if (outputFormat.attribute(i).isNominal()
  74.     || outputFormat.attribute(i).isNumeric()) {
  75.   outputFormat.renameAttribute(i, outputFormat.attribute(i).name()
  76.        + (m_InstanceRange < 0 ? '-' : '+')
  77.        + Math.abs(m_InstanceRange));
  78. } else {
  79.   throw new UnsupportedAttributeTypeException("Only numeric and nominal attributes may be "
  80.                                                       + " manipulated in time series.");
  81. }
  82.       }
  83.     }
  84.     setOutputFormat(outputFormat);
  85.     return true;
  86.   }
  87.   
  88.   /**
  89.    * Creates a new instance the same as one instance (the "destination")
  90.    * but with some attribute values copied from another instance
  91.    * (the "source")
  92.    *
  93.    * @param source the source instance
  94.    * @param dest the destination instance
  95.    * @return the new merged instance
  96.    */
  97.   protected Instance mergeInstances(Instance source, Instance dest) {
  98.     Instances outputFormat = outputFormatPeek();
  99.     double[] vals = new double[outputFormat.numAttributes()];
  100.     for(int i = 0; i < vals.length; i++) {
  101.       if (m_SelectedCols.isInRange(i)) {
  102. if (source != null) {
  103.   vals[i] = source.value(i);
  104. }
  105.       } else {
  106. vals[i] = dest.value(i);
  107.       }
  108.     }
  109.     Instance inst = null;
  110.     if (dest instanceof SparseInstance) {
  111.       inst = new SparseInstance(dest.weight(), vals);
  112.     } else {
  113.       inst = new Instance(dest.weight(), vals);
  114.     }
  115.     inst.setDataset(dest.dataset());
  116.     return inst;
  117.   }
  118.   
  119.   /**
  120.    * Main method for testing this class.
  121.    *
  122.    * @param argv should contain arguments to the filter: use -h for help
  123.    */
  124.   public static void main(String [] argv) {
  125.     try {
  126.       if (Utils.getFlag('b', argv)) {
  127.   Filter.batchFilterFile(new TimeSeriesTranslate(), argv); 
  128.       } else {
  129. Filter.filterFile(new TimeSeriesTranslate(), argv);
  130.       }
  131.     } catch (Exception ex) {
  132.       System.out.println(ex.getMessage());
  133.     }
  134.   }
  135. }