Search in sources :

Example 36 with Edge

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

the class Knowledge method isViolatedBy.

public boolean isViolatedBy(Graph graph) {
    for (Edge edge : graph.getEdges()) {
        if (!edge.isDirected()) {
            continue;
        }
        Node from = Edges.getDirectedEdgeTail(edge);
        Node to = Edges.getDirectedEdgeHead(edge);
        if (isForbidden(from.getName(), to.getName())) {
            System.out.println("Edge forbidden: " + edge);
            return true;
        }
    }
    return false;
}
Also used : Node(edu.cmu.tetrad.graph.Node) Edge(edu.cmu.tetrad.graph.Edge)

Example 37 with Edge

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

the class FasDci method search.

// ==========================PUBLIC METHODS===========================//
/**
 * Discovers all adjacencies in data.  The procedure is to remove edges in the graph which connect pairs of
 * variables which are independent conditional on some other set of variables in the graph (the "sepset"). These are
 * removed in tiers.  First, edges which are independent conditional on zero other variables are removed, then edges
 * which are independent conditional on one other variable are removed, then two, then three, and so on, until no
 * more edges can be removed from the graph.  The edges which remain in the graph after this procedure are the
 * adjacencies in the data.
 *
 * @return a SepSet, which indicates which variables are independent conditional on which other variables
 */
public SepsetMapDci search() {
    this.logger.log("info", "Starting Fast Adjacency Search (DCI).");
    // Remove edges forbidden both ways.
    Set<Edge> edges = graph.getEdges();
    for (Edge _edge : edges) {
        String name1 = _edge.getNode1().getName();
        String name2 = _edge.getNode2().getName();
        if (knowledge.isForbidden(name1, name2) && knowledge.isForbidden(name2, name1)) {
            graph.removeEdge(_edge);
            this.logger.log("edgeRemoved", "Removed " + _edge + " because it was " + "forbidden by background knowledge.");
        }
    }
    // this.logger.info("Depth = " + ((depth == Integer
    // .MAX_VALUE) ? "Unlimited" : Integer.toString(depth)));
    SepsetMapDci sepset = new SepsetMapDci();
    int _depth = depth;
    if (_depth == -1) {
        _depth = 1000;
    }
    for (int d = 0; d <= _depth; d++) {
        boolean more = searchAtDepth(graph, independenceTest, new Knowledge2(), sepset, d);
        if (!more) {
            break;
        }
    }
    // verifySepsetIntegrity(sepset);
    this.logger.log("info", "Finishing Fast Adjacency Search.");
    return sepset;
}
Also used : Knowledge2(edu.cmu.tetrad.data.Knowledge2) Edge(edu.cmu.tetrad.graph.Edge)

Example 38 with Edge

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

the class Fci 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);
    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();
        new FciOrient(new SepsetsSet(this.sepsets, independenceTest)).ruleR0(graph);
        for (Edge edge : new ArrayList<>(graph.getEdges())) {
            if (Thread.currentThread().isInterrupted()) {
                break;
            }
            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);
                if (verbose) {
                    System.out.println("Possible DSEP Removed " + x + "--- " + y + " sepset = " + 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 FciOrient fciOrient = new FciOrient(new SepsetsSet(this.sepsets, 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 39 with Edge

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

the class Vicinity method findRangeU.

// ======%====%=======Private methods===========%==========%=========
// ******************* This finds the range when edge is Undirected **********
private int findRangeU(Edge edge, int chunksize) {
    Set<Edge> edges = new HashSet<>();
    // System.out.println("edges is empty?"+edges.isEmpty());
    // System.out.println("edges is null?"+edges == null);
    NodeEqualityMode.setEqualityMode(NodeEqualityMode.Type.OBJECT);
    int range = 0 - chunksize;
    while (edges.isEmpty()) {
        // increment range by chunk
        range += chunksize;
        // initialize the edge sets
        Set<Edge> node1edges1 = new HashSet<>();
        Set<Edge> node1edges2 = new HashSet<>();
        /*for Vicinity5 the first and second arguments of the for loop need to be modified so that
            * they respect that a single increment of the x/y/z grid scales with that dimension of the voxel
             */
        // create separate range values for x y and z, scaled by xdist ydist zdist
        int xrange = (int) Math.ceil(range / xDist);
        int yrange = (int) Math.ceil(range / yDist);
        int zrange = (int) Math.ceil(range / zDist);
        // list edges with either endpoint near node1
        for (int x = getX(edge.getNode1(), locationMap) - xrange; x <= getX(edge.getNode1(), locationMap) + xrange; x++) {
            for (int y = getY(edge.getNode1(), locationMap) - yrange; y <= getY(edge.getNode1(), locationMap) + yrange; y++) {
                for (int z = getZ(edge.getNode1(), locationMap) - zrange; z <= getZ(edge.getNode1(), locationMap) + zrange; z++) {
                    if (x < xLow || x > xHigh || y < yLow || y > yHigh || z < zLow || z > zHigh)
                        continue;
                    if (Coords1.get(Arrays.asList(x, y, z)) != null)
                        node1edges1.addAll(Coords1.get(Arrays.asList(x, y, z)));
                    if (Coords2.get(Arrays.asList(x, y, z)) != null)
                        node1edges2.addAll(Coords2.get(Arrays.asList(x, y, z)));
                }
            }
        }
        // for bugchecking
        // System.out.println("node1edges1 is empty? "+node1edges1.isEmpty());
        // System.out.println("node1edges2 is empty? "+node1edges2.isEmpty());
        int x2 = getX(edge.getNode2(), locationMap);
        int y2 = getY(edge.getNode2(), locationMap);
        int z2 = getZ(edge.getNode2(), locationMap);
        // if one or both of the above lists is nonempty, find edges where the other endpoint is near node2!
        if (!node1edges1.isEmpty()) {
            for (Edge edge11 : node1edges1) {
                int x = getX(edge11.getNode2(), locationMap);
                int y = getY(edge11.getNode2(), locationMap);
                int z = getZ(edge11.getNode2(), locationMap);
                /*for Vicinity5 the first and second arguments of the for loop need to be modified so that
            * they respect that a single increment of the x/y/z grid scales with that dimension of the voxel
             */
                if (x >= x2 - xrange && x <= x2 + xrange && y >= y2 - yrange && y <= y2 + yrange && z >= z2 - zrange && z <= z2 + zrange) {
                    edges.add(edge11);
                }
            }
        }
        if (!node1edges2.isEmpty()) {
            for (Edge edge12 : node1edges2) {
                int x = getX(edge12.getNode1(), locationMap);
                int y = getY(edge12.getNode1(), locationMap);
                int z = getZ(edge12.getNode1(), locationMap);
                /*for Vicinity5 the first and second arguments of the for loop need to be modified so that
                     *  * they respect that a single increment of the x/y/z grid scales with that dimension of the voxel
                     *  Any time that xyz indexes are compared using range, then some rescaling needs to be done to
                     *  account for how much distance is covered by one increment of that index dimension
                     *  */
                if (x >= x2 - xrange && x <= x2 + xrange && y >= y2 - yrange && y <= y2 + yrange && z >= z2 - zrange && z <= z2 + zrange) {
                    edges.add(edge12);
                }
            }
        }
    // System.out.println("edges is empty?"+edges.isEmpty()+" at range "+range);
    // System.out.println(edges);
    }
    return range;
}
Also used : Edge(edu.cmu.tetrad.graph.Edge)

Example 40 with Edge

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

the class SessionWrappers method addEdge.

public static void addEdge(SessionWrapper sessionWrapper, String nodeName1, String nodeName2) {
    // Retrieve the nodes from the session wrapper.
    Node node1 = sessionWrapper.getNode(nodeName1);
    Node node2 = sessionWrapper.getNode(nodeName2);
    // Make sure nodes existed in the session wrapper by these names.
    if (node1 == null) {
        throw new RuntimeException("There was no node by name nodeName1 in " + "the session wrapper: " + nodeName1);
    }
    if (node2 == null) {
        throw new RuntimeException("There was no node by name nodeName2 in " + "the session wrapper: " + nodeName2);
    }
    // Construct an edge.
    SessionNodeWrapper nodeWrapper1 = (SessionNodeWrapper) node1;
    SessionNodeWrapper nodeWrapper2 = (SessionNodeWrapper) node2;
    Edge edge = new Edge(nodeWrapper1, nodeWrapper2, Endpoint.TAIL, Endpoint.ARROW);
    // Add the edge.
    sessionWrapper.addEdge(edge);
}
Also used : SessionNodeWrapper(edu.cmu.tetradapp.model.SessionNodeWrapper) SessionNode(edu.cmu.tetrad.session.SessionNode) Node(edu.cmu.tetrad.graph.Node) 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