Blog‎ > ‎

Getting Started with NOESIS for Python

posted Sep 19, 2018, 7:19 AM by Víctor Martínez
A few months ago the NOESIS project team released the Python binding for NOESIS. This binding provides access to most NOESIS functions and allows an easy integration with the Python ecosystem for data science and machine learning. These features make the NOESIS for Python module a powerful tool for those who want to perform advanced analysis of complex networks using the Python programming language. This introductory tutorial shows how to install NOESIS for Python and how to start using it for analyzing networks by providing some simple examples.

Installing NOESIS for Python

There are two ways to install NOESIS for Python: from the Python Package Index (PyPI) and from source code.

On the one hand, you can install NOESIS for Python from PyPi using the pip package manager. The following command installs the latest available version of NOESIS for Python:
pip install noesis

On the other hand, if you prefer to install NOESIS for Python from source code, you have to clone the project repository to create a local copy on your computer as shown below:
git clone https://github.com/fvictor/noesis-python.git
Next, you have to move to the project folder and launch the installation process:
cd noesis-python
python setup.py install

Both methods should notify you if the installation was successful or there was any problem. If you find any problem during the installation due to the javabridge package dependency, we recommend you manually installing this package as described in its documentation.

First Steps with NOESIS for Python

In order to use NOESIS from Python, we start by importing the Noesis class from the corresponding module as follows:
from noesis import Noesis

The Noesis class is a wrapper that provides all the methods required to use the features implemented in the NOESIS package. This class provides a session-oriented access point to the execution of network algorithms implemented in Java within the NOESIS package. A session, which involves running a Java Virtual Machine (JVM) instance, is automatically created when instantiating the Noesis class. The session must be manually closed using the end method provided by the Noesis class. The following code snippet shows how to instantiate the Noesis class to create a session and how to close it using the end method:
ns = Noesis()
# Your NOESIS-related code here
ns.end()

Now that we know how to start and end a NOESIS session in Python, we can start using some interesting NOESIS features. For example, let's load a network to start our analysis. We are going to load a neural network of Caenorhabditis elegans, which can be found in the datasets section under the name "celegansneural.gml". Since this network is stored using the Graph Modeling Language (GML) file format, we must instantiate the corresponding network reader and use the provided read method as follows:
network_reader = ns.create_network_reader('GML')
network = network_reader.read('celegansneural.gml')

The returned network object contains the loaded network and provides several methods to manipulate the network. For example, we can get the degree of a given node node_index using the degree method:
node_degree = network.degree(node_index)

Running Network Analysis Algorithms

The Noesis class provides simple mechanisms for accessing to the algorithms implemented in NOESIS. For example, if we want to compute the closeness of each node in the previously loaded network, we would write the following lines of code:
closeness_scorer = ns.create_node_scorer('Closeness')
closeness = closeness_scorer.compute(network)
The closeness_scorer object created using the create_node_scorer method provides the compute method, which called by passing the network as an argument computes and returns the array of scores given by the definition of node closeness.

Other NOESIS algorithms can be used in a similar way by calling the appropriate Noesis class method. For example, the create_link_scorer method can be used to create link scorers, which can compute scores based on topological properties defined for links. The following example shows how to create a link scorer using the common neighbors metric and how to apply it to the network:
link_scorer_cn = ns.create_link_scorer('CommonNeighbors')
link_scores = link_scorer_cn.compute(network)
The returned link_scores is a list of tuples containing the source and the target nodes of each link, along with the score value computed by the algorithm.

Others method like create_community_detector and create_layout provide a similar interface for detecting communities and computing node coordinates for pleasant graph visualization, respectively.

Putting Everything Together

The previous sections described in isolation the initial steps required to begin with the NOESIS for Python package. In this section, we put together all these steps to carry out a basic network analysis from end to end. We intend to study the centrality of nodes in the neural network of the Caenorhabditis elegans. For this task, we are going to load the network and compute node closeness using NOESIS as shown in the previous sections. Next, we are going to draw the network and plot the histogram of closeness scores using the popular matplotlib library, in order to show how easy it is to integrate NOESIS with the Python ecosystem for data analysis.
from noesis import Noesis
import matplotlib.pyplot as plt

ns = Noesis()

# Read the network
network_reader = ns.create_network_reader('GML')
network = network_reader.read('celegansneural.gml')

# Compute the layout
fr_layout = ns.create_layout('FruchtermanReingold')
x,y = fr_layout.compute(network)

# Draw the network
plt.subplot(1, 2, 1)
plt.axis('off')
for source in range(network.nodes()):
    for target in network.out_links(source):
        plt.plot([x[source], x[target]], [y[source], y[target]], c='0.8', zorder=1)
plt.scatter(x,y, c='r', zorder=2)

# Compute closeness in the network
closeness_scorer = ns.create_node_scorer('Closeness')
closeness = closeness_scorer.compute(network)

# Draw the closeness histogram
plt.subplot(1, 2, 2)
plt.hist(closeness, 40)
plt.xlabel('Closeness')
plt.ylabel('Frequency')
plt.show()

ns.end()
The previous code generates the following network representation and histogram of node closeness:

These results suggest that most nodes have a closeness score between 0.1 and 0.4. However, it is interesting to observe that a very significant number of nodes have a closeness score close to 1, which suggests that they could play an important role in the network. This straightforward example shows how easy it is to use NOESIS from Python and to take advantage of the large collection of Python libraries for the study of complex networks.

What's next?

This tutorial is intended to be a brief introduction on how to start using NOESIS from Python. You can find more comprehensive information about how to use this module in the official documentation. You could also find the NOESIS wiki useful, as it introduces the techniques implemented in the NOESIS framework that you can use from the NOESIS for Python module.
Comments