Search in sources :

Example 31 with Edge

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

the class PngWriter method writePng.

public static void writePng(Graph graph, File file) {
    // circleLayout(graph, 200, 200, 175);
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    // Remove self-loops.
    graph = new EdgeListGraph(graph);
    for (Node node : graph.getNodes()) {
        for (Edge edge : new ArrayList<>(graph.getEdges(node, node))) {
            graph.removeEdge(edge);
        }
    }
    final GraphWorkbench workbench = new GraphWorkbench(graph);
    int maxx = 0;
    int maxy = 0;
    for (Node node : graph.getNodes()) {
        if (node.getCenterX() > maxx) {
            maxx = node.getCenterX();
        }
        if (node.getCenterY() > maxy) {
            maxy = node.getCenterY();
        }
    }
    workbench.setSize(new Dimension(maxx + 50, maxy + 50));
    panel.add(workbench, BorderLayout.CENTER);
    JDialog dialog = new JDialog();
    dialog.add(workbench);
    dialog.pack();
    Dimension size = workbench.getSize();
    BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_BYTE_INDEXED);
    Graphics2D graphics = image.createGraphics();
    workbench.paint(graphics);
    image.flush();
    // Write the image to resultFile.
    try {
        ImageIO.write(image, "PNG", file);
    } catch (IOException e1) {
        throw new RuntimeException("Could not write to " + file, e1);
    }
}
Also used : Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Edge(edu.cmu.tetrad.graph.Edge)

Example 32 with Edge

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

the class MixedFgesDiscretingContinuousVariables method convertBack.

