Package weka.filters
Class SimpleStreamFilter
java.lang.Object
weka.filters.Filter
weka.filters.SimpleFilter
weka.filters.SimpleStreamFilter
- All Implemented Interfaces:
Serializable
,CapabilitiesHandler
,CapabilitiesIgnorer
,CommandlineRunnable
,OptionHandler
,RevisionHandler
,StreamableFilter
- Direct Known Subclasses:
ClassAssigner
,FixedDictionaryStringToWordVector
,MultiFilter
,NumericCleaner
,RemoveByName
,RenameAttribute
,SortLabels
This filter is a superclass for simple stream filters.
General notes:
The following code snippet uses the filter
Only the following abstract methods need to be implemented:
And the getCapabilities() method must return what kind of attributes and classes the filter can handle. If more options are necessary, then the following methods need to be overriden:
Valid filter-specific options are: -D
Turns on output of debugging information.
- After the first call of batchFinished() the field m_FirstBatchDone is set
to
true
.
The following code snippet uses the filter
SomeFilter
on a
dataset that is loaded from filename
.
import weka.core.*; import weka.filters.*; import java.io.*; ... SomeFilter filter = new SomeFilter(); // set necessary options for the filter Instances data = new Instances( new BufferedReader( new FileReader(filename))); Instances filteredData = Filter.useFilter(data, filter);Implementation:
Only the following abstract methods need to be implemented:
- globalInfo()
- determineOutputFormat(Instances)
- process(Instance)
And the getCapabilities() method must return what kind of attributes and classes the filter can handle. If more options are necessary, then the following methods need to be overriden:
- listOptions()
- setOptions(String[])
- getOptions()
public static void main(String[] args) { runFilter(new <Filtername>(), args); }Example implementation:
import weka.core.*; import weka.core.Capabilities.*; import weka.filters.*; import java.util.Random; public class SimpleStream extends SimpleStreamFilter { public String globalInfo() { return "A simple stream filter that adds an attribute 'bla' at the end containing a random number."; } public Capabilities getCapabilities() { Capabilities result = super.getCapabilities(); result.enableAllAttributes(); result.enableAllClasses(); result.enable(Capability.NO_CLASS); // filter doesn't need class to be set return result; } protected Instances determineOutputFormat(Instances inputFormat) { Instances result = new Instances(inputFormat, 0); result.insertAttributeAt(new Attribute("bla"), result.numAttributes()); return result; } protected Instance process(Instance inst) { double[] values = new double[inst.numAttributes() + 1]; for (int n = 0; n < inst.numAttributes(); n++) values[n] = inst.value(n); values[values.length - 1] = new Random().nextInt(); Instance result = new DenseInstance(1, values); return result; } public static void main(String[] args) { runFilter(new SimpleStream(), args); } }Options:
Valid filter-specific options are: -D
Turns on output of debugging information.
- Version:
- $Revision: 12037 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
-
SimpleBatchFilter
input(Instance)
batchFinished()
Filter.m_FirstBatchDone
- Serialized Form
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Signify that this batch of input to the filter is finished.boolean
Input an instance for filtering.Methods inherited from class weka.filters.SimpleFilter
globalInfo, setInputFormat
Methods inherited from class weka.filters.Filter
batchFilterFile, debugTipText, doNotCheckCapabilitiesTipText, filterFile, getCapabilities, getCapabilities, getCopyOfInputFormat, getDebug, getDoNotCheckCapabilities, getOptions, getOutputFormat, getRevision, isFirstBatchDone, isNewBatch, isOutputFormatDefined, listOptions, main, makeCopies, makeCopy, mayRemoveInstanceAfterFirstBatchDone, numPendingOutput, output, outputPeek, postExecution, preExecution, run, runFilter, setDebug, setDoNotCheckCapabilities, setOptions, toString, useFilter, wekaStaticWrapper
-
Constructor Details
-
SimpleStreamFilter
public SimpleStreamFilter()
-
-
Method Details
-
input
Input an instance for filtering. Filter requires all training instances be read before producing output.- Overrides:
input
in classFilter
- Parameters:
instance
- the input instance- Returns:
- true if the filtered instance may now be collected with output().
- Throws:
IllegalStateException
- if no input structure has been definedException
- if something goes wrong
-
batchFinished
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 setInputFormat has been re-assigned or new options have been set).- Overrides:
batchFinished
in classFilter
- Returns:
- true if there are instances pending output
- Throws:
IllegalStateException
- if no input format has been set.NullPointerException
- if no input structure has been defined,Exception
- if there was a problem finishing the batch.
-