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.

Examples:

>>> import array
>>> starts = array.array("I", [0, 2, 4, 5])
>>> edges = array.array("I", [1, 2, 0, 3, 7])
>>> check_csr_arrays(starts, edges)
ValueError: Value in edges "7" must ben in the range [0,3].

edgelist_to_csr_arrays(edgelist)

Converts an edge list to compressed sparse row (CSR) arrays. The edge list should be a sequence of tuples, where each tuple represents an edge in the graph. Each tuple should contain two integers representing the nodes of the edge. The function returns the CSR arrays, as well as a dictionary mapping the original node names to the new node names.

Parameters:

Name Type Description Default
edgelist Iterable[Tuple[int, int]]

An iterable of tuples, where each tuple contains two integers representing the nodes of an edge in the graph.

required

Returns:

Type Description
Tuple[array, array, Dict[int, int]]

A tuple containing the starts array, edges array, and a dictionary mapping the original node names to the new node names.

Examples:

>>> starts, edges, rename_dict = edgelist_to_csr_arrays([(0, 1), (1, 2), (2, 0)])
>>> check_csr_arrays(starts, edges)

networkx_to_ic_model(graph, *, activation_prob=None, rng=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
rng Generator | BitGenerator | None

Random number generator to use for the model. If not set, creates a new generator by default.

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.

Examples:

>>> import networkx as nx
>>> graph = nx.erdos_renyi_graph(10, 0.5)
>>> model, _ = networkx_to_ic_model(graph)

networkx_to_lt_model(graph, rng=None)

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
rng Generator | BitGenerator | None

Random number generator to use for the model. If not set, creates a new generator by default.

None

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.

Examples:

>>> import networkx as nx
>>> graph = nx.erdos_renyi_graph(10, 0.5)
>>> model, _ = networkx_to_lt_model(graph)

set_activation_random_sample(graph, weight_set, *, rng=None)

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
rng Generator | BitGenerator | None

Random number generator to use for the model. If not set, creates a new generator by default.

None

Examples:

>>> import networkx as nx
>>> graph = nx.complete_graph(5)
>>> set_activation_random_sample(graph, range_start=0.1, range_end=0.2)
>>> model, _ = networkx_to_ic_model(graph)

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

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
rng Generator | BitGenerator | None

Random number generator to use for the model. If not set, creates a new generator by default.

None

Examples:

>>> import networkx as nx
>>> graph = nx.complete_graph(5)
>>> set_activation_uniformly_random(graph, range_start=0.1, range_end=0.2)
>>> model, _ = networkx_to_ic_model(graph)

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

Examples:

>>> import networkx as nx
>>> graph = nx.complete_graph(5)
>>> set_activation_weighted_cascade(graph, range_start=0.1, range_end=0.2)
>>> model, _ = networkx_to_ic_model(graph)