Search in sources :

Example 1 with IntTextField

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

the class ParameterPanel method getIntTextField.

private IntTextField getIntTextField(final String parameter, final Parameters parameters, final int defaultValue, final double lowerBound, final double upperBound) {
    final IntTextField field = new IntTextField(parameters.getInt(parameter, defaultValue), 8);
    field.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            if (value == field.getValue()) {
                return oldValue;
            }
            if (value < lowerBound) {
                return oldValue;
            }
            if (value > upperBound) {
                return oldValue;
            }
            try {
                parameters.set(parameter, value);
            } catch (Exception e) {
            // Ignore.
            }
            return value;
        }
    });
    return field;
}
Also used : IntTextField(edu.cmu.tetradapp.util.IntTextField)

Example 2 with IntTextField

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

the class LogParamsEditor method buildGui.

// ================================= Private Methods ===============================//
/**
 * Constructs the Gui used to edit properties; called from each constructor.
 * Constructs labels and text fields for editing each property and adds
 * appropriate listeners.
 */
private void buildGui() {
    setLayout(new BorderLayout());
    final DoubleTextField aField = new DoubleTextField(params.getDouble("a", 10.0), 6, NumberFormatUtil.getInstance().getNumberFormat());
    aField.setFilter(new DoubleTextField.Filter() {

        public double filter(double value, double oldValue) {
            try {
                params.set("a", value);
                return value;
            } catch (IllegalArgumentException e) {
                return oldValue;
            }
        }
    });
    final IntTextField baseField = new IntTextField(params.getInt("base", 0), 4);
    baseField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                params.set("base", value);
                return value;
            } catch (IllegalArgumentException e) {
                return oldValue;
            }
        }
    });
    // continue workbench construction.
    Box b1 = Box.createVerticalBox();
    Box b2 = Box.createHorizontalBox();
    b2.add(new JLabel("<html>" + "The input dataset will be logarithmically transformed by applying f(x) = ln(a + x) to each data point x." + "<br> Can also 'unlog' the data i.e., apply g(x) = exp(x) - a, or override the base"));
    Box b9 = Box.createHorizontalBox();
    b9.add(Box.createHorizontalGlue());
    b9.add(new JLabel("<html> base (use 0 for natural log and base <i>e</i>): </html>"));
    b9.add(baseField);
    Box b7 = Box.createHorizontalBox();
    b7.add(Box.createHorizontalGlue());
    b7.add(new JLabel("<html><i>a =  </i></html>"));
    b7.add(aField);
    JCheckBox unlog = new JCheckBox();
    unlog.setSelected(params.getBoolean("unlog", false));
    unlog.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JCheckBox box = (JCheckBox) e.getSource();
            params.set("unlog", box.isSelected());
        }
    });
    Box b8 = Box.createHorizontalBox();
    b8.add(Box.createHorizontalGlue());
    b8.add(new JLabel("<html>Unlog: </html>"));
    b8.add(unlog);
    b1.add(b2);
    b1.add(Box.createVerticalStrut(5));
    b1.add(b7);
    b1.add(Box.createHorizontalGlue());
    b1.add(b8);
    b1.add(Box.createHorizontalGlue());
    b1.add(b9);
    b1.add(Box.createHorizontalGlue());
    add(b1, BorderLayout.CENTER);
}
Also used : DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) ActionEvent(java.awt.event.ActionEvent) ActionListener(java.awt.event.ActionListener) IntTextField(edu.cmu.tetradapp.util.IntTextField)

Example 3 with IntTextField

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

the class RandomSamplerAction method editor.

private JComponent editor() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    final IntTextField sampleSizeField = new IntTextField(getSampleSize(), 6);
    sampleSizeField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                setSampleSize(value);
                return value;
            } catch (IllegalArgumentException e) {
                return oldValue;
            }
        }
    });
    // continue workbench construction.
    Box b1 = Box.createVerticalBox();
    Box b2 = Box.createHorizontalBox();
    b2.add(new JLabel("<html>" + "The input dataset will be sampled with replacement to create a new" + "<br>dataset with the number of samples entered below." + "<br>The editable default sample size is 100." + "</html>"));
    Box b7 = Box.createHorizontalBox();
    b7.add(Box.createHorizontalGlue());
    b7.add(new JLabel("<html>" + "<i>Sample size:  </i>" + "</html>"));
    b7.add(sampleSizeField);
    b1.add(b2);
    b1.add(Box.createVerticalStrut(5));
    b1.add(b7);
    b1.add(Box.createHorizontalGlue());
    panel.add(b1, BorderLayout.CENTER);
    return panel;
}
Also used : IntTextField(edu.cmu.tetradapp.util.IntTextField)

Example 4 with IntTextField

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

the class PathsAction method actionPerformed.

public void actionPerformed(ActionEvent e) {
    final Graph graph = workbench.getGraph();
    textArea = new JTextArea();
    JScrollPane scroll = new JScrollPane(textArea);
    scroll.setPreferredSize(new Dimension(600, 600));
    List<Node> allNodes = graph.getNodes();
    Collections.sort(allNodes, new Comparator<Node>() {

        @Override
        public int compare(Node o1, Node o2) {
            return o1.compareTo(o2);
        }
    });
    allNodes.add(new GraphNode("SELECT_ALL"));
    Node[] array = allNodes.toArray(new Node[0]);
    Node pathFrom = graph.getNode(Preferences.userRoot().get("pathFrom", ""));
    if (pathFrom == null) {
        nodes1 = Collections.singletonList(graph.getNodes().get(0));
    } else {
        nodes1 = Collections.singletonList(pathFrom);
    }
    JComboBox node1Box = new JComboBox(array);
    node1Box.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JComboBox box = (JComboBox) e.getSource();
            Node node = (Node) box.getSelectedItem();
            System.out.println(node);
            if ("SELECT_ALL".equals(node.getName())) {
                nodes1 = new ArrayList<>(graph.getNodes());
            } else {
                nodes1 = Collections.singletonList(node);
            }
            Preferences.userRoot().put("pathFrom", node.getName());
        // update(graph, textArea, nodes1, nodes2, method);
        }
    });
    node1Box.setSelectedItem(nodes1.get(0));
    Node pathTo = graph.getNode(Preferences.userRoot().get("pathTo", ""));
    if (pathTo == null) {
        nodes2 = Collections.singletonList(graph.getNodes().get(0));
    } else {
        nodes2 = Collections.singletonList(pathTo);
    }
    JComboBox node2Box = new JComboBox(array);
    node2Box.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JComboBox box = (JComboBox) e.getSource();
            Node node = (Node) box.getSelectedItem();
            System.out.println(node);
            if ("SELECT_ALL".equals(node.getName())) {
                nodes2 = new ArrayList<>(graph.getNodes());
            } else {
                nodes2 = Collections.singletonList(node);
            }
            Preferences.userRoot().put("pathTo", node.getName());
        // update(graph, textArea, nodes1, nodes2, method);
        }
    });
    node2Box.setSelectedItem(nodes2.get(0));
    JComboBox methodBox = new JComboBox(new String[] { "Directed Paths", "Semidirected Paths", "Treks", "Adjacents" });
    this.method = Preferences.userRoot().get("pathMethod", "Directed Paths");
    methodBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JComboBox box = (JComboBox) e.getSource();
            method = (String) box.getSelectedItem();
            Preferences.userRoot().put("pathMethod", method);
        // update(graph, textArea, nodes1, nodes2, method);
        }
    });
    methodBox.setSelectedItem(this.method);
    IntTextField maxField = new IntTextField(Preferences.userRoot().getInt("pathMaxLength", 3), 2);
    maxField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                setMaxLength(value);
                // update(graph, textArea, nodes1, nodes2, method);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    JButton updateButton = new JButton(("Update"));
    updateButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            update(graph, textArea, nodes1, nodes2, method);
        }
    });
    allDirectedPaths(graph, textArea, nodes1, nodes2);
    Box b = Box.createVerticalBox();
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("From "));
    b1.add(node1Box);
    b1.add(Box.createHorizontalGlue());
    b1.add(new JLabel(" To "));
    b1.add(node2Box);
    b1.add(Box.createHorizontalGlue());
    b1.add(methodBox);
    b1.add(new JLabel("Max length"));
    b1.add(maxField);
    b1.add(updateButton);
    b.add(b1);
    Box b2 = Box.createHorizontalBox();
    b2.add(scroll);
    textArea.setCaretPosition(0);
    b.add(b2);
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(b);
    EditorWindow window = new EditorWindow(panel, "Directed Paths", "Close", false, workbench);
    DesktopController.getInstance().addEditorWindow(window, JLayeredPane.PALETTE_LAYER);
    window.setVisible(true);
    update(graph, textArea, nodes1, nodes2, method);
}
Also used : ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) IntTextField(edu.cmu.tetradapp.util.IntTextField) ActionListener(java.awt.event.ActionListener)

Example 5 with IntTextField

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

the class PcSearchParamEditor 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);
    DataModel dataModel1 = null;
    Graph graph = null;
    for (Object parentModel1 : this.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<Node> it = graph.getNodes().iterator();
        varNames = new ArrayList();
        Node temp;
        while (it.hasNext()) {
            temp = 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).");
    }
    this.params.set("varNames", varNames);
    IntTextField depthField = new IntTextField(this.params.getInt("depth", -1), 4);
    depthField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                PcSearchParamEditor.this.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 {
                    PcSearchParamEditor.this.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 b2 = Box.createHorizontalBox();
        b2.add(new JLabel("Alpha Value:"));
        b2.add(Box.createGlue());
        b2.add(alphaField);
        add(b2);
        add(Box.createVerticalStrut(10));
    }
    Box b3 = Box.createHorizontalBox();
    b3.add(new JLabel("Search Depth:"));
    b3.add(Box.createGlue());
    b3.add(depthField);
    add(b3);
    add(Box.createVerticalStrut(10));
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) DataWrapper(edu.cmu.tetradapp.model.DataWrapper) DagWrapper(edu.cmu.tetradapp.model.DagWrapper) MatteBorder(javax.swing.border.MatteBorder) IntTextField(edu.cmu.tetradapp.util.IntTextField) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper) DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper) GraphWrapper(edu.cmu.tetradapp.model.GraphWrapper) Graph(edu.cmu.tetrad.graph.Graph) DataModel(edu.cmu.tetrad.data.DataModel)

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