Skip to content

Commit

Permalink
Merge branch 'master' of github.com:benmaier/netwulf
Browse files Browse the repository at this point in the history
  • Loading branch information
benmaier committed Apr 11, 2019
2 parents 9967727 + b86d383 commit b020f0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
12 changes: 8 additions & 4 deletions netwulf/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,16 @@ def log_message(self, format, *args):
def visualize(network,
port=9853,
verbose=False,
config=None):
config=None,
plot_in_cell_below=True):
"""
Visualize a network interactively using Ulf Aslak's d3 web app.
Saves the network as json, saves the passed config and runs
a local HTTP server which then runs the web app.
Parameters
----------
network : networkx.Graph or networkx.DiGraph
network : networkx.Graph or networkx.DiGraph or node-link dictionary
The network to visualize
port : int, default : 9853
The port at which to run the server locally.
Expand Down Expand Up @@ -245,7 +246,10 @@ def visualize(network,
configpath = os.path.join(web_dir, configname)

with open(filepath,'w') as f:
json.dump(nx.node_link_data(network), f, iterable_as_array=True)
if type(network) is nx.Graph:
json.dump(nx.node_link_data(network), f, iterable_as_array=True)
else:
json.dump(network, f, iterable_as_array=True)

with open(configpath,'w') as f:
json.dump(this_config, f)
Expand Down Expand Up @@ -301,7 +305,7 @@ def visualize(network,
# apparently this is how it has to be on Windows
is_jupyter = 'JPY_PARENT_PID' in env

if is_jupyter and not is_keyboard_interrupted:
if is_jupyter and plot_in_cell_below and not is_keyboard_interrupted:
fig, ax = wulf.draw_netwulf(posted_network_properties)

return posted_network_properties, posted_config
Expand Down
7 changes: 4 additions & 3 deletions netwulf/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,13 @@ def draw_netwulf(network_properties, fig=None, ax=None, figsize=None):

# filter out node positions for links
width = network_properties['xlim'][1] - network_properties['xlim'][0]
pos = { node['id']: np.array([node['x'], node['y']]) for node in network_properties['nodes'] }
height = network_properties['ylim'][1] - network_properties['ylim'][0]
pos = { node['id']: np.array([node['x'], height - node['y']]) for node in network_properties['nodes'] }

lines = []
linewidths = []
for link in network_properties['links']:
u, v = link['link']
u, v = link['source'], link['target']
lines.append([
[pos[u][0], pos[v][0]],
[pos[u][1], pos[v][1]]
Expand All @@ -175,7 +176,7 @@ def draw_netwulf(network_properties, fig=None, ax=None, figsize=None):
node_colors = []

for node in network_properties['nodes']:
XY.append([node['x'], node['y']])
XY.append([node['x'], height - node['y']])
# size has to be given in points**2
size.append( 2*node['radius'] )
node_colors.append(node['color'])
Expand Down

0 comments on commit b020f0d

Please sign in to comment.