source: branches/MetisMQI/src/main/java/weka/filters/unsupervised/attribute/PotentialClassIgnorer.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: 5.4 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 *    PotentialClassIgnorer.java
19 *    Copyright (C) 2003 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.filters.unsupervised.attribute;
24
25import weka.core.Instances;
26import weka.core.Option;
27import weka.core.OptionHandler;
28import weka.core.Utils;
29import weka.filters.Filter;
30
31import java.util.Enumeration;
32import java.util.Vector;
33
34/**
35 * This filter should be extended by other unsupervised attribute
36 * filters to allow processing of the class attribute if that's
37 * required. It the class is to be ignored it is essential that the
38 * extending filter does not change the position (i.e. index) of the
39 * attribute that is originally the class attribute !
40 *
41 * @author Eibe Frank (eibe@cs.waikato.ac.nz), Mark Hall (mhall@cs.waikato.ac.nz)
42 * @version $Revision: 1.7 $
43 */
44public abstract class PotentialClassIgnorer
45  extends Filter
46  implements OptionHandler {
47
48  /** for serialization */
49  private static final long serialVersionUID = 8625371119276845454L;
50
51  /** True if the class is to be unset */
52  protected boolean m_IgnoreClass = false;
53
54  /** Storing the class index */
55  protected int m_ClassIndex = -1;
56
57  /**
58   * Returns an enumeration describing the available options.
59   *
60   * @return            an enumeration of all the available options.
61   */
62  public Enumeration listOptions() {
63    Vector result = new Vector();
64     
65    result.addElement(new Option(
66        "\tUnsets the class index temporarily before the filter is\n"
67        + "\tapplied to the data.\n"
68        + "\t(default: no)",
69        "unset-class-temporarily", 1, "-unset-class-temporarily"));
70
71    return result.elements();
72  }
73
74  /**
75   * Parses a list of options for this object.
76   *
77   * @param options     the list of options as an array of strings
78   * @throws Exception  if an option is not supported
79   */
80  public void setOptions(String[] options) throws Exception {
81    setIgnoreClass(Utils.getFlag("unset-class-temporarily", options));
82  }
83
84  /**
85   * Gets the current settings of the filter.
86   *
87   * @return            an array of strings suitable for passing to setOptions
88   */
89  public String[] getOptions() {
90    Vector        result;
91
92    result = new Vector();
93
94    if (getIgnoreClass())
95      result.add("-unset-class-temporarily");
96
97    return (String[]) result.toArray(new String[result.size()]);
98  }
99
100  /**
101   * Sets the format of the input instances. If the filter is able to
102   * determine the output format before seeing any input instances, it
103   * does so here. This default implementation clears the output format
104   * and output queue, and the new batch flag is set. Overriders should
105   * call <code>super.setInputFormat(Instances)</code>
106   *
107   * @param instanceInfo an Instances object containing the input instance
108   * structure (any instances contained in the object are ignored - only the
109   * structure is required).
110   * @return true if the outputFormat may be collected immediately
111   * @throws Exception if the inputFormat can't be set successfully
112   */
113  public boolean setInputFormat(Instances instanceInfo) throws Exception {
114
115    boolean result = super.setInputFormat(instanceInfo);
116    if (m_IgnoreClass) {
117      m_ClassIndex = inputFormatPeek().classIndex();
118      inputFormatPeek().setClassIndex(-1);
119    }     
120    return result;
121  }
122
123  /**
124   * Gets the format of the output instances. This should only be called
125   * after input() or batchFinished() has returned true. The relation
126   * name of the output instances should be changed to reflect the
127   * action of the filter (eg: add the filter name and options).
128   *
129   * @return an Instances object containing the output instance
130   * structure only.
131   * @throws NullPointerException if no input structure has been
132   * defined (or the output format hasn't been determined yet)
133   */
134  public Instances getOutputFormat() {
135
136    if (m_IgnoreClass) {
137      outputFormatPeek().setClassIndex(m_ClassIndex);
138    }
139    return super.getOutputFormat();
140  }
141
142  /**
143   * Returns the tip text for this property
144   *
145   * @return            tip text for this property suitable for
146   *                    displaying in the explorer/experimenter gui
147   */
148  public String ignoreClassTipText() {
149    return "The class index will be unset temporarily before the filter is applied.";
150  }
151
152  /**
153   * Set the IgnoreClass value. Set this to true if the
154   * class index is to be unset before the filter is applied.
155   *
156   * @param newIgnoreClass The new IgnoreClass value.
157   */
158  public void setIgnoreClass(boolean newIgnoreClass) {
159    m_IgnoreClass = newIgnoreClass;
160  }
161 
162  /**
163   * Gets the IgnoreClass value. If this to true then the
164   * class index is to unset before the filter is applied.
165   *
166   * @return the current IgnoreClass value.
167   */
168  public boolean getIgnoreClass() {
169    return m_IgnoreClass;
170  }
171}
Note: See TracBrowser for help on using the repository browser.