Search in sources :

Example 21 with GraphNode

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

the class IndependenceFactsModel method loadFacts.

public static IndependenceFactsModel loadFacts(Reader reader) throws IOException {
    IndependenceFactsModel facts = new IndependenceFactsModel();
    Set<String> names = new HashSet<>();
    Map<String, Node> nodes = new HashMap<>();
    BufferedReader in = new BufferedReader(reader);
    String line;
    while ((line = in.readLine()) != null) {
        String[] tokens = line.split("[ ,;_|]+");
        if (tokens.length == 0)
            continue;
        if (tokens.length < 2)
            throw new IllegalArgumentException("Must specify at least two variables--e.g. X1 X2, for X1 _||_ X2.");
        for (String token : tokens) {
            names.add(token);
            if (!nodes.containsKey(token)) {
                nodes.put(token, new GraphNode(token));
            }
        }
        List<Node> z = new ArrayList<>();
        for (int i = 2; i < tokens.length; i++) {
            z.add(nodes.get(tokens[i]));
        }
        facts.add(new IndependenceFact(nodes.get(tokens[0]), nodes.get(tokens[1]), z));
    }
    return facts;
}
Also used : GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) BufferedReader(java.io.BufferedReader) IndependenceFact(edu.cmu.tetrad.graph.IndependenceFact)

Example 22 with GraphNode

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

the class SemEstimatorGibbsParams method serializableInstance.

/**
 * Generates a simple exemplar of this class to test serialization.
 */
public static SemEstimatorGibbsParams serializableInstance() {
    SemGraph graph = new SemGraph();
    graph.addNode(new GraphNode("X"));
    return new SemEstimatorGibbsParams(new SemIm(new SemPm(graph)), false, 0.0d, 1);
}
Also used : SemGraph(edu.cmu.tetrad.graph.SemGraph) GraphNode(edu.cmu.tetrad.graph.GraphNode)

Example 23 with GraphNode

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

the class XdslXmlParser method buildIM.

private BayesIm buildIM(Element element0, Map<String, String> displayNames) {
    Elements elements = element0.getChildElements();
    for (int i = 0; i < elements.size(); i++) {
        if (!"cpt".equals(elements.get(i).getQualifiedName())) {
            throw new IllegalArgumentException("Expecting cpt element.");
        }
    }
    Dag dag = new Dag();
    // Get the nodes.
    for (int i = 0; i < elements.size(); i++) {
        Element cpt = elements.get(i);
        String name = cpt.getAttribute(0).getValue();
        if (displayNames == null) {
            dag.addNode(new GraphNode(name));
        } else {
            dag.addNode(new GraphNode(displayNames.get(name)));
        }
    }
    // Get the edges.
    for (int i = 0; i < elements.size(); i++) {
        Element cpt = elements.get(i);
        Elements cptElements = cpt.getChildElements();
        for (int j = 0; j < cptElements.size(); j++) {
            Element cptElement = cptElements.get(j);
            if (cptElement.getQualifiedName().equals("parents")) {
                String list = cptElement.getValue();
                String[] parentNames = list.split(" ");
                for (String name : parentNames) {
                    if (displayNames == null) {
                        edu.cmu.tetrad.graph.Node parent = dag.getNode(name);
                        edu.cmu.tetrad.graph.Node child = dag.getNode(cpt.getAttribute(0).getValue());
                        dag.addDirectedEdge(parent, child);
                    } else {
                        edu.cmu.tetrad.graph.Node parent = dag.getNode(displayNames.get(name));
                        edu.cmu.tetrad.graph.Node child = dag.getNode(displayNames.get(cpt.getAttribute(0).getValue()));
                        dag.addDirectedEdge(parent, child);
                    }
                }
            }
        }
        String name;
        if (displayNames == null) {
            name = cpt.getAttribute(0).getValue();
        } else {
            name = displayNames.get(cpt.getAttribute(0).getValue());
        }
        dag.addNode(new GraphNode(name));
    }
    // PM
    BayesPm pm = new BayesPm(dag);
    for (int i = 0; i < elements.size(); i++) {
        Element cpt = elements.get(i);
        String varName = cpt.getAttribute(0).getValue();
        Node node;
        if (displayNames == null) {
            node = dag.getNode(varName);
        } else {
            node = dag.getNode(displayNames.get(varName));
        }
        Elements cptElements = cpt.getChildElements();
        List<String> stateNames = new ArrayList<>();
        for (int j = 0; j < cptElements.size(); j++) {
            Element cptElement = cptElements.get(j);
            if (cptElement.getQualifiedName().equals("state")) {
                Attribute attribute = cptElement.getAttribute(0);
                String stateName = attribute.getValue();
                stateNames.add(stateName);
            }
        }
        pm.setCategories(node, stateNames);
    }
    // IM
    BayesIm im = new MlBayesIm(pm);
    for (int nodeIndex = 0; nodeIndex < elements.size(); nodeIndex++) {
        Element cpt = elements.get(nodeIndex);
        Elements cptElements = cpt.getChildElements();
        for (int j = 0; j < cptElements.size(); j++) {
            Element cptElement = cptElements.get(j);
            if (cptElement.getQualifiedName().equals("probabilities")) {
                String list = cptElement.getValue();
                String[] probsStrings = list.split(" ");
                List<Double> probs = new ArrayList<>();
                for (String probString : probsStrings) {
                    probs.add(Double.parseDouble(probString));
                }
                int count = -1;
                for (int row = 0; row < im.getNumRows(nodeIndex); row++) {
                    for (int col = 0; col < im.getNumColumns(nodeIndex); col++) {
                        im.setProbability(nodeIndex, row, col, probs.get(++count));
                    }
                }
            }
        }
    }
    return im;
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) Attribute(nu.xom.Attribute) Element(nu.xom.Element) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) ArrayList(java.util.ArrayList) GraphNode(edu.cmu.tetrad.graph.GraphNode) Dag(edu.cmu.tetrad.graph.Dag) Elements(nu.xom.Elements) Node(edu.cmu.tetrad.graph.Node) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesPm(edu.cmu.tetrad.bayes.BayesPm)

Example 24 with GraphNode

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

the class TestEvidence method sampleBayesIm2.

private static BayesIm sampleBayesIm2() {
    Node a = new GraphNode("a");
    Node b = new GraphNode("b");
    Node c = new GraphNode("c");
    Dag graph;
    graph = new Dag();
    graph.addNode(a);
    graph.addNode(b);
    graph.addNode(c);
    graph.addDirectedEdge(a, b);
    graph.addDirectedEdge(a, c);
    graph.addDirectedEdge(b, c);
    BayesPm bayesPm = new BayesPm(graph);
    bayesPm.setNumCategories(b, 3);
    BayesIm bayesIm1 = new MlBayesIm(bayesPm);
    bayesIm1.setProbability(0, 0, 0, .3);
    bayesIm1.setProbability(0, 0, 1, .7);
    bayesIm1.setProbability(1, 0, 0, .3);
    bayesIm1.setProbability(1, 0, 1, .4);
    bayesIm1.setProbability(1, 0, 2, .3);
    bayesIm1.setProbability(1, 1, 0, .6);
    bayesIm1.setProbability(1, 1, 1, .1);
    bayesIm1.setProbability(1, 1, 2, .3);
    bayesIm1.setProbability(2, 0, 0, .9);
    bayesIm1.setProbability(2, 0, 1, .1);
    bayesIm1.setProbability(2, 1, 0, .1);
    bayesIm1.setProbability(2, 1, 1, .9);
    bayesIm1.setProbability(2, 2, 0, .5);
    bayesIm1.setProbability(2, 2, 1, .5);
    bayesIm1.setProbability(2, 3, 0, .2);
    bayesIm1.setProbability(2, 3, 1, .8);
    bayesIm1.setProbability(2, 4, 0, .6);
    bayesIm1.setProbability(2, 4, 1, .4);
    bayesIm1.setProbability(2, 5, 0, .7);
    bayesIm1.setProbability(2, 5, 1, .3);
    return bayesIm1;
}
Also used : MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) BayesIm(edu.cmu.tetrad.bayes.BayesIm) MlBayesIm(edu.cmu.tetrad.bayes.MlBayesIm) GraphNode(edu.cmu.tetrad.graph.GraphNode) Node(edu.cmu.tetrad.graph.Node) GraphNode(edu.cmu.tetrad.graph.GraphNode) Dag(edu.cmu.tetrad.graph.Dag) BayesPm(edu.cmu.tetrad.bayes.BayesPm)

Example 25 with GraphNode

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

the class TestFruchtermanReingoldLayout method testLayout2.

@Test
public void testLayout2() {
    Dag dag = new Dag();
    GraphNode x1 = new GraphNode("X1");
    GraphNode x2 = new GraphNode("X2");
    x1.setCenter(40, 5);
    x2.setCenter(50, 5);
    dag.addNode(x1);
    dag.addNode(x2);
    dag.addDirectedEdge(x1, x2);
    Dag dag2 = new Dag(dag);
    FruchtermanReingoldLayout layout = new FruchtermanReingoldLayout(dag);
    layout.doLayout();
    assertEquals(dag, dag2);
}
Also used : GraphNode(edu.cmu.tetrad.graph.GraphNode) Dag(edu.cmu.tetrad.graph.Dag) FruchtermanReingoldLayout(edu.cmu.tetrad.graph.FruchtermanReingoldLayout) Test(org.junit.Test)

Aggregations

GraphNode (edu.cmu.tetrad.graph.GraphNode)41 Node (edu.cmu.tetrad.graph.Node)37 Dag (edu.cmu.tetrad.graph.Dag)20 Test (org.junit.Test)11 EdgeListGraph (edu.cmu.tetrad.graph.EdgeListGraph)7 Graph (edu.cmu.tetrad.graph.Graph)7 DisplayNode (edu.cmu.tetradapp.workbench.DisplayNode)6 BayesIm (edu.cmu.tetrad.bayes.BayesIm)3 BayesPm (edu.cmu.tetrad.bayes.BayesPm)3 MlBayesIm (edu.cmu.tetrad.bayes.MlBayesIm)3 IndependenceFact (edu.cmu.tetrad.graph.IndependenceFact)3 ArrayList (java.util.ArrayList)3 FruchtermanReingoldLayout (edu.cmu.tetrad.graph.FruchtermanReingoldLayout)2 DiscreteVariable (edu.cmu.tetrad.data.DiscreteVariable)1 IndependenceFacts (edu.cmu.tetrad.data.IndependenceFacts)1 Endpoint (edu.cmu.tetrad.graph.Endpoint)1 SemGraph (edu.cmu.tetrad.graph.SemGraph)1 TimeLagGraph (edu.cmu.tetrad.graph.TimeLagGraph)1 IndependenceFactsModel (edu.cmu.tetradapp.model.IndependenceFactsModel)1 BufferedReader (java.io.BufferedReader)1