~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/filters/unsupervised/attribute/FirstOrder.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *    FirstOrder.java
 
19
 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
 
 
24
package weka.filters.unsupervised.attribute;
 
25
 
 
26
import weka.core.Attribute;
 
27
import weka.core.Capabilities;
 
28
import weka.core.FastVector;
 
29
import weka.core.Instance;
 
30
import weka.core.Instances;
 
31
import weka.core.Option;
 
32
import weka.core.OptionHandler;
 
33
import weka.core.Range;
 
34
import weka.core.SparseInstance;
 
35
import weka.core.UnsupportedAttributeTypeException;
 
36
import weka.core.Utils;
 
37
import weka.core.Capabilities.Capability;
 
38
import weka.filters.Filter;
 
39
import weka.filters.StreamableFilter;
 
40
import weka.filters.UnsupervisedFilter;
 
41
 
 
42
import java.util.Enumeration;
 
43
import java.util.Vector;
 
44
 
 
45
/** 
 
46
 <!-- globalinfo-start -->
 
47
 * This instance filter takes a range of N numeric attributes and replaces them with N-1 numeric attributes, the values of which are the difference between consecutive attribute values from the original instance. eg: <br/>
 
48
 * <br/>
 
49
 * Original attribute values<br/>
 
50
 * <br/>
 
51
 *    0.1, 0.2, 0.3, 0.1, 0.3<br/>
 
52
 * <br/>
 
53
 * New attribute values<br/>
 
54
 * <br/>
 
55
 *    0.1, 0.1, -0.2, 0.2<br/>
 
56
 * <br/>
 
57
 * The range of attributes used is taken in numeric order. That is, a range spec of 7-11,3-5 will use the attribute ordering 3,4,5,7,8,9,10,11 for the differences, NOT 7,8,9,10,11,3,4,5.
 
58
 * <p/>
 
59
 <!-- globalinfo-end -->
 
60
 * 
 
61
 <!-- options-start -->
 
62
 * Valid options are: <p/>
 
63
 * 
 
64
 * <pre> -R &lt;index1,index2-index4,...&gt;
 
65
 *  Specify list of columns to take the differences between.
 
66
 *  First and last are valid indexes.
 
67
 *  (default none)</pre>
 
68
 * 
 
69
 <!-- options-end -->
 
70
 *
 
71
 * @author Len Trigg (trigg@cs.waikato.ac.nz)
 
72
 * @version $Revision: 1.8 $
 
73
 */
 
74
public class FirstOrder 
 
75
  extends Filter
 
