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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations