source: branches/MetisMQI/src/main/java/weka/filters/unsupervised/attribute/TimeSeriesTranslate.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 6.7 KB
Line 
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/*
18 *    TimeSeriesTranslate.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.filters.unsupervised.attribute;
25
26import weka.core.Capabilities;
27import weka.core.Instance; 
28import weka.core.DenseInstance;
29import weka.core.Instances;
30import weka.core.RevisionUtils;
31import weka.core.SparseInstance;
32import weka.core.UnsupportedAttributeTypeException;
33import weka.core.Capabilities.Capability;
34import weka.core.Utils;
35
36/**
37 <!-- globalinfo-start -->
38 * An instance filter that assumes instances form time-series data and replaces attribute values in the current instance with the equivalent attribute values of some previous (or future) instance. For instances where the desired value is unknown either the instance may be dropped, or missing values used. Skips the class attribute if it is set.
39 * <p/>
40 <!-- globalinfo-end -->
41 *
42 <!-- options-start -->
43 * Valid options are: <p/>
44 *
45 * <pre> -R &lt;index1,index2-index4,...&gt;
46 *  Specify list of columns to translate in time. First and
47 *  last are valid indexes. (default none)</pre>
48 *
49 * <pre> -V
50 *  Invert matching sense (i.e. calculate for all non-specified columns)</pre>
51 *
52 * <pre> -I &lt;num&gt;
53 *  The number of instances forward to translate values
54 *  between. A negative number indicates taking values from
55 *  a past instance. (default -1)</pre>
56 *
57 * <pre> -M
58 *  For instances at the beginning or end of the dataset where
59 *  the translated values are not known, remove those instances
60 *  (default is to use missing values).</pre>
61 *
62 <!-- options-end -->
63 *
64 * @author Len Trigg (trigg@cs.waikato.ac.nz)
65 * @version $Revision: 5987 $
66 */
67public class TimeSeriesTranslate 
68  extends AbstractTimeSeries {
69 
70  /** for serialization */
71  static final long serialVersionUID = -8901621509691785705L;
72
73  /**
74   * Returns a string describing this classifier
75   * @return a description of the classifier suitable for
76   * displaying in the explorer/experimenter gui
77   */
78  public String globalInfo() {
79    return
80        "An instance filter that assumes instances form time-series data and "
81      + "replaces attribute values in the current instance with the equivalent "
82      + "attribute values of some previous (or future) instance. For "
83      + "instances where the desired value is unknown either the instance may "
84      + "be dropped, or missing values used. Skips the class attribute if it is set.";
85  }
86
87  /**
88   * Returns the Capabilities of this filter.
89   *
90   * @return            the capabilities of this object
91   * @see               Capabilities
92   */
93  public Capabilities getCapabilities() {
94    Capabilities result = super.getCapabilities();
95    result.disableAll();
96
97    // attributes
98    result.enableAllAttributes();
99    result.enable(Capability.MISSING_VALUES);
100   
101    // class
102    result.enableAllClasses();
103    result.enable(Capability.MISSING_CLASS_VALUES);
104    result.enable(Capability.NO_CLASS);
105   
106    return result;
107  }
108
109  /**
110   * Sets the format of the input instances.
111   *
112   * @param instanceInfo an Instances object containing the input instance
113   * structure (any instances contained in the object are ignored - only the
114   * structure is required).
115   * @return true if the outputFormat may be collected immediately
116   * @throws UnsupportedAttributeTypeException if selected
117   * attributes are not numeric or nominal.
118   */
119  public boolean setInputFormat(Instances instanceInfo) throws Exception {
120
121    if ((instanceInfo.classIndex() > 0) && (!getFillWithMissing())) {
122      throw new IllegalArgumentException("TimeSeriesTranslate: Need to fill in missing values " +
123                                         "using appropriate option when class index is set.");
124    }
125    super.setInputFormat(instanceInfo);
126    // Create the output buffer
127    Instances outputFormat = new Instances(instanceInfo, 0); 
128    for(int i = 0; i < instanceInfo.numAttributes(); i++) {
129      if (i != instanceInfo.classIndex()) {
130        if (m_SelectedCols.isInRange(i)) {
131          if (outputFormat.attribute(i).isNominal()
132              || outputFormat.attribute(i).isNumeric()) {
133            outputFormat.renameAttribute(i, outputFormat.attribute(i).name()
134                                         + (m_InstanceRange < 0 ? '-' : '+')
135                                         + Math.abs(m_InstanceRange));
136          } else {
137            throw new UnsupportedAttributeTypeException("Only numeric and nominal attributes may be "
138                                                        + " manipulated in time series.");
139          }
140        }
141      }
142    }
143    outputFormat.setClassIndex(instanceInfo.classIndex());
144    setOutputFormat(outputFormat);
145    return true;
146  }
147 
148  /**
149   * Creates a new instance the same as one instance (the "destination")
150   * but with some attribute values copied from another instance
151   * (the "source")
152   *
153   * @param source the source instance
154   * @param dest the destination instance
155   * @return the new merged instance
156   */
157  protected Instance mergeInstances(Instance source, Instance dest) {
158
159    Instances outputFormat = outputFormatPeek();
160    double[] vals = new double[outputFormat.numAttributes()];
161    for(int i = 0; i < vals.length; i++) {
162      if ((i != outputFormat.classIndex()) && (m_SelectedCols.isInRange(i))) {
163        if (source != null) {
164          vals[i] = source.value(i);
165        } else {
166          vals[i] = Utils.missingValue();
167        }
168      } else {
169        vals[i] = dest.value(i);
170      }
171    }
172    Instance inst = null;
173    if (dest instanceof SparseInstance) {
174      inst = new SparseInstance(dest.weight(), vals);
175    } else {
176      inst = new DenseInstance(dest.weight(), vals);
177    }
178    inst.setDataset(dest.dataset());
179    return inst;
180  }
181 
182  /**
183   * Returns the revision string.
184   *
185   * @return            the revision
186   */
187  public String getRevision() {
188    return RevisionUtils.extract("$Revision: 5987 $");
189  }
190 
191  /**
192   * Main method for testing this class.
193   *
194   * @param argv should contain arguments to the filter: use -h for help
195   */
196  public static void main(String [] argv) {
197    runFilter(new TimeSeriesTranslate(), argv);
198  }
199}
Note: See TracBrowser for help on using the repository browser.