| 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 | * SimpleFilter.java |
|---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.filters; |
|---|
| 24 | |
|---|
| 25 | import weka.filters.Filter; |
|---|
| 26 | import weka.core.Instances; |
|---|
| 27 | import weka.core.Option; |
|---|
| 28 | import weka.core.OptionHandler; |
|---|
| 29 | import weka.core.Utils; |
|---|
| 30 | |
|---|
| 31 | import java.util.Enumeration; |
|---|
| 32 | import java.util.Vector; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * This filter contains common behavior of the SimpleBatchFilter and the |
|---|
| 36 | * SimpleStreamFilter. |
|---|
| 37 | * |
|---|
| 38 | * @author FracPete (fracpete at waikato dot ac dot nz) |
|---|
| 39 | * @version $Revision: 1.4 $ |
|---|
| 40 | * @see SimpleBatchFilter |
|---|
| 41 | * @see SimpleStreamFilter |
|---|
| 42 | */ |
|---|
| 43 | public abstract class SimpleFilter |
|---|
| 44 | extends Filter |
|---|
| 45 | implements OptionHandler { |
|---|
| 46 | |
|---|
| 47 | /** for serialization */ |
|---|
| 48 | private static final long serialVersionUID = 5702974949137433141L; |
|---|
| 49 | |
|---|
| 50 | /** Whether debugging is on */ |
|---|
| 51 | protected boolean m_Debug = false; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Returns a string describing this classifier. |
|---|
| 55 | * |
|---|
| 56 | * @return a description of the classifier suitable for |
|---|
| 57 | * displaying in the explorer/experimenter gui |
|---|
| 58 | */ |
|---|
| 59 | public abstract String globalInfo(); |
|---|
| 60 | |
|---|
| 61 | /** |
|---|
| 62 | * Returns an enumeration describing the available options. |
|---|
| 63 | * |
|---|
| 64 | * @return an enumeration of all the available options. |
|---|
| 65 | */ |
|---|
| 66 | public Enumeration listOptions() { |
|---|
| 67 | Vector result = new Vector(); |
|---|
| 68 | |
|---|
| 69 | result.addElement(new Option( |
|---|
| 70 | "\tTurns on output of debugging information.", |
|---|
| 71 | "D", 0, "-D")); |
|---|
| 72 | |
|---|
| 73 | return result.elements(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Parses a list of options for this object. |
|---|
| 78 | * Also resets the state of the filter (this reset doesn't affect the |
|---|
| 79 | * options). |
|---|
| 80 | * |
|---|
| 81 | * @param options the list of options as an array of strings |
|---|
| 82 | * @throws Exception if an option is not supported |
|---|
| 83 | * @see #reset() |
|---|
| 84 | */ |
|---|
| 85 | public void setOptions(String[] options) throws Exception { |
|---|
| 86 | reset(); |
|---|
| 87 | |
|---|
| 88 | setDebug(Utils.getFlag('D', options)); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Gets the current settings of the filter. |
|---|
| 93 | * |
|---|
| 94 | * @return an array of strings suitable for passing to setOptions |
|---|
| 95 | */ |
|---|
| 96 | public String[] getOptions() { |
|---|
| 97 | Vector result; |
|---|
| 98 | |
|---|
| 99 | result = new Vector(); |
|---|
| 100 | |
|---|
| 101 | if (getDebug()) |
|---|
| 102 | result.add("-D"); |
|---|
| 103 | |
|---|
| 104 | return (String[]) result.toArray(new String[result.size()]); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Sets the debugging mode |
|---|
| 109 | * |
|---|
| 110 | * @param value if true, debugging information is output |
|---|
| 111 | */ |
|---|
| 112 | public void setDebug(boolean value) { |
|---|
| 113 | m_Debug = value; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Returns the current debugging mode state. |
|---|
| 118 | * |
|---|
| 119 | * @return true if debugging mode is on |
|---|
| 120 | */ |
|---|
| 121 | public boolean getDebug() { |
|---|
| 122 | return m_Debug; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Returns the tip text for this property |
|---|
| 127 | * @return tip text for this property suitable for |
|---|
| 128 | * displaying in the explorer/experimenter gui |
|---|
| 129 | */ |
|---|
| 130 | public String debugTipText() { |
|---|
| 131 | return "Turns on output of debugging information."; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /** |
|---|
| 135 | * resets the filter, i.e., m_NewBatch to true and m_FirstBatchDone to |
|---|
| 136 | * false. |
|---|
| 137 | * |
|---|
| 138 | * @see #m_NewBatch |
|---|
| 139 | * @see #m_FirstBatchDone |
|---|
| 140 | */ |
|---|
| 141 | protected void reset() { |
|---|
| 142 | m_NewBatch = true; |
|---|
| 143 | m_FirstBatchDone = false; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * returns true if the output format is immediately available after the |
|---|
| 148 | * input format has been set and not only after all the data has been |
|---|
| 149 | * seen (see batchFinished()) |
|---|
| 150 | * |
|---|
| 151 | * @return true if the output format is immediately available |
|---|
| 152 | * @see #batchFinished() |
|---|
| 153 | * @see #setInputFormat(Instances) |
|---|
| 154 | */ |
|---|
| 155 | protected abstract boolean hasImmediateOutputFormat(); |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * Determines the output format based on the input format and returns |
|---|
| 159 | * this. In case the output format cannot be returned immediately, i.e., |
|---|
| 160 | * immediateOutputFormat() returns false, then this method will be called |
|---|
| 161 | * from batchFinished(). |
|---|
| 162 | * |
|---|
| 163 | * @param inputFormat the input format to base the output format on |
|---|
| 164 | * @return the output format |
|---|
| 165 | * @throws Exception in case the determination goes wrong |
|---|
| 166 | * @see #hasImmediateOutputFormat() |
|---|
| 167 | * @see #batchFinished() |
|---|
| 168 | */ |
|---|
| 169 | protected abstract Instances determineOutputFormat(Instances inputFormat) throws Exception; |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Processes the given data (may change the provided dataset) and returns |
|---|
| 173 | * the modified version. This method is called in batchFinished(). |
|---|
| 174 | * |
|---|
| 175 | * @param instances the data to process |
|---|
| 176 | * @return the modified data |
|---|
| 177 | * @throws Exception in case the processing goes wrong |
|---|
| 178 | * @see #batchFinished() |
|---|
| 179 | */ |
|---|
| 180 | protected abstract Instances process(Instances instances) throws Exception; |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * Sets the format of the input instances. |
|---|
| 184 | * Also resets the state of the filter (this reset doesn't affect the |
|---|
| 185 | * options). |
|---|
| 186 | * |
|---|
| 187 | * @param instanceInfo an Instances object containing the input instance |
|---|
| 188 | * structure (any instances contained in the object |
|---|
| 189 | * are ignored - only the structure is required). |
|---|
| 190 | * @return true if the outputFormat may be collected |
|---|
| 191 | * immediately |
|---|
| 192 | * @see #reset() |
|---|
| 193 | */ |
|---|
| 194 | public boolean setInputFormat(Instances instanceInfo) throws Exception { |
|---|
| 195 | super.setInputFormat(instanceInfo); |
|---|
| 196 | |
|---|
| 197 | reset(); |
|---|
| 198 | |
|---|
| 199 | if (hasImmediateOutputFormat()) |
|---|
| 200 | setOutputFormat(determineOutputFormat(instanceInfo)); |
|---|
| 201 | |
|---|
| 202 | return hasImmediateOutputFormat(); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|