Skip to content

Fix: Remove spurious nodes in dot files due to \n #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion causal_testing/specification/causal_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ class CausalDAG(nx.DiGraph):
def __init__(self, dot_path: str = None, **attr):
super().__init__(**attr)
if dot_path:
pydot_graph = pydot.graph_from_dot_file(dot_path)
with open(dot_path, 'r', encoding='utf-8') as file:
dot_content = file.read().replace('\n', '')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be worth adding a comment describing this, as the problem this is solving is not obvious from the code.

# Previously, we used pydot_graph_from_file() to read in the dot_path directly, however,
# this method does not currently have a way of removing spurious nodes.
# Workaround: Read in the file using open(), remove new lines, and then create the pydot_graph.
pydot_graph = pydot.graph_from_dot_data(dot_content)
self.graph = nx.DiGraph(nx.drawing.nx_pydot.from_pydot(pydot_graph[0]))
else:
self.graph = nx.DiGraph()
Expand Down