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.
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.
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.
19
* Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
23
package weka.filters.unsupervised.attribute;
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;
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;
39
import java.util.Enumeration;
40
import java.util.Vector;
43
<!-- globalinfo-start -->
44
* Removes attributes of a given type.
46
<!-- globalinfo-end -->
48
<!-- options-start -->
49
* Valid options are: <p/>
51
* <pre> -T <nominal|numeric|string|date|relational>
52
* Attribute type to delete. Valid options are "nominal",
53
* "numeric", "string", "date" and "relational".
54
* (default "string")</pre>
57
* Invert matching sense (i.e. only keep specified columns)</pre>
61
* @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
62
* @version $Revision: 1.7 $
64
public class RemoveType
66
implements UnsupervisedFilter, StreamableFilter, OptionHandler {
68
/** for serialization */
69
static final long serialVersionUID = -3563999462782486279L;
71
/** The attribute filter used to do the filtering */
72
protected Remove m_attributeFilter = new Remove();
74
/** The type of attribute to delete */
75
protected int m_attTypeToDelete = Attribute.STRING;
77
/** Whether to invert selection */
78
protected boolean m_invert = false;
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")
90
* Returns the Capabilities of this filter.
92
* @return the capabilities of this object
95
public Capabilities getCapabilities() {
96
Capabilities result = super.getCapabilities();
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);
107
result.enableAllClasses();
108
result.enable(Capability.MISSING_CLASS_VALUES);
109
result.enable(Capability.NO_CLASS);
115
* Sets the format of the input instances.
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
123
public boolean setInputFormat(Instances instanceInfo) throws Exception {
125
super.setInputFormat(instanceInfo);
127
int[] attsToDelete = new int[instanceInfo.numAttributes()];
129
for (int i=0; i<instanceInfo.numAttributes(); i++) {
130
if ((i == instanceInfo.classIndex() && !m_invert)) {
131
continue; // skip class
133
if (instanceInfo.attribute(i).type() == m_attTypeToDelete)
134
attsToDelete[numToDelete++] = i;
137
int[] finalAttsToDelete = new int[numToDelete];
138
System.arraycopy(attsToDelete, 0, finalAttsToDelete, 0, numToDelete);
140
m_attributeFilter.setAttributeIndicesArray(finalAttsToDelete);
141
m_attributeFilter.setInvertSelection(m_invert);
143
boolean result = m_attributeFilter.setInputFormat(instanceInfo);
144
Instances afOutputFormat = m_attributeFilter.getOutputFormat();
146
// restore old relation name to hide attribute filter stamp
147
afOutputFormat.setRelationName(instanceInfo.relationName());
149
setOutputFormat(afOutputFormat);
154
* Input an instance for filtering.
156
* @param instance the input instance
157
* @return true if the filtered instance may now be
158
* collected with output().
160
public boolean input(Instance instance) {
162
return m_attributeFilter.input(instance);
166
* Signify that this batch of input to the filter is finished.
168
* @return true if there are instances pending output
169
* @throws Exception if something goes wrong
171
public boolean batchFinished() throws Exception {
173
return m_attributeFilter.batchFinished();
177
* Output an instance after filtering and remove from the output queue.
179
* @return the instance that has most recently been filtered (or null if
180
* the queue is empty).
182
public Instance output() {
184
return m_attributeFilter.output();
188
* Output an instance after filtering but do not remove from the
191
* @return the instance that has most recently been filtered (or null if
192
* the queue is empty).
194
public Instance outputPeek() {
196
return m_attributeFilter.outputPeek();
200
* Returns the number of instances pending output
202
* @return the number of instances pending output
204
public int numPendingOutput() {
206
return m_attributeFilter.numPendingOutput();
210
* Returns whether the output format is ready to be collected
212
* @return true if the output format is set
214
public boolean isOutputFormatDefined() {
216
return m_attributeFilter.isOutputFormatDefined();
220
* Returns an enumeration describing the available options.
222
* @return an enumeration of all the available options.
224
public Enumeration listOptions() {
226
Vector newVector = new Vector(2);
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>"));
234
newVector.addElement(new Option(
235
"\tInvert matching sense (i.e. only keep specified columns)",
238
return newVector.elements();
242
* Parses a given list of options. <p/>
244
<!-- options-start -->
245
* Valid options are: <p/>
247
* <pre> -T <nominal|numeric|string|date|relational>
248
* Attribute type to delete. Valid options are "nominal",
249
* "numeric", "string", "date" and "relational".
250
* (default "string")</pre>
253
* Invert matching sense (i.e. only keep specified columns)</pre>
257
* @param options the list of options as an array of strings
258
* @throws Exception if an option is not supported
260
public void setOptions(String[] options) throws Exception {
262
String tString = Utils.getOption('T', options);
263
if (tString.length() != 0) setAttributeTypeString(tString);
264
setInvertSelection(Utils.getFlag('V', options));
266
if (getInputFormat() != null) {
267
setInputFormat(getInputFormat());
272
* Gets the current settings of the filter.
274
* @return an array of strings suitable for passing to setOptions
276
public String [] getOptions() {
278
String [] options = new String [3];
281
if (getInvertSelection()) {
282
options[current++] = "-V";
284
options[current++] = "-T";
285
options[current++] = getAttributeTypeString();
287
while (current < options.length) {
288
options[current++] = "";
294
* Returns a string describing this filter
296
* @return a description of the filter suitable for
297
* displaying in the explorer/experimenter gui
299
public String globalInfo() {
301
return "Removes attributes of a given type.";
305
* Returns the tip text for this property
307
* @return tip text for this property suitable for
308
* displaying in the explorer/experimenter gui
310
public String attributeTypeTipText() {
312
return "The type of attribute to remove.";
316
* Sets the attribute type to be deleted by the filter.
318
* @param type a TAGS_ATTRIBUTETYPE of the new type the filter should delete
320
public void setAttributeType(SelectedTag type) {
322
if (type.getTags() == TAGS_ATTRIBUTETYPE) {
323
m_attTypeToDelete = type.getSelectedTag().getID();
328
* Gets the attribute type to be deleted by the filter.
330
* @return the attribute type as a selected tag TAGS_ATTRIBUTETYPE
332
public SelectedTag getAttributeType() {
334
return new SelectedTag(m_attTypeToDelete, TAGS_ATTRIBUTETYPE);
338
* Returns the tip text for this property
340
* @return tip text for this property suitable for
341
* displaying in the explorer/experimenter gui
343
public String invertSelectionTipText() {
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.";
351
* Get whether the supplied columns are to be removed or kept
353
* @return true if the supplied columns will be kept
355
public boolean getInvertSelection() {
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.
365
* @param invert the new invert setting
367
public void setInvertSelection(boolean invert) {
373
* Gets the attribute type to be deleted by the filter as a string.
375
* @return the attribute type as a String
377
protected String getAttributeTypeString() {
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";
388
* Sets the attribute type to be deleted by the filter.
390
* @param typeString a String representing the new type the filter should delete
392
protected void setAttributeTypeString(String typeString) {
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;
403
* Main method for testing this class.
405
* @param argv should contain arguments to the filter: use -h for help
407
public static void main(String [] argv) {
408
runFilter(new RemoveType(), argv);