cxMate Service DEMO

By Ayato Shimada, Mitsuhiro Eto

This DEMO shows

  1. detect communities using an igraph's community detection algorithm
  2. paint communities (nodes and edges) in different colors
  3. perform layout using graph-tool's sfdp algorithm


In [6]:
# Tested on:
!python --version


Python 3.5.4 :: Continuum Analytics, Inc.

Send CX to service using requests module

Services are built on a server

You don't have to construct graph libraries in your local environment.
It is very easy to use python-igraph and graph-tools.

In order to send CX

  • requests : to send CX file to service in Python. (curl also can be used.)
  • json : to convert object to a CX formatted string.

In [16]:
import requests
import json

In [17]:
url_community = 'http://localhost:80' # igraph's community detection service URL
url_layout = 'http://localhost:3000' # graph-tool's layout service URL
headers = {'Content-type': 'application/json'}

Network used for DEMO

This DEMO uses yeastHQSubnet.cx as original network.

  • 2924 nodes
  • 6827 edges

1. igraph community detection and color generator service

In order to detect communities, igraph's community detection service can be used.

How to use the service on Jupyter Notebook

  1. open the CX file using open()
  2. set parameters in dictionary format. (About parameters, see the document of service.)
  3. post the CX data to URL of service using requests.post()

In [20]:
data = open('./yeastHQSubnet.cx') # 1.
parameter = {'type': 'leading_eigenvector', 'clusters': 5, 'palette': 'husl'} # 2.
r = requests.post(url=url_community, headers=headers, data=data, params=parameter) # 3.

What happened?

Output contains

graph with community membership + color assignment for each group.

  • node1 : group 1, red
  • node2 : group 1, red
  • node3 : group 2, green ...

You don't have to create your own color palette manually.

To save and look the output data, you can use r.json()['data']

Note

  • When you use this output as input of next service, you must use json.dumps(r.json()['data'])
  • You must replace single quotation to double quotation in output file.

In [21]:
import re
with open('output1.cx', 'w') as f:
    # single quotation -> double quotation
    output = re.sub(string=str(r.json()['data']), pattern="'", repl='"')
    f.write(output)

3. graph-tool layout service

In order to perform layout algorithm, graph-tool's layout algorithm service can be used.

C++ optimized parallel, community-structure-aware layout algorithms

You can use the community structure as a parameter for layout, and result reflects its structure.

You can use graph-tool's service in the same way as igraph's service.
Both input and output of cxMate service are CX, NOT igraph's object, graph-tool's object and so on.
So, you don't have to convert igraph object to graph-tools object.

How to use the service on Jupyter Notebook

  1. open the CX file using json.dumps(r.json()['data'])
  2. set parameters in dictionary format. (About parameters, see the document of service.)
  3. post the CX data to URL of service using requests.post()

In [22]:
data2 = json.dumps(r.json()['data']) # 1.
parameter = {'only-layout': False, 'groups': 'community'} # 2. 
r2 = requests.post(url=url_layout, headers=headers, data=data2, params=parameter) # 3.

Save .cx file

To save and look the output data, you can use r.json()['data']


In [23]:
import re
with open('output2.cx', 'w') as f:
    # single quotation -> double quotation
    output = re.sub(string=str(r2.json()['data']), pattern="'", repl='"')
    f.write(output)

Color Palette

If you want to change color of communities, you can do it easily.
Many color palettes of seaborn can be used. (See http://seaborn.pydata.org/tutorial/color_palettes.html)


In [11]:
%matplotlib inline
import seaborn as sns, numpy as np
from ipywidgets import interact, FloatSlider

Default Palette

Without setting parameter 'palette', 'husl' is used as color palette.


In [2]:
def show_husl(n):
    sns.palplot(sns.color_palette('husl', n))
print('palette: husl')
interact(show_husl, n=10);


palette: husl

Other palettes


In [3]:
def show_pal0(palette):
    sns.palplot(sns.color_palette(palette, 24))
interact(show_pal0, palette='deep muted pastel bright dark colorblind'.split());



In [4]:
sns.choose_colorbrewer_palette('qualitative');



In [5]:
sns.choose_colorbrewer_palette('sequential');



In [ ]: