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;
}
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));
}
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();
}
Aggregations