use of edu.cmu.tetrad.search.IndTestDSep in project tetrad by cmu-phil.
the class IndependenceFactsEditor method buildGui.
// ========================PUBLIC METHODS==========================//
/**
* Performs the action of opening a session from a file.
*/
private void buildGui() {
// this.independenceTest = getIndTestProducer().getIndependenceTest();
final List<String> varNames = new ArrayList<>();
varNames.add("VAR");
varNames.addAll(getDataVars());
varNames.add("?");
varNames.add("+");
final JComboBox variableBox = new JComboBox();
DefaultComboBoxModel aModel1 = new DefaultComboBoxModel(varNames.toArray(new String[varNames.size()]));
aModel1.setSelectedItem("VAR");
variableBox.setModel(aModel1);
variableBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox box = (JComboBox) e.getSource();
String var = (String) box.getSelectedItem();
LinkedList<String> vars = getVars();
int size = vars.size();
if ("VAR".equals(var)) {
return;
}
if ("?".equals(var)) {
if (size >= 0 && !vars.contains("+")) {
vars.addLast(var);
}
} else if ("+".equals(var)) {
if (size >= 2) {
vars.addLast(var);
}
} else if ((vars.indexOf("?") < 2) && !(vars.contains("+")) && !(vars.contains(var))) {
vars.add(var);
}
resetText();
// This is a workaround to an introduced bug in the JDK whereby
// repeated selections of the same item send out just one
// action event.
DefaultComboBoxModel aModel = new DefaultComboBoxModel(varNames.toArray(new String[varNames.size()]));
aModel.setSelectedItem("VAR");
variableBox.setModel(aModel);
}
});
final JButton delete = new JButton("Delete");
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!getVars().isEmpty()) {
getVars().removeLast();
resetText();
}
}
});
textField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if ('?' == e.getKeyChar()) {
variableBox.setSelectedItem("?");
} else if ('+' == e.getKeyChar()) {
variableBox.setSelectedItem("+");
} else if ('\b' == e.getKeyChar()) {
vars.removeLast();
resetText();
}
e.consume();
}
});
delete.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if ('?' == e.getKeyChar()) {
variableBox.setSelectedItem("?");
} else if ('+' == e.getKeyChar()) {
variableBox.setSelectedItem("+");
} else if ('\b' == e.getKeyChar()) {
vars.removeLast();
resetText();
}
}
});
variableBox.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
if ('\b' == e.getKeyChar()) {
vars.removeLast();
resetText();
}
}
});
JButton list = new JButton("LIST");
list.setFont(new Font("Dialog", Font.BOLD, 14));
list.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
generateResults();
}
});
Box b1 = Box.createVerticalBox();
Box b2 = Box.createHorizontalBox();
b2.add(new JLabel("Compares conditional independence tests from the given sources: "));
b2.add(Box.createHorizontalGlue());
b1.add(b2);
for (int i = 0; i < indTestProducers.size(); i++) {
Box b2a = Box.createHorizontalBox();
b2a.add(new JLabel(indTestProducers.get(i).getName() + ": " + getIndependenceTest(i).toString()));
b2a.add(Box.createHorizontalGlue());
b1.add(b2a);
}
b1.add(Box.createVerticalStrut(5));
Box b3 = Box.createHorizontalBox();
b3.add(getTextField());
b3.add(variableBox);
b3.add(delete);
b1.add(b3);
b1.add(Box.createVerticalStrut(10));
tableModel = new AbstractTableModel() {
public String getColumnName(int column) {
if (column == 0) {
return "Index";
}
if (column == 1) {
return "Fact";
} else if (column >= 2) {
// "Judgment";
return indTestProducers.get(column - 2).getName();
}
return null;
}
public int getColumnCount() {
return 2 + indTestProducers.size();
}
public int getRowCount() {
return results.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
if (rowIndex > results.size())
return null;
if (columnIndex == 0) {
return results.get(rowIndex).get(0).getIndex();
}
if (columnIndex == 1) {
return results.get(rowIndex).get(0).getFact();
}
IndependenceResult independenceResult = results.get(rowIndex).get(columnIndex - 2);
for (int i = 0; i < indTestProducers.size(); i++) {
if (columnIndex == i + 2) {
if (getIndependenceTest(i) instanceof IndTestDSep) {
if (independenceResult.getType() == IndependenceResult.Type.INDEPENDENT) {
return "D-SEPARATED";
} else if (independenceResult.getType() == IndependenceResult.Type.DEPENDENT) {
return "d-connected";
} else if (independenceResult.getType() == IndependenceResult.Type.UNDETERMINED) {
return "*";
}
} else {
if (isShowPs()) {
return nf.format(independenceResult.getpValue());
} else {
if (independenceResult.getType() == IndependenceResult.Type.INDEPENDENT) {
return "INDEPENDENT";
} else if (independenceResult.getType() == IndependenceResult.Type.DEPENDENT) {
return "dependent";
} else if (independenceResult.getType() == IndependenceResult.Type.UNDETERMINED) {
return "*";
}
}
}
}
}
return null;
}
public Class getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return Number.class;
}
if (columnIndex == 1) {
return String.class;
} else if (columnIndex == 2) {
return Number.class;
} else {
return Number.class;
}
}
};
JTable table = new JTable(tableModel);
table.getColumnModel().getColumn(0).setMinWidth(40);
table.getColumnModel().getColumn(0).setMaxWidth(40);
table.getColumnModel().getColumn(1).setMinWidth(200);
table.getColumnModel().getColumn(1).setCellRenderer(new Renderer(this));
for (int i = 2; i < table.getColumnModel().getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setMinWidth(100);
table.getColumnModel().getColumn(i).setMaxWidth(100);
table.getColumnModel().getColumn(i).setCellRenderer(new Renderer(this));
}
JTableHeader header = table.getTableHeader();
header.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JTableHeader header = (JTableHeader) e.getSource();
Point point = e.getPoint();
int col = header.columnAtPoint(point);
int sortCol = header.getTable().convertColumnIndexToModel(col);
sortByColumn(sortCol, true);
}
});
JScrollPane scroll = new JScrollPane(table);
scroll.setPreferredSize(new Dimension(400, 400));
b1.add(scroll);
Box b4 = Box.createHorizontalBox();
b4.add(new JLabel("Limit list to "));
IntTextField field = new IntTextField(getListLimit(), 7);
field.setFilter(new IntTextField.Filter() {
public int filter(int value, int oldValue) {
try {
setListLimit(value);
return value;
} catch (Exception e) {
return oldValue;
}
}
});
b4.add(field);
b4.add(new JLabel(" items."));
b4.add(Box.createHorizontalStrut(10));
final JButton showPValues = new JButton("Show P Values");
showPValues.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
toggleShowPs();
}
@Override
public void mouseReleased(MouseEvent e) {
toggleShowPs();
}
});
b4.add(showPValues);
b4.add(Box.createHorizontalGlue());
b4.add(list);
b1.add(b4);
b1.add(Box.createVerticalStrut(10));
JPanel panel = this;
panel.setLayout(new BorderLayout());
panel.add(b1, BorderLayout.CENTER);
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
}
use of edu.cmu.tetrad.search.IndTestDSep in project tetrad by cmu-phil.
the class TestPcd method checkWithKnowledge.
/**
* Presents the input graph to FCI and checks to make sure the output of FCI is equivalent to the given output
* graph.
*/
private void checkWithKnowledge(String inputGraph, String outputGraph, IKnowledge knowledge) {
// Set up graph and node objects.
Graph graph = GraphConverter.convert(inputGraph);
// Set up search.
IndependenceTest independence = new IndTestDSep(graph);
Pc pc = new Pc(independence);
pc.setKnowledge(knowledge);
// Run search
Graph resultGraph = pc.search();
// Build comparison graph.
Graph trueGraph = GraphConverter.convert(outputGraph);
resultGraph = GraphUtils.replaceNodes(resultGraph, trueGraph.getNodes());
// Do test.
assertTrue(resultGraph.equals(trueGraph));
}
Aggregations