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