Writing+your+own+Filter

toc
 * Note:** This is also covered in chapter //Extending WEKA// of the WEKA manual in versions later than 3.6.1/3.7.0 or snapshots of the stable-3.6/developer version later than 10/01/2010.

= Simple approach = The classes and  provide a simplified approach to implementing ones own filter.

Due to siginificant changes in the API beyond version 3.5.3, there are now two different HOWTOs available. Please choose the version of Weka you want to develop for:
 * <= 3.5.3
 * > 3.5.3 (that includes 3.6.x and 3.7.x)

= Default approach = This article covers the default approach, in case one doesn't use the simple approach or doesn't have the developer version at hand.

= Packages = A few comments about the different filter sub-packages: > contains supervised filters, i.e., filters that take class distributions into account. > Must implement the interface. > filters that work column-wise. >> filters that work row-wise. > contains unsupervised filters, i.e., they work without taking any class distributions into account. > Must implement the interface. >> filters that work column-wise. >> filters that work row-wise.

 = Revisions = As of 14/04/2007 (> 3.5.7), filters also implement the interface. This provides the functionality of obtaining the Subversion revision from within Java. Filters that are not part of the official Weka distribution will have to implement the method as follows, which will return a dummy revision of //1.0//: code format="java" /**  * Returns the revision string. *   * @return		the revision */ public String getRevision { return RevisionUtils.extract("$Revision: 1.0 $"); } code

= Integration = After finishing the coding stage, it's time to integrate your filter in the Weka framework, i.e., to make it available in the Explorer, Experimenter, etc. Starting with version **3.4.4**, Weka supports an automatic discovery of derived classes in your classpath, managed by the //GenericPropertiesCreator//.

The GenericObjectEditor article shows you how to tell Weka where to find your filter and therefore displaying it in the //GenericObjectEditor// (filters work in the same fashion as classifiers, regarding the discovery).


 * NB:** Weka 3.5.8 turned the automatic discovery off by default!

= Testing = Weka provides already a test framework to ensure the basic functionality of a filter. It is essential for the filter to pass these tests.

Option handling
If your filter implements, check the **option handling** of your filter with the following tool from commandline: code format="bash" weka.core.CheckOptionHandler -W classname [-- additional parameters] code All tests need to return //yes//.

GenericObjectEditor (> 3.5.5)
The class checks whether all the properties available in the GUI have a tooltip accompanying them and whether the  method is declared: weka.core.CheckGOE -W classname [-- additional parameters]

All tests, once again, need to return //yes//.

Source code (> 3.5.6)
Filters that implement the interface can output Java code of their internal representation. In order to check the generated code, one should not only compile the code, but also test it with the following test class: code format="text" weka.filters.CheckSource code This class takes the original Weka filter, the generated code and the dataset used for generating the source code (and an optional class index) as parameters. It builds the Weka filter on the dataset and compares the output, the one from the Weka filter and the one from the generated source code, whether they are the same.

Here's an example call for and the generated class  (it wraps the actual generated code in a pseudo-filter): code format="bash" java weka.filters.CheckSource \ -W weka.filters.unsupervised.attribute.ReplaceMissingValues \ -S weka.filters.WekaWrapper \ -t data.arff code It needs to return //Tests OK!//.

Unit tests
In order to make sure that your filter applies to the Weka criteria, you should add your filter to the [|junit] unit test framework, i.e., by creating a Test class.

How to check out the unit test framework, you can find here.

= See also =
 * GenericObjectEditor/GenericPropertiesCreator

= Links =
 * [|junit]