public abstract class Filter extends java.lang.Object implements java.io.Serializable, CapabilitiesHandler, RevisionHandler, OptionHandler, CapabilitiesIgnorer, CommandlineRunnable
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..
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
m_Debug
Whether the classifier is run in debug mode.
|
protected boolean |
m_DoNotCheckCapabilities
Whether capabilities should not be checked before classifier is built.
|
protected boolean |
m_FirstBatchDone
True if the first batch has been done
|
protected RelationalLocator |
m_InputRelAtts
Indices of relational attributes in the input format
|
protected StringLocator |
m_InputStringAtts
Indices of string attributes in the input format
|
protected boolean |
m_NewBatch
Record whether the filter is at the start of a batch
|
protected RelationalLocator |
m_OutputRelAtts
Indices of relational attributes in the output format
|
protected StringLocator |
m_OutputStringAtts
Indices of string attributes in the output format
|
| Constructor and Description |
|---|
Filter() |
| Modifier and Type | Method and Description |
|---|---|
static void |
batchFilterFile(Filter filter,
java.lang.String[] options)
Method for testing filters ability to process multiple batches.
|
boolean |
batchFinished()
Signify that this batch of input to the filter is finished.
|
protected void |
bufferInput(Instance instance)
Adds the supplied input instance to the inputformat dataset for later
processing.
|
protected void |
copyValues(Instance instance,
boolean isInput)
Copies string/relational values contained in the instance copied to a new
dataset.
|
protected void |
copyValues(Instance instance,
boolean instSrcCompat,
Instances srcDataset,
Instances destDataset)
Takes string/relational values referenced by an Instance and copies them
from a source dataset to a destination dataset.
|
java.lang.String |
debugTipText()
Returns the tip text for this property
|
java.lang.String |
doNotCheckCapabilitiesTipText()
Returns the tip text for this property
|
static void |
filterFile(Filter filter,
java.lang.String[] options)
Method for testing filters.
|
protected void |
flushInput()
This will remove all buffered instances from the inputformat dataset.
|
Capabilities |
getCapabilities()
Returns the Capabilities of this filter.
|
Capabilities |
getCapabilities(Instances data)
Returns the Capabilities of this filter, customized based on the data.
|
Instances |
getCopyOfInputFormat()
Gets a copy of just the structure of the input format instances.
|
boolean |
getDebug()
Get whether debugging is turned on.
|
boolean |
getDoNotCheckCapabilities()
Get whether capabilities checking is turned off.
|
protected Instances |
getInputFormat()
Gets the currently set inputformat instances.
|
java.lang.String[] |
getOptions()
Gets the current settings of the filter.
|
Instances |
getOutputFormat()
Gets the format of the output instances.
|
java.lang.String |
getRevision()
Returns the revision string.
|
protected void |
initInputLocators(Instances data,
int[] indices)
Initializes the input attribute locators.
|
protected void |
initOutputLocators(Instances data,
int[] indices)
Initializes the output attribute locators.
|
boolean |
input(Instance instance)
Input an instance for filtering.
|
protected Instances |
inputFormatPeek()
Returns a reference to the current input format without copying it.
|
boolean |
isFirstBatchDone()
Returns true if the first batch of instances got processed.
|
boolean |
isNewBatch()
Returns true if the a new batch was started, either a new instance of the
filter was created or the batchFinished() method got called.
|
boolean |
isOutputFormatDefined()
Returns whether the output format is ready to be collected
|
java.util.Enumeration<Option> |
listOptions()
Returns an enumeration describing the available options.
|
static void |
main(java.lang.String[] args)
Main method for testing this class.
|
static Filter[] |
makeCopies(Filter model,
int num)
Creates a given number of deep copies of the given filter using
serialization.
|
static Filter |
makeCopy(Filter model)
Creates a deep copy of the given filter using serialization.
|
boolean |
mayRemoveInstanceAfterFirstBatchDone()
Default implementation returns false.
|
int |
numPendingOutput()
Returns the number of instances pending output
|
Instance |
output()
Output an instance after filtering and remove from the output queue.
|
protected Instances |
outputFormatPeek()
Returns a reference to the current output format without copying it.
|
Instance |
outputPeek()
Output an instance after filtering but do not remove from the output queue.
|
void |
postExecution()
Perform any teardown stuff that might need to happen after execution.
|
void |
preExecution()
Perform any setup stuff that might need to happen before commandline
execution.
|
protected void |
push(Instance instance)
Adds an output instance to the queue.
|
protected void |
push(Instance instance,
boolean copyInstance)
Adds an output instance to the queue.
|
protected void |
resetQueue()
Clears the output queue.
|
void |
run(java.lang.Object toRun,
java.lang.String[] options)
Execute the supplied object.
|
static void |
runFilter(Filter filter,
java.lang.String[] options)
runs the filter instance with the given options.
|
void |
setDebug(boolean debug)
Set debugging mode.
|
void |
setDoNotCheckCapabilities(boolean doNotCheckCapabilities)
Set whether not to check capabilities.
|
boolean |
setInputFormat(Instances instanceInfo)
Sets the format of the input instances.
|
void |
setOptions(java.lang.String[] options)
Parses a given list of options.
|
protected void |
setOutputFormat(Instances outputFormat)
Sets the format of output instances.
|
protected void |
testInputFormat(Instances instanceInfo)
tests the data whether the filter can actually handle it
|
java.lang.String |
toString()
Returns a description of the filter, by default only the classname.
|
static Instances |
useFilter(Instances data,
Filter filter)
Filters an entire set of instances through a filter and returns the new
set.
|
static java.lang.String |
wekaStaticWrapper(Sourcable filter,
java.lang.String className,
Instances input,
Instances output)
generates source code from the filter
|
protected StringLocator m_OutputStringAtts
protected StringLocator m_InputStringAtts
protected RelationalLocator m_OutputRelAtts
protected RelationalLocator m_InputRelAtts
protected boolean m_NewBatch
protected boolean m_FirstBatchDone
protected boolean m_Debug
protected boolean m_DoNotCheckCapabilities
public boolean isNewBatch()
m_NewBatch,
batchFinished()public boolean isFirstBatchDone()
m_FirstBatchDone,
batchFinished()public boolean mayRemoveInstanceAfterFirstBatchDone()
public Capabilities getCapabilities()
getCapabilities in interface CapabilitiesHandlerCapabilitiespublic java.lang.String getRevision()
getRevision in interface RevisionHandlerpublic Capabilities getCapabilities(Instances data)
data - the data to use for customizationgetCapabilities()protected void setOutputFormat(Instances outputFormat)
outputFormat - the new output formatprotected Instances getInputFormat()
public Instances getCopyOfInputFormat()
protected Instances inputFormatPeek()
protected Instances outputFormatPeek()
protected void push(Instance instance)
instance - the instance to be added to the queue.protected void push(Instance instance, boolean copyInstance)
instance - the instance to be added to the queue.copyInstance - whether instance is to be copiedprotected void resetQueue()
protected void bufferInput(Instance instance)
instance - the Instance to buffer.protected void initInputLocators(Instances data, int[] indices)
data - the data to initialize the locators withindices - if not null, the indices to which to restrict the locatingprotected void initOutputLocators(Instances data, int[] indices)
data - the data to initialize the locators withindices - if not null, the indices to which to restrict the locatingprotected void copyValues(Instance instance, boolean isInput)
instance - the Instance containing the string/relational values to
copy.isInput - if true the input format and input attribute locators are
used otherwise the output format and output locatorsprotected void copyValues(Instance instance, boolean instSrcCompat, Instances srcDataset, Instances destDataset)
instance - the instance containing references to strings/ relational
values in the source dataset that will have references updated to
be valid for the destination dataset.instSrcCompat - true if the instance structure is the same as the
source, or false if it is the same as the destination (i.e. which
of the string/relational attribute indices contains the correct
locations for this instance).srcDataset - the dataset for which the current instance
string/relational value references are valid (after any position
mapping if needed)destDataset - the dataset for which the current instance
string/relational value references need to be inserted (after any
position mapping if needed)protected void flushInput()
protected void testInputFormat(Instances instanceInfo) throws java.lang.Exception
instanceInfo - the data to testjava.lang.Exception - if the test failspublic boolean setInputFormat(Instances instanceInfo) throws java.lang.Exception
super.setInputFormat(Instances)instanceInfo - an Instances object containing the input instance
structure (any instances contained in the object are ignored -
only the structure is required).java.lang.Exception - if the inputFormat can't be set successfullypublic Instances getOutputFormat()
java.lang.NullPointerException - if no input structure has been defined (or the
output format hasn't been determined yet)public boolean input(Instance instance) throws java.lang.Exception
instance - the input instancejava.lang.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.public boolean batchFinished()
throws java.lang.Exception
java.lang.NullPointerException - if no input structure has been defined,java.lang.Exception - if there was a problem finishing the batch.public Instance output()
java.lang.NullPointerException - if no output structure has been definedpublic Instance outputPeek()
java.lang.NullPointerException - if no input structure has been definedpublic int numPendingOutput()
java.lang.NullPointerException - if no input structure has been definedpublic boolean isOutputFormatDefined()
public static Filter makeCopy(Filter model) throws java.lang.Exception
model - the filter to copyjava.lang.Exception - if an error occurspublic static Filter[] makeCopies(Filter model, int num) throws java.lang.Exception
model - the filter to copynum - the number of filter copies to create.java.lang.Exception - if an error occurspublic static Instances useFilter(Instances data, Filter filter) throws java.lang.Exception
data - the data to be filteredfilter - the filter to be usedjava.lang.Exception - if the filter can't be used successfullypublic java.lang.String toString()
toString in class java.lang.Objectpublic static java.lang.String wekaStaticWrapper(Sourcable filter, java.lang.String className, Instances input, Instances output) throws java.lang.Exception
filter - the filter to output as sourceclassName - the name of the generated classinput - the input data the header is generated foroutput - the output data the header is generated forjava.lang.Exception - if source code cannot be generatedpublic static void filterFile(Filter filter, java.lang.String[] options) throws java.lang.Exception
filter - the filter to useoptions - should contain the following arguments: java.lang.Exception - if something goes wrong or the user requests help on
command optionspublic static void batchFilterFile(Filter filter, java.lang.String[] options) throws java.lang.Exception
filter - the filter to useoptions - should contain the following arguments: java.lang.Exception - if something goes wrong or the user requests help on
command optionspublic static void runFilter(Filter filter, java.lang.String[] options)
filter - the filter to runoptions - the commandline optionspublic java.util.Enumeration<Option> listOptions()
listOptions in interface OptionHandlerpublic void setOptions(java.lang.String[] options)
throws java.lang.Exception
-D
If set, filter is run in debug mode and may output additional info to the
console.
-do-not-check-capabilities
If set, filter capabilities are not checked before filter is built (use
with caution).
setOptions in interface OptionHandleroptions - the list of options as an array of stringsjava.lang.Exception - if an option is not supportedpublic java.lang.String[] getOptions()
getOptions in interface OptionHandlerpublic void setDebug(boolean debug)
debug - true if debug output should be printedpublic boolean getDebug()
public java.lang.String debugTipText()
public void setDoNotCheckCapabilities(boolean doNotCheckCapabilities)
setDoNotCheckCapabilities in interface CapabilitiesIgnorerdoNotCheckCapabilities - true if capabilities are not to be checked.public boolean getDoNotCheckCapabilities()
getDoNotCheckCapabilities in interface CapabilitiesIgnorerpublic java.lang.String doNotCheckCapabilitiesTipText()
public void preExecution()
throws java.lang.Exception
preExecution in interface CommandlineRunnablejava.lang.Exception - if a problem occurs during setuppublic void run(java.lang.Object toRun,
java.lang.String[] options)
throws java.lang.Exception
run in interface CommandlineRunnabletoRun - the object to executeoptions - any options to pass to the objectjava.lang.Exception - if the object is not of the expected type.public void postExecution()
throws java.lang.Exception
postExecution in interface CommandlineRunnablejava.lang.Exception - if a problem occurs during teardownpublic static void main(java.lang.String[] args)
args - should contain arguments to the filter: use -h for help