Skip to content

Utilities

cynetdiff.utils

Functions used to convert NetworkX graphs to usable models.

check_csr_arrays(starts, edges)

Asserts that the graph represented by starts and edges is in valid compressed sparse row (CSR) format. Useful for debugging, before the manual construction of a model.

Parameters:

Name Type Description Default
starts array

An array of start indices for each node's edges in the edge array. Type of array elements must be unsigned int.

required
edges array

An array of edges represented as integer indices of nodes. Type of array elements must be unsigned int.

required

Raises:

Type Description
ValueError

If the input parameters are not in valid CSR format.

networkx_to_ic_model(graph, *, activation_prob=None, _include_succcess_prob=False)

Converts a NetworkX graph into an Independent Cascade model. Includes activation probability values if they are defined on each edge under the key "activation_prob". Activation probability should only be set on either edges or through the function argument, not both.

Payoffs for each node are included if they are defined on each node under the key "payoff".

Parameters:

Name Type Description Default
graph Graph or DiGraph

A NetworkX graph or directed graph.

required
activation_prob float

Activation probability for the Independent Cascade model, by default None. If not set, and "activation_prob" key not found on edges, set to 0.1.

None
_include_succcess_prob bool

If True, includes success probabilities for each edge. These probabilities are then stored in the edge data dictionary with the key "success_prob", by default False. Used mainly for testing.

False

Returns:

Type Description
tuple[IndependentCascadeModel, dict[Any, int]]

A tuple with the instance of IndependentCascadeModel using the given graph and a dictionary mapping the nodes of the graph to their integer labels in the model.

networkx_to_lt_model(graph)

Converts a NetworkX graph into a Linear Threshold model. Includes influence values if they are defined on each edge under the key "influence".

Payoffs for each node are included if they are defined on each node under the key "payoff".

Parameters:

Name Type Description Default
graph Graph or DiGraph

A NetworkX graph or directed graph.

required

Returns:

Type Description
tuple[LinearThresholdModel, dict[Any, int]]

A tuple with the instance of LinearThresholdModel using the given graph and a dictionary mapping the nodes of the graph to their integer labels in the model.

set_activation_random_sample(graph, weight_set)

Set activation probability on each edge uniformly at random from the given weight set. Should be used on graphs before creating the independent cascade model.

Parameters:

Name Type Description Default
graph Graph or DiGraph

A NetworkX graph or directed graph.

required
weight_set AbstractSet[float]

The set of weights to sample from. Assigns each edge in the input graph a weight uniformly at random from this set.

required

set_activation_uniformly_random(graph, *, range_start=0.0, range_end=1.0)

Set activation probability on each edge uniformly at random in the range [range_start, range_end]. Must have that 0.0 <= range_start < range_end <= 1.0. Should be used on graphs before creating the independent cascade model.

Parameters:

Name Type Description Default
graph Graph or DiGraph

A NetworkX graph or directed graph.

required
range_start float

The start of the range to sample activation probabilities from. If not set, defaults to 0.0.

0.0
range_end float

The end of the range to sample activation probabilities from. If not set, defaults to 1.0.

1.0

set_activation_weighted_cascade(graph)

Set activation probability on each edge (u,v) to 1/in_degree(v). Graph must be directed. Should be used on graphs before creating the independent cascade model.

Parameters:

Name Type Description Default
graph DiGraph

A NetworkX directed graph.

required