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

« back to all changes in this revision

Viewing changes to weka/filters/unsupervised/attribute/RemoveType.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
 *    RemoveType.java
 
19
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.filters.unsupervised.attribute;
 
24
 
 
25
import weka.core.Attribute;
 
26
import weka.core.Capabilities;
 
27
import weka.core.Instance;
 
28
import weka.core.Instances;
 
29
import weka.core.Option;
 
30
import weka.core.OptionHandler;
 
31
import weka.core.SelectedTag;
 
32
import weka.core.Tag;
 
33
import weka.core.Utils;
 
34
import weka.core.Capabilities.Capability;
 
35
import weka.filters.Filter;
 
36
import weka.filters.StreamableFilter;
 
37
import weka.filters.UnsupervisedFilter;
 
38
 
 
39
import java.util.Enumeration;
 
40
import java.util.Vector;
 
41
 
 
42
/** 
 
43
 <!-- globalinfo-start -->
 
44
 * Removes attributes of a given type.
 
45
 * <p/>
 
46
 <!-- globalinfo-end -->
 
47
 * 
 
48
 <!-- options-start -->
 
49
 * Valid options are: <p/>
 
50
 * 
 
51
 * <pre> -T &lt;nominal|numeric|string|date|relational&gt;
 
52
 *  Attribute type to delete. Valid options are "nominal", 
 
53
 *  "numeric", "string", "date" and "relational".
 
54
 *  (default "string")</pre>
 
55
 * 
 
56
 * <pre> -V
 
57
 *  Invert matching sense (i.e. only keep specified columns)</pre>
 
58
 * 
 
59
 <!-- options-end -->
 
60
 *
 
61
 * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
 
62
 * @version $Revision: 1.7 $
 
63
 */
 
64
public class RemoveType 
 
65
  extends Filter
 
