Search in sources :

Example 1 with SemEvidence

use of edu.cmu.tetrad.sem.SemEvidence 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 2 with SemEvidence

use of edu.cmu.tetrad.sem.SemEvidence in project tetrad by cmu-phil.

the class TestSemEvidence method testSemEvidence.

@Test
public void testSemEvidence() {
    Graph graph = constructGraph1();
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    SemEvidence evidence = new SemEvidence(semIm);
    evidence.setManipulated(1, true);
    assertTrue(evidence.isManipulated(1));
}
Also used : SemEvidence(edu.cmu.tetrad.sem.SemEvidence) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Example 3 with SemEvidence

use of edu.cmu.tetrad.sem.SemEvidence in project tetrad by cmu-phil.

the class TestSemUpdater method testUpdate.

@Test
public void testUpdate() {
    Graph graph = constructGraph1();
    SemPm semPm = new SemPm(graph);
    SemIm semIm = new SemIm(semPm);
    List<Node> nodes = semIm.getVariableNodes();
    SemUpdater semUpdater = new SemUpdater(semIm);
    SemEvidence evidence = new SemEvidence(semIm);
    evidence.getProposition().setValue(nodes.get(4), 10.0);
    evidence.getProposition().setValue(nodes.get(2), 1.5);
    semUpdater.setEvidence(evidence);
    evidence.setManipulated(1, true);
    semUpdater.getManipulatedSemIm();
    semUpdater.getUpdatedSemIm();
}
Also used : SemUpdater(edu.cmu.tetrad.sem.SemUpdater) SemEvidence(edu.cmu.tetrad.sem.SemEvidence) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) SemPm(edu.cmu.tetrad.sem.SemPm) SemIm(edu.cmu.tetrad.sem.SemIm) Test(org.junit.Test)

Aggregations

SemEvidence (edu.cmu.tetrad.sem.SemEvidence)3 SemIm (edu.cmu.tetrad.sem.SemIm)3 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)2 Graph (edu.cmu.tetrad.graph.Graph)2 Node (edu.cmu.tetrad.graph.Node)2 SemPm (edu.cmu.tetrad.sem.SemPm)2 Test (org.junit.Test)2 GraphNode (edu.cmu.tetrad.graph.GraphNode)1 SemUpdater (edu.cmu.tetrad.sem.SemUpdater)1 DoubleTextField (edu.cmu.tetradapp.util.DoubleTextField)1 Component (java.awt.Component)1 Container (java.awt.Container)1 Dimension (java.awt.Dimension)1 FocusTraversalPolicy (java.awt.FocusTraversalPolicy)1 Box (javax.swing.Box)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 JLabel (javax.swing.JLabel)1 EmptyBorder (javax.swing.border.EmptyBorder)1