Example usage for weka.gui GenericObjectEditor setValue

List of usage examples for weka.gui GenericObjectEditor setValue

Introduction

In this page you can find the example usage for weka.gui GenericObjectEditor setValue.

Prototype

@Override
public void setValue(Object o) 

Source Link

Document

Sets the current Object.

Usage

From source file:moa.gui.WEKAClassOptionEditComponent.java

License:Open Source License

public void editObject() {
    final GenericObjectEditor goe = new GenericObjectEditor(true);
    goe.setClassType(editedOption.getRequiredType());
    try {//from  w  w  w  . j a va  2  s.  c  o  m
        String[] options = Utils.splitOptions(editedOption.getValueAsCLIString());
        String classname = options[0];
        options[0] = "";
        Object obj = Class.forName(classname).newInstance();
        if (obj instanceof weka.core.OptionHandler) {
            ((weka.core.OptionHandler) obj).setOptions(options);
        }
        goe.setValue(obj);
        ((GOEPanel) goe.getCustomEditor()).addOkListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Object obj = goe.getValue();
                String s = obj.getClass().getName();
                if (obj instanceof weka.core.OptionHandler) {
                    s += " " + Utils.joinOptions(((weka.core.OptionHandler) obj).getOptions());
                }
                setEditState(s.trim());
            }
        });
        PropertyDialog dialog;
        if (PropertyDialog.getParentDialog(this) != null) {
            dialog = new PropertyDialog(PropertyDialog.getParentDialog(this), goe);
        } else {
            dialog = new PropertyDialog(PropertyDialog.getParentFrame(this), goe);
        }
        dialog.setModal(true);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:sirius.trainer.step3.SelectFeaturePane.java

License:Open Source License

/**
 * Pops up generic object editor with list of conversion filters
 *
 * @param f the File/*from   ww w  .j  a va 2 s. c  o  m*/
 */
private void converterQuery(final File f) {
    final GenericObjectEditor convEd = new GenericObjectEditor(true);
    try {
        convEd.setClassType(weka.core.converters.Loader.class);
        convEd.setValue(new weka.core.converters.CSVLoader());
        ((GenericObjectEditor.GOEPanel) convEd.getCustomEditor()).addOkListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tryConverter((Loader) convEd.getValue(), f);
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    //PropertyDialog pd = new PropertyDialog(convEd, 100, 100);
}

From source file:tr.gov.ulakbim.jDenetX.gui.WEKAClassOptionEditComponent.java

License:Open Source License

public void editObject() {
    final GenericObjectEditor goe = new GenericObjectEditor(true);
    goe.setClassType(editedOption.getRequiredType());
    try {// w  ww.  j a va2 s.  c o  m
        String[] options = Utils.splitOptions(editedOption.getValueAsCLIString());
        String classname = options[0];
        options[0] = "";
        Object obj = Class.forName(classname).newInstance();
        if (obj instanceof weka.core.OptionHandler)
            ((weka.core.OptionHandler) obj).setOptions(options);
        goe.setValue(obj);
        ((GOEPanel) goe.getCustomEditor()).addOkListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object obj = goe.getValue();
                String s = obj.getClass().getName();
                if (obj instanceof weka.core.OptionHandler)
                    s += " " + Utils.joinOptions(((weka.core.OptionHandler) obj).getOptions());
                setEditState(s.trim());
            }
        });
        PropertyDialog dialog;
        if (PropertyDialog.getParentDialog(this) != null)
            dialog = new PropertyDialog(PropertyDialog.getParentDialog(this), goe);
        else
            dialog = new PropertyDialog(PropertyDialog.getParentFrame(this), goe);
        dialog.setModal(true);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:trainableSegmentation.Weka_Segmentation.java

License:GNU General Public License

/**
 * Show advanced settings dialog//from  w  w w .  j a v a2  s  .  co  m
 *
 * @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;
}