66
  implements UnsupervisedFilter, StreamableFilter, OptionHandler {
 
67
 
 
68
  /** for serialization */
 
69
  static final long serialVersionUID = -3563999462782486279L;
 
70
  
 
71
  /** The attribute filter used to do the filtering */
 
72
  protected Remove m_attributeFilter = new Remove();
 
73
 
 
74
  /** The type of attribute to delete */
 
75
  protected int m_attTypeToDelete = Attribute.STRING;
 
76
 
 
77
  /** Whether to invert selection */
 
78
  protected boolean m_invert = false;
 
79
  
 
80
  /** Tag allowing selection of attribute type to delete */
 
81
  public static final Tag [] TAGS_ATTRIBUTETYPE = {
 
82
    new Tag(Attribute.NOMINAL, "Delete nominal attributes"),
 
83
    new Tag(Attribute.NUMERIC, "Delete numeric attributes"),
 
84
    new Tag(Attribute.STRING, "Delete string attributes"),
 
85
    new Tag(Attribute.DATE, "Delete date attributes"),
 
86
    new Tag(Attribute.RELATIONAL, "Delete relational attributes")
 
87
  };
 
88
 
 
89
  /** 
 
90
   * Returns the Capabilities of this filter.
 
91
   *
 
92
   * @return            the capabilities of this object
 
93
   * @see               Capabilities
 
94
   */
 
95
  public Capabilities getCapabilities() {
 
96
    Capabilities result = super.getCapabilities();
 
97
 
 
98
    // attributes
 
99
    result.enable(Capability.NOMINAL_ATTRIBUTES);
 
100
    result.enable(Capability.NUMERIC_ATTRIBUTES);
 
101
    result.enable(Capability.DATE_ATTRIBUTES);
 
102
    result.enable(Capability.STRING_ATTRIBUTES);
 
103
    result.enable(Capability.RELATIONAL_ATTRIBUTES);
 
104
    result.enable(Capability.MISSING_VALUES);
 
105
    
 
106
    // class
 
107
    result.enableAllClasses();
 
108
    result.enable(Capability.MISSING_CLASS_VALUES);
 
109
    result.enable(Capability.NO_CLASS);
 
110
    
 
111
    return result;
 
112
  }
 
113
 
 
114
  /**
 
115
   * Sets the format of the input instances.
 
116
   *
 
117
   * @param instanceInfo an Instances object containing the input instance
 
118
   * structure (any instances contained in the object are ignored - only the
 
119
   * structure is required).
 
120
   * @return true if the outputFormat may be collected immediately
 
121
   * @throws Exception if the inputFormat can't be set successfully 
 
122
   */ 
 
123
  public boolean setInputFormat(Instances instanceInfo) throws Exception {
 
124
    
 
125
    super.setInputFormat(instanceInfo);
 
126
 
 
127
    int[] attsToDelete = new int[instanceInfo.numAttributes()];
 
128
    int numToDelete = 0;
 
129
    for (int i=0; i<instanceInfo.numAttributes(); i++) {
 
130
      if ((i == instanceInfo.classIndex() && !m_invert)) {
 
131
        continue; // skip class
 
132
      }
 
133
      if (instanceInfo.attribute(i).type() == m_attTypeToDelete)
 
134
        attsToDelete[numToDelete++] = i;
 
135
    }
 
136
 
 
137
    int[] finalAttsToDelete = new int[numToDelete];
 
138
    System.arraycopy(attsToDelete, 0, finalAttsToDelete, 0, numToDelete);
 
139
    
 
140
    m_attributeFilter.setAttributeIndicesArray(finalAttsToDelete);
 
141
    m_attributeFilter.setInvertSelection(m_invert);
 
142
    
 
143
    boolean result = m_attributeFilter.setInputFormat(instanceInfo);
 
144
    Instances afOutputFormat = m_attributeFilter.getOutputFormat();
 
145
    
 
146
    // restore old relation name to hide attribute filter stamp
 
147
    afOutputFormat.setRelationName(instanceInfo.relationName());
 
148
 
 
149
    setOutputFormat(afOutputFormat);
 
150
    return result;
 
151
  }
 
152
 
 
153
  /**
 
154
   * Input an instance for filtering.
 
155
   *
 
156
   * @param instance the input instance
 
157
   * @return true if the filtered instance may now be
 
158
   * collected with output().
 
159
   */
 
160
  public boolean input(Instance instance) {
 
161
    
 
162
    return m_attributeFilter.input(instance);
 
163
  }
 
164
 
 
165
  /**
 
166
   * Signify that this batch of input to the filter is finished.
 
167
   *
 
168
   * @return true if there are instances pending output
 
169
   * @throws Exception if something goes wrong
 
170
   */  
 
171
  public boolean batchFinished() throws Exception {
 
172
 
 
173
    return m_attributeFilter.batchFinished();
 
174
  }
 
175
 
 
176
  /**
 
177
   * Output an instance after filtering and remove from the output queue.
 
178
   *
 
179
   * @return the instance that has most recently been filtered (or null if
 
180
   * the queue is empty).
 
181
   */
 
182
  public Instance output() {
 
183
 
 
184
    return m_attributeFilter.output();
 
185
  }
 
186
 
 
187
  /**
 
188
   * Output an instance after filtering but do not remove from the
 
189
   * output queue.
 
190
   *
 
191
   * @return the instance that has most recently been filtered (or null if
 
192
   * the queue is empty).
 
193
   */
 
194
  public Instance outputPeek() {
 
195
 
 
196
    return m_attributeFilter.outputPeek();
 
197
  }
 
198
 
 
199
  /**
 
200
   * Returns the number of instances pending output
 
201
   *
 
202
   * @return the number of instances  pending output
 
203
   */  
 
204
  public int numPendingOutput() {
 
205
  
 
206
    return m_attributeFilter.numPendingOutput();
 
207
  }
 
208
  
 
209
  /**
 
210
   * Returns whether the output format is ready to be collected
 
211
   *
 
212
   * @return true if the output format is set
 
213
   */  
 
214
  public boolean isOutputFormatDefined() {
 
215
 
 
216
    return m_attributeFilter.isOutputFormatDefined();
 
217
  }
 
218
 
 
219
  /**
 
220
   * Returns an enumeration describing the available options.
 
221
   *
 
222
   * @return an enumeration of all the available options.
 
223
   */
 
224
  public Enumeration listOptions() {
 
225
 
 
226
    Vector newVector = new Vector(2);
 
227
 
 
228
    newVector.addElement(new Option(
 
229
        "\tAttribute type to delete. Valid options are \"nominal\", \n"
 
230
        + "\t\"numeric\", \"string\", \"date\" and \"relational\".\n"
 
231
        + "\t(default \"string\")",
 
232
        "T", 1, "-T <nominal|numeric|string|date|relational>"));
 
233
 
 
234
    newVector.addElement(new Option(
 
235
        "\tInvert matching sense (i.e. only keep specified columns)",
 
236
        "V", 0, "-V"));
 
237
 
 
238
    return newVector.elements();
 
239
  }
 
240
 
 
241
  /**
 
242
   * Parses a given list of options. <p/>
 
243
   * 
 
244
   <!-- options-start -->
 
245
   * Valid options are: <p/>
 
246
   * 
 
247
   * <pre> -T &lt;nominal|numeric|string|date|relational&gt;
 
248
   *  Attribute type to delete. Valid options are "nominal", 
 
249
   *  "numeric", "string", "date" and "relational".
 
250
   *  (default "string")</pre>
 
251
   * 
 
252
   * <pre> -V
 
253
   *  Invert matching sense (i.e. only keep specified columns)</pre>
 
254
   * 
 
255
   <!-- options-end -->
 
256
   *
 
257
   * @param options the list of options as an array of strings
 
258
   * @throws Exception if an option is not supported
 
259
   */
 
260
  public void setOptions(String[] options) throws Exception {
 
261
    
 
262
    String tString = Utils.getOption('T', options);
 
263
    if (tString.length() != 0) setAttributeTypeString(tString);
 
264
    setInvertSelection(Utils.getFlag('V', options));
 
265
 
 
266
    if (getInputFormat() != null) {
 
267
      setInputFormat(getInputFormat());
 
268
    }
 
269
  }
 
270
 
 
271
  /**
 
272
   * Gets the current settings of the filter.
 
273
   *
 
274
   * @return an array of strings suitable for passing to setOptions
 
275
   */
 
276
  public String [] getOptions() {
 
277
 
 
278
    String [] options = new String [3];
 
279
    int current = 0;
 
280
 
 
281
    if (getInvertSelection()) {
 
282
      options[current++] = "-V";
 
283
    }
 
284
    options[current++] = "-T";
 
285
    options[current++] = getAttributeTypeString();
 
286
    
 
287
    while (current < options.length) {
 
288
      options[current++] = "";
 
289
    }
 
290
    return options;
 
291
  }
 
292
 
 
293
  /**
 
294
   * Returns a string describing this filter
 
295
   *
 
296
   * @return a description of the filter suitable for
 
297
   * displaying in the explorer/experimenter gui
 
298
   */
 
299
  public String globalInfo() {
 
300
 
 
301
    return "Removes attributes of a given type.";
 
302
  }
 
303
 
 
304
  /**
 
305
   * Returns the tip text for this property
 
306
   *
 
307
   * @return tip text for this property suitable for
 
308
   * displaying in the explorer/experimenter gui
 
309
   */
 
310
  public String attributeTypeTipText() {
 
311
 
 
312
    return "The type of attribute to remove.";
 
313
  }
 
314
 
 
315
  /**
 
316
   * Sets the attribute type to be deleted by the filter.
 
317
   *
 
318
   * @param type a TAGS_ATTRIBUTETYPE of the new type the filter should delete
 
319
   */
 
320
  public void setAttributeType(SelectedTag type) {
 
321
    
 
322
    if (type.getTags() == TAGS_ATTRIBUTETYPE) {
 
323
      m_attTypeToDelete = type.getSelectedTag().getID();
 
324
    }
 
325
  }
 
326
 
 
327
  /**
 
328
   * Gets the attribute type to be deleted by the filter.
 
329
   *
 
330
   * @return the attribute type as a selected tag TAGS_ATTRIBUTETYPE
 
331
   */
 
332
  public SelectedTag getAttributeType() {
 
333
 
 
334
    return new SelectedTag(m_attTypeToDelete, TAGS_ATTRIBUTETYPE);
 
335
  }
 
336
 
 
337
  /**
 
338
   * Returns the tip text for this property
 
339
   *
 
340
   * @return tip text for this property suitable for
 
341
   * displaying in the explorer/experimenter gui
 
342
   */
 
343
  public String invertSelectionTipText() {
 
344
 
 
345
    return "Determines whether action is to select or delete."
 
346
      + " If set to true, only the specified attributes will be kept;"
 
347
      + " If set to false, specified attributes will be deleted.";
 
348
  }
 
349
 
 
350
  /**
 
351
   * Get whether the supplied columns are to be removed or kept
 
352
   *
 
353
   * @return true if the supplied columns will be kept
 
354
   */
 
355
  public boolean getInvertSelection() {
 
356
 
 
357
    return m_invert;
 
358
  }
 
359
 
 
360
  /**
 
361
   * Set whether selected columns should be removed or kept. If true the 
 
362
   * selected columns are kept and unselected columns are deleted. If false
 
363
   * selected columns are deleted and unselected columns are kept.
 
364
   *
 
365
   * @param invert the new invert setting
 
366
   */
 
367
  public void setInvertSelection(boolean invert) {
 
368
 
 
369
    m_invert = invert;
 
370
  }
 
371
 
 
372
  /**
 
373
   * Gets the attribute type to be deleted by the filter as a string.
 
374
   *
 
375
   * @return the attribute type as a String
 
376
   */
 
377
  protected String getAttributeTypeString() {
 
378
 
 
379
    if (m_attTypeToDelete == Attribute.NOMINAL) return "nominal";
 
380
    else if (m_attTypeToDelete == Attribute.NUMERIC) return "numeric";
 
381
    else if (m_attTypeToDelete == Attribute.STRING) return "string";
 
382
    else if (m_attTypeToDelete == Attribute.DATE) return "date";
 
383
    else if (m_attTypeToDelete == Attribute.RELATIONAL) return "relational";
 
384
    else return "unknown";
 
385
  }
 
386
 
 
387
  /**
 
388
   * Sets the attribute type to be deleted by the filter.
 
389
   *
 
390
   * @param typeString a String representing the new type the filter should delete
 
391
   */
 
392
  protected void setAttributeTypeString(String typeString) {
 
393
 
 
394
    typeString = typeString.toLowerCase();
 
395
    if (typeString.equals("nominal")) m_attTypeToDelete = Attribute.NOMINAL;
 
396
    else if (typeString.equals("numeric")) m_attTypeToDelete = Attribute.NUMERIC;
 
397
    else if (typeString.equals("string")) m_attTypeToDelete = Attribute.STRING;
 
398
    else if (typeString.equals("date")) m_attTypeToDelete = Attribute.DATE;
 
399
    else if (typeString.equals("relational")) m_attTypeToDelete = Attribute.RELATIONAL;
 
400
  }
 
401
 
 
402
  /**
 
403
   * Main method for testing this class.
 
404
   *
 
405
   * @param argv should contain arguments to the filter: use -h for help
 
406
   */
 
407
  public static void main(String [] argv) {
 
408
    runFilter(new RemoveType(), argv);
 
409
  }
 
410
}