Code Monkey home page Code Monkey logo

communitylayout's Introduction

Community Layout

Layout of large networkx graphs with community segmentation

A small package used to visualise community-structured networkx graphs.

Layout of communities individually, with communities themselves arranged according to their inter-links, allows far faster visualisation of large graphs.

Note: This is an early version, and usage will likely be changed as new functionality is added.

Usage

The layout class is accessed via:

from community_layout.layout_class import CommunityLayout

Position calculation is during __init__. After __init__, CommunityLayout(G).full_positions is an dictionary of keys corresponding to node positions, as in networkx layout algorithms.

Basic usage is as follows:

import networkx as nx
from community_layout.layout_class import CommunityLayout

G = nx.gnm_random_graph(2000, 40000)
layout = CommunityLayout(G)
pos = layout.full_positions

There are several parameters that can be changed by the user:

CommunityLayout(self,
                G,
                community_compression = 0.25,
                layout_algorithm = nx.spring_layout,
                layout_kwargs = {"k":75, "iterations":1000},
                community_algorithm = community.louvain_communities,
                community_kwargs = {"resolution":2})

These are designed to work with networkx functions, but custom functions will work assuming similar syntax and that they return the same objects.

community_algorithm can also be a list of pre-determined communities, ie ["community_1":{"node_id1", "node_id2",...}, "community_2":{"node_id3", "node_id4",...}].

community_compression controls the scale compression of individual community sub-layouts. Lower values will result in each community occupying smaller portions of the overall layout.

Optionally we also include figure plotting:

layout.display(colors = None,
               bundle = False,
               complex_alphas = True,
               ax = None)

Which, if passed ax = matplotlib.Axes, returns the same. If no Axes is passed, it saves a .png in the working directory and calls plt.show().

Note: Edge bundling is currently a much heavier process than the community layout process.

Requirements

Requires the packages numpy, pandas, tqdm, scipy and networkx to be installed.

matplotlib is required for figure plotting. Edge bundling requires datashader and scikit-image.

communitylayout's People

Contributors

neutralpronoun avatar

Watchers

 avatar

communitylayout's Issues

Issue when node isn't in a community

Sometimes you get this error:


KeyError Traceback (most recent call last)
File .../networkx/drawing/nx_pylab.py:429, in draw_networkx_nodes(G, pos, nodelist, node_size, node_color, node_shape, alpha, cmap, vmin, vmax, ax, linewidths, edgecolors, label, margins)
428 try:
--> 429 xy = np.asarray([pos[v] for v in nodelist])
430 except KeyError as err:

KeyError: 'code_summary-100'

The above exception was the direct cause of the following exception:

NetworkXError Traceback (most recent call last)
Cell In[24], line 1
----> 1 layout.display()

File .../community_layout/layout_class.py:153, in CommunityLayout.display(self, colors, bundle, complex_alphas, ax)
151 else:
152 save = False
--> 153 nx.draw_networkx_nodes(self.G, pos=self.full_positions, node_size=2, ax = ax, node_color=colors)
154 nx.draw_networkx_edges(self.G, pos=self.full_positions, node_size=2, width=alphas, alpha=alphas, ax = ax)
156 if save:

File .../networkx/drawing/nx_pylab.py:431, in draw_networkx_nodes(G, pos, nodelist, node_size, node_color, node_shape, alpha, cmap, vmin, vmax, ax, linewidths, edgecolors, label, margins)
429 xy = np.asarray([pos[v] for v in nodelist])
430 except KeyError as err:
--> 431 raise nx.NetworkXError(f"Node {err} has no position.") from err
433 if isinstance(alpha, Iterable):
434 node_color = apply_alpha(node_color, alpha, nodelist, cmap, vmin, vmax)

NetworkXError: Node 'code_summary-100' has no position.

Which I assume is due to some nodes not being in a community.

Nodes that aren't in communities aren't important for this display so can be hidden, but we need to update the code to do that rather than raise

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.