Skip to content

Commit

Permalink
Compute Graph Nodes lazily if none supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Sep 20, 2017
1 parent 5e646d6 commit 06765b7
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions holoviews/element/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,27 @@ def __init__(self, data, **params):
node_info = None
if edgepaths is not None and not isinstance(edgepaths, EdgePaths):
edgepaths = EdgePaths(edgepaths)
self.nodes = nodes
self._nodes = nodes
self._edgepaths = edgepaths
super(Graph, self).__init__(edges, **params)
if self.nodes is None:
nodes = layout_nodes(self)
if node_info:
nodes = nodes.clone(datatype=['pandas', 'dictionary'])
for d in node_info.dimensions():
nodes = nodes.add_dimension(d, len(nodes.vdims),
node_info.dimension_values(d),
vdim=True)
self.nodes = nodes
if self._nodes is None and node_info:
nodes = self.nodes.clone(datatype=['pandas', 'dictionary'])
for d in node_info.dimensions():
nodes = nodes.add_dimension(d, len(nodes.vdims),
node_info.dimension_values(d),
vdim=True)
self._nodes = nodes
if self._edgepaths:
mismatch = []
for kd1, kd2 in zip(self.nodes.kdims, self.edgepaths.kdims):
if kd1 != kd2:
print(kd1, kd2)
mismatch.append('%s != %s' % (kd1, kd2))
if mismatch:
raise ValueError('Ensure that the first two key dimensions on '
'Nodes and EdgePaths match: %s' % ', '.join(mismatch))
self.redim = graph_redim(self, mode='dataset')


def clone(self, data=None, shared_data=True, new_type=None, *args, **overrides):
if data is None:
data = (self.data, self.nodes)
Expand Down Expand Up @@ -180,11 +178,30 @@ def range(self, dimension, data_range=True):

def dimensions(self, selection='all', label=False):
dimensions = super(Graph, self).dimensions(selection, label)
if self.nodes and selection == 'ranges':
return dimensions+self.nodes.dimensions(selection, label)
if selection == 'ranges':
if self._nodes:
node_dims = self.nodes.dimensions(selection, label)
else:
node_dims = Nodes.kdims+Nodes.vdims
if label in ['name', True, 'short']:
node_dims = [d.name for d in node_dims]
elif label in ['long', 'label']:
node_dims = [d.label for d in node_dims]
return dimensions+node_dims
return dimensions


@property
def nodes(self):
"""
If no Nodes were supplied computes the Nodes element the first
time they have been supplied.

This comment has been minimized.

Copy link
@jbednar

jbednar Sep 20, 2017

Member

supplied->requested?

This comment has been minimized.

Copy link
@philippjfr

philippjfr Sep 20, 2017

Author Member

Yes, thanks.

"""
if self._nodes is None:
self._nodes = layout_nodes(self)
return self._nodes


@property
def edgepaths(self):
"""
Expand Down

0 comments on commit 06765b7

Please sign in to comment.