The cxnet module with igraph

The functions and classes documented below are valid if you use the cxnet with igraph. If you want to use cxnet with networkx, the usage of the functions and classes or even the existence of them may differ.

Creating and getting networks

cxnet.debnetwork()

Creates the network of deb packages in Linux distributions using deb packages.

Returns:
A cxnet.Network instance.

It needs the python-apt package installed.

cxnet.load_netdata(filename)

Loads netdata from a gml file in netdata directory.

If it is not there, it will download it.

Parameter:
filename: string
The name of the file we want to load (without extension) or the start of the filename. It loads the first file that match.
Returns:
A Network object with the archived network in it.

Example:

>>> import cxnet
I have found an rc file: /home/ha/.cxnetrc.py.
The graph_module was set in the rc file to "igraph".
I will use igraph. (It have been imported.)
>>> net=cxnet.load_netdata("as-")
'netdata/as-22july06.gml' have been loaded.
>>> print(net.summary())
IGRAPH U--- 22963 48436 -- as-22july06.gml
+ attr: info (g), name (g), id (v), label (v)
>>> print(net['info'])
The file as-22july06.gml contains a symmetrized snapshot of the structure
of the Internet at the level of autonomous systems, reconstructed from BGP
tables posted at archive.routeviews.org.  This snapshot was created by Mark
Newman from data for July 22, 2006 and is not previously published.
cxnet.get_netdata(archive_name=None, unzip=True, path='/home/ha/.cxnet')

Get network data from archives.

It stores it in the netdata subdirectory of the current directory. The downloaded zipped files are in the netdata_zip subdirectory of the current directory.

Parameter:
archive_name: 'newman' or 'deb'

If newman, it downloads a lot of network form Mark Newman’s site.

If deb, it downloads a lot of deb package dependency network created with cxnet.

unzip: boolean
If True, it will unzip the archive files.

Example:

>>> import cxnet
I have found an rc file: /home/ha/.cxnetrc.py.
The graph_module was set in the rc file to "igraph".
I will use igraph. (It have been imported.)
>>> cxnet.get_netdata('deb')
ubuntu-10.04-packages-2010-08-19.zip
    ubuntu-10.04-packages-2010-08-19.gml, ubuntu-10.04-packages-2010-08-19.txt
ubuntu-10.04-packages-2010-10-25.zip
    ubuntu-10.04-packages-2010-10-25.gml, ubuntu-10.04-packages-2010-10-25.txt
ubuntu-9.10-packages-2010-07-16.zip
    ubuntu-9.10-packages-2010-07-16.gml, ubuntu-9.10-packages-2010-07-16.txt
ubuntu-9.10-packages-2010-07-22.zip
    ubuntu-9.10-packages-2010-07-22.gml, ubuntu-9.10-packages-2010-07-22.txt
debian-6.0-packages-2011-02-03.zip
    debian-6.0-packages-2011-02-03.gml, debian-6.0-packages-2011-02-03.txt

The Network class

The Network class is derived from the igraph.Graph class. The Graph class is imported in the cxnet class for convenience, so it is available as:

class cxnet.Graph

The methods of the Graph module is described in the igraph homepage.

class cxnet.Network(*args, **kwds)

Complex Network, Igraph version

Derived from the igraph.Graph class. Methods defined here start with “cx” to find them easier.

It includes the plot function as well.

plot(target=None, labels=True, **kwargs)

It is almost same az igraph.plot().

But it has not a graph argument, and left and right margins are wider by default, and edges are orange if there are vertex labels.

Parameter:
target: string or None
As in igraph.plot.
labels: list or boolean
If it is a list, it will passed to the vs[“label”]. If it is true, and no “label” attribute, but there is “name” attribute, the “name” will passed to “label”.
cxlargest_degrees(direction=None, limit=None, print_it=False)

Returns with the vertices with the largest degrees.

Parameters:
direction: default None
Can be ‘in’, ‘out’, or None, it will be use indegree, outdegree or (plain) degree respectively.
limit: integer or None, default None

Only the packages with the degree < limit will listed.

If None, a limit will be set (good for debian package dependency network):

  • plain: 500
  • in: 300
  • out: 50
Returns:
hd: a list of pairs of (degree, package name)

Example:

cn.cxlargest_degree(direction='in', limit=500)
cxneighbors(pkg_name)

Returns with the successors and predecessors (“out and inneighbors”) if it is a directed networks. Neighbors otherwise.

Parameters:
pkg_name: string
The name of the package. (You can find it with the cxfind method.)
Returns:
In a directed network

returns a tuple of (predecessors, successors) where

  • predecessors are list of neighbors on incoming edges,
  • successors are list of neighbors on outcoming edges.
In an undirected network
returns a tuple of (neighbors, None) where neighbors are list of neighbors.
cxneighborhood(pkg_name, plot=True, **kwargs)

Returns with the successors and predecessors and the package itself and can plot them.

Parameters:
pkg_name: string or integer
The name of the package or the index of the vertex.
plot: boolean or string, default False
  • If it is one of “pdf”, “png”, “svg” or “ps”, the output will be a file like neighbors_of_pkg_name.pdf. With igraph 0.6.1+ you can use “eps” as well.
  • If other string, the output will be a file named in the string.
  • If True, plot to the screen.
  • If False, do not plot.
return_network: boolean
If True, returns with a cxnet.Network instance instead of VertexSequence
Returns:
Vertex sequence of the neighborhood.

Example:

>>> import cxnet
>>> cxnet.load_netdata('ubuntu-10.10')
'netdata/ubuntu-10.10-packages-2011-05-01.gml' have been loaded.
>>> net.cxfind('vim')
['jvim-canna', 'libtext-vimcolor-perl', 'vim', 'vim-addon-manager', 'vim-common', 'vim-dbg', 'vim-gnome', 'vim-gtk', 'vim-gui-common', 'vim-latexsuite', 'vim-nox', 'vim-rails', 'vim-runtime', 'vim-tiny', 'vim-vimoutliner']
>>> net.cxneighborhood("vim")
<igraph.VertexSeq object at ...>
>>> net.cxneighborhood("vim", plot=False)
<igraph.VertexSeq object at ...>
cxdegdist(**kwargs)

Returns with a DegreDistribution class analyzing and plotting distribution.

Parameter:
kwargs:
parameters to the DegreeDistribution class Eg. direction {“in”, “out”, None}
Returns:
dd: DegreeDistribution class object (see help(dd) )
cxfind(namepart, verbosity=None)

Returns the ordered list of package names containing the given part.

Parameter:
namepart: string
A part of the package name. At least 2 characters.
Returns:
names: ordered list of strings
The ordered list of package names containing namepart.

The DegreeDistribution class

class cxnet.DegreeDistribution(network_or_degree_list, verbose=True, **kwargs)

Make, analyze and plot the degree distribution.

Parameters:
network_or_degree_list:
the investigated network (NetworkX or IGraph) or the list of degrees or the dict of the form {vertex:degree}.
direction: “in”, “out”, None [default]
the direction of the connection to count. If None, plain degree will be used.

some variables:

  • dd: the degree distribution az a pair of (k, p_k) values
  • dd_smeared: as the dd, but with bin smearing
  • max_deg: maximum of degree
  • number_of_vertices: the number of vertices in the network
  • n_0: the number of vertices with zero degree

more important functions:

  • summary: Write informations to screen or file about the distribution.
  • plot, loglog, semilogy, errorbar: Plotting functions as in pylab.
  • bin_smearing: Return the bin smeared distribution.
  • set_binning: Set the type of the binning.
  • cumulative_plot: Plots the cumulative distribution.
cumulative_distribution()

Return the cumulative degree distribution

The distribution is a list of (k, P(>k)) pairs.

cumulative_plot(with_powerlaw=False, **kwargs)

Plot the cumulative distribution.

Parameter:
with_powerlaw: boolean
Whether to plot the calculated powerlaw as well or not.
exponent(k_min=5.5)

Return with exponent and sigma of exponent.

Parameters:
k_min: float or integer, k_min>0
the minimal degree in the equation referenced below

The exponent end error are caculated with the equations written in [1] page 4.

[1]
@article{newman-2005-46,
  url = {http://arxiv.org/abs/cond-mat/0412004},
  author = {M.~E.~J. Newman},
  title = {Power laws, Pareto distributions and Zipf's law},
  journal = {Contemporary Physics},
  volume = {46},
  pages = {323},
  year = {2005},
}

[2]
@misc{clauset-2007,
  url = {http://arxiv.org/abs/0706.1062},
  author = {Aaron Clauset and Cosma Rohilla Shalizi and M.~E.~J. Newman},
  title = {Power-law distributions in empirical data},
  year = {2007}
}
plot_powerlaw(gamma=None, **kwargs)

Plot a power law function with exponent gamma or self.gamma.

Parameters:
gamma: float, “cumulative” or None
if float, -gamma will be the exponent, if “cumulative” then -self.gamma+1 else -self.gamma.
kwargs:
Keyword argumentums are forwarded to the plot function.

Return with the plot.

set_binning(binning=None)

Sets the type of binning, and calculates the binned distribution.

plot(plot=None, **kwargs)

Plot the bin smeared degree distribution.

loglog(**kwargs)

Plot the bin smeared degree distribution with loglog scales (log scale in each axes).

semilogy(**kwargs)

Plot the bin smeared degree distribution with semilogy scales (log scale in y axis).

errorbar(**kwargs)

Plot the degree distribution with the fits.

summary(verbosity=0, file=None)

Write informations to screen or file about the distribution.

Parameters:

file: if string, it will be the name of the file,
if None (default), it will write to screen (stdout).
verbosity: integer (0,1,2), default 0
The bigger the value is, the more information you get.