source: branches/MetisMQI/src/main/java/weka/filters/unsupervised/attribute/NominalToString.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: 8.8 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 * NominalToString.java
19 * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.filters.unsupervised.attribute;
24
25import weka.core.Attribute;
26import weka.core.Capabilities;
27import weka.core.FastVector;
28import weka.core.Instance; 
29import weka.core.DenseInstance;
30import weka.core.Instances;
31import weka.core.Option;
32import weka.core.OptionHandler;
33import weka.core.RevisionUtils;
34import weka.core.SingleIndex;
35import weka.core.Range;
36import weka.core.UnsupportedAttributeTypeException;
37import weka.core.Utils;
38import weka.core.Capabilities.Capability;
39import weka.filters.Filter;
40import weka.filters.UnsupervisedFilter;
41
42import java.util.Enumeration;
43import java.util.Vector;
44
45/**
46 <!-- globalinfo-start -->
47 * Converts a nominal attribute (i.e. set number of values) to string (i.e. unspecified number of values).
48 * <p/>
49 <!-- globalinfo-end -->
50 *
51 <!-- options-start -->
52 * Valid options are: <p/>
53 *
54 * <pre> -C &lt;col&gt;
55 *  Sets the range of attributes to convert (default last).</pre>
56 *
57 <!-- options-end -->
58 *
59 * @author FracPete (fracpete at waikato dot ac dot nz)
60 * @version $Revision: 5987 $
61 */
62public class NominalToString
63  extends Filter
64  implements UnsupervisedFilter, OptionHandler {
65
66  /** for serialization */
67  static final long serialVersionUID = 8655492378380068939L;
68 
69  /** The attribute's index setting. */
70  private Range m_AttIndex = new Range("last");
71
72  /**
73   * Returns a string describing this filter
74   *
75   * @return            a description of the filter suitable for
76   *                    displaying in the explorer/experimenter gui
77   */
78  public String globalInfo() {
79    return 
80        "Converts a nominal attribute (i.e. set number of values) to string "
81      + "(i.e. unspecified number of values).";
82  }
83
84  /**
85   * Returns the Capabilities of this filter.
86   *
87   * @return            the capabilities of this object
88   * @see               Capabilities
89   */
90  public Capabilities getCapabilities() {
91    Capabilities result = super.getCapabilities();
92    result.disableAll();
93
94    // attributes
95    result.enableAllAttributes();
96    result.enable(Capability.MISSING_VALUES);
97   
98    // class
99    result.enableAllClasses();
100    result.enable(Capability.MISSING_CLASS_VALUES);
101    result.enable(Capability.NO_CLASS);
102   
103    return result;
104  }
105
106  /**
107   * Sets the format of the input instances.
108   *
109   * @param instanceInfo        an Instances object containing the input
110   *                            instance structure (any instances contained
111   *                            in the object are ignored - only the
112   *                            structure is required).
113   * @return                    true if the outputFormat may be collected
114   *                            immediately.
115   * @throws UnsupportedAttributeTypeException  if the selected attribute
116   *                                            a string attribute.
117   * @throws Exception          if the input format can't be set
118   *                            successfully.
119   */
120  public boolean setInputFormat(Instances instanceInfo) throws Exception {
121    super.setInputFormat(instanceInfo);
122
123    m_AttIndex.setUpper(instanceInfo.numAttributes() - 1);
124   
125    /*    if (!instanceInfo.attribute(m_AttIndex.getIndex()).isNominal())
126      throw new UnsupportedAttributeTypeException("Chosen attribute is not of "
127      + "type nominal."); */
128
129    return false;
130  }
131
132  /**
133   * Input an instance for filtering. The instance is processed
134   * and made available for output immediately.
135   *
136   * @param instance            the input instance.
137   * @return                    true if the filtered instance may now be
138   *                            collected with output().
139   * @throws IllegalStateException      if no input structure has been defined.
140   */
141  public boolean input(Instance instance) {
142    if (getInputFormat() == null)
143      throw new IllegalStateException("No input instance format defined");
144
145    if (m_NewBatch) {
146      resetQueue();
147      m_NewBatch = false;
148    }
149
150    if (isOutputFormatDefined()) {
151      Instance newInstance = (Instance) instance.copy();
152      push(newInstance);
153      return true;
154    }
155
156    bufferInput(instance);
157    return false;
158  }
159
160
161  /**
162   * Signifies that this batch of input to the filter is finished. If the
163   * filter requires all instances prior to filtering, output() may now
164   * be called to retrieve the filtered instances.
165   *
166   * @return            true if there are instances pending output.
167   * @throws IllegalStateException      if no input structure has been defined.
168   */
169  public boolean batchFinished() {
170    if (getInputFormat() == null)
171      throw new IllegalStateException("No input instance format defined");
172
173    if (!isOutputFormatDefined()) {
174      setOutputFormat();
175
176      // Convert pending input instances
177      for(int i = 0; i < getInputFormat().numInstances(); i++)
178        push((Instance) getInputFormat().instance(i).copy());
179    } 
180
181    flushInput();
182    m_NewBatch = true;
183
184    return (numPendingOutput() != 0);
185  }
186
187
188  /**
189   * Returns an enumeration describing the available options.
190   *
191   * @return            an enumeration of all the available options.
192   */
193  public Enumeration listOptions() {
194    Vector result = new Vector();
195
196    result.addElement(new Option(
197        "\tSets the range of attributes to convert (default last).",
198        "C", 1, "-C <col>"));
199
200    return result.elements();
201  }
202
203
204  /**
205   * Parses a given list of options. <p/>
206   *
207   <!-- options-start -->
208   * Valid options are: <p/>
209   *
210   * <pre> -C &lt;col&gt;
211   *  Sets the range of attributes to convert (default last).</pre>
212   *
213   <!-- options-end -->
214   *
215   * @param options     the list of options as an array of strings
216   * @throws Exception  if an option is not supported
217   */
218  public void setOptions(String[] options) throws Exception {
219    String      tmpStr;
220   
221    tmpStr = Utils.getOption('C', options);
222    if (tmpStr.length() != 0)
223      setAttributeIndexes(tmpStr);
224    else
225      setAttributeIndexes("last");
226       
227    if (getInputFormat() != null)
228      setInputFormat(getInputFormat());
229  }
230
231  /**
232   * Gets the current settings of the filter.
233   *
234   * @return            an array of strings suitable for passing to setOptions
235   */
236  public String[] getOptions() {
237    Vector    result;
238
239    result = new Vector();
240
241    result.add("-C");
242    result.add("" + (getAttributeIndexes()));
243
244    return (String[]) result.toArray(new String[result.size()]);         
245  }
246
247  /**
248   * Returns the tip text for this property
249   *
250   * @return            tip text for this property suitable for
251   *                    displaying in the explorer/experimenter gui
252   */
253  public String attributeIndexesTipText() {
254    return "Sets a range attributes to process. Any non-nominal "
255      + "attributes in the range are left untouched (\"first\" and \"last\" are valid values)";
256  }
257
258  /**
259   * Get the index of the attribute used.
260   *
261   * @return            the index of the attribute
262   */
263  public String getAttributeIndexes() {
264    //    return m_AttIndex.getSingleIndex();
265    return m_AttIndex.getRanges();
266  }
267
268  /**
269   * Sets index of the attribute used.
270   *
271   * @param attIndex    the index of the attribute
272   */
273  public void setAttributeIndexes(String attIndex) {
274    //    m_AttIndex.setSingleIndex(attIndex);
275    m_AttIndex.setRanges(attIndex);
276  }
277
278  /**
279   * Set the output format. Takes the current average class values
280   * and m_InputFormat and calls setOutputFormat(Instances)
281   * appropriately.
282   */
283  private void setOutputFormat() {
284    Instances   newData;
285    FastVector  newAtts;
286     
287    // Compute new attributes
288    newAtts = new FastVector(getInputFormat().numAttributes());
289    for (int j = 0; j < getInputFormat().numAttributes(); j++) {
290      Attribute att = getInputFormat().attribute(j);
291
292      if (!att.isNominal() || !m_AttIndex.isInRange(j))
293        newAtts.addElement(att); 
294      else
295        newAtts.addElement(new Attribute(att.name(), (FastVector) null));
296    }
297     
298    // Construct new header
299    newData = new Instances(getInputFormat().relationName(), newAtts, 0);
300    newData.setClassIndex(getInputFormat().classIndex());
301
302    setOutputFormat(newData);
303  }
304 
305  /**
306   * Returns the revision string.
307   *
308   * @return            the revision
309   */
310  public String getRevision() {
311    return RevisionUtils.extract("$Revision: 5987 $");
312  }
313 
314  /**
315   * Main method for testing this class.
316   *
317   * @param args        should contain arguments to the filter: use -h for help
318   */
319  public static void main(String [] args) {
320    runFilter(new NominalToString(), args);
321  }
322}
323
Note: See TracBrowser for help on using the repository browser.