~ubuntu-branches/ubuntu/vivid/weka/vivid

« back to all changes in this revision

Viewing changes to src/main/java/weka/filters/unsupervised/instance/SubsetByExpression.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2011-11-24 12:35:17 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20111124123517-kn23dn5u8pi4liq0
Tags: 3.6.6-1
* Team upload.
* New upstream version (Closes: #649734)
* Update rules and build-deps for default-jdk and default-jre.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
package weka.filters.unsupervised.instance;
23
23
 
24
24
import weka.core.Capabilities;
 
25
import weka.core.Instance;
25
26
import weka.core.Instances;
26
27
import weka.core.Option;
27
28
import weka.core.RevisionUtils;
108
109
 *   not ismissing(ATT3)<br/>
109
110
 * <p/>
110
111
 <!-- globalinfo-end -->
111
 
 *
 
112
 * 
112
113
 <!-- options-start -->
113
114
 * Valid options are: <p/>
114
 
 *
 
115
 * 
115
116
 * <pre> -E &lt;expr&gt;
116
117
 *  The expression to use for filtering
117
118
 *  (default: true).</pre>
118
 
 *
 
119
 * 
 
120
 * <pre> -F
 
121
 *  Apply the filter to instances that arrive after the first
 
122
 *  (training) batch. The default is to not apply the filter (i.e.
 
123
 *  always return the instance)</pre>
 
124
 * 
119
125
 <!-- options-end -->
120
126
 *
121
127
 * @author  fracpete (fracpete at waikato dot ac dot nz)
122
 
 * @version $Revision: 6113 $
 
128
 * @version $Revision: 7599 $
123
129
 */
124
130
public class SubsetByExpression
125
131
  extends SimpleBatchFilter {
126
132
 
127
133
  /** for serialization. */
128
134
  private static final long serialVersionUID = 5628686110979589602L;
129
 
 
 
135
  
130
136
  /** the expresion to use for filtering. */
131
137
  protected String m_Expression = "true";
132
 
 
 
138
  
 
139
  /** Whether to filter instances after the first batch has been processed */
 
140
  protected boolean m_filterAfterFirstBatch = false;
 
141
  
133
142
  /**
134
143
   * Returns a string describing this filter.
135
144
   *
137
146
   *                    displaying in the explorer/experimenter gui
138
147
   */
139
148
  public String globalInfo() {
140
 
    return
 
149
    return 
141
150
        "Filters instances according to a user-specified expression.\n\n"
142
151
      + "Grammar:\n\n"
143
152
      + "boolexpr_list ::= boolexpr_list boolexpr_part | boolexpr_part;\n"
208
217
      + "  from the 'labor' UCI dataset:\n"
209
218
      + "  not ismissing(ATT3)\n"
210
219
      ;
 
220
  }  
 
221
  
 
222
  /**
 
223
   * Input an instance for filtering. Filter requires all
 
224
   * training instances be read before producing output (calling the method
 
225
   * batchFinished() makes the data available). If this instance is part of
 
226
   * a new batch, m_NewBatch is set to false.
 
227
   *
 
228
   * @param instance    the input instance
 
229
   * @return            true if the filtered instance may now be
 
230
   *                    collected with output().
 
231
   * @throws  IllegalStateException if no input structure has been defined
 
232
   * @throws Exception  if something goes wrong
 
233
   * @see               #batchFinished()
 
234
   */
 
235
  public boolean input(Instance instance) throws Exception {
 
236
    if (getInputFormat() == null)
 
237
      throw new IllegalStateException("No input instance format defined");
 
238
    
 
239
    if (m_NewBatch) {
 
240
      resetQueue();
 
241
      m_NewBatch = false;
 
242
    }
 
243
 
 
244
    bufferInput(instance);
 
245
    
 
246
    int numReturnedFromParser = 0;
 
247
    if (isFirstBatchDone()) {
 
248
      Instances inst = new Instances(getInputFormat());
 
249
      inst = process(inst);
 
250
      numReturnedFromParser = inst.numInstances();
 
251
      for (int i = 0; i < inst.numInstances(); i++)
 
252
        push(inst.instance(i));
 
253
      flushInput();
 
254
    }
 
255
    
 
256
    return (numReturnedFromParser > 0);
211
257
  }
212
258
 
213
259
  /**
217
263
   */
218
264
  public Enumeration listOptions() {
219
265
    Vector      result;
220
 
 
 
266
    
221
267
    result = new Vector();
222
268
 
223
269
    result.addElement(new Option(
224
270
        "\tThe expression to use for filtering\n"
225
271
        + "\t(default: true).",
226
272
        "E", 1, "-E <expr>"));
 
273
    
 
274
    result.addElement(new Option(
 
275
        "\tApply the filter to instances that arrive after the first\n" +
 
276
        "\t(training) batch. The default is to not apply the filter (i.e.\n" +
 
277
        "\talways return the instance)",
 
278
        "F", 0, "-F"));
227
279
 
228
280
    return result.elements();
229
281
  }
231
283
 
232
284
  /**
233
285
   * Parses a given list of options. <p/>
234
 
   *
 
286
   * 
235
287
   <!-- options-start -->
236
288
   * Valid options are: <p/>
237
 
   *
 
289
   * 
238
290
   * <pre> -E &lt;expr&gt;
239
291
   *  The expression to use for filtering
240
292
   *  (default: true).</pre>
241
 
   *
 
293
   * 
 
294
   * <pre> -F
 
295
   *  Apply the filter to instances that arrive after the first
 
296
   *  (training) batch. The default is to not apply the filter (i.e.
 
297
   *  always return the instance)</pre>
 
298
   * 
242
299
   <!-- options-end -->
243
300
   *
244
301
   * @param options the list of options as an array of strings
246
303
   */
247
304
  public void setOptions(String[] options) throws Exception {
248
305
    String      tmpStr;
249
 
 
 
306
    
250
307
    tmpStr = Utils.getOption('E', options);
251
308
    if (tmpStr.length() != 0)
252
309
      setExpression(tmpStr);
253
310
    else
254
311
      setExpression("true");
255
 
 
 
312
    
 
313
    m_filterAfterFirstBatch = Utils.getFlag('F', options);
 
314
   
256
315
    if (getInputFormat() != null)
257
316
      setInputFormat(getInputFormat());
258
317
  }
264
323
   */
265
324
  public String[] getOptions() {
266
325
    Vector<String>      result;
267
 
 
 
326
    
268
327
    result = new Vector();
269
328
 
270
329
    result.add("-E");
271
330
    result.add("" + getExpression());
 
331
    
 
332
    if (m_filterAfterFirstBatch) {
 
333
      result.add("-F");
 
334
    }
272
335
 
273
336
    return result.toArray(new String[result.size()]);
274
337
  }
275
338
 
276
 
  /**
 
339
  /** 
277
340
   * Returns the Capabilities of this filter.
278
341
   *
279
342
   * @return            the capabilities of this object
288
351
    result.enable(Capability.NUMERIC_ATTRIBUTES);
289
352
    result.enable(Capability.DATE_ATTRIBUTES);
290
353
    result.enable(Capability.MISSING_VALUES);
291
 
 
 
354
    
292
355
    // class
293
356
    result.enable(Capability.NOMINAL_CLASS);
294
357
    result.enable(Capability.NUMERIC_CLASS);
295
358
    result.enable(Capability.DATE_CLASS);
296
359
    result.enable(Capability.MISSING_CLASS_VALUES);
297
360
    result.enable(Capability.NO_CLASS);
298
 
 
 
361
    
299
362
    return result;
300
363
  }
301
364
 
319
382
 
320
383
  /**
321
384
   * Returns the tip text for this property.
322
 
   *
 
385
   * 
323
386
   * @return            tip text for this property suitable for
324
387
   *                    displaying in the explorer/experimenter gui
325
388
   */
326
389
  public String expressionTipText() {
327
390
    return "The expression to used for filtering the dataset.";
328
391
  }
 
392
  
 
393
  /**
 
394
   * Set whether to apply the filter to instances that arrive once
 
395
   * the first (training) batch has been seen. The default is to
 
396
   * not apply the filter and just return each instance input. This
 
397
   * is so that, when used in the FilteredClassifier, a test instance
 
398
   * does not get "consumed" by the filter and a prediction is always
 
399
   * generated.
 
400
   * 
 
401
   * @param b true if the filter should be applied to instances that
 
402
   * arrive after the first (training) batch has been processed.
 
403
   */
 
404
  public void setFilterAfterFirstBatch(boolean b) {
 
405
    m_filterAfterFirstBatch = b;
 
406
  }
 
407
  
 
408
  /**
 
409
   * Get whether to apply the filter to instances that arrive once
 
410
   * the first (training) batch has been seen. The default is to
 
411
   * not apply the filter and just return each instance input. This
 
412
   * is so that, when used in the FilteredClassifier, a test instance
 
413
   * does not get "consumed" by the filter and a prediction is always
 
414
   * generated.
 
415
   * 
 
416
   * @return true if the filter should be applied to instances that
 
417
   * arrive after the first (training) batch has been processed.
 
418
   */
 
419
  public boolean getFilterAfterFirstBatch() {
 
420
    return m_filterAfterFirstBatch;
 
421
  }
 
422
  
 
423
  /**
 
424
   * Returns the tip text for this property.
 
425
   * 
 
426
   * @return            tip text for this property suitable for
 
427
   *                    displaying in the explorer/experimenter gui
 
428
   */
 
429
  public String filterAfterFirstBatchTipText() {
 
430
    return "Whether to apply the filtering process to instances that " +
 
431
                "are input after the first (training) batch. The default " +
 
432
                "is false so that, when used in a FilteredClassifier, test" +
 
433
                " instances do not potentially get 'consumed' by the filter " +
 
434
                "an a prediction is always made.";
 
435
  }
329
436
 
330
437
  /**
331
 
   * Determines the output format based on the input format and returns
 
438
   * Determines the output format based on the input format and returns 
332
439
   * this.
333
440
   *
334
441
   * @param inputFormat     the input format to base the output format on
337
444
   */
338
445
  protected Instances determineOutputFormat(Instances inputFormat)
339
446
      throws Exception {
340
 
 
 
447
    
341
448
    return new Instances(inputFormat, 0);
342
449
  }
343
450
 
351
458
   * @see               #batchFinished()
352
459
   */
353
460
  protected Instances process(Instances instances) throws Exception {
354
 
    if (!isFirstBatchDone())
 
461
    if (!isFirstBatchDone() || m_filterAfterFirstBatch) {
355
462
      return Parser.filter(m_Expression, instances);
356
 
    else
 
463
    } else {
357
464
      return instances;
 
465
    }
358
466
  }
359
467
 
360
468
  /**
361
469
   * Returns the revision string.
362
 
   *
 
470
   * 
363
471
   * @return            the revision
364
472
   */
365
473
  public String getRevision() {
366
 
    return RevisionUtils.extract("$Revision: 6113 $");
 
474
    return RevisionUtils.extract("$Revision: 7599 $");
367
475
  }
368
476
 
369
477
  /**
375
483
    runFilter(new SubsetByExpression(), args);
376
484
  }
377
485
}
 
486