Example usage for weka.classifiers AbstractClassifier setOptions

List of usage examples for weka.classifiers AbstractClassifier setOptions

Introduction

In this page you can find the example usage for weka.classifiers AbstractClassifier setOptions.

Prototype

@Override
public void setOptions(String[] options) throws Exception 

Source Link

Document

Parses a given list of options.

Usage

From source file:trainableSegmentation.Weka_Segmentation.java

License:GNU General Public License

/**
 * Show advanced settings dialog/*from  ww  w  .  ja v  a2  s. com*/
 *
 * @return false when canceled
 */
public boolean showSettingsDialog() {
    GenericDialogPlus gd = new GenericDialogPlus("Segmentation settings");

    final boolean[] oldEnableFeatures = wekaSegmentation.getEnabledFeatures();

    gd.addMessage("Training features:");
    final int rows = (int) Math.round(FeatureStack.availableFeatures.length / 2.0);
    gd.addCheckboxGroup(rows, 2, FeatureStack.availableFeatures, oldEnableFeatures);

    if (wekaSegmentation.getLoadedTrainingData() != null) {
        final Vector<Checkbox> v = gd.getCheckboxes();
        for (Checkbox c : v)
            c.setEnabled(false);
        gd.addMessage("WARNING: no features are selectable while using loaded data");
    }

    // Expected membrane thickness
    gd.addNumericField("Membrane thickness:", wekaSegmentation.getMembraneThickness(), 0);
    // Membrane patch size
    gd.addNumericField("Membrane patch size:", wekaSegmentation.getMembranePatchSize(), 0);
    // Field of view
    gd.addNumericField("Minimum sigma:", wekaSegmentation.getMinimumSigma(), 1);
    gd.addNumericField("Maximum sigma:", wekaSegmentation.getMaximumSigma(), 1);

    if (wekaSegmentation.getLoadedTrainingData() != null) {
        for (int i = 0; i < 4; i++)
            ((TextField) gd.getNumericFields().get(i)).setEnabled(false);
    }

    gd.addMessage("Classifier options:");

    // Add Weka panel for selecting the classifier and its options
    GenericObjectEditor m_ClassifierEditor = new GenericObjectEditor();
    PropertyPanel m_CEPanel = new PropertyPanel(m_ClassifierEditor);
    m_ClassifierEditor.setClassType(Classifier.class);
    m_ClassifierEditor.setValue(wekaSegmentation.getClassifier());

    // add classifier editor panel
    gd.addComponent(m_CEPanel, GridBagConstraints.HORIZONTAL, 1);

    Object c = (Object) m_ClassifierEditor.getValue();
    String originalOptions = "";
    String originalClassifierName = c.getClass().getName();
    if (c instanceof OptionHandler) {
        originalOptions = Utils.joinOptions(((OptionHandler) c).getOptions());
    }

    gd.addMessage("Class names:");
    for (int i = 0; i < wekaSegmentation.getNumOfClasses(); i++)
        gd.addStringField("Class " + (i + 1), wekaSegmentation.getClassLabel(i), 15);

    gd.addMessage("Advanced options:");
    gd.addCheckbox("Homogenize classes", wekaSegmentation.doHomogenizeClasses());
    gd.addButton("Save feature stack", new SaveFeatureStackButtonListener(
            "Select location to save feature stack", wekaSegmentation.getFeatureStackArray()));
    gd.addSlider("Result overlay opacity", 0, 100, win.overlayOpacity);
    gd.addHelp("http://fiji.sc/wiki/Trainable_Segmentation_Plugin");

    gd.showDialog();

    if (gd.wasCanceled())
        return false;

    final int numOfFeatures = FeatureStack.availableFeatures.length;

    final boolean[] newEnableFeatures = new boolean[numOfFeatures];

    boolean featuresChanged = false;

    // Read checked features and check if any of them changed
    for (int i = 0; i < numOfFeatures; i++) {
        newEnableFeatures[i] = gd.getNextBoolean();
        if (newEnableFeatures[i] != oldEnableFeatures[i]) {
            featuresChanged = true;
            // Macro recording
            record(SET_FEATURE,
                    new String[] { FeatureStack.availableFeatures[i] + "=" + newEnableFeatures[i] });
        }
    }
    if (featuresChanged) {
        wekaSegmentation.setEnabledFeatures(newEnableFeatures);
    }

    // Membrane thickness
    final int newThickness = (int) gd.getNextNumber();
    if (newThickness != wekaSegmentation.getMembraneThickness()) {
        featuresChanged = true;
        wekaSegmentation.setMembraneThickness(newThickness);
        // Macro recording
        record(SET_MEMBRANE_THICKNESS, new String[] { Integer.toString(newThickness) });
    }
    // Membrane patch size
    final int newPatch = (int) gd.getNextNumber();
    if (newPatch != wekaSegmentation.getMembranePatchSize()) {
        featuresChanged = true;
        // Macro recording
        record(SET_MEMBRANE_PATCH, new String[] { Integer.toString(newPatch) });
        wekaSegmentation.setMembranePatchSize(newPatch);
    }
    // Field of view (minimum and maximum sigma/radius for the filters)
    final float newMinSigma = (float) gd.getNextNumber();
    if (newMinSigma != wekaSegmentation.getMinimumSigma() && newMinSigma > 0) {
        featuresChanged = true;
        // Macro recording
        record(SET_MINIMUM_SIGMA, new String[] { Float.toString(newMinSigma) });
        wekaSegmentation.setMinimumSigma(newMinSigma);
    }

    final float newMaxSigma = (float) gd.getNextNumber();
    if (newMaxSigma != wekaSegmentation.getMaximumSigma()
            && newMaxSigma >= wekaSegmentation.getMinimumSigma()) {
        featuresChanged = true;
        // Macro recording
        record(SET_MAXIMUM_SIGMA, new String[] { Float.toString(newMaxSigma) });
        wekaSegmentation.setMaximumSigma(newMaxSigma);
    }
    if (wekaSegmentation.getMinimumSigma() > wekaSegmentation.getMaximumSigma()) {
        IJ.error("Error in the field of view parameters: they will be reset to default values");
        wekaSegmentation.setMinimumSigma(0f);
        wekaSegmentation.setMaximumSigma(16f);
    }

    // Set classifier and options
    c = (Object) m_ClassifierEditor.getValue();
    String options = "";
    final String[] optionsArray = ((OptionHandler) c).getOptions();
    if (c instanceof OptionHandler) {
        options = Utils.joinOptions(optionsArray);
    }
    //System.out.println("Classifier after choosing: " + c.getClass().getName() + " " + options);
    if (originalClassifierName.equals(c.getClass().getName()) == false
            || originalOptions.equals(options) == false) {
        AbstractClassifier cls;
        try {
            cls = (AbstractClassifier) (c.getClass().newInstance());
            cls.setOptions(optionsArray);
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }

        wekaSegmentation.setClassifier(cls);
        // Macro recording
        record(SET_CLASSIFIER, new String[] { c.getClass().getName(), options });

        IJ.log("Current classifier: " + c.getClass().getName() + " " + options);
    }

    boolean classNameChanged = false;
    for (int i = 0; i < wekaSegmentation.getNumOfClasses(); i++) {
        String s = gd.getNextString();
        if (null == s || 0 == s.length()) {
            IJ.log("Invalid name for class " + (i + 1));
            continue;
        }
        s = s.trim();
        if (!s.equals(wekaSegmentation.getClassLabel(i))) {
            if (0 == s.toLowerCase().indexOf("add to "))
                s = s.substring(7);

            wekaSegmentation.setClassLabel(i, s);
            classNameChanged = true;
            addExampleButton[i].setText("Add to " + s);
            // Macro recording
            record(CHANGE_CLASS_NAME, new String[] { Integer.toString(i), s });
        }
    }

    // Update flag to homogenize number of class instances      
    final boolean homogenizeClasses = gd.getNextBoolean();
    if (wekaSegmentation.doHomogenizeClasses() != homogenizeClasses) {
        wekaSegmentation.setDoHomogenizeClasses(homogenizeClasses);
        // Macro recording
        record(SET_HOMOGENIZATION, new String[] { Boolean.toString(homogenizeClasses) });
    }

    // Update result overlay alpha
    final int newOpacity = (int) gd.getNextNumber();
    if (newOpacity != win.overlayOpacity) {
        win.overlayOpacity = newOpacity;
        win.overlayAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, win.overlayOpacity / 100f);
        win.resultOverlay.setComposite(win.overlayAlpha);

        // Macro recording
        record(SET_OPACITY, new String[] { Integer.toString(win.overlayOpacity) });

        if (showColorOverlay)
            displayImage.updateAndDraw();
    }

    // If there is a change in the class names,
    // the data set (instances) must be updated.
    if (classNameChanged) {
        // Pack window to update buttons
        win.pack();
    }

    // Update feature stack if necessary
    if (featuresChanged) {
        // Force features to be updated
        wekaSegmentation.setFeaturesDirty();
    } else // This checks if the feature stacks were updated while using the save feature stack button
    if (wekaSegmentation.getFeatureStackArray().isEmpty() == false
            && wekaSegmentation.getFeatureStackArray().getReferenceSliceIndex() != -1)
        wekaSegmentation.setUpdateFeatures(false);

    return true;
}

From source file:trainableSegmentation.Weka_Segmentation.java

License:GNU General Public License

/**
 * Set classifier for current segmentation
 * //from   w ww  .j  a  va 2  s.  c o m
 * @param classifierName classifier name with complete package information
 * @param options classifier options
 */
public static void setClassifier(String classifierName, String options) {
    final ImageWindow iw = WindowManager.getCurrentImage().getWindow();
    if (iw instanceof CustomWindow) {
        final CustomWindow win = (CustomWindow) iw;
        final WekaSegmentation wekaSegmentation = win.getWekaSegmentation();

        try {
            AbstractClassifier cls = (AbstractClassifier) (Class.forName(classifierName).newInstance());
            cls.setOptions(options.split(" "));
            wekaSegmentation.setClassifier(cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}