SciPy

tethne.model.social.tapmodel module

Classes and methods related to the TAPModel.

class tethne.model.social.tapmodel.TAPModel(G, theta, damper=0.5)[source]

Represents a Topical Affinity Propogation (TAP) social model.

The TAP model assumes that authors can be influenced by other authors with whom they are acquainted to adopt particular features (eg topics) in their writing. The TAP model takes a weighted social graph, and the probabilities that each author will generate a document containing particular topics. For a complete description of the model, see Tang et al. 2009, or the presentation on which the paper was based.

Parameters:

G : nx.Graph()

Should have weight attribute in the range [0.,1.] indicating the strength of the relationship between authors (eg the number of coauthored papers).

theta : dict

Distribution over topics for authors. Should have N keys, with values shaped T; N == len(G.nodes()) and T is the number of topics.

Examples

There are two ways to generate a TAPModel:

To generate a TAPModel from a single coauthors() graph and a corpus model, instantiate and build a TAPModel directly:

>>> from tethne.networks import authors
>>> g = authors.coauthors(C.all_papers())    # C is a Corpus.

>>> from tethne.model import managers, TAPModel
>>> atheta = managers.TAPModelManager().author_theta(C.all_papers())

>>> model = TAPModel(g, atheta)
>>> model.build()

To generate a TAPModel from a Corpus that takes time into account, use a TAPModelManager.

Use TAPModel.graph() to retrieve an influence graph for a particular topic.

build(max_iter=1000)[source]

Estimate the TAPModel.

This may take a considerable amount of time, depending on the size of the social graph and the number of features/topics.

Parameters:

max_iter : int

(default: 500) Maximum number of iterations.

graph(k)[source]

Retrieve an influence graph for a particular topic.

Parameters:

k : int

A topic index.

Returns:

NetworkX DiGraph object

An influence graph. Edge direction and their weight indicate influence strength. Node attribute theta is the probability that an author will generate a paper that includes topic k.

Examples

>>> g = model.graph(0)   # model is a TAPModel.
>>> g
<networkx.classes.graph.Graph at 0x10b2692d0>

You can then use a method from writers to generate a network datafile for visualization in Cytoscape or Gephi.

_images/tap_topic0.png
prep()[source]
prime(alt_r, alt_a, alt_G)[source]

Assign posterior r and a values from a previous model as priors for this model.

Parameters:

alt_r : dict

{ i: array-like [ j, k ] for i in G.nodes() } Must be from a model with the same topics.

alt_a :dict

{ i: array-like [ j, k ] for i in G.nodes() } Must be from a model with the same topics.

alt_G : nx.Graph

Need not be the same shape as G, but node names must be consistent.