public abstract class SimpleStreamFilter extends SimpleFilter implements StreamableFilter
true
. 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:
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 Instance(1, values); return result; } public static void main(String[] args) { runFilter(new SimpleStream(), args); } }Options:
SimpleBatchFilter
,
input(Instance)
,
batchFinished()
,
Filter.m_FirstBatchDone
,
Serialized Formm_Debug
m_FirstBatchDone, m_InputRelAtts, m_InputStringAtts, m_NewBatch, m_OutputRelAtts, m_OutputStringAtts
Constructor and Description |
---|
SimpleStreamFilter() |
Modifier and Type | Method and Description |
---|---|
boolean |
batchFinished()
Signify that this batch of input to the filter is finished.
|
protected abstract Instances |
determineOutputFormat(Instances inputFormat)
Determines the output format based on the input format and returns
this.
|
protected boolean |
hasImmediateOutputFormat()
Returns true if the output format is immediately available after the
input format has been set and not only after all the data has been
seen (see batchFinished()).
|
boolean |
input(Instance instance)
Input an instance for filtering.
|
protected void |
preprocess(Instances instances)
In case the output format cannot be returned immediately, this method
is called before the actual processing of the instances.
|
protected abstract Instance |
process(Instance instance)
processes the given instance (may change the provided instance) and
returns the modified version.
|
protected Instances |
process(Instances instances)
Processes the given data (may change the provided dataset) and returns
the modified version.
|
debugTipText, getDebug, getOptions, globalInfo, listOptions, reset, setDebug, setInputFormat, setOptions
batchFilterFile, bufferInput, copyValues, copyValues, filterFile, flushInput, getCapabilities, getCapabilities, getInputFormat, getOutputFormat, getRevision, initInputLocators, initOutputLocators, inputFormatPeek, isFirstBatchDone, isNewBatch, isOutputFormatDefined, main, makeCopies, makeCopy, numPendingOutput, output, outputFormatPeek, outputPeek, push, resetQueue, runFilter, setOutputFormat, testInputFormat, toString, useFilter, wekaStaticWrapper
protected boolean hasImmediateOutputFormat()
hasImmediateOutputFormat
in class SimpleFilter
batchFinished()
,
SimpleFilter.setInputFormat(Instances)
,
Filter.m_FirstBatchDone
protected abstract Instances determineOutputFormat(Instances inputFormat) throws Exception
determineOutputFormat
in class SimpleFilter
inputFormat
- the input format to base the output format onException
- in case the determination goes wronghasImmediateOutputFormat()
,
batchFinished()
,
preprocess(Instances)
protected abstract Instance process(Instance instance) throws Exception
instance
- the instance to processException
- in case the processing goes wrongprotected Instances process(Instances instances) throws Exception
process
in class SimpleFilter
instances
- the data to processException
- in case the processing goes wrongbatchFinished()
,
process(Instance)
protected void preprocess(Instances instances)
instances
- the instances to work onhasImmediateOutputFormat()
,
determineOutputFormat(Instances)
public boolean input(Instance instance) throws Exception
input
in class Filter
instance
- the input instanceIllegalStateException
- if no input structure has been definedException
- if something goes wrongpublic boolean batchFinished() throws Exception
batchFinished
in class Filter
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.Copyright © 2015 University of Waikato, Hamilton, NZ. All rights reserved.