Search in sources :

Example 6 with IndTestDSep

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));
}
Also used : JTableHeader(javax.swing.table.JTableHeader) IndTestDSep(edu.cmu.tetrad.search.IndTestDSep) IntTextField(edu.cmu.tetradapp.util.IntTextField) EmptyBorder(javax.swing.border.EmptyBorder) DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer) AbstractTableModel(javax.swing.table.AbstractTableModel) IndependenceResult(edu.cmu.tetradapp.model.IndependenceResult)

Example 7 with IndTestDSep

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));
}
Also used : IndependenceTest(edu.cmu.tetrad.search.IndependenceTest) IndTestDSep(edu.cmu.tetrad.search.IndTestDSep) Graph(edu.cmu.tetrad.graph.Graph) Pc(edu.cmu.tetrad.search.Pc)

Aggregations

IndTestDSep (edu.cmu.tetrad.search.IndTestDSep)7 IndependenceTest (edu.cmu.tetrad.search.IndependenceTest)5 Graph (edu.cmu.tetrad.graph.Graph)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 ContinuousVariable (edu.cmu.tetrad.data.ContinuousVariable)2 Mbfs (edu.cmu.tetrad.search.Mbfs)2 IntTextField (edu.cmu.tetradapp.util.IntTextField)2 EmptyBorder (javax.swing.border.EmptyBorder)2 AbstractTableModel (javax.swing.table.AbstractTableModel)2 JTableHeader (javax.swing.table.JTableHeader)2 DataSet (edu.cmu.tetrad.data.DataSet)1 Discretizer (edu.cmu.tetrad.data.Discretizer)1 Node (edu.cmu.tetrad.graph.Node)1 Pc (edu.cmu.tetrad.search.Pc)1 Pcd (edu.cmu.tetrad.search.Pcd)1 SemIm (edu.cmu.tetrad.sem.SemIm)1 SemPm (edu.cmu.tetrad.sem.SemPm)1 IndependenceResult (edu.cmu.tetradapp.model.IndependenceResult)1 IndTestMultinomialLogisticRegressionWald (edu.pitt.csb.mgm.IndTestMultinomialLogisticRegressionWald)1