Skip to content
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

add build graph networkx #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions hiddenlayer/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,33 @@ def build_dot(self):
dot.edge(str(a), str(b), label)
return dot

def build_nx(self):
"""Generate a Networkx graph.

Returns a Networkx Digraph object.
"""
import networkx as nx

# Build GraphViz Digraph
G = nx.DiGraph()
nodes = []
for k, n in self.nodes.items():
label = "{} ".format(n.title)
if n.caption:
label += "{} ".format(n.caption)
if n.repeat > 1:
label += "x{}".format(n.repeat)
label = " " + label + " "
nodes.append((str(k), {'label': label, 'shape': 'box'}))
G.add_nodes_from(nodes)

for a, b, label in self.edges:
if isinstance(label, (list, tuple)):
label = "x".join([str(l or "?") for l in label])
G.add_edge(str(a), str(b), label=label)
# dot.edge(str(a), str(b), label)
return G

def _repr_svg_(self):
"""Allows Jupyter notebook to render the graph automatically."""
return self.build_dot()._repr_svg_()
Expand Down