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