Search in sources :

Example 26 with Edge

use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.

the class ForbiddenGraphModel method createKnowledge.

private void createKnowledge(Parameters params) {
    IKnowledge knwl = getKnowledge();
    if (knwl == null) {
        return;
    }
    knwl.clear();
    List<String> varNames = getVarNames();
    getKnowledgeBoxInput().getVariableNames().stream().filter(e -> !e.startsWith("E_")).forEach(e -> {
        varNames.add(e);
        knwl.addVariable(e);
    });
    if (resultGraph == null) {
        throw new NullPointerException("I couldn't find a parent graph.");
    }
    List<Node> nodes = resultGraph.getNodes();
    int numOfNodes = nodes.size();
    for (int i = 0; i < numOfNodes; i++) {
        for (int j = i + 1; j < numOfNodes; j++) {
            Node n1 = nodes.get(i);
            Node n2 = nodes.get(j);
            if (n1.getName().startsWith("E_") || n2.getName().startsWith("E_")) {
                continue;
            }
            Edge edge = resultGraph.getEdge(n1, n2);
            if (edge != null && edge.isDirected()) {
                knwl.setForbidden(edge.getNode2().getName(), edge.getNode1().getName());
            }
        }
    }
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) SortedSet(java.util.SortedSet) IKnowledge(edu.cmu.tetrad.data.IKnowledge) TetradLogger(edu.cmu.tetrad.util.TetradLogger) Graph(edu.cmu.tetrad.graph.Graph) Parameters(edu.cmu.tetrad.util.Parameters) TetradSerializableUtils(edu.cmu.tetrad.util.TetradSerializableUtils) Node(edu.cmu.tetrad.graph.Node) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) KnowledgeBoxInput(edu.cmu.tetrad.data.KnowledgeBoxInput) Edge(edu.cmu.tetrad.graph.Edge) Knowledge2(edu.cmu.tetrad.data.Knowledge2) IKnowledge(edu.cmu.tetrad.data.IKnowledge) Node(edu.cmu.tetrad.graph.Node) Edge(edu.cmu.tetrad.graph.Edge)

Example 27 with Edge

use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.

the class AbstractWorkbench method finishEdge.

/**
 * Finishes the drawing of a new edge.
 *
 * @see #startEdge
 */
private void finishEdge() {
    if (getTrackedEdge() == null) {
        return;
    }
    // Retrieve the two display components this edge should connect.
    DisplayNode comp1 = getTrackedEdge().getComp1();
    Point p = getTrackedEdge().getTrackPoint();
    DisplayNode comp2 = findNearestNode(p);
    // Edges to self are not supported.
    if (comp1 != comp2) {
        // Construct the model edge
        try {
            Node node1 = (Node) (getDisplayToModel().get(comp1));
            Node node2 = (Node) (getDisplayToModel().get(comp2));
            Edge modelEdge = getNewModelEdge(node1, node2);
            // Add model edge to model; this will result in an event fired
            // back from the model to add a display edge, so we can remove
            // the tracked edge and forget about it.
            graph.addEdge(modelEdge);
            setGraph(graph);
            firePropertyChange("modelChanged", null, null);
        } catch (Exception e) {
            e.printStackTrace();
            if (isNodeEdgeErrorsReported()) {
                JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e.getMessage());
            }
        }
    }
    remove((Component) getTrackedEdge());
    repaint();
    // reset the tracked edge to null to wait for the next attempt
    // at adding an edge.
    this.trackedEdge = null;
}
Also used : Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) Point(java.awt.Point) Edge(edu.cmu.tetrad.graph.Edge)

Example 28 with Edge

use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.

the class AbstractWorkbench method setGraphWithoutNotify.

// ============================PRIVATE METHODS=========================//
/**
 * Sets the display workbench model to the indicated model. (Called when the
 * workbench is first constructed as well as whenever the workbench model is
 * changed.)
 */
private void setGraphWithoutNotify(Graph graph) {
    if (graph == null) {
        throw new IllegalArgumentException("Graph model cannot be null.");
    }
    if (graph instanceof SessionWrapper) {
        this.graph = graph;
    } else {
        this.graph = graph;
        if (graph.isPag()) {
            GraphUtils.addPagColoring(new EdgeListGraph(graph));
        }
    }
    this.modelEdgesToDisplay = new HashMap<>();
    this.modelNodesToDisplay = new HashMap<>();
    this.displayToModel = new HashMap();
    this.displayToLabels = new HashMap();
    removeAll();
    graph.addPropertyChangeListener(this.propChangeHandler);
    // extract the current contents from the model...
    List<Node> nodes = graph.getNodes();
    for (Node node : nodes) {
        if (!getModelNodesToDisplay().containsKey(node)) {
            addNode(node);
        }
    }
    Set<Edge> edges = graph.getEdges();
    for (Edge edge : edges) {
        if (!getModelEdgesToDisplay().containsKey(edge)) {
            addEdge(edge);
        }
    }
    adjustPreferredSize();
    if (getPreferredSize().getWidth() > getMaxX()) {
        setMaxX((int) getPreferredSize().getWidth());
    }
    if (getPreferredSize().getHeight() > getMaxY()) {
        setMaxY((int) getPreferredSize().getHeight());
    }
    revalidate();
    repaint();
}
Also used : HashMap(java.util.HashMap) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Edge(edu.cmu.tetrad.graph.Edge) SessionWrapper(edu.cmu.tetradapp.model.SessionWrapper)

Example 29 with Edge

use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.

the class PossibleDsepFci method search.

// ============================== Public Methods =========================//
/**
 * Performs pairwise comparisons of each variable in the graph with the variables that have not already been
 * checked. We get the Possible-D-Sep sets for the pair of variables, and we check to see if they are independent
 * conditional on some subset of the union of Possible-D-Sep sets. This method returns the SepSet passed in the
 * constructor (if any), possibly augmented by some edge removals in this step. The GaSearchGraph passed in the
 * constructor is directly changed.
 */
public SepsetMap search() {
    for (Edge edge : new ArrayList<>(graph.getEdges())) {
        Node x = edge.getNode1();
        Node y = edge.getNode2();
        List<Node> condSet = getSepset(x, y);
        if (condSet != null) {
            for (Node n : condSet) {
                if (!(graph.getAdjacentNodes(n).contains(x) || graph.getAdjacentNodes(n).contains(y))) {
                    System.out.println("Not adjacent");
                }
            }
            graph.removeEdge(x, y);
            sepset.set(x, y, condSet);
            System.out.println("Removed " + x + "--- " + y + " sepset = " + condSet);
        }
    }
    return sepset;
}
Also used : Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) Edge(edu.cmu.tetrad.graph.Edge)

Example 30 with Edge

use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.

the class TimeoutComparison method getSubgraph.

private Graph getSubgraph(Graph graph, boolean discrete1, boolean discrete2, DataModel DataModel) {
    if (discrete1 && discrete2) {
        Graph newGraph = new EdgeListGraph(graph.getNodes());
        for (Edge edge : graph.getEdges()) {
            Node node1 = DataModel.getVariable(edge.getNode1().getName());
            Node node2 = DataModel.getVariable(edge.getNode2().getName());
            if (node1 instanceof DiscreteVariable && node2 instanceof DiscreteVariable) {
                newGraph.addEdge(edge);
            }
        }
        return newGraph;
    } else if (!discrete1 && !discrete2) {
        Graph newGraph = new EdgeListGraph(graph.getNodes());
        for (Edge edge : graph.getEdges()) {
            Node node1 = DataModel.getVariable(edge.getNode1().getName());
            Node node2 = DataModel.getVariable(edge.getNode2().getName());
            if (node1 instanceof ContinuousVariable && node2 instanceof ContinuousVariable) {
                newGraph.addEdge(edge);
            }
        }
        return newGraph;
    } else {
        Graph newGraph = new EdgeListGraph(graph.getNodes());
        for (Edge edge : graph.getEdges()) {
            Node node1 = DataModel.getVariable(edge.getNode1().getName());
            Node node2 = DataModel.getVariable(edge.getNode2().getName());
            if (node1 instanceof DiscreteVariable && node2 instanceof ContinuousVariable) {
                newGraph.addEdge(edge);
            }
            if (node1 instanceof ContinuousVariable && node2 instanceof DiscreteVariable) {
                newGraph.addEdge(edge);
            }
        }
        return newGraph;
    }
}
Also used : ContinuousVariable(edu.cmu.tetrad.data.ContinuousVariable) DiscreteVariable(edu.cmu.tetrad.data.DiscreteVariable) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) TakesInitialGraph(edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Edge(edu.cmu.tetrad.graph.Edge)

Aggregations

Edge (edu.cmu.tetrad.graph.Edge)43 Node (edu.cmu.tetrad.graph.Node)23 Graph (edu.cmu.tetrad.graph.Graph)14 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)11 ArrayList (java.util.ArrayList)7 GraphNode (edu.cmu.tetrad.graph.GraphNode)5 Endpoint (edu.cmu.tetrad.graph.Endpoint)4 SessionNode (edu.cmu.tetrad.session.SessionNode)4 List (java.util.List)4 Knowledge2 (edu.cmu.tetrad.data.Knowledge2)3 Parameters (edu.cmu.tetrad.util.Parameters)3 SessionNodeWrapper (edu.cmu.tetradapp.model.SessionNodeWrapper)3 Point (java.awt.Point)3 HashSet (java.util.HashSet)3 TakesInitialGraph (edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph)2 IKnowledge (edu.cmu.tetrad.data.IKnowledge)2 KnowledgeBoxInput (edu.cmu.tetrad.data.KnowledgeBoxInput)2 KnowledgeEdge (edu.cmu.tetrad.data.KnowledgeEdge)2 TetradLogger (edu.cmu.tetrad.util.TetradLogger)2 TetradSerializableUtils (edu.cmu.tetrad.util.TetradSerializableUtils)2