Skip to content

Commit 2fe2ec3

Browse files
committed
Encapsulate node names and attr_map in double quotes
Before running `to_pydot`, we encapsulate node names and attr_map in double quotes to avoid syntax errors when the node names contain special characters. Implements the workarounds described in #1176 (comment) and pydot/pydot#258 (comment)
1 parent b755c84 commit 2fe2ec3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

datajoint/diagram.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,34 @@ def _make_graph(self):
300300
nx.relabel_nodes(graph, mapping, copy=False)
301301
return graph
302302

303+
@staticmethod
304+
def _stringify_and_encapsulate_edge_attributes(graph):
305+
"""
306+
Modifies the `nx.Graph`'s edge attribute `attr_map` to be a string representation
307+
of the attribute map, and encapsulates the string in double quotes.
308+
Changes the graph in place.
309+
310+
Implements workaround described in
311+
https://github.com/pydot/pydot/issues/258#issuecomment-795798099
312+
"""
313+
for u, v, *_, edgedata in graph.edges(data=True):
314+
if "attr_map" in edgedata:
315+
graph.edges[u, v]["attr_map"] = '"{0}"'.format(
316+
edgedata["attr_map"]
317+
)
318+
319+
@staticmethod
320+
def _stringify_and_encapsulate_node_names(graph):
321+
"""
322+
Modifies the `nx.Graph`'s node names string representations encapsulated in
323+
double quotes.
324+
"""
325+
nx.relabel_nodes(
326+
graph,
327+
{node: '"{0}"'.format(node) for node in graph.nodes()},
328+
copy=False
329+
)
330+
303331
def make_dot(self):
304332
graph = self._make_graph()
305333
graph.nodes()
@@ -368,6 +396,8 @@ def make_dot(self):
368396
for node, d in dict(graph.nodes(data=True)).items()
369397
}
370398

399+
self._stringify_and_encapsulate_node_names(graph)
400+
self._stringify_and_encapsulate_edge_attributes(graph)
371401
dot = nx.drawing.nx_pydot.to_pydot(graph)
372402
for node in dot.get_nodes():
373403
node.set_shape("circle")

0 commit comments

Comments
 (0)