Beispiel #1
0
def chimera_layout(G, scale=1., center=None, dim=2):
    """Positions the nodes of graph G in a Chimera cross topology.

    NumPy (https://scipy.org) is required for this function.

    Parameters
    ----------
    G : NetworkX graph
        Should be a Chimera graph or a subgraph of a
        Chimera graph. If every node in G has a `chimera_index`
        attribute, those are used to place the nodes. Otherwise makes
        a best-effort attempt to find positions.

    scale : float (default 1.)
        Scale factor. When scale = 1,  all positions fit within [0, 1]
        on the x-axis and [-1, 0] on the y-axis.

    center : None or array (default None)
        Coordinates of the top left corner.

    dim : int (default 2)
        Number of dimensions. When dim > 2, all extra dimensions are
        set to 0.

    Returns
    -------
    pos : dict
        A dictionary of positions keyed by node.

    Examples
    --------
    >>> G = dnx.chimera_graph(1)
    >>> pos = dnx.chimera_layout(G)

    """

    if not isinstance(G, nx.Graph):
        empty_graph = nx.Graph()
        empty_graph.add_edges_from(G)
        G = empty_graph

    # now we get chimera coordinates for the translation
    # first, check if we made it
    if G.graph.get("family") == "chimera":
        m = G.graph['rows']
        n = G.graph['columns']
        t = G.graph['tile']
        # get a node placement function
        xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim)

        if G.graph.get('labels') == 'coordinate':
            pos = {v: xy_coords(*v) for v in G.nodes()}
        elif G.graph.get('data'):
            pos = {
                v: xy_coords(*dat['chimera_index'])
                for v, dat in G.nodes(data=True)
            }
        else:
            coord = chimera_coordinates(m, n, t)
            pos = {
                v: xy_coords(*coord.linear_to_chimera(v))
                for v in G.nodes()
            }
    else:
        # best case scenario, each node in G has a chimera_index attribute. Otherwise
        # we will try to determine it using the find_chimera_indices function.
        if all('chimera_index' in dat for __, dat in G.nodes(data=True)):
            chimera_indices = {
                v: dat['chimera_index']
                for v, dat in G.nodes(data=True)
            }
        else:
            chimera_indices = find_chimera_indices(G)

        # we could read these off of the name attribute for G, but we would want the values in
        # the nodes to override the name in case of conflict.
        m = max(idx[0] for idx in chimera_indices.values()) + 1
        n = max(idx[1] for idx in chimera_indices.values()) + 1
        t = max(idx[3] for idx in chimera_indices.values()) + 1
        xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim)

        # compute our coordinates
        pos = {
            v: xy_coords(i, j, u, k)
            for v, (i, j, u, k) in chimera_indices.items()
        }

    return pos
Beispiel #2
0
def chimera_layout(G, scale=1., center=None, dim=2):
    """Positions the nodes in a Chimera lattice.

    NumPy (http://scipy.org) is required for this function.

    Parameters
    ----------
    G : graph
        A networkx graph. Should be a Chimera graph or a subgraph of a
        Chimera graph. If every node in G has a 'chimera_index'
        attribute, then those are used to place the nodes. Otherwise will
        attempt to find positions, but is not guarunteed to succeed.

    scale : float (default 1.)
        Scale factor. When scale = 1 the all positions will fit within [0, 1]
        on the x-axis and [-1, 0] on the y-axis.

    center : None or array (default None)
        Coordinates of the top left corner.

    dim : int (default 2)
        Number of dimensions. When dim > 2, all extra dimensions are
        set to 0.

    Returns
    -------
    pos : dict
        A dictionary of positions keyed by node.

    Examples
    --------
    >>> G = dnx.chimera_graph(1)
    >>> pos = dnx.chimera_layout(G)

    """

    if not isinstance(G, nx.Graph):
        empty_graph = nx.Graph()
        empty_graph.add_nodes_from(G)
        G = empty_graph

    # best case scenario, each node in G has a chimera_index attribute. Otherwise
    # we will try to determine it using the find_chimera_indices function.
    if all('chimera_index' in dat for __, dat in G.nodes(data=True)):
        chimera_indices = {
            v: dat['chimera_index']
            for v, dat in G.nodes(data=True)
        }
    else:
        chimera_indices = find_chimera_indices(G)

    # we could read these off of the name attribute for G, but we would want the values in
    # the nodes to override the name in case of conflict.
    m = max(idx[0] for idx in itervalues(chimera_indices)) + 1
    n = max(idx[1] for idx in itervalues(chimera_indices)) + 1
    t = max(idx[3] for idx in itervalues(chimera_indices)) + 1

    # ok, given the chimera indices, let's determine the coordinates
    xy_coords = chimera_node_placer_2d(m, n, t, scale, center, dim)
    pos = {
        v: xy_coords(i, j, u, k)
        for v, (i, j, u, k) in iteritems(chimera_indices)
    }

    return pos