示例#1
0
def ds_layout(nodes, edges=[], layout='circular'):
    '''
    Return nodes positions based on set layout.]]]]]
    '''
    if layout == 'circular':
        return circular_layout(nodes)
    elif layout == 'forceatlas2':
        return forceatlas2_layout(nodes, edges)
    return random_layout(nodes)
示例#2
0
def on_server_loaded(server_context):
    # Load the raw NX GRAPH, compute ForceLayout node position, and Hammer_bundle the edges
    r_graph_file = os.getenv('CF_GRAPH')
    logger.info('Loading CF_GRAPH={}'.format(r_graph_file))

    r_graph = nx.read_yaml(r_graph_file)
    pd_nodes = pd.DataFrame([(node, node) for node in r_graph.nodes],
                            columns=['id', 'node'])
    pd_nodes.set_index('id', inplace=True)
    pd_edges = pd.DataFrame(list(r_graph.edges), columns=['source', 'target'])

    logger.info('Laying out {} nodes'.format(len(pd_nodes)))
    pd_nodes_layout = forceatlas2_layout(pd_nodes, pd_edges)
    pd_nodes_layout.to_pickle('nodes.pkl')

    logger.info('Bundling {} edges'.format(len(pd_edges)))
    h_bundle = hammer_bundle(pd_nodes_layout, pd_edges)
    h_bundle.to_pickle('edges-bundled.pkl')

    return
示例#3
0
 def plot_graph(self, method='networkx', options={}):
     digr = self.make_graph()
     if method == 'networkx':
         dflt_options = {
             'node_color': 'black',
             'node_size': 20,
             'width': 1,
         }
         opts = {**dflt_options, **options}
         nx.draw_kamada_kawai(digr, **opts)
     elif method == 'datashader':
         digr = digr.to_undirected()
         nodes = pd.DataFrame(digr.nodes(), columns=['name'])
         edges = pd.DataFrame(digr.edges(), columns=['source', 'target'])
         iterations = {
             'iterations': int(np.ceil(np.sqrt(len(nodes)))),
             **options
         }['iterations']
         fd = forceatlas2_layout(nodes, edges, iterations=iterations)
         bundle = {'bundle': False, **options}['bundle']
         if bundle:
             return graphplot(fd, hammer_bundle(fd, edges))
         else:
             return graphplot(fd, connect_edges(fd, edges))
示例#4
0
def test_forceatlas2_unpositioned_nodes_with_unweighted_edges(
        nodes_without_positions, edges):
    df = forceatlas2_layout(nodes_without_positions, edges)
    assert len(nodes_without_positions) == len(df)
    assert not df.equals(nodes_without_positions)
示例#5
0
    def my_graphplot(nodes,
                     edges,
                     name="",
                     canvas=None,
                     cat=None,
                     margin=0.05):
        if canvas is None:
            xr = nodes.x.min() - margin, nodes.x.max() + margin
            yr = nodes.y.min() - margin, nodes.y.max() + margin
            canvas = ds.Canvas(x_range=xr, y_range=yr, **cvsopts)

        np = my_nodesplot(nodes, name + " nodes", canvas, cat)
        ep = edgesplot(edges, name + " edges", canvas)
        return tf.stack(ep, np, how="over", name=name)

    forcedirected = forceatlas2_layout(nodes, edges)
    fd = forcedirected

    fd.iat[0, 2] = (fd.x.min() + fd.x.max()) / 2  # center focus node on x
    fd.iat[0, 3] = 1  # center focus node on y

    ## save image?!

    image = tf.Image(
        my_graphplot(fd,
                     connect_edges(fd, edges),
                     "Force-directed",
                     cat="type",
                     margin=0.02))

    export_image(image, filename=file)
示例#6
0
def test_forceatlas2_positioned_nodes_with_weighted_edges(
        nodes, weighted_edges):
    df = forceatlas2_layout(nodes, weighted_edges)
    assert df.equals(nodes)
示例#7
0
def test_forceatlas2_unpositioned_nodes_with_weighted_edges(nodes_without_positions, weighted_edges):
    df = forceatlas2_layout(nodes_without_positions, weighted_edges)
    assert len(nodes_without_positions) == len(df)
    assert not df.equals(nodes_without_positions)
fuzzy_df.shape

label_df = pd.read_csv("PRJNA301554.hydroponic.sample_annotations.txt",
                       index_col=0,
                       sep='\t')
labels = label_df.astype('object').reset_index()

fuzzy_edges = pd.DataFrame(fuzzy_df.stack()).reset_index()
fuzzy_edges.columns = ['source', 'target', 'weight']
fuzzy_edges.head(2)

cutoff = fuzzy_edges['weight'].quantile(.95)
filtered = fuzzy_edges[fuzzy_edges['weight'] > cutoff]
print(f'Cutoff: {cutoff:.3}')
print(f'Remaining shape: {filtered.shape}')

atlas_layout = forceatlas2_layout(labels, fuzzy_edges, iterations=100)

hv_opts = {}
hv_opts['padding'] = 0.1
hv_opts['width'] = 600
hv_opts['height'] = 400
hv_opts['cmap'] = 'Set1'
hv_opts['xaxis'] = None
hv_opts['yaxis'] = None
hv_opts['size'] = 5

vdims = ['Sample', 'Treatment', 'Genotype', 'Subspecies', 'Time']
hv_nodes = hv.Nodes(atlas_layout, kdims=['x', 'y', 'index'], vdims=vdims)
hv_nodes.opts(**hv_opts)