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