use of edu.cmu.tetradapp.model.IndependenceResult in project tetrad by cmu-phil.
the class IndependenceFactsEditor method generateResults.
private void generateResults() {
results = new ArrayList<>();
List<String> dataVars = getDataVars();
if (getVars().size() < 2) {
tableModel.fireTableDataChanged();
return;
}
// Need a choice generator over the ?'s, and a depth choice generator over the +'s. The +'s all have to come
// at the end at index >= 2.
int numQuestionMarksFirstTwo = 0;
int numQuestionMarksRest = 0;
int numPluses = 0;
int numFixed = 0;
for (int i = 0; i < vars.size(); i++) {
String var = vars.get(i);
if ("?".equals(var) && i < 2)
numQuestionMarksFirstTwo++;
else if ("?".equals(var))
numQuestionMarksRest++;
else if ("+".equals(var))
numPluses++;
else
numFixed++;
}
int[] questionMarkFirstTwoIndices = new int[numQuestionMarksFirstTwo];
int[] questionMarkRestIndices = new int[numQuestionMarksRest];
int[] plusIndices = new int[numPluses];
int[] fixedIndices = new int[numFixed];
String[] fixedVars = new String[numFixed];
int _i = -1;
int _j = -1;
int _k = -1;
int _l = -1;
for (int i = 0; i < vars.size(); i++) {
if ("?".equals(vars.get(i)) && i < 2)
questionMarkFirstTwoIndices[++_i] = i;
else if ("?".equals(vars.get(i)))
questionMarkRestIndices[++_j] = i;
else if ("+".equals(vars.get(i)))
plusIndices[++_k] = i;
else {
fixedIndices[++_l] = i;
fixedVars[_l] = vars.get(i);
}
}
List<String> vars1 = new ArrayList<>(dataVars);
vars1.removeAll(vars);
ChoiceGenerator gen1 = new ChoiceGenerator(vars1.size(), questionMarkFirstTwoIndices.length);
int[] choice1;
LOOP: while ((choice1 = gen1.next()) != null) {
List<String> s2 = asList(choice1, vars1);
List<String> vars2 = new ArrayList<>(vars1);
vars2.removeAll(s2);
ChoiceGenerator gen2 = new ChoiceGenerator(vars2.size(), questionMarkRestIndices.length);
int[] choice2;
while ((choice2 = gen2.next()) != null) {
List<String> s3 = asList(choice2, vars2);
List<String> vars3 = new ArrayList<>(vars2);
vars3.removeAll(s3);
DepthChoiceGenerator gen3 = new DepthChoiceGenerator(vars3.size(), plusIndices.length);
int[] choice3;
while ((choice3 = gen3.next()) != null) {
results.add(new ArrayList<IndependenceResult>());
for (int prod = 0; prod < indTestProducers.size(); prod++) {
String[] vars4 = new String[fixedIndices.length + questionMarkFirstTwoIndices.length + questionMarkRestIndices.length + choice3.length];
for (int i = 0; i < fixedIndices.length; i++) {
vars4[fixedIndices[i]] = fixedVars[i];
}
for (int i = 0; i < choice1.length; i++) {
vars4[questionMarkFirstTwoIndices[i]] = vars1.get(choice1[i]);
}
for (int i = 0; i < choice2.length; i++) {
vars4[questionMarkRestIndices[i]] = vars2.get(choice2[i]);
}
for (int i = 0; i < choice3.length; i++) {
vars4[plusIndices[i]] = vars3.get(choice3[i]);
}
IndependenceTest independenceTest = getIndependenceTest(prod);
Node x = independenceTest.getVariable(vars4[0]);
Node y = independenceTest.getVariable(vars4[1]);
List<Node> z = new ArrayList<>();
for (int i = 2; i < vars4.length; i++) {
z.add(independenceTest.getVariable(vars4[i]));
}
IndependenceResult.Type indep;
double pValue;
try {
indep = independenceTest.isIndependent(x, y, z) ? IndependenceResult.Type.INDEPENDENT : IndependenceResult.Type.DEPENDENT;
pValue = independenceTest.getPValue();
} catch (Exception e) {
indep = IndependenceResult.Type.UNDETERMINED;
pValue = Double.NaN;
}
results.get(results.size() - 1).add(new IndependenceResult(results.size(), factString(x, y, z), indep, pValue));
}
if (results.size() > getListLimit())
break LOOP;
}
}
}
model.setResults(results);
tableModel.fireTableDataChanged();
}
use of edu.cmu.tetradapp.model.IndependenceResult 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));
}
Aggregations