analyze Package

analyze Package

The tethne.analyze sub-package provides additional analysis methods not provided by NetworkX, as well as methods for using NetworkX algorithms on an entire GraphCollection.

collection Methods for analyzing GraphCollection objects.
graph Methods for network analysis.

collection Module

Methods for analyzing GraphCollection objects.

For the most part, these methods simply provide systematic access to algorithms in NetworkX.

tethne.analyze.collection.algorithm(C, method, **kwargs)[source]

Apply NetworkX method to each Graph in GraphCollection.

Passes kwargs to specified NetworkX method for each Graph, and returns a dictionary of results indexed by element (node or edge) and graph index (e.g. date).

Parameters :

C : GraphCollection

The GraphCollection to analyze. The specified method will be applied to each Graph in C.

method : string

Name of a method in NetworkX to execute on graph collection.

**kwargs :

A list of keyword arguments that should correspond to the parameters of the specified method.

Returns :

results : dict

A nested dictionary of results: results/elem(node or edge)/graph index.

Raises :

ValueError :

If name is not in networkx, or if no such method exists.

Examples

Betweenness centrality:

>>> import tethne.analyze as az
>>> BC = az.collection.algorithm(C, 'betweenness_centrality')
>>> print BC[0]
{1999: 0.010101651117889644,
2000: 0.0008689093723107329,
2001: 0.010504898852426189,
2002: 0.009338654511194512,
2003: 0.007519105636349891}
tethne.analyze.collection.attachment_probability(C)[source]

Calculates the observed attachment probability for each node at each time-step.

Attachment probability is calculated based on the observed new edges in the next time-step. So if a node acquires new edges at time t, this will accrue to the node’s attachment probability at time t-1. Thus at a given time, one can ask whether degree and attachment probability are related.

Parameters :

C : GraphCollection

Must be sliced by ‘date’. See GraphCollection.slice().

Returns :

probs : dict

Keyed by index in C.graphs, and then by node.

tethne.analyze.collection.connected(C, method, **kwargs)[source]

Performs analysis methods from networkx.connected on each graph in the collection.

Parameters :

C : GraphCollection

The GraphCollection to analyze. The specified method will be applied to each Graph in C.

method : string

Name of method in networkx.connected.

**kwargs : kwargs

Keyword arguments, passed directly to method.

Returns :

results : dictionary

Keys are graph indices, values are output of method for that graph.

Raises :

ValueError :

If name is not in networkx.connected, or if no such method exists.

Examples

>>> import tethne.data as ds
>>> import tethne.analyze as az
>>> import networkx as nx
>>> C = ds.GraphCollection()
>>> # Generate some random graphs
>>> for graph_index in xrange(1999, 2004):
>>>     g = nx.random_regular_graph(4, 100)
>>>     C[graph_index] = g
>>> results = az.collection.connected(C, 'connected', k=None)
>>> print results
{1999: False,
2000: False,
2001: False,
2002: False,
2003: False }
tethne.analyze.collection.edge_history(C, source, target, attribute, verbose=False)[source]

Returns a dictionary of attribute vales for each Graph in C for a single edge.

Parameters :

C : GraphCollection

source : str

Identifier for source node.

target : str

Identifier for target node.

attribute : str

The attribute of interest; e.g. ‘betweenness_centrality’

verbose : bool

If True, prints status and debug messages.

Returns :

history : dict

Keys are Graph keys in C; values are attribute values for edge.

tethne.analyze.collection.node_global_closeness_centrality(C, node)[source]

Calculates global closeness centrality for node in each graph in GraphCollection C.

tethne.analyze.collection.node_history(C, node, attribute, verbose=False)[source]

Returns a dictionary of attribute values for each Graph in C for a single node.

Parameters :

C : GraphCollection

node : str

The node of interest.

attribute : str

The attribute of interest; e.g. ‘betweenness_centrality’

verbose : bool

If True, prints status and debug messages.

Returns :

history : dict

Keys are Graph keys in C; values are attribute values for node.

graph Module

Methods for network analysis.

tethne.analyze.graph.global_closeness_centrality(g, normalize=True)[source]

Calculates global closeness centrality for all nodes in the network.

See node_global_closeness_centrality() for more information.

Parameters :

g : networkx.Graph

normalize : boolean

If True, normalizes centrality based on the average shortest path length. Default is True.

Returns :

C : dict

Dictionary of results, with node identifiers as keys and gcc as values.

tethne.analyze.graph.node_global_closeness_centrality(g, node, normalize=True)[source]

Calculates the global closeness centrality of a single node in the network.

Closeness centrality is based on the average shortest path length between a focal node and all other nodes in the network. For multi-component graphs, conventional closeness centrality metrics fail because it is not possible to traverse between a given node and all other nodes in the graph. Global closeness centrality is calculated in a way that yields values even for multi-component graphs. For an example of how global closeness centrality can be used to analyze co-authorship networks, see the blog post here.

To calculate the global closeness centrality of a single node, try:

>>> import tethne.analyze as az
>>> ngbc = az.node_global_closeness_centrality(BC, 'LEE 1975 EVOLUTION')
>>> ngbc
0.154245

You can calculate the global closeness centrality of all nodes in the network using global_closeness_centrality() .

>>> GBC = az.global_closeness_centrality(BC)
>>> GBC
{'a': 0.0, 'c': 0.0, 'b': 0.6666666666666666, 'd': 0.0}

For connected graphs, this is equivalent to conventional betweenness centrality. For disconnected graphs, works around infinite path lengths between nodes in different components.

Parameters :

g : networkx.Graph

node : any

Identifier of node of interest in g.

normalize : boolean

If True, normalizes centrality based on the average shortest path length. Default is True.

Returns :

c : float

Global closeness centrality of node.

Table Of Contents

Next topic

matrices Package

This Page