📄 partitionedmultifilter.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * PartitionedMultiFilter.java * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand * */package weka.filters.unsupervised.attribute;import weka.core.Attribute;import weka.core.Capabilities;import weka.core.FastVector;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.Range;import weka.core.SparseInstance;import weka.core.Utils;import weka.core.Capabilities.Capability;import weka.filters.AllFilter;import weka.filters.Filter;import weka.filters.SimpleBatchFilter;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * A filter that applies filters on subsets of attributes and assembles the output into a new dataset. Attributes that are not covered by any of the ranges can be either retained or removed from the output. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> * * <pre> -D * Turns on output of debugging information.</pre> * * <pre> -F <classname [options]> * A filter to apply (can be specified multiple times).</pre> * * <pre> -R <range> * An attribute range (can be specified multiple times). * For each filter a range must be supplied. 'first' and 'last' * are valid indices.</pre> * * <pre> -U * Flag for leaving unused attributes out of the output, by default * these are included in the filter output.</pre> * <!-- options-end --> * * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision: 1.2 $ * @see weka.filters.StreamableFilter */public class PartitionedMultiFilter extends SimpleBatchFilter { /** for serialization */ private static final long serialVersionUID = -6293720886005713120L; /** The filters */ protected Filter m_Filters[] = {new AllFilter()}; /** The attribute ranges */ protected Range m_Ranges[] = {new Range("first-last")}; /** Whether unused attributes are left out of the output */ protected boolean m_RemoveUnused = false; /** the indices of the unused attributes */ protected int[] m_IndicesUnused = new int[0]; /** * Returns a string describing this filter * @return a description of the filter suitable for * displaying in the explorer/experimenter gui */ public String globalInfo() { return "A filter that applies filters on subsets of attributes and " + "assembles the output into a new dataset. Attributes that are " + "not covered by any of the ranges can be either retained or removed " + "from the output."; } /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); Enumeration enm = super.listOptions(); while (enm.hasMoreElements()) result.add(enm.nextElement()); result.addElement(new Option( "\tA filter to apply (can be specified multiple times).", "F", 1, "-F <classname [options]>")); result.addElement(new Option( "\tAn attribute range (can be specified multiple times).\n" + "\tFor each filter a range must be supplied. 'first' and 'last'\n" + "\tare valid indices.", "R", 1, "-R <range>")); result.addElement(new Option( "\tFlag for leaving unused attributes out of the output, by default\n" + "\tthese are included in the filter output.", "U", 0, "-U")); return result.elements(); } /** * Parses a list of options for this object. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -D * Turns on output of debugging information.</pre> * * <pre> -F <classname [options]> * A filter to apply (can be specified multiple times).</pre> * * <pre> -R <range> * An attribute range (can be specified multiple times). * For each filter a range must be supplied. 'first' and 'last' * are valid indices.</pre> * * <pre> -U * Flag for leaving unused attributes out of the output, by default * these are included in the filter output.</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String tmpStr; String classname; String[] options2; Vector objects; super.setOptions(options); setRemoveUnused(Utils.getFlag("U", options)); objects = new Vector(); while ((tmpStr = Utils.getOption("F", options)).length() != 0) { options2 = Utils.splitOptions(tmpStr); classname = options2[0]; options2[0] = ""; objects.add(Utils.forName(Filter.class, classname, options2)); } // at least one filter if (objects.size() == 0) objects.add(new AllFilter()); setFilters((Filter[]) objects.toArray(new Filter[objects.size()])); objects = new Vector(); while ((tmpStr = Utils.getOption("R", options)).length() != 0) { objects.add(new Range(tmpStr)); } // at least one Range if (objects.size() == 0) objects.add(new Range("first-last")); setRanges((Range[]) objects.toArray(new Range[objects.size()])); // is number of filters the same as ranges? checkDimensions(); } /** * Gets the current settings of the filter. * * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { Vector result; String[] options; int i; result = new Vector(); options = super.getOptions(); for (i = 0; i < options.length; i++) result.add(options[i]); if (getRemoveUnused()) result.add("-U"); for (i = 0; i < getFilters().length; i++) { result.add("-F"); result.add(getFilterSpec(getFilter(i))); } for (i = 0; i < getRanges().length; i++) { result.add("-R"); result.add("" + getRange(i).getRanges()); } return (String[]) result.toArray(new String[result.size()]); } /** * checks whether the dimensions of filters and ranges fit together * * @throws Exception if dimensions differ */ protected void checkDimensions() throws Exception { if (getFilters().length != getRanges().length) throw new IllegalArgumentException( "Number of filters (= " + getFilters().length + ") " + "and ranges (= " + getRanges().length + ") don't match!"); } /** * Returns the Capabilities of this filter. * * @return the capabilities of this object * @see Capabilities */ public Capabilities getCapabilities() { Capabilities result; if (getFilters().length == 0) result = super.getCapabilities(); else result = getFilters()[0].getCapabilities(); // disable attributes result.disable(Capability.STRING_ATTRIBUTES); result.disableDependency(Capability.STRING_ATTRIBUTES); result.disable(Capability.RELATIONAL_ATTRIBUTES); result.disableDependency(Capability.RELATIONAL_ATTRIBUTES); return result; } /** * Sets whether unused attributes (ones that are not covered by any of the * ranges) are removed from the output. * * @param value if true then the unused attributes get removed */ public void setRemoveUnused(boolean value) { m_RemoveUnused = value; } /** * Gets whether unused attributes (ones that are not covered by any of the * ranges) are removed from the output. * * @return true if unused attributes are removed */ public boolean getRemoveUnused() { return m_RemoveUnused; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String removeUnusedTipText() { return "If true then unused attributes (ones that are not covered by any " + "of the ranges) will be removed from the output."; } /** * Sets the list of possible filters to choose from. * Also resets the state of the filter (this reset doesn't affect the * options). * * @param filters an array of filters with all options set. * @see #reset() */ public void setFilters(Filter[] filters) { m_Filters = filters; reset(); } /** * Gets the list of possible filters to choose from. * * @return the array of Filters */ public Filter[] getFilters() { return m_Filters; } /** * Returns the tip text for this property * * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String filtersTipText() { return "The base filters to be used."; } /** * Gets a single filter from the set of available filters. * * @param index the index of the filter wanted * @return the Filter */ public Filter getFilter(int index) { return m_Filters[index]; } /** * returns the filter classname and the options as one string * * @param filter the filter to get the specs for * @return the classname plus options
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -