Search in sources :

Example 11 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class SplitCasesParamsEditor method setup.

public void setup() {
    SplitCasesSpec spec = (SplitCasesSpec) this.params.get("splitCasesSpec", null);
    if (spec != null) {
        spec = getDefaultSpec(this.dataSet.getNumRows(), this.params.getInt("numSplits", 3));
    }
    numSplitsField = new IntTextField(this.params.getInt("numSplits", 3), 2);
    numSplitsField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            setNumSplits(value);
            return value;
        }
    });
    splitEditorPanel = new JPanel();
    splitEditorPanel.setLayout(new BorderLayout());
    setNumSplits(params.getInt("numSplits", 3));
    JRadioButton shuffleButton = new JRadioButton("Shuffled order");
    JRadioButton noShuffleButton = new JRadioButton("Original order");
    shuffleButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            params.set("dataShuffled", true);
        }
    });
    noShuffleButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            params.set("dataShuffled", false);
        }
    });
    ButtonGroup group = new ButtonGroup();
    group.add(shuffleButton);
    group.add(noShuffleButton);
    shuffleButton.setSelected(params.getBoolean("dataShuffled", true));
    Box b1 = Box.createVerticalBox();
    Box b2 = Box.createHorizontalBox();
    b2.add(new JLabel("Using data in:"));
    b2.add(Box.createHorizontalGlue());
    b1.add(b2);
    Box b3 = Box.createHorizontalBox();
    b3.add(Box.createHorizontalStrut(10));
    b3.add(shuffleButton);
    b3.add(Box.createHorizontalGlue());
    b1.add(b3);
    Box b4 = Box.createHorizontalBox();
    b4.add(Box.createHorizontalStrut(10));
    b4.add(noShuffleButton);
    b4.add(Box.createHorizontalGlue());
    b1.add(b4);
    b1.add(Box.createVerticalStrut(10));
    Box b5 = Box.createHorizontalBox();
    b5.add(new JLabel("Split data into "));
    b5.add(numSplitsField);
    b5.add(new JLabel(" subsets as follows:"));
    b5.add(Box.createHorizontalGlue());
    b1.add(b5);
    b1.add(Box.createVerticalStrut(10));
    b1.add(splitEditorPanel);
    b1.add(Box.createVerticalGlue());
    setLayout(new BorderLayout());
    add(b1, BorderLayout.CENTER);
}
Also used : ActionListener(java.awt.event.ActionListener) IntTextField(edu.cmu.tetradapp.util.IntTextField) ActionEvent(java.awt.event.ActionEvent) SplitCasesSpec(edu.cmu.tetrad.data.SplitCasesSpec)

Example 12 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField 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 13 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField 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);
}
Also used : ItemEvent(java.awt.event.ItemEvent) ActionEvent(java.awt.event.ActionEvent) DecimalFormat(java.text.DecimalFormat) MatteBorder(javax.swing.border.MatteBorder) IntTextField(edu.cmu.tetradapp.util.IntTextField) ArrayList(java.util.ArrayList) List(java.util.List) DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) Parameters(edu.cmu.tetrad.util.Parameters) ActionListener(java.awt.event.ActionListener) ItemListener(java.awt.event.ItemListener)

Example 14 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class SearchParamEditor method setup.

public void setup() {
    /*
      The variable names from the object being searched over (usually data).
     */
    List varNames = (List<String>) params.get("varNames", null);
    DataModel dataModel1 = null;
    Graph graph = null;
    for (Object parentModel1 : parentModels) {
        if (parentModel1 instanceof DataWrapper) {
            DataWrapper dataWrapper = (DataWrapper) parentModel1;
            dataModel1 = dataWrapper.getSelectedDataModel();
        }
        if (parentModel1 instanceof GraphWrapper) {
            GraphWrapper graphWrapper = (GraphWrapper) parentModel1;
            graph = graphWrapper.getGraph();
        }
        if (parentModel1 instanceof DagWrapper) {
            DagWrapper dagWrapper = (DagWrapper) parentModel1;
            graph = dagWrapper.getDag();
        }
        if (parentModel1 instanceof SemGraphWrapper) {
            SemGraphWrapper semGraphWrapper = (SemGraphWrapper) parentModel1;
            graph = semGraphWrapper.getGraph();
        }
    }
    if (dataModel1 != null) {
        varNames = new ArrayList(dataModel1.getVariableNames());
    } else if (graph != null) {
        Iterator it = graph.getNodes().iterator();
        varNames = new ArrayList();
        Node temp;
        while (it.hasNext()) {
            temp = (Node) it.next();
            if (temp.getNodeType() == NodeType.MEASURED) {
                varNames.add(temp.getName());
            }
        }
    } else {
        throw new NullPointerException("Null model (no graph or data model " + "passed to the search).");
    }
    params.set("varNames", varNames);
    IntTextField depthField = new IntTextField(params.getInt("depth", -1), 4);
    depthField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                params.set("depth", value);
                Preferences.userRoot().putInt("depth", value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    double alpha = params.getDouble("alpha", 0.001);
    if (!Double.isNaN(alpha)) {
        alphaField = new DoubleTextField(alpha, 4, NumberFormatUtil.getInstance().getNumberFormat());
        alphaField.setFilter(new DoubleTextField.Filter() {

            public double filter(double value, double oldValue) {
                try {
                    params.set("alpha", 0.001);
                    Preferences.userRoot().putDouble("alpha", value);
                    return value;
                } catch (Exception e) {
                    return oldValue;
                }
            }
        });
    }
    setBorder(new MatteBorder(10, 10, 10, 10, super.getBackground()));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    if (!Double.isNaN(alpha)) {
        Box b0 = Box.createHorizontalBox();
        b0.add(new JLabel("Alpha Value:"));
        b0.add(Box.createGlue());
        b0.add(alphaField);
        add(b0);
        add(Box.createVerticalStrut(10));
    }
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Search Depth:"));
    b1.add(Box.createGlue());
    b1.add(depthField);
    add(b1);
    add(Box.createVerticalStrut(10));
}
Also used : DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) Node(edu.cmu.tetrad.graph.Node) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper) GraphWrapper(edu.cmu.tetradapp.model.GraphWrapper) ArrayList(java.util.ArrayList) DataWrapper(edu.cmu.tetradapp.model.DataWrapper) DagWrapper(edu.cmu.tetradapp.model.DagWrapper) MatteBorder(javax.swing.border.MatteBorder) Graph(edu.cmu.tetrad.graph.Graph) DataModel(edu.cmu.tetrad.data.DataModel) IntTextField(edu.cmu.tetradapp.util.IntTextField) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper)

Example 15 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class Sem2DataParamsEditor method setup.

public void setup() {
    // set up text and ties them to the parameters object being edited.
    IntTextField sampleSizeField = new IntTextField(getParams().getInt("sampleSize", 1000), 4);
    sampleSizeField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                getParams().set("sampleSize", value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    JCheckBox latentVarsBox = new JCheckBox("Include Latent Variables");
    latentVarsBox.setHorizontalTextPosition(SwingConstants.LEFT);
    latentVarsBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JCheckBox b = (JCheckBox) e.getSource();
            getParams().set("includeLatents", b.isSelected());
        }
    });
    // JCheckBox positiveOnlyBox = new JCheckBox("Positive Data Only");
    // positiveOnlyBox.setHorizontalTextPosition(SwingConstants.LEFT);
    // positiveOnlyBox.addActionListener(new ActionListener(){
    // public void actionPerformed(ActionEvent e) {
    // JCheckBox b = (JCheckBox)e.getSource();
    // getParameters().setPositiveDataOnly(b.isSelected());
    // }
    // });
    setLayout(new BorderLayout());
    // continue workbench construction.
    Box b6 = Box.createVerticalBox();
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Sample size:  "));
    b1.add(Box.createHorizontalGlue());
    b1.add(sampleSizeField);
    Box b2 = Box.createHorizontalBox();
    b2.add(latentVarsBox);
    b2.add(Box.createHorizontalGlue());
    // Box b3 = Box.createHorizontalBox();
    // b3.add(positiveOnlyBox);
    // b3.add(Box.createHorizontalGlue());
    b6.add(b1);
    // b6.add(Box.createVerticalStrut(5));
    // b6.add(hBox);
    b6.add(Box.createVerticalGlue());
    add(b6, BorderLayout.CENTER);
}
Also used : ActionListener(java.awt.event.ActionListener) IntTextField(edu.cmu.tetradapp.util.IntTextField) ActionEvent(java.awt.event.ActionEvent)

Aggregations

IntTextField (edu.cmu.tetradapp.util.IntTextField)20 ActionEvent (java.awt.event.ActionEvent)11 ActionListener (java.awt.event.ActionListener)11 DoubleTextField (edu.cmu.tetradapp.util.DoubleTextField)6 ArrayList (java.util.ArrayList)5 EmptyBorder (javax.swing.border.EmptyBorder)5 List (java.util.List)4 MatteBorder (javax.swing.border.MatteBorder)4 DataModel (edu.cmu.tetrad.data.DataModel)3 Graph (edu.cmu.tetrad.graph.Graph)3 Node (edu.cmu.tetrad.graph.Node)3 DagWrapper (edu.cmu.tetradapp.model.DagWrapper)3 DataWrapper (edu.cmu.tetradapp.model.DataWrapper)3 GraphWrapper (edu.cmu.tetradapp.model.GraphWrapper)3 SemGraphWrapper (edu.cmu.tetradapp.model.SemGraphWrapper)3 Iterator (java.util.Iterator)3 IndTestDSep (edu.cmu.tetrad.search.IndTestDSep)2 Parameters (edu.cmu.tetrad.util.Parameters)2 DecimalFormat (java.text.DecimalFormat)2 AbstractTableModel (javax.swing.table.AbstractTableModel)2