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 | * RemoveRange.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | package weka.filters.unsupervised.instance; |
---|
25 | |
---|
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.Range; |
---|
32 | import weka.core.RevisionUtils; |
---|
33 | import weka.core.Utils; |
---|
34 | import weka.core.Capabilities.Capability; |
---|
35 | import weka.filters.Filter; |
---|
36 | import weka.filters.UnsupervisedFilter; |
---|
37 | |
---|
38 | import java.util.Enumeration; |
---|
39 | import java.util.Vector; |
---|
40 | |
---|
41 | /** |
---|
42 | <!-- globalinfo-start --> |
---|
43 | * A filter that removes a given range of instances of a dataset. |
---|
44 | * <p/> |
---|
45 | <!-- globalinfo-end --> |
---|
46 | * |
---|
47 | <!-- options-start --> |
---|
48 | * Valid options are: <p/> |
---|
49 | * |
---|
50 | * <pre> -R <inst1,inst2-inst4,...> |
---|
51 | * Specifies list of instances to select. First and last |
---|
52 | * are valid indexes. (required) |
---|
53 | * </pre> |
---|
54 | * |
---|
55 | * <pre> -V |
---|
56 | * Specifies if inverse of selection is to be output. |
---|
57 | * </pre> |
---|
58 | * |
---|
59 | <!-- options-end --> |
---|
60 | * |
---|
61 | * @author Eibe Frank (eibe@cs.waikato.ac.nz) |
---|
62 | * @version $Revision: 5499 $ |
---|
63 | */ |
---|
64 | public class RemoveRange |
---|
65 | extends Filter |
---|
66 | implements UnsupervisedFilter, OptionHandler { |
---|
67 | |
---|
68 | /** for serialization */ |
---|
69 | static final long serialVersionUID = -3064641215340828695L; |
---|
70 | |
---|
71 | /** Range of instances requested by the user. */ |
---|
72 | private Range m_Range = new Range("first-last"); |
---|
73 | |
---|
74 | /** |
---|
75 | * Returns an enumeration describing the available options. |
---|
76 | * |
---|
77 | * @return an enumeration of all the available options. |
---|
78 | */ |
---|
79 | public Enumeration listOptions() { |
---|
80 | |
---|
81 | Vector newVector = new Vector(6); |
---|
82 | |
---|
83 | newVector.addElement(new Option( |
---|
84 | "\tSpecifies list of instances to select. First and last\n" |
---|
85 | +"\tare valid indexes. (required)\n", |
---|
86 | "R", 1, "-R <inst1,inst2-inst4,...>")); |
---|
87 | |
---|
88 | newVector.addElement(new Option( |
---|
89 | "\tSpecifies if inverse of selection is to be output.\n", |
---|
90 | "V", 0, "-V")); |
---|
91 | |
---|
92 | return newVector.elements(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Parses a given list of options. <p/> |
---|
97 | * |
---|
98 | <!-- options-start --> |
---|
99 | * Valid options are: <p/> |
---|
100 | * |
---|
101 | * <pre> -R <inst1,inst2-inst4,...> |
---|
102 | * Specifies list of instances to select. First and last |
---|
103 | * are valid indexes. (required) |
---|
104 | * </pre> |
---|
105 | * |
---|
106 | * <pre> -V |
---|
107 | * Specifies if inverse of selection is to be output. |
---|
108 | * </pre> |
---|
109 | * |
---|
110 | <!-- options-end --> |
---|
111 | * |
---|
112 | * @param options the list of options as an array of string.s |
---|
113 | * @throws Exception if an option is not supported. |
---|
114 | */ |
---|
115 | public void setOptions(String[] options) throws Exception { |
---|
116 | |
---|
117 | String str = Utils.getOption('R', options); |
---|
118 | if (str.length() != 0) { |
---|
119 | setInstancesIndices(str); |
---|
120 | } else { |
---|
121 | setInstancesIndices("first-last"); |
---|
122 | } |
---|
123 | setInvertSelection(Utils.getFlag('V', options)); |
---|
124 | |
---|
125 | if (getInputFormat() != null) { |
---|
126 | setInputFormat(getInputFormat()); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * Gets the current settings of the filter. |
---|
132 | * |
---|
133 | * @return an array of strings suitable for passing to setOptions. |
---|
134 | */ |
---|
135 | public String [] getOptions() { |
---|
136 | |
---|
137 | String [] options = new String [8]; |
---|
138 | int current = 0; |
---|
139 | |
---|
140 | if (getInvertSelection()) { |
---|
141 | options[current++] = "-V"; |
---|
142 | } |
---|
143 | options[current++] = "-R"; options[current++] = getInstancesIndices(); |
---|
144 | while (current < options.length) { |
---|
145 | options[current++] = ""; |
---|
146 | } |
---|
147 | return options; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Returns a string describing this filter |
---|
152 | * |
---|
153 | * @return a description of the filter suitable for |
---|
154 | * displaying in the GUI. |
---|
155 | */ |
---|
156 | public String globalInfo() { |
---|
157 | |
---|
158 | return "A filter that removes a given range of instances of a dataset."; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Returns the tip text for this property |
---|
163 | * |
---|
164 | * @return tip text for this property suitable for |
---|
165 | * displaying in the explorer/experimenter gui |
---|
166 | */ |
---|
167 | public String instancesIndicesTipText() { |
---|
168 | |
---|
169 | return "The range of instances to select. First and last are valid indexes."; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Gets ranges of instances selected. |
---|
174 | * |
---|
175 | * @return a string containing a comma-separated list of ranges |
---|
176 | */ |
---|
177 | public String getInstancesIndices() { |
---|
178 | |
---|
179 | return m_Range.getRanges(); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Sets the ranges of instances to be selected. If provided string |
---|
184 | * is null, ranges won't be used for selecting instances. |
---|
185 | * |
---|
186 | * @param rangeList a string representing the list of instances. |
---|
187 | * eg: first-3,5,6-last |
---|
188 | * @throws IllegalArgumentException if an invalid range list is supplied |
---|
189 | */ |
---|
190 | public void setInstancesIndices(String rangeList) { |
---|
191 | |
---|
192 | m_Range.setRanges(rangeList); |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * Returns the tip text for this property |
---|
197 | * |
---|
198 | * @return tip text for this property suitable for |
---|
199 | * displaying in the explorer/experimenter gui |
---|
200 | */ |
---|
201 | public String invertSelectionTipText() { |
---|
202 | |
---|
203 | return "Whether to invert the selection."; |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * Gets if selection is to be inverted. |
---|
208 | * |
---|
209 | * @return true if the selection is to be inverted |
---|
210 | */ |
---|
211 | public boolean getInvertSelection() { |
---|
212 | |
---|
213 | return m_Range.getInvert(); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Sets if selection is to be inverted. |
---|
218 | * |
---|
219 | * @param inverse true if inversion is to be performed |
---|
220 | */ |
---|
221 | public void setInvertSelection(boolean inverse) { |
---|
222 | |
---|
223 | m_Range.setInvert(inverse); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * Returns the Capabilities of this filter. |
---|
228 | * |
---|
229 | * @return the capabilities of this object |
---|
230 | * @see Capabilities |
---|
231 | */ |
---|
232 | public Capabilities getCapabilities() { |
---|
233 | Capabilities result = super.getCapabilities(); |
---|
234 | result.disableAll(); |
---|
235 | |
---|
236 | // attributes |
---|
237 | result.enableAllAttributes(); |
---|
238 | result.enable(Capability.MISSING_VALUES); |
---|
239 | |
---|
240 | // class |
---|
241 | result.enableAllClasses(); |
---|
242 | result.enable(Capability.MISSING_CLASS_VALUES); |
---|
243 | result.enable(Capability.NO_CLASS); |
---|
244 | |
---|
245 | return result; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Sets the format of the input instances. |
---|
250 | * |
---|
251 | * @param instanceInfo an Instances object containing the input instance |
---|
252 | * structure (any instances contained in the object are ignored - only the |
---|
253 | * structure is required). |
---|
254 | * @return true because outputFormat can be collected immediately |
---|
255 | * @throws Exception if the input format can't be set successfully |
---|
256 | */ |
---|
257 | public boolean setInputFormat(Instances instanceInfo) throws Exception { |
---|
258 | |
---|
259 | super.setInputFormat(instanceInfo); |
---|
260 | setOutputFormat(instanceInfo); |
---|
261 | return true; |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Input an instance for filtering. Filter requires all |
---|
266 | * training instances be read before producing output. |
---|
267 | * |
---|
268 | * @param instance the input instance |
---|
269 | * @return true if the filtered instance may now be |
---|
270 | * collected with output(). |
---|
271 | * @throws IllegalStateException if no input structure has been defined |
---|
272 | */ |
---|
273 | public boolean input(Instance instance) { |
---|
274 | |
---|
275 | if (getInputFormat() == null) { |
---|
276 | throw new IllegalStateException("No input instance format defined"); |
---|
277 | } |
---|
278 | if (m_NewBatch) { |
---|
279 | resetQueue(); |
---|
280 | m_NewBatch = false; |
---|
281 | } |
---|
282 | if (isFirstBatchDone()) { |
---|
283 | push(instance); |
---|
284 | return true; |
---|
285 | } else { |
---|
286 | bufferInput(instance); |
---|
287 | return false; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * Signify that this batch of input to the filter is |
---|
293 | * finished. Output() may now be called to retrieve the filtered |
---|
294 | * instances. |
---|
295 | * |
---|
296 | * @return true if there are instances pending output |
---|
297 | * @throws IllegalStateException if no input structure has been defined |
---|
298 | */ |
---|
299 | public boolean batchFinished() { |
---|
300 | |
---|
301 | if (getInputFormat() == null) { |
---|
302 | throw new IllegalStateException("No input instance format defined"); |
---|
303 | } |
---|
304 | |
---|
305 | if (!isFirstBatchDone()) { |
---|
306 | // Push instances for output into output queue |
---|
307 | m_Range.setUpper(getInputFormat().numInstances() - 1); |
---|
308 | for (int i = 0; i < getInputFormat().numInstances(); i++) { |
---|
309 | if (!m_Range.isInRange(i)) { |
---|
310 | push(getInputFormat().instance(i)); |
---|
311 | } |
---|
312 | } |
---|
313 | } |
---|
314 | else { |
---|
315 | for (int i = 0; i < getInputFormat().numInstances(); i++) { |
---|
316 | push(getInputFormat().instance(i)); |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | flushInput(); |
---|
321 | |
---|
322 | m_NewBatch = true; |
---|
323 | m_FirstBatchDone = true; |
---|
324 | |
---|
325 | return (numPendingOutput() != 0); |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * Returns the revision string. |
---|
330 | * |
---|
331 | * @return the revision |
---|
332 | */ |
---|
333 | public String getRevision() { |
---|
334 | return RevisionUtils.extract("$Revision: 5499 $"); |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * Main method for testing this class. |
---|
339 | * |
---|
340 | * @param argv should contain arguments to the filter: use -h for help |
---|
341 | */ |
---|
342 | public static void main(String [] argv) { |
---|
343 | runFilter(new RemoveRange(), argv); |
---|
344 | } |
---|
345 | } |
---|