76
  implements UnsupervisedFilter, StreamableFilter, OptionHandler {
 
77
 
 
78
  /** for serialization */
 
79
  static final long serialVersionUID = -7500464545400454179L;
 
80
  
 
81
  /** Stores which columns to take differences between */
 
82
  protected Range m_DeltaCols = new Range();
 
83
 
 
84
  /**
 
85
   * Returns a string describing this filter
 
86
   *
 
87
   * @return a description of the filter suitable for
 
88
   * displaying in the explorer/experimenter gui
 
89
   */
 
90
  public String globalInfo() {
 
91
 
 
92
    return "This instance filter takes a range of N numeric attributes and replaces "
 
93
      + "them with N-1 numeric attributes, the values of which are the difference "
 
94
      + "between consecutive attribute values from the original instance. eg: \n\n"
 
95
      + "Original attribute values\n\n"
 
96
      + "   0.1, 0.2, 0.3, 0.1, 0.3\n\n"
 
97
      + "New attribute values\n\n"
 
98
      + "   0.1, 0.1, -0.2, 0.2\n\n"
 
99
      + "The range of attributes used is taken in numeric order. That is, a range "
 
100
      + "spec of 7-11,3-5 will use the attribute ordering 3,4,5,7,8,9,10,11 for the "
 
101
      + "differences, NOT 7,8,9,10,11,3,4,5.";
 
102
  }
 
103
 
 
104
  /**
 
105
   * Returns an enumeration describing the available options.
 
106
   *
 
107
   * @return an enumeration of all the available options.
 
108
   */
 
109
  public Enumeration listOptions() {
 
110
 
 
111
    Vector newVector = new Vector(1);
 
112
 
 
113
    newVector.addElement(new Option(
 
114
              "\tSpecify list of columns to take the differences between.\n"
 
115
              + "\tFirst and last are valid indexes.\n"
 
116
              + "\t(default none)",
 
117
              "R", 1, "-R <index1,index2-index4,...>"));
 
118
 
 
119
    return newVector.elements();
 
120
  }
 
121
 
 
122
 
 
123
  /**
 
124
   * Parses a given list of options. <p/>
 
125
   * 
 
126
   <!-- options-start -->
 
127
   * Valid options are: <p/>
 
128
   * 
 
129
   * <pre> -R &lt;index1,index2-index4,...&gt;
 
130
   *  Specify list of columns to take the differences between.
 
131
   *  First and last are valid indexes.
 
132
   *  (default none)</pre>
 
133
   * 
 
134
   <!-- options-end -->
 
135
   *
 
136
   * @param options the list of options as an array of strings
 
137
   * @throws Exception if an option is not supported
 
138
   */
 
139
  public void setOptions(String[] options) throws Exception {
 
140
 
 
141
    String deltaList = Utils.getOption('R', options);
 
142
    if (deltaList.length() != 0) {
 
143
      setAttributeIndices(deltaList);
 
144
    } else {
 
145
      setAttributeIndices("");
 
146
    }
 
147
    
 
148
    if (getInputFormat() != null)
 
149
      setInputFormat(getInputFormat());
 
150
  }
 
151
 
 
152
 
 
153
  /**
 
154
   * Gets the current settings of the filter.
 
155
   *
 
156
   * @return an array of strings suitable for passing to setOptions
 
157
   */
 
158
  public String [] getOptions() {
 
159
 
 
160
    String [] options = new String [2];
 
161
    int current = 0;
 
162
 
 
163
    if (!getAttributeIndices().equals("")) {
 
164
      options[current++] = "-R"; options[current++] = getAttributeIndices();
 
165
    }
 
166
 
 
167
    while (current < options.length) {
 
168
      options[current++] = "";
 
169
    }
 
170
    return options;
 
171
  }
 
172
 
 
173
  /** 
 
174
   * Returns the Capabilities of this filter.
 
175
   *
 
176
   * @return            the capabilities of this object
 
177
   * @see               Capabilities
 
178
   */
 
179
  public Capabilities getCapabilities() {
 
180
    Capabilities result = super.getCapabilities();
 
181
 
 
182
    // attributes
 
183
    result.enableAllAttributes();
 
184
    result.enable(Capability.MISSING_VALUES);
 
185
    
 
186
    // class
 
187
    result.enableAllClasses();
 
188
    result.enable(Capability.MISSING_CLASS_VALUES);
 
189
    result.enable(Capability.NO_CLASS);
 
190
    
 
191
    return result;
 
192
  }
 
193
 
 
194
  /**
 
195
   * Sets the format of the input instances.
 
196
   *
 
197
   * @param instanceInfo an Instances object containing the input instance
 
198
   * structure (any instances contained in the object are ignored - only the
 
199
   * structure is required).
 
200
   * @return true if the outputFormat may be collected immediately
 
201
   * @throws UnsupportedAttributeTypeException if any of the
 
202
   * selected attributes are not numeric 
 
203
   * @throws Exception if only one attribute has been selected.
 
204
   */
 
205
  public boolean setInputFormat(Instances instanceInfo) throws Exception {
 
206
 
 
207
    super.setInputFormat(instanceInfo);
 
208
 
 
209
    m_DeltaCols.setUpper(getInputFormat().numAttributes() - 1);
 
210
    int selectedCount = 0;
 
211
    for (int i = getInputFormat().numAttributes() - 1; i >= 0; i--) {
 
212
      if (m_DeltaCols.isInRange(i)) {
 
213
        selectedCount++;
 
214
        if (!getInputFormat().attribute(i).isNumeric()) {
 
215
          throw new UnsupportedAttributeTypeException("Selected attributes must be all numeric");
 
216
        }
 
217
      }
 
218
    }
 
219
    if (selectedCount == 1) {
 
220
      throw new Exception("Cannot select only one attribute.");
 
221
    }
 
222
 
 
223
    // Create the output buffer
 
224
    FastVector newAtts = new FastVector();
 
225
    boolean inRange = false;
 
226
    String foName = null;
 
227
    int clsIndex = -1;
 
228
    for(int i = 0; i < instanceInfo.numAttributes(); i++) {
 
229
      if (m_DeltaCols.isInRange(i) && (i != instanceInfo.classIndex())) {
 
230
        if (inRange) {
 
231
          Attribute newAttrib = new Attribute(foName);
 
232
          newAtts.addElement(newAttrib);
 
233
        }
 
234
        foName = instanceInfo.attribute(i).name();
 
235
        foName = "'FO " + foName.replace('\'', ' ').trim() + '\'';
 
236
        inRange = true;
 
237
      } else {
 
238
        newAtts.addElement((Attribute)instanceInfo.attribute(i).copy());
 
239
        if ((i == instanceInfo.classIndex()))
 
240
          clsIndex = newAtts.size() - 1;
 
241
      }      
 
242
    }
 
243
    Instances data = new Instances(instanceInfo.relationName(), newAtts, 0);
 
244
    data.setClassIndex(clsIndex);
 
245
    setOutputFormat(data);
 
246
    return true;
 
247
  }
 
248
  
 
249
 
 
250
  /**
 
251
   * Input an instance for filtering. Ordinarily the instance is processed
 
252
   * and made available for output immediately. Some filters require all
 
253
   * instances be read before producing output.
 
254
   *
 
255
   * @param instance the input instance
 
256
   * @return true if the filtered instance may now be
 
257
   * collected with output().
 
258
   * @throws IllegalStateException if no input format has been defined.
 
259
   */
 
260
  public boolean input(Instance instance) {
 
261
 
 
262
    if (getInputFormat() == null) {
 
263
      throw new IllegalStateException("No input instance format defined");
 
264
    }
 
265
    if (m_NewBatch) {
 
266
      resetQueue();
 
267
      m_NewBatch = false;
 
268
    }
 
269
 
 
270
    Instances outputFormat = outputFormatPeek();
 
271
    double[] vals = new double[outputFormat.numAttributes()];
 
272
    boolean inRange = false;
 
273
    double lastVal = Instance.missingValue();
 
274
    int i, j;
 
275
    for(i = 0, j = 0; j < outputFormat.numAttributes(); i++) {
 
276
      if (m_DeltaCols.isInRange(i) && (i != instance.classIndex())) {
 
277
        if (inRange) {
 
278
          if (Instance.isMissingValue(lastVal) || instance.isMissing(i)) {
 
279
            vals[j++] = Instance.missingValue();
 
280
          } else {
 
281
            vals[j++] = instance.value(i) - lastVal;
 
282
          }
 
283
        } else {
 
284
          inRange = true;
 
285
        }
 
286
        lastVal = instance.value(i);
 
287
      } else {
 
288
        vals[j++] = instance.value(i);
 
289
      }
 
290
    }
 
291
 
 
292
    Instance inst = null;
 
293
    if (instance instanceof SparseInstance) {
 
294
      inst = new SparseInstance(instance.weight(), vals);
 
295
    } else {
 
296
      inst = new Instance(instance.weight(), vals);
 
297
    }
 
298
    inst.setDataset(getOutputFormat());
 
299
    copyValues(inst, false, instance.dataset(), getOutputFormat());
 
300
    inst.setDataset(getOutputFormat());
 
301
    push(inst);
 
302
    return true;
 
303
  }
 
304
 
 
305
 
 
306
  /**
 
307
   * Returns the tip text for this property
 
308
   *
 
309
   * @return tip text for this property suitable for
 
310
   * displaying in the explorer/experimenter gui
 
311
   */
 
312
  public String attributeIndicesTipText() {
 
313
    return "Specify range of attributes to act on."
 
314
      + " This is a comma separated list of attribute indices, with"
 
315
      + " \"first\" and \"last\" valid values. Specify an inclusive"
 
316
      + " range with \"-\". E.g: \"first-3,5,6-10,last\".";
 
317
  }
 
318
 
 
319
  /**
 
320
   * Get the current range selection
 
321
   *
 
322
   * @return a string containing a comma separated list of ranges
 
323
   */
 
324
  public String getAttributeIndices() {
 
325
 
 
326
    return m_DeltaCols.getRanges();
 
327
  }
 
328
 
 
329
  /**
 
330
   * Set which attributes are to be deleted (or kept if invert is true)
 
331
   *
 
332
   * @param rangeList a string representing the list of attributes. Since
 
333
   * the string will typically come from a user, attributes are indexed from
 
334
   * 1. <br>
 
335
   * eg: first-3,5,6-last
 
336
   * @throws Exception if an invalid range list is supplied
 
337
   */
 
338
  public void setAttributeIndices(String rangeList) throws Exception {
 
339
 
 
340
    m_DeltaCols.setRanges(rangeList);
 
341
  }
 
342
 
 
343
  /**
 
344
   * Set which attributes are to be deleted (or kept if invert is true)
 
345
   *
 
346
   * @param attributes an array containing indexes of attributes to select.
 
347
   * Since the array will typically come from a program, attributes are indexed
 
348
   * from 0.
 
349
   * @throws Exception if an invalid set of ranges is supplied
 
350
   */
 
351
  public void setAttributeIndicesArray(int [] attributes) throws Exception {
 
352
 
 
353
    setAttributeIndices(Range.indicesToRangeList(attributes));
 
354
  }
 
355
 
 
356
  /**
 
357
   * Main method for testing this class.
 
358
   *
 
359
   * @param argv should contain arguments to the filter: use -h for help
 
360
   */
 
361
  public static void main(String [] argv) {
 
362
    runFilter(new FirstOrder(), argv);
 
363
  }
 
364
}