Skip to content

Commit

Permalink
Minor bug fix for adding custom node attributes (#2723)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed May 25, 2018
1 parent 49370f9 commit 3c64110
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion holoviews/element/graphs.py
Expand Up @@ -400,7 +400,7 @@ def from_networkx(cls, G, layout_function, nodes=None, **kwargs):
idx_dim = nodes.kdims[-1].name
xs, ys = zip(*[v for k, v in sorted(positions.items())])
indices = list(nodes.dimension_values(idx_dim))
edges = [(src, tgt) for (src, tgt) in edges if src in indices and tgt in indices]
edges = [edge for edge in edges if edge[0] in indices and edge[1] in indices]
nodes = nodes.select(**{idx_dim: [eid for e in edges for eid in e]}).sort()
nodes = nodes.add_dimension('x', 0, xs)
nodes = nodes.add_dimension('y', 1, ys).clone(new_type=cls.node_type)
Expand Down
13 changes: 13 additions & 0 deletions tests/element/testgraphelement.py
Expand Up @@ -167,6 +167,19 @@ def test_from_networkx_only_nodes(self):
G.add_nodes_from([1, 2, 3])
graph = Graph.from_networkx(G, nx.circular_layout)
self.assertEqual(graph.nodes.dimension_values(2), np.array([1, 2, 3]))

@attr(optional=1)
def test_from_networkx_custom_nodes(self):
try:
import networkx as nx
except:
raise SkipTest('Test requires networkx to be installed')
FG = nx.Graph()
FG.add_weighted_edges_from([(1,2,0.125), (1,3,0.75), (2,4,1.2), (3,4,0.375)])
nodes = Dataset([(1, 'A'), (2, 'B'), (3, 'A'), (4, 'B')], 'index', 'some_attribute')
graph = Graph.from_networkx(FG, nx.circular_layout, nodes=nodes)
self.assertEqual(graph.nodes.dimension_values('some_attribute'), np.array(['A', 'B', 'A', 'B']))


class ChordTests(ComparisonTestCase):

Expand Down

0 comments on commit 3c64110

Please sign in to comment.