File: circular.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (49 lines) | stat: -rw-r--r-- 1,559 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
"""
Circular Layout
===============

This module contains several graph layouts which rely heavily on circles.
"""

import numpy as np

from ..util import _straight_line_vertices, issparse


def circular(adjacency_mat, directed=False):
    """Places all nodes on a single circle.

    Parameters
    ----------
    adjacency_mat : matrix or sparse
        The graph adjacency matrix
    directed : bool
        Whether the graph is directed. If this is True, is will also
        generate the vertices for arrows, which can be passed to an
        ArrowVisual.

    Yields
    ------
    (node_vertices, line_vertices, arrow_vertices) : tuple
        Yields the node and line vertices in a tuple. This layout only yields a
        single time, and has no builtin animation
    """
    if issparse(adjacency_mat):
        adjacency_mat = adjacency_mat.tocoo()

    num_nodes = adjacency_mat.shape[0]

    t = np.linspace(0, 2 * np.pi, num_nodes, endpoint=False)

    # Visual coordinate system is between 0 and 1, so generate a circle with
    # radius 0.5 and center it at the point (0.5, 0.5).
    node_coords = (0.5 * np.array([np.cos(t), np.sin(t)]) + 0.5).T
    node_coords = node_coords.astype(np.float32)

    line_vertices, arrows = _straight_line_vertices(adjacency_mat,
                                                    node_coords, directed)

    yield node_coords, line_vertices, arrows