Search in sources :

Example 16 with DoubleTextField

use of edu.cmu.tetradapp.util.DoubleTextField in project tetrad by cmu-phil.

the class SemUpdaterEditor method getUpdatePanel.

private Box getUpdatePanel() {
    final SemEvidence evidence = semUpdater.getEvidence();
    focusTraversalOrder.clear();
    Box b = Box.createVerticalBox();
    Box b0 = Box.createHorizontalBox();
    b0.add(new JLabel("<html>" + "In the list below, specify values for variables you have evidence " + "<br>for. Click the 'Do Update Now' button to view updated model."));
    b0.add(Box.createHorizontalGlue());
    b.add(b0);
    b.add(Box.createVerticalStrut(10));
    Box d = Box.createHorizontalBox();
    d.add(new JLabel("Variable = value"));
    d.add(Box.createHorizontalGlue());
    d.add(new JLabel("Manipulated"));
    b.add(d);
    for (int i = 0; i < evidence.getNumNodes(); i++) {
        Box c = Box.createHorizontalBox();
        SemIm semIm = evidence.getSemIm();
        Node node = semIm.getVariableNodes().get(i);
        String name = node.getName();
        JLabel label = new JLabel(name + " =  ") {

            private static final long serialVersionUID = 820570350956700782L;

            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
        };
        c.add(label);
        double mean = evidence.getProposition().getValue(i);
        final DoubleTextField field = new DoubleTextField(mean, 5, NumberFormatUtil.getInstance().getNumberFormat());
        field.setFilter((value, oldValue) -> {
            try {
                final int nodeIndex = labels.get(field);
                if (Double.isNaN(value) && evidence.isManipulated(nodeIndex)) {
                    throw new IllegalArgumentException();
                }
                evidence.getProposition().setValue(nodeIndex, value);
                SemIm updatedSem = semUpdater.getUpdatedSemIm();
                semImEditor.displaySemIm(updatedSem, semImEditor.getTabSelectionIndex(), semImEditor.getMatrixSelection());
                return value;
            } catch (IllegalArgumentException e) {
                return oldValue;
            }
        });
        labels.put(field, i);
        variablesToTextFields.put(i, field);
        focusTraversalOrder.add(field);
        c.add(field);
        c.add(Box.createHorizontalStrut(2));
        c.add(Box.createHorizontalGlue());
        JCheckBox checkbox = new JCheckBox() {

            private static final long serialVersionUID = -3808843047563493212L;

            @Override
            public Dimension getMaximumSize() {
                return getPreferredSize();
            }
        };
        checkbox.setSelected(evidence.isManipulated(i));
        checkBoxesToVariables.put(checkbox, i);
        variablesToCheckboxes.put(i, checkbox);
        checkbox.addActionListener((e) -> {
            JCheckBox chkbox = (JCheckBox) e.getSource();
            boolean selected = chkbox.isSelected();
            Integer o = checkBoxesToVariables.get(chkbox);
            // If no value has been set for this variable, set it to
            // the mean.
            double value = evidence.getProposition().getValue(o);
            if (Double.isNaN(value)) {
                DoubleTextField dblTxtField = variablesToTextFields.get(o);
                SemIm semIM = semUpdater.getSemIm();
                Node varNode = semIM.getVariableNodes().get(o);
                double semIMMean = semIM.getMean(varNode);
                dblTxtField.setValue(semIMMean);
            }
            semUpdater.getEvidence().setManipulated(o, selected);
            SemIm updatedSem = semUpdater.getUpdatedSemIm();
            semImEditor.displaySemIm(updatedSem, semImEditor.getTabSelectionIndex(), semImEditor.getMatrixSelection());
        });
        checkbox.setBackground(Color.WHITE);
        checkbox.setBorder(null);
        c.add(checkbox);
        c.setMaximumSize(new Dimension(1000, 30));
        b.add(c);
    }
    b.add(Box.createVerticalGlue());
    Box b2 = Box.createHorizontalBox();
    b2.add(Box.createHorizontalGlue());
    JButton button = new JButton("Do Update Now");
    button.addActionListener((e) -> {
        SemIm updatedSem = semUpdater.getUpdatedSemIm();
        semImEditor.displaySemIm(updatedSem, semImEditor.getTabSelectionIndex(), semImEditor.getMatrixSelection());
        semUpdater.setEvidence(new SemEvidence(updatedSem));
    });
    b2.add(button);
    b.add(b2);
    b.setBorder(new EmptyBorder(5, 5, 5, 5));
    setFocusTraversalPolicy(new FocusTraversalPolicy() {

        @Override
        public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {
            int index = focusTraversalOrder.indexOf(aComponent);
            int size = focusTraversalOrder.size();
            if (index != -1) {
                return focusTraversalOrder.get((index + 1) % size);
            } else {
                return getFirstComponent(focusCycleRoot);
            }
        }

        @Override
        public Component getComponentBefore(Container focusCycleRoot, Component aComponent) {
            int index = focusTraversalOrder.indexOf(aComponent);
            int size = focusTraversalOrder.size();
            if (index != -1) {
                return focusTraversalOrder.get((index - 1) % size);
            } else {
                return getFirstComponent(focusCycleRoot);
            }
        }

        @Override
        public Component getFirstComponent(Container focusCycleRoot) {
            return focusTraversalOrder.getFirst();
        }

        @Override
        public Component getLastComponent(Container focusCycleRoot) {
            return focusTraversalOrder.getLast();
        }

        @Override
        public Component getDefaultComponent(Container focusCycleRoot) {
            return getFirstComponent(focusCycleRoot);
        }
    });
    setFocusCycleRoot(true);
    return b;
}
Also used : SemEvidence(edu.cmu.tetrad.sem.SemEvidence) DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) Node(edu.cmu.tetrad.graph.Node) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) FocusTraversalPolicy(java.awt.FocusTraversalPolicy) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) Dimension(java.awt.Dimension) JCheckBox(javax.swing.JCheckBox) Container(java.awt.Container) EmptyBorder(javax.swing.border.EmptyBorder) Component(java.awt.Component) SemIm(edu.cmu.tetrad.sem.SemIm)

Example 17 with DoubleTextField

use of edu.cmu.tetradapp.util.DoubleTextField in project tetrad by cmu-phil.

the class LingSearchEditor method getToolbar.

/**
 * Construct the toolbar panel.
 */
protected JPanel getToolbar() {
    JPanel toolbar = new JPanel();
    getExecuteButton().setText("Execute*");
    getExecuteButton().addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            execute();
        }
    });
    Box b1 = Box.createVerticalBox();
    Box b21 = Box.createVerticalBox();
    Box b211 = Box.createHorizontalBox();
    b211.add(new JLabel("Threshold "));
    Parameters params = getAlgorithmRunner().getParams();
    double pruneFactor = params.getDouble("threshold", 0.5);
    DoubleTextField field = new DoubleTextField(pruneFactor, 8, NumberFormatUtil.getInstance().getNumberFormat());
    field.setFilter(new DoubleTextField.Filter() {

        public double filter(double value, double oldValue) {
            if (value > 0.0) {
                setThreshold(value);
                return value;
            }
            return oldValue;
        }
    });
    b211.add(field);
    b21.add(b211);
    JPanel paramsPanel = new JPanel();
    paramsPanel.add(b21);
    paramsPanel.setBorder(new TitledBorder("Parameters"));
    b1.add(paramsPanel);
    b1.add(Box.createVerticalStrut(10));
    Box b2 = Box.createHorizontalBox();
    b2.add(Box.createGlue());
    b2.add(getExecuteButton());
    b1.add(b2);
    b1.add(Box.createVerticalStrut(10));
    if (getAlgorithmRunner().getDataModel() instanceof DataSet) {
        Box b3 = Box.createHorizontalBox();
        b3.add(Box.createGlue());
        b1.add(b3);
    }
    if (getAlgorithmRunner().getParams() instanceof Parameters) {
        b1.add(Box.createVerticalStrut(5));
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalGlue());
        b1.add(hBox);
        b1.add(Box.createVerticalStrut(5));
    }
    Box b4 = Box.createHorizontalBox();
    JLabel label = new JLabel("<html>" + "*Please note that some" + "<br>searches may take a" + "<br>long time to complete." + "</html>");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setVerticalAlignment(SwingConstants.CENTER);
    label.setBorder(new TitledBorder(""));
    b4.add(label);
    b1.add(Box.createVerticalStrut(10));
    b1.add(b4);
    toolbar.add(b1);
    return toolbar;
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) ActionListener(java.awt.event.ActionListener) DataSet(edu.cmu.tetrad.data.DataSet) ActionEvent(java.awt.event.ActionEvent) TitledBorder(javax.swing.border.TitledBorder)

Example 18 with DoubleTextField

use of edu.cmu.tetradapp.util.DoubleTextField in project tetrad by cmu-phil.

the class LingamSearchEditor method getToolbar.

/**
 * Construct the toolbar panel.
 */
protected JPanel getToolbar() {
    JPanel toolbar = new JPanel();
    getExecuteButton().setText("Execute*");
    getExecuteButton().addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            execute();
        }
    });
    Box b1 = Box.createVerticalBox();
    Box b21 = Box.createVerticalBox();
    Box b211 = Box.createHorizontalBox();
    b211.add(new JLabel("Prune Factor "));
    Parameters params = getAlgorithmRunner().getParams();
    double pruneFactor = params.getDouble("pruneFactor", 1.0);
    DoubleTextField field = new DoubleTextField(pruneFactor, 8, NumberFormatUtil.getInstance().getNumberFormat());
    field.setFilter(new DoubleTextField.Filter() {

        public double filter(double value, double oldValue) {
            if (value > 0.0) {
                setPruneFactor(value);
                return value;
            }
            return oldValue;
        }
    });
    b211.add(field);
    b21.add(b211);
    JPanel paramsPanel = new JPanel();
    paramsPanel.add(b21);
    paramsPanel.setBorder(new TitledBorder("Parameters"));
    b1.add(paramsPanel);
    b1.add(Box.createVerticalStrut(10));
    Box b2 = Box.createHorizontalBox();
    b2.add(Box.createGlue());
    b2.add(getExecuteButton());
    b1.add(b2);
    b1.add(Box.createVerticalStrut(10));
    if (getAlgorithmRunner().getDataModel() instanceof DataSet) {
        Box b3 = Box.createHorizontalBox();
        b3.add(Box.createGlue());
        b1.add(b3);
    }
    if (getAlgorithmRunner().getParams() instanceof Parameters) {
        b1.add(Box.createVerticalStrut(5));
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalGlue());
        b1.add(hBox);
        b1.add(Box.createVerticalStrut(5));
    }
    Box b4 = Box.createHorizontalBox();
    JLabel label = new JLabel("<html>" + "*Please note that some" + "<br>searches may take a" + "<br>long time to complete." + "</html>");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setVerticalAlignment(SwingConstants.CENTER);
    label.setBorder(new TitledBorder(""));
    b4.add(label);
    b1.add(Box.createVerticalStrut(10));
    b1.add(b4);
    toolbar.add(b1);
    return toolbar;
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) ActionListener(java.awt.event.ActionListener) DataSet(edu.cmu.tetrad.data.DataSet) ActionEvent(java.awt.event.ActionEvent) TitledBorder(javax.swing.border.TitledBorder)

Example 19 with DoubleTextField

use of edu.cmu.tetradapp.util.DoubleTextField in project tetrad by cmu-phil.

the class MbSearchParamEditor method setup.

public void setup() {
    /*
      The variable names from the object being searched over (usually data).
     */
    List<String> varNames = (List<String>) params().get("varNames", null);
    if (varNames == null) {
        varNames = getVarsFromData(parentModels);
        if (varNames == null) {
            varNames = getVarsFromGraph(parentModels);
        }
        if (varNames == null) {
            throw new IllegalStateException("Variables are not accessible.");
        }
        params().set("varNames", varNames);
    }
    setBorder(new MatteBorder(10, 10, 10, 10, super.getBackground()));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    // Set up components.
    String[] variableNames = varNames.toArray(new String[varNames.size()]);
    Arrays.sort(variableNames);
    JComboBox varsBox = new JComboBox(variableNames);
    varsBox.setMaximumSize(new Dimension(80, 24));
    varsBox.setPreferredSize(new Dimension(80, 24));
    String targetName = params.getString("targetName", null);
    if (!Arrays.asList(variableNames).contains(targetName)) {
        params.set("targetName", variableNames[0]);
        targetName = params.getString("targetName", null);
    }
    if (targetName == null) {
        targetName = (String) varsBox.getSelectedItem();
    } else {
        varsBox.setSelectedItem(targetName);
    }
    setTargetName(targetName);
    params().set("targetName", targetName());
    varsBox.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            JComboBox box = (JComboBox) e.getSource();
            setTargetName((String) box.getSelectedItem());
            params().set("targetName", targetName());
        }
    });
    DoubleTextField alphaField = new DoubleTextField(params.getDouble("alpha", 0.001), 8, new DecimalFormat("0.0########"));
    alphaField.setFilter(new DoubleTextField.Filter() {

        public double filter(double value, double oldValue) {
            try {
                params().set("alpha", 0.001);
                Preferences.userRoot().putDouble("alpha", params().getDouble("alpha", 0.001));
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    IntTextField pcDepthField = new IntTextField(params().getInt("depth", -1), 4);
    pcDepthField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                params().set("depth", value);
                Preferences.userRoot().putInt("pcDepth", params().getInt("depth", -1));
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    JButton knowledgeButton = new JButton("Edit");
    knowledgeButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            openKnowledgeEditor();
        }
    });
    JCheckBox preventCycles = new JCheckBox();
    preventCycles.setSelected(params.getBoolean("aggressivelyPreventCycles", false));
    preventCycles.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JCheckBox box = (JCheckBox) e.getSource();
            Parameters p = params;
            p.set("aggressivelyPreventCycles", box.isSelected());
        }
    });
    // Do Layout.
    Box hBox = Box.createHorizontalBox();
    hBox.add(new JLabel("Aggressively Prevent Cycles:"));
    hBox.add(Box.createHorizontalGlue());
    hBox.add(preventCycles);
    add(hBox);
    add(Box.createVerticalStrut(5));
    Box b0 = Box.createHorizontalBox();
    b0.add(new JLabel("Target:"));
    b0.add(Box.createRigidArea(new Dimension(10, 0)));
    b0.add(Box.createHorizontalGlue());
    b0.add(varsBox);
    add(b0);
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Alpha:"));
    b1.add(Box.createRigidArea(new Dimension(10, 0)));
    b1.add(Box.createHorizontalGlue());
    b1.add(alphaField);
    add(b1);
    Box b2 = Box.createHorizontalBox();
    b2.add(new JLabel("Depth:"));
    b2.add(Box.createRigidArea(new Dimension(10, 0)));
    b2.add(Box.createHorizontalGlue());
    b2.add(pcDepthField);
    add(b2);
// Box b3 = Box.createHorizontalBox();
// b3.add(new JLabel("Knowledge:"));
// b3.add(Box.createRigidArea(new Dimension(10, 0)));
// b3.add(Box.createHorizontalGlue());
// b3.add(knowledgeButton);
// 
// 
// add(b3);
}
Also used : ItemEvent(java.awt.event.ItemEvent) ActionEvent(java.awt.event.ActionEvent) DecimalFormat(java.text.DecimalFormat) MatteBorder(javax.swing.border.MatteBorder) IntTextField(edu.cmu.tetradapp.util.IntTextField) ArrayList(java.util.ArrayList) List(java.util.List) DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) Parameters(edu.cmu.tetrad.util.Parameters) ActionListener(java.awt.event.ActionListener) ItemListener(java.awt.event.ItemListener)

Example 20 with DoubleTextField

use of edu.cmu.tetradapp.util.DoubleTextField in project tetrad by cmu-phil.

the class BoxCoxParamsEditor method setup.

/**
 * Builds the panel.
 */
public void setup() {
    DoubleTextField lambda = new DoubleTextField(params.getDouble("lambda", 0), 8, new DecimalFormat("0.0"));
    lambda.setFilter(new DoubleTextField.Filter() {

        public double filter(double value, double oldValue) {
            if (value >= 0) {
                params.set("lambda", value);
                return value;
            } else {
                return oldValue;
            }
        }
    });
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Lambda: "));
    b1.add(Box.createHorizontalGlue());
    b1.add(Box.createHorizontalStrut(15));
    b1.add(lambda);
    b1.setBorder(new EmptyBorder(10, 10, 10, 10));
    add(b1, BorderLayout.CENTER);
}
Also used : DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) DecimalFormat(java.text.DecimalFormat) EmptyBorder(javax.swing.border.EmptyBorder)

Aggregations

DoubleTextField (edu.cmu.tetradapp.util.DoubleTextField)26 ActionEvent (java.awt.event.ActionEvent)10 ActionListener (java.awt.event.ActionListener)10 DecimalFormat (java.text.DecimalFormat)7 IntTextField (edu.cmu.tetradapp.util.IntTextField)6 ArrayList (java.util.ArrayList)6 Parameters (edu.cmu.tetrad.util.Parameters)5 DataModel (edu.cmu.tetrad.data.DataModel)4 Node (edu.cmu.tetrad.graph.Node)4 DataWrapper (edu.cmu.tetradapp.model.DataWrapper)4 List (java.util.List)4 EmptyBorder (javax.swing.border.EmptyBorder)4 MatteBorder (javax.swing.border.MatteBorder)4 TitledBorder (javax.swing.border.TitledBorder)4 DataSet (edu.cmu.tetrad.data.DataSet)3 Graph (edu.cmu.tetrad.graph.Graph)3 DagWrapper (edu.cmu.tetradapp.model.DagWrapper)3 GraphWrapper (edu.cmu.tetradapp.model.GraphWrapper)3 SemGraphWrapper (edu.cmu.tetradapp.model.SemGraphWrapper)3 Iterator (java.util.Iterator)3