All Packages  Class Hierarchy  This Package  Previous  Next  Index  WEKA's home

Class weka.filters.Filter

java.lang.Object
    |
    +----weka.filters.Filter

public abstract class Filter
extends java.lang.Object
implements java.io.Serializable
An abstract class for instance filters: objects that take instances as input, carry out some transformation on the instance and then output the instance. The method implementations in this class assume that most of the work will be done in the methods overridden by subclasses.

A simple example of filter use. This example doesn't remove instances from the output queue until all instances have been input, so has higher memory consumption than an approach that uses output instances as they are made available:

  Filter filter = ..some type of filter..
  Instances instances = ..some instances..
  for (int i = 0; i < data.numInstances(); i++) {
    filter.input(data.instance(i));
  }
  filter.batchFinished();
  Instances newData = filter.outputFormat();
  Instance processed;
  while ((processed = filter.output()) != null) {
    newData.add(processed);
  }
  ..do something with newData..
 

Version:
$Revision: 1.21 $
Author:
Len Trigg (trigg@cs.waikato.ac.nz)

Constructor Index

 o Filter()
 

Method Index

 o batchFilterFile(Filter, String[])
Method for testing filters ability to process multiple batches.
 o batchFinished()
Signify that this batch of input to the filter is finished.
 o filterFile(Filter, String[])
Method for testing filters.
 o getOutputFormat()
Gets the format of the output instances.
 o input(Instance)
Input an instance for filtering.
 o inputFormat(Instances)
  Deprecated
 o isOutputFormatDefined()
Returns whether the output format is ready to be collected
 o main(String[])
Main method for testing this class.
 o numPendingOutput()
Returns the number of instances pending output
 o output()
Output an instance after filtering and remove from the output queue.
 o outputFormat()
  Deprecated
 o outputPeek()
Output an instance after filtering but do not remove from the output queue.
 o setInputFormat(Instances)
Sets the format of the input instances.
 o useFilter(Instances, Filter)
Filters an entire set of instances through a filter and returns the new set.

Constructor Detail

 o Filter
public Filter()

Method Detail

 o inputFormat
public boolean inputFormat(Instances instanceInfo) throws java.lang.Exception
Note: inputFormat() is deprecated.use setInputFormat(Instances) instead.

 o setInputFormat
public boolean setInputFormat(Instances instanceInfo) throws java.lang.Exception
          Sets the format of the input instances. If the filter is able to determine the output format before seeing any input instances, it does so here. This default implementation clears the output format and output queue, and the new batch flag is set. Overriders should call super.setInputFormat(Instances)
Parameters:
instanceInfo - an Instances object containing the input instance structure (any instances contained in the object are ignored - only the structure is required).
Returns:
true if the outputFormat may be collected immediately
Throws:
java.lang.Exception - if the inputFormat can't be set successfully
 o outputFormat
public final Instances outputFormat()
Note: outputFormat() is deprecated.use getOutputFormat() instead.

 o getOutputFormat
public final Instances getOutputFormat()
          Gets the format of the output instances. This should only be called after input() or batchFinished() has returned true. The relation name of the output instances should be changed to reflect the action of the filter (eg: add the filter name and options).
Returns:
an Instances object containing the output instance structure only.
Throws:
NullPointerException - if no input structure has been defined (or the output format hasn't been determined yet)
 o input
public boolean input(Instance instance) throws java.lang.Exception
          Input an instance for filtering. Ordinarily the instance is processed and made available for output immediately. Some filters require all instances be read before producing output, in which case output instances should be collected after calling batchFinished(). If the input marks the start of a new batch, the output queue is cleared. This default implementation assumes all instance conversion will occur when batchFinished() is called.
Parameters:
instance - the input instance
Returns:
true if the filtered instance may now be collected with output().
Throws:
NullPointerException - if the input format has not been defined.
java.lang.Exception - if the input instance was not of the correct format or if there was a problem with the filtering.
 o batchFinished
public boolean batchFinished() throws java.lang.Exception
          Signify that this batch of input to the filter is finished. If the filter requires all instances prior to filtering, output() may now be called to retrieve the filtered instances. Any subsequent instances filtered should be filtered based on setting obtained from the first batch (unless the inputFormat has been re-assigned or new options have been set). This default implementation assumes all instance processing occurs during inputFormat() and input().
Returns:
true if there are instances pending output
Throws:
NullPointerException - if no input structure has been defined,
java.lang.Exception - if there was a problem finishing the batch.
 o output
public Instance output()
          Output an instance after filtering and remove from the output queue.
Returns:
the instance that has most recently been filtered (or null if the queue is empty).
Throws:
NullPointerException - if no output structure has been defined
 o outputPeek
public Instance outputPeek()
          Output an instance after filtering but do not remove from the output queue.
Returns:
the instance that has most recently been filtered (or null if the queue is empty).
Throws:
NullPointerException - if no input structure has been defined
 o numPendingOutput
public int numPendingOutput()
          Returns the number of instances pending output
Returns:
the number of instances pending output
Throws:
NullPointerException - if no input structure has been defined
 o isOutputFormatDefined
public boolean isOutputFormatDefined()
          Returns whether the output format is ready to be collected
Returns:
true if the output format is set
 o useFilter
public static Instances useFilter(Instances data,
                                  Filter filter) throws java.lang.Exception
          Filters an entire set of instances through a filter and returns the new set.
Parameters:
data - the data to be filtered
filter - the filter to be used
Returns:
the filtered set of data
Throws:
java.lang.Exception - if the filter can't be used successfully
 o filterFile
public static void filterFile(Filter filter,
                              java.lang.String options[]) throws java.lang.Exception
          Method for testing filters.
Parameters:
argv - should contain the following arguments:
-i input_file
-o output_file
-c class_index
or -h for help on options
Throws:
java.lang.Exception - if something goes wrong or the user requests help on command options
 o batchFilterFile
public static void batchFilterFile(Filter filter,
                                   java.lang.String options[]) throws java.lang.Exception
          Method for testing filters ability to process multiple batches.
Parameters:
argv - should contain the following arguments:
-i (first) input file
-o (first) output file
-r (second) input file
-s (second) output file
-c class_index
or -h for help on options
Throws:
java.lang.Exception - if something goes wrong or the user requests help on command options
 o main
public static void main(java.lang.String args[])
          Main method for testing this class.
Parameters:
argv - should contain arguments to the filter: use -h for help

All Packages  Class Hierarchy  This Package  Previous  Next  Index  WEKA's home