Skip to content

Commit

Permalink
matplotlib renders special *_canvas coordinates; set singleton nodes …
Browse files Browse the repository at this point in the history
…default true
  • Loading branch information
ulfaslak committed May 17, 2019
1 parent c1d5860 commit b4bef4a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion netwulf/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def log_message(self, format, *args):
'link_alpha': 0.5,
'link_width_unevenness': 0.5,
# Thresholding
'display_singleton_nodes': False,
'display_singleton_nodes': True,
'min_link_weight_percentile': 0,
'max_link_weight_percentile': 100
}
Expand Down
32 changes: 15 additions & 17 deletions netwulf/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from matplotlib.collections import LineCollection, EllipseCollection


def bind_properties_to_network(G,
def bind_properties_to_network(network,
network_properties,
bind_node_positions=True,
bind_node_color=True,
Expand All @@ -21,11 +21,11 @@ def bind_properties_to_network(G,
bind_link_color=True,
bind_link_alpha=True):
"""
Binds properties from the interactive visualization to the `G` object.
Binds calculated positional values to the network as node attributes `x` and `y`.
Parameters
----------
G : networkx.Graph or networkx.DiGraph
network : networkx.Graph or something alike
The network object to which the position should be bound
network_properties : dict
The network properties which are returned from the
Expand All @@ -48,32 +48,30 @@ def bind_properties_to_network(G,
if bind_node_positions:
x = { node['id']: node['x'] for node in network_properties['nodes'] }
y = { node['id']: node['y'] for node in network_properties['nodes'] }
nx.set_node_attributes(G, x, 'x')
nx.set_node_attributes(G, y, 'y')
G.graph['xlim'] = network_properties['xlim']
G.graph['ylim'] = network_properties['ylim']
nx.set_node_attributes(network, x, 'x')
nx.set_node_attributes(network, y, 'y')
network.graph['rescale'] = False
if bind_node_color:
color = { node['id']: node['color'] for node in network_properties['nodes'] }
nx.set_node_attributes(G, color, 'color')
nx.set_node_attributes(network, color, 'color')
if bind_node_radius:
radius = { node['id']: node['radius'] for node in network_properties['nodes'] }
nx.set_node_attributes(G, radius, 'radius')
nx.set_node_attributes(network, radius, 'radius')

# Add individual link attributes
if bind_link_width:
width = { (link['source'], link['target']): link['width'] for link in network_properties['links'] }
nx.set_edge_attributes(G, width, 'width')
nx.set_edge_attributes(network, width, 'width')

# Add global style properties
if bind_node_stroke_color:
G.graph['nodeStrokeColor'] = network_properties['nodeStrokeColor']
network.graph['nodeStrokeColor'] = network_properties['nodeStrokeColor']
if bind_node_stroke_width:
G.graph['nodeStrokeWidth'] = network_properties['nodeStrokeWidth']
network.graph['nodeStrokeWidth'] = network_properties['nodeStrokeWidth']
if bind_link_color:
G.graph['linkColor'] = network_properties['linkColor']
network.graph['linkColor'] = network_properties['linkColor']
if bind_link_alpha:
G.graph['linkAlpha'] = network_properties['linkAlpha']

network.graph['linkAlpha'] = network_properties['linkAlpha']

def get_filtered_network(network,edge_weight_key=None,node_group_key=None):
"""
Expand Down Expand Up @@ -193,7 +191,7 @@ 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]
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'] }
pos = { node['id']: np.array([node['x_canvas'], height - node['y_canvas']]) for node in network_properties['nodes'] }

lines = []
linewidths = []
Expand Down Expand Up @@ -224,7 +222,7 @@ def draw_netwulf(network_properties, fig=None, ax=None, figsize=None):
node_colors = []

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

0 comments on commit b4bef4a

Please sign in to comment.