Example usage for weka.filters AllFilter AllFilter

List of usage examples for weka.filters AllFilter AllFilter

Introduction

In this page you can find the example usage for weka.filters AllFilter AllFilter.

Prototype

AllFilter

Source Link

Usage

From source file:adams.data.instancesanalysis.PCA.java

License:Open Source License

/**
 * Performs the actual analysis.//from w  ww. ja  va 2 s . c  o m
 *
 * @param data   the data to analyze
 * @return      null if successful, otherwise error message
 * @throws Exception   if analysis fails
 */
@Override
protected String doAnalyze(Instances data) throws Exception {
    String result;
    Remove remove;
    PublicPrincipalComponents pca;
    int i;
    Capabilities caps;
    PartitionedMultiFilter2 part;
    Range rangeUnsupported;
    Range rangeSupported;
    TIntList listNominal;
    Range rangeNominal;
    ArrayList<ArrayList<Double>> coeff;
    Instances filtered;
    SpreadSheet transformed;
    WekaInstancesToSpreadSheet conv;
    String colName;

    result = null;
    m_Loadings = null;
    m_Scores = null;

    if (!m_AttributeRange.isAllRange()) {
        if (isLoggingEnabled())
            getLogger().info("Filtering attribute range: " + m_AttributeRange.getRange());
        remove = new Remove();
        remove.setAttributeIndicesArray(m_AttributeRange.getIntIndices());
        remove.setInvertSelection(true);
        remove.setInputFormat(data);
        data = Filter.useFilter(data, remove);
    }
    if (isLoggingEnabled())
        getLogger().info("Performing PCA...");

    listNominal = new TIntArrayList();
    if (m_SkipNominal) {
        for (i = 0; i < data.numAttributes(); i++) {
            if (i == data.classIndex())
                continue;
            if (data.attribute(i).isNominal())
                listNominal.add(i);
        }
    }

    // check for unsupported attributes
    caps = new PublicPrincipalComponents().getCapabilities();
    m_Supported = new TIntArrayList();
    m_Unsupported = new TIntArrayList();
    for (i = 0; i < data.numAttributes(); i++) {
        if (!caps.test(data.attribute(i)) || (i == data.classIndex()) || (listNominal.contains(i)))
            m_Unsupported.add(i);
        else
            m_Supported.add(i);
    }
    data.setClassIndex(-1);

    m_NumAttributes = m_Supported.size();

    // the principal components will delete the attributes without any distinct values.
    // this checks which instances will be kept.
    m_Kept = new ArrayList<>();
    for (i = 0; i < m_Supported.size(); i++) {
        if (data.numDistinctValues(m_Supported.get(i)) > 1)
            m_Kept.add(m_Supported.get(i));
    }

    // build a model using the PublicPrincipalComponents
    pca = new PublicPrincipalComponents();
    pca.setMaximumAttributes(m_MaxAttributes);
    pca.setVarianceCovered(m_Variance);
    pca.setMaximumAttributeNames(m_MaxAttributeNames);
    part = null;
    if (m_Unsupported.size() > 0) {
        rangeUnsupported = new Range();
        rangeUnsupported.setMax(data.numAttributes());
        rangeUnsupported.setIndices(m_Unsupported.toArray());
        rangeSupported = new Range();
        rangeSupported.setMax(data.numAttributes());
        rangeSupported.setIndices(m_Supported.toArray());
        part = new PartitionedMultiFilter2();
        part.setFilters(new Filter[] { pca, new AllFilter(), });
        part.setRanges(new weka.core.Range[] { new weka.core.Range(rangeSupported.getRange()),
                new weka.core.Range(rangeUnsupported.getRange()), });
    }
    try {
        if (part != null)
            part.setInputFormat(data);
        else
            pca.setInputFormat(data);
    } catch (Exception e) {
        result = Utils.handleException(this, "Failed to set data format", e);
    }

    transformed = null;
    if (result == null) {
        try {
            if (part != null)
                filtered = weka.filters.Filter.useFilter(data, part);
            else
                filtered = weka.filters.Filter.useFilter(data, pca);
        } catch (Exception e) {
            result = Utils.handleException(this, "Failed to apply filter", e);
            filtered = null;
        }
        if (filtered != null) {
            conv = new WekaInstancesToSpreadSheet();
            conv.setInput(filtered);
            result = conv.convert();
            if (result == null) {
                transformed = (SpreadSheet) conv.getOutput();
                // shorten column names again
                if (part != null) {
                    for (i = 0; i < transformed.getColumnCount(); i++) {
                        colName = transformed.getColumnName(i);
                        colName = colName.replaceFirst("filtered-[0-9]*-", "");
                        transformed.getHeaderRow().getCell(i).setContentAsString(colName);
                    }
                }
            }
        }
    }

    if (result == null) {
        // get the coefficients from the filter
        m_Scores = transformed;
        coeff = pca.getCoefficients();
        m_Loadings = extractLoadings(data, coeff);
        m_Loadings.setName("Loadings for " + data.relationName());
    }

    return result;
}

From source file:adams.flow.transformer.WekaFilter.java

License:Open Source License

/**
 * Adds options to the internal list of options.
 */// ww w.  j a v  a 2s . c  o m
@Override
public void defineOptions() {
    super.defineOptions();

    m_OptionManager.add("filter", "filter", new AllFilter());

    m_OptionManager.add("model-loading-type", "modelLoadingType", ModelLoadingType.AUTO);

    m_OptionManager.add("model", "modelFile", new PlaceholderFile("."));

    m_OptionManager.add("source", "modelActor", new CallableActorReference());

    m_OptionManager.add("storage", "modelStorage", new StorageName());

    m_OptionManager.add("init-once", "initializeOnce", false);

    m_OptionManager.add("keep", "keepRelationName", false);

    m_OptionManager.add("output-container", "outputContainer", false);
}

From source file:adams.gui.menu.BatchFilterDatasets.java

License:Open Source License

/**
 * Launches the functionality of the menu item.
 *//*from   w w w  .  ja v a  2 s  . co  m*/
@Override
public void launch() {
    final WizardPane wizard;
    WekaSelectMultipleDatasetsPage infiles;
    ParameterPanelPage paramsFilter;
    ParameterPanelPage paramsOutput;
    Properties props;
    WekaSelectDatasetPage outfile;
    FinalPage finalpage;
    Filter filter;
    final ChildFrame frame;

    // configuration
    filter = new AllFilter();

    // wizard
    wizard = new WizardPane();
    wizard.setCustomFinishText("Filter");

    infiles = new WekaSelectMultipleDatasetsPage("Input");
    infiles.setDescription("Select the Weka datasets to batch-filter.\n" + "You have to choose at least two.\n"
            + "The first dataset is used to set up the filter, all subsequent files get filtered with this set up.");
    infiles.setPageCheck(new PageCheck() {
        @Override
        public boolean checkPage(AbstractWizardPage page) {
            Properties props = page.getProperties();
            try {
                String[] files = OptionUtils
                        .splitOptions(props.getProperty(WekaSelectMultipleDatasetsPage.KEY_FILES));
                return (files.length >= 2);
            } catch (Exception e) {
                getLogger().log(Level.SEVERE, "Failed to obtain files:", e);
            }
            return false;
        }
    });
    wizard.addPage(infiles);

    paramsFilter = new ParameterPanelPage("Filter");
    paramsFilter.setDescription("Set up the filter that is used to filter the datasets.\n"
            + "If no class attribute is to be set, simply empty the 'Class' property. "
            + "You can use 'first' and 'last' as well as 1-based indices.");
    paramsFilter.getParameterPanel().addPropertyType("Filter", PropertyType.OBJECT_EDITOR);
    paramsFilter.getParameterPanel().setChooser("Filter",
            new GenericObjectEditorPanel(weka.filters.Filter.class, filter, true));
    paramsFilter.getParameterPanel().addPropertyType("Class", PropertyType.STRING);
    paramsFilter.getParameterPanel().addPropertyType("Keep relation name", PropertyType.BOOLEAN);
    paramsFilter.getParameterPanel().setPropertyOrder(new String[] { "Filter", "Class", "Keep relation name" });
    props = new Properties();
    props.setProperty("Filter", OptionUtils.getCommandLine(filter));
    props.setProperty("Class", "last");
    props.setBoolean("Keep relation name", false);
    paramsFilter.getParameterPanel().setProperties(props);
    wizard.addPage(paramsFilter);

    paramsOutput = new ParameterPanelPage("Output");
    paramsOutput.setDescription(
            "Select the directory where to place the generated datasets in ARFF format (the input file names get reused for the output).");
    paramsOutput.getParameterPanel().addPropertyType("Output", PropertyType.DIRECTORY_ABSOLUTE);
    props = new Properties();
    props.setPath("Output", ".");
    paramsOutput.getParameterPanel().setProperties(props);
    wizard.addPage(paramsOutput);

    finalpage = new FinalPage();
    finalpage.setLogo(null);
    finalpage.setDescription("<html><h2>Ready</h2>Please click on <b>Filter</b> to start the process.</html>");
    wizard.addPage(finalpage);
    frame = createChildFrame(wizard, GUIHelper.getDefaultDialogDimension());
    wizard.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!e.getActionCommand().equals(WizardPane.ACTION_FINISH)) {
                frame.dispose();
                return;
            }
            Properties props = wizard.getProperties(false);
            String[] files = null;
            Filter filter = null;
            String classIndex = null;
            File outdir = null;
            boolean keep = false;
            try {
                files = OptionUtils.splitOptions(props.getProperty(WekaSelectMultipleDatasetsPage.KEY_FILES));
                filter = (Filter) OptionUtils.forAnyCommandLine(Filter.class, props.getProperty("Filter"));
                classIndex = props.getProperty("Class");
                outdir = new File(props.getPath("Output"));
                keep = props.getBoolean("Keep relation name");
            } catch (Exception ex) {
                GUIHelper.showErrorMessage(getOwner(),
                        "Failed to get setup from wizard!\n" + Utils.throwableToString(ex));
                return;
            }
            batchFilter(frame, files, filter, classIndex, keep, outdir);
        }
    });
}

From source file:meka.experiment.statisticsexporters.WekaFilter.java

License:Open Source License

/**
 * Returns the default keys used for aggregation.
 *
 * @return          the default
 */
protected Filter getDefaultFilter() {
    return new AllFilter();
}

From source file:zhaop.textmining.proj.MultiFilter.java

License:Open Source License

/**
 * Parses a list of options for this object.
 * <p/>//from ww  w .  j a  v  a  2  s . c o  m
 * 
 * <!-- options-start --> Valid options are:
 * <p/>
 * 
 * <pre>
 * -D
 *  Turns on output of debugging information.
 * </pre>
 * 
 * <pre>
 * -F &lt;classname [options]&gt;
 *  A filter to apply (can be specified multiple times).
 * </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 filter;
    String[] options2;
    Vector filters;

    super.setOptions(options);

    filters = new Vector();
    while ((tmpStr = Utils.getOption("F", options)).length() != 0) {
        options2 = Utils.splitOptions(tmpStr);
        filter = options2[0];
        options2[0] = "";
        filters.add(Utils.forName(Filter.class, filter, options2));
    }

    // at least one filter
    if (filters.size() == 0)
        filters.add(new AllFilter());

    setFilters((Filter[]) filters.toArray(new Filter[filters.size()]));
}