private Graph convertBack(DataSet Dk, Graph p) {
    Graph p2 = new EdgeListGraph(Dk.getVariables());
    for (int i = 0; i < p.getNodes().size(); i++) {
        for (int j = i + 1; j < p.getNodes().size(); j++) {
            Node v1 = p.getNodes().get(i);
            Node v2 = p.getNodes().get(j);
            Edge e = p.getEdge(v1, v2);
            if (e != null) {
                Node w1 = Dk.getVariable(e.getNode1().getName());
                Node w2 = Dk.getVariable(e.getNode2().getName());
                Edge e2 = new Edge(w1, w2, e.getEndpoint1(), e.getEndpoint2());
                p2.addEdge(e2);
            }
        }
    }
    return p2;
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Edge(edu.cmu.tetrad.graph.Edge)

Example 33 with Edge

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

the class MixedFgesTreatingDiscreteAsContinuous method convertBack.

private Graph convertBack(DataSet Dk, Graph p) {
    Graph p2 = new EdgeListGraph(Dk.getVariables());
    for (int i = 0; i < p.getNodes().size(); i++) {
        for (int j = i + 1; j < p.getNodes().size(); j++) {
            Node v1 = p.getNodes().get(i);
            Node v2 = p.getNodes().get(j);
            Edge e = p.getEdge(v1, v2);
            if (e != null) {
                Node w1 = Dk.getVariable(e.getNode1().getName());
                Node w2 = Dk.getVariable(e.getNode2().getName());
                Edge e2 = new Edge(w1, w2, e.getEndpoint1(), e.getEndpoint2());
                p2.addEdge(e2);
            }
        }
    }
    return p2;
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Graph(edu.cmu.tetrad.graph.Graph) Node(edu.cmu.tetrad.graph.Node) EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) Edge(edu.cmu.tetrad.graph.Edge)

Example 34 with Edge

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

the class TsFci method search.

public Graph search(IFas fas) {
    logger.log("info", "Starting FCI algorithm.");
    logger.log("info", "Independence test = " + getIndependenceTest() + ".");
    fas.setKnowledge(getKnowledge());
    fas.setDepth(depth);
    fas.setVerbose(verbose);
    // fas.setInitialGraph(initialGraph);
    this.graph = fas.search();
    this.sepsets = fas.getSepsets();
    graph.reorientAllWith(Endpoint.CIRCLE);
    SepsetProducer sp = new SepsetsPossibleDsep(graph, independenceTest, knowledge, depth, maxPathLength);
    sp.setVerbose(verbose);
    // // Optional step: Possible Dsep. (Needed for correctness but very time consuming.)
    if (isPossibleDsepSearchDone()) {
        // long time1 = System.currentTimeMillis();
        TsFciOrient tsFciOrient = new TsFciOrient(new SepsetsSet(this.sepsets, independenceTest), independenceTest);
        tsFciOrient.setKnowledge(knowledge);
        tsFciOrient.ruleR0(graph);
        for (Edge edge : new ArrayList<>(graph.getEdges())) {
            Node x = edge.getNode1();
            Node y = edge.getNode2();
            List<Node> sepset = sp.getSepset(x, y);
            if (sepset != null) {
                graph.removeEdge(x, y);
                sepsets.set(x, y, sepset);
                System.out.println("Possible DSEP Removed " + x + "--- " + y + " sepset = " + sepset);
                // This is another added component to enforce repeating structure, specifically for possibleDsep
                // added 4.27.2016
                removeSimilarPairs(getIndependenceTest(), x, y, sepset);
            }
        }
        // long time2 = System.currentTimeMillis();
        // logger.log("info", "Step C: " + (time2 - time1) / 1000. + "s");
        // 
        // // Step FCI D.
        // long time3 = System.currentTimeMillis();
        // 
        // System.out.println("Starting possible dsep search");
        // PossibleDsepFci possibleDSep = new PossibleDsepFci(graph, independenceTest);
        // possibleDSep.setMaxDegree(getPossibleDsepDepth());
        // possibleDSep.setKnowledge(getKnowledge());
        // possibleDSep.setMaxPathLength(maxPathLength);
        // this.sepsets.addAll(possibleDSep.search());
        // long time4 = System.currentTimeMillis();
        // logger.log("info", "Step D: " + (time4 - time3) / 1000. + "s");
        // System.out.println("Starting possible dsep search");
        // Reorient all edges as o-o.
        graph.reorientAllWith(Endpoint.CIRCLE);
    }
    // Step CI C (Zhang's step F3.)
    long time5 = System.currentTimeMillis();
    // fciOrientbk(getKnowledge(), graph, independenceTest.getVariable());    - Robert Tillman 2008
    // fciOrientbk(getKnowledge(), graph, variables);
    // new FciOrient(graph, new Sepsets(this.sepsets)).ruleR0(new Sepsets(this.sepsets));
    long time6 = System.currentTimeMillis();
    logger.log("info", "Step CI C: " + (time6 - time5) / 1000. + "s");
    final TsFciOrient fciOrient = new TsFciOrient(new SepsetsSet(this.sepsets, independenceTest), independenceTest);
    fciOrient.setCompleteRuleSetUsed(completeRuleSetUsed);
    fciOrient.setMaxPathLength(maxPathLength);
    fciOrient.setKnowledge(knowledge);
    fciOrient.ruleR0(graph);
    fciOrient.doFinalOrientation(graph);
    graph.setPag(true);
    return graph;
}
Also used : Node(edu.cmu.tetrad.graph.Node) KnowledgeEdge(edu.cmu.tetrad.data.KnowledgeEdge) Edge(edu.cmu.tetrad.graph.Edge)

Example 35 with Edge

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

the class WFges method search.

public Graph search() {
    score.setPenaltyDiscount(penaltyDiscount);
    Graph g = fges.search();
    Graph out = new EdgeListGraph(searchVariables);
    for (int i = 0; i < searchVariables.size(); i++) {
        for (int j = i + 1; j < searchVariables.size(); j++) {
            Node x = searchVariables.get(i);
            Node y = searchVariables.get(j);
            List<Node> xNodes = variablesPerNode.get(x);
            List<Node> yNodes = variablesPerNode.get(y);
            int left = 0;
            int right = 0;
            int total = 0;
            for (int k = 0; k < xNodes.size(); k++) {
                for (int l = 0; l < yNodes.size(); l++) {
                    Edge e = g.getEdge(xNodes.get(k), yNodes.get(l));
                    if (e != null) {
                        total++;
                        if (e.pointsTowards(xNodes.get(k)))
                            left++;
                        if (e.pointsTowards(yNodes.get(l)))
                            right++;
                    }
                }
            }
            if (total > 0) {
                if (left == total)
                    out.addDirectedEdge(y, x);
                else if (right == total)
                    out.addDirectedEdge(x, y);
                else
                    out.addUndirectedEdge(x, y);
            }
        }
    }
    return out;
}
Also used : EdgeListGraph(edu.cmu.tetrad.graph.EdgeListGraph) 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