use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.
the class ForbiddenGraphModel method createKnowledge.
private void createKnowledge(Parameters params) {
IKnowledge knwl = getKnowledge();
if (knwl == null) {
return;
}
knwl.clear();
List<String> varNames = getVarNames();
getKnowledgeBoxInput().getVariableNames().stream().filter(e -> !e.startsWith("E_")).forEach(e -> {
varNames.add(e);
knwl.addVariable(e);
});
if (resultGraph == null) {
throw new NullPointerException("I couldn't find a parent graph.");
}
List<Node> nodes = resultGraph.getNodes();
int numOfNodes = nodes.size();
for (int i = 0; i < numOfNodes; i++) {
for (int j = i + 1; j < numOfNodes; j++) {
Node n1 = nodes.get(i);
Node n2 = nodes.get(j);
if (n1.getName().startsWith("E_") || n2.getName().startsWith("E_")) {
continue;
}
Edge edge = resultGraph.getEdge(n1, n2);
if (edge != null && edge.isDirected()) {
knwl.setForbidden(edge.getNode2().getName(), edge.getNode1().getName());
}
}
}
}
use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.
the class AbstractWorkbench method finishEdge.
/**
* Finishes the drawing of a new edge.
*
* @see #startEdge
*/
private void finishEdge() {
if (getTrackedEdge() == null) {
return;
}
// Retrieve the two display components this edge should connect.
DisplayNode comp1 = getTrackedEdge().getComp1();
Point p = getTrackedEdge().getTrackPoint();
DisplayNode comp2 = findNearestNode(p);
// Edges to self are not supported.
if (comp1 != comp2) {
// Construct the model edge
try {
Node node1 = (Node) (getDisplayToModel().get(comp1));
Node node2 = (Node) (getDisplayToModel().get(comp2));
Edge modelEdge = getNewModelEdge(node1, node2);
// Add model edge to model; this will result in an event fired
// back from the model to add a display edge, so we can remove
// the tracked edge and forget about it.
graph.addEdge(modelEdge);
setGraph(graph);
firePropertyChange("modelChanged", null, null);
} catch (Exception e) {
e.printStackTrace();
if (isNodeEdgeErrorsReported()) {
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), e.getMessage());
}
}
}
remove((Component) getTrackedEdge());
repaint();
// reset the tracked edge to null to wait for the next attempt
// at adding an edge.
this.trackedEdge = null;
}
use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.
the class AbstractWorkbench method setGraphWithoutNotify.
// ============================PRIVATE METHODS=========================//
/**
* Sets the display workbench model to the indicated model. (Called when the
* workbench is first constructed as well as whenever the workbench model is
* changed.)
*/
private void setGraphWithoutNotify(Graph graph) {
if (graph == null) {
throw new IllegalArgumentException("Graph model cannot be null.");
}
if (graph instanceof SessionWrapper) {
this.graph = graph;
} else {
this.graph = graph;
if (graph.isPag()) {
GraphUtils.addPagColoring(new EdgeListGraph(graph));
}
}
this.modelEdgesToDisplay = new HashMap<>();
this.modelNodesToDisplay = new HashMap<>();
this.displayToModel = new HashMap();
this.displayToLabels = new HashMap();
removeAll();
graph.addPropertyChangeListener(this.propChangeHandler);
// extract the current contents from the model...
List<Node> nodes = graph.getNodes();
for (Node node : nodes) {
if (!getModelNodesToDisplay().containsKey(node)) {
addNode(node);
}
}
Set<Edge> edges = graph.getEdges();
for (Edge edge : edges) {
if (!getModelEdgesToDisplay().containsKey(edge)) {
addEdge(edge);
}
}
adjustPreferredSize();
if (getPreferredSize().getWidth() > getMaxX()) {
setMaxX((int) getPreferredSize().getWidth());
}
if (getPreferredSize().getHeight() > getMaxY()) {
setMaxY((int) getPreferredSize().getHeight());
}
revalidate();
repaint();
}
use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.
the class PossibleDsepFci method search.
// ============================== Public Methods =========================//
/**
* Performs pairwise comparisons of each variable in the graph with the variables that have not already been
* checked. We get the Possible-D-Sep sets for the pair of variables, and we check to see if they are independent
* conditional on some subset of the union of Possible-D-Sep sets. This method returns the SepSet passed in the
* constructor (if any), possibly augmented by some edge removals in this step. The GaSearchGraph passed in the
* constructor is directly changed.
*/
public SepsetMap search() {
for (Edge edge : new ArrayList<>(graph.getEdges())) {
Node x = edge.getNode1();
Node y = edge.getNode2();
List<Node> condSet = getSepset(x, y);
if (condSet != null) {
for (Node n : condSet) {
if (!(graph.getAdjacentNodes(n).contains(x) || graph.getAdjacentNodes(n).contains(y))) {
System.out.println("Not adjacent");
}
}
graph.removeEdge(x, y);
sepset.set(x, y, condSet);
System.out.println("Removed " + x + "--- " + y + " sepset = " + condSet);
}
}
return sepset;
}
use of edu.cmu.tetrad.graph.Edge in project tetrad by cmu-phil.
the class TimeoutComparison method getSubgraph.
private Graph getSubgraph(Graph graph, boolean discrete1, boolean discrete2, DataModel DataModel) {
if (discrete1 && discrete2) {
Graph newGraph = new EdgeListGraph(graph.getNodes());
for (Edge edge : graph.getEdges()) {
Node node1 = DataModel.getVariable(edge.getNode1().getName());
Node node2 = DataModel.getVariable(edge.getNode2().getName());
if (node1 instanceof DiscreteVariable && node2 instanceof DiscreteVariable) {
newGraph.addEdge(edge);
}
}
return newGraph;
} else if (!discrete1 && !discrete2) {
Graph newGraph = new EdgeListGraph(graph.getNodes());
for (Edge edge : graph.getEdges()) {
Node node1 = DataModel.getVariable(edge.getNode1().getName());
Node node2 = DataModel.getVariable(edge.getNode2().getName());
if (node1 instanceof ContinuousVariable && node2 instanceof ContinuousVariable) {
newGraph.addEdge(edge);
}
}
return newGraph;
} else {
Graph newGraph = new EdgeListGraph(graph.getNodes());
for (Edge edge : graph.getEdges()) {
Node node1 = DataModel.getVariable(edge.getNode1().getName());
Node node2 = DataModel.getVariable(edge.getNode2().getName());
if (node1 instanceof DiscreteVariable && node2 instanceof ContinuousVariable) {
newGraph.addEdge(edge);
}
if (node1 instanceof ContinuousVariable && node2 instanceof DiscreteVariable) {
newGraph.addEdge(edge);
}
}
return newGraph;
}
}
Aggregations