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

Improvements to forceatlas2 algorithm #488

Merged
merged 7 commits into from
Oct 14, 2017
Merged
Changes from 4 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
28 changes: 21 additions & 7 deletions datashader/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ def __call__(self, nodes, edges, **params):
return df


def _extract_points_from_nodes(nodes):
def _extract_points_from_nodes(nodes, params, dtype=None):
if 'x' in nodes.columns and 'y' in nodes.columns:
points = np.asarray(nodes[['x', 'y']])
else:
points = np.asarray(np.random.random((len(nodes), 2)))
points = np.asarray(np.random.random((len(nodes), params.dim)), dtype=dtype)
return points


def _convert_graph_to_sparse_matrix(nodes, edges, dtype=None, format='csr'):
def _convert_graph_to_sparse_matrix(nodes, edges, params, dtype=None, format='csr'):
nlen = len(nodes)
if 'id' in nodes:
index = dict(zip(nodes['id'].values, range(nlen)))
else:
index = dict(zip(nodes.index.values, range(nlen)))

if 'weight' not in edges:
if params.ignore_weights or 'weight' not in edges:
edges = edges.copy()
edges['weight'] = np.ones(len(edges))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very expensive step, and doesn't seem feasible as an approach in general, but I guess we can merge and come back to that later.


Expand All @@ -115,11 +115,22 @@ def _convert_graph_to_sparse_matrix(nodes, edges, dtype=None, format='csr'):
for src, dst, weight in [tuple(edge) for edge in edge_values]
if src in index and dst in index))

# symmetrize matrix
# Symmetrize matrix
d = data + data
r = rows + cols
c = cols + rows

# Check for nodes pointing to themselves
loops = edges[edges['source'] == edges['target']]
if len(loops):
loop_values = loops[['source', 'target', 'weight']].values
diag_index, diag_data = zip(*((index[src], -weight)
for src, dst, weight in [tuple(edge) for edge in loop_values]
if src in index and dst in index))
d += diag_data
r += diag_index
c += diag_index

M = scipy.sparse.coo_matrix((d, (r, c)), shape=(nlen, nlen), dtype=dtype)
return M.asformat(format)

Expand Down Expand Up @@ -206,14 +217,17 @@ class forceatlas2_layout(LayoutAlgorithm):
Random seed used to initialize the pseudo-random number
generator.""")

ignore_weights = param.Boolean(False, doc="""
Whether to use weights during layout""")

def __call__(self, nodes, edges, **params):
p = param.ParamOverrides(self, params)

np.random.seed(p.seed)

# Convert graph into sparse adjacency matrix and array of points
points = _extract_points_from_nodes(nodes)
matrix = _convert_graph_to_sparse_matrix(nodes, edges)
points = _extract_points_from_nodes(nodes, p, dtype='f')
matrix = _convert_graph_to_sparse_matrix(nodes, edges, p, dtype='f')

if p.k is None:
p.k = np.sqrt(1.0 / len(points))
Expand Down