From 8cf02e151c3a34f9b70eaa91800399eb58146efa Mon Sep 17 00:00:00 2001 From: f-allian Date: Tue, 12 Mar 2024 11:06:20 +0000 Subject: [PATCH 1/4] fix: remove spurious nodes in dot files. --- causal_testing/specification/causal_dag.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/causal_testing/specification/causal_dag.py b/causal_testing/specification/causal_dag.py index 1b47d3af..2e82cbb4 100644 --- a/causal_testing/specification/causal_dag.py +++ b/causal_testing/specification/causal_dag.py @@ -133,7 +133,9 @@ 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') as file: + dot_content = file.read().replace('\n', '') + 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() From 7e604a42c7c107ac98fef1e3fc721b6fe7430a9e Mon Sep 17 00:00:00 2001 From: f-allian Date: Tue, 12 Mar 2024 11:16:25 +0000 Subject: [PATCH 2/4] fix: linting previous commit --- causal_testing/specification/causal_dag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/causal_testing/specification/causal_dag.py b/causal_testing/specification/causal_dag.py index 2e82cbb4..9acb692e 100644 --- a/causal_testing/specification/causal_dag.py +++ b/causal_testing/specification/causal_dag.py @@ -133,7 +133,7 @@ class CausalDAG(nx.DiGraph): def __init__(self, dot_path: str = None, **attr): super().__init__(**attr) if dot_path: - with open(dot_path, 'r') as file: + with open(dot_path, 'r', encoding='utf-8') as file: dot_content = file.read().replace('\n', '') pydot_graph = pydot.graph_from_dot_data(dot_content) self.graph = nx.DiGraph(nx.drawing.nx_pydot.from_pydot(pydot_graph[0])) From 675bf4e394471b4e1e09f40dfef77984534492de Mon Sep 17 00:00:00 2001 From: f-allian Date: Tue, 12 Mar 2024 11:35:12 +0000 Subject: [PATCH 3/4] fix: added in-line comment to describe this change. --- causal_testing/specification/causal_dag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/causal_testing/specification/causal_dag.py b/causal_testing/specification/causal_dag.py index 9acb692e..885f564b 100644 --- a/causal_testing/specification/causal_dag.py +++ b/causal_testing/specification/causal_dag.py @@ -134,7 +134,7 @@ def __init__(self, dot_path: str = None, **attr): super().__init__(**attr) if dot_path: with open(dot_path, 'r', encoding='utf-8') as file: - dot_content = file.read().replace('\n', '') + dot_content = file.read().replace('\n', '') # Remove any newlines before creating 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: From f9db25d33f998bdd279160ec322c3fe28c24fa6e Mon Sep 17 00:00:00 2001 From: f-allian Date: Tue, 12 Mar 2024 11:42:15 +0000 Subject: [PATCH 4/4] fix: added more descriptive comments for the changes required. --- causal_testing/specification/causal_dag.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/causal_testing/specification/causal_dag.py b/causal_testing/specification/causal_dag.py index 885f564b..f85e0b58 100644 --- a/causal_testing/specification/causal_dag.py +++ b/causal_testing/specification/causal_dag.py @@ -134,7 +134,10 @@ def __init__(self, dot_path: str = None, **attr): super().__init__(**attr) if dot_path: with open(dot_path, 'r', encoding='utf-8') as file: - dot_content = file.read().replace('\n', '') # Remove any newlines before creating the pydot_graph. + dot_content = file.read().replace('\n', '') + # 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: