The geometric_network
class¶
-
class
Complex_Contagions.
geometric_network
(network_type, size, **kwargs)¶ Bases:
object
Geometric Network object to run complex contagions on.
-
N
¶ Size, number of nodes in the network.
- Type
int
-
M
¶ Total number of edges in the network.
- Type
int
-
graph
¶ Networkx graph corresponding to the geometric_network object. You can use all the networkx library with this attribute.
- Type
a Networkx object
-
pos
¶ A dictionary of nodes and their spatial location.
- Type
dict
-
A
¶ Adjacency matrix of the graph. Use .todense() or .toarray() to manipulate.
- Type
A Scipy sparse matrix
-
text
¶ a simple description of the network.
- Type
str
- Parameters
network_type (str) – Type of the network to be created. It can be
2D_lattice
or ‘ring_lattice``.size (int) – Size of the network to be initated. If 2D_lattice, there will be size**2 many total nodes.
**kwargs –
- tiling: int
Should be provided if the network type is
2D_lattice
. Tiling of the 2d lattice. It can be 3,4,6 for now. This is the number of neighbors to be connected.
**kwargs –
- periodic: bool
Should be provided if the network type is
2D_lattice
. if True, edges of the planar lattice are going to be glued together. Seenetworkx.grid_2d_graph
.
**kwargs –
- banded: bool
Should be provided if the network type is
ring_lattice
. If True, the closestband_length
many neigbors from right and left is going to be connected to every node, creating a banding.
**kwargs –
- band_length: int
Sould be provided if the network type is
ring_lattice
. Geometric degree divided by 2. Note that geometric degree must be an even number.
-
add_noise_to_geometric
(noise_type, d2)¶ This method adds non-geometric edges to the network that are long range. Depending on the ‘noise_type’ the way we add these long range edges differ. If noise_type =
ER_like
, then there will be d2 many non geometric edges ON AVERAGE for every node. When the noise_type =k_regular
, every node will have exactly d2 many long range edges.- Parameters
noise_type (str) – ER_like or k_regular
d2 (int) – degree to assign non-geometric edges to every node
- Returns
- Return type
None. Updates the geometric_network.A
- Raises
ValueError – if the geometric_network.N * d2 is an odd number.
-
average_over_trials
(matrix)¶ Helper funtion to take the averages over trials of the given matrix.
- Parameters
matrix (array k x Trials) – Matrix to take the average over trials. Matrix have to be k x Trials. This can be the size of the contagion or first activation times depending on what you need.
- Returns
mean_matrix – Mean matrix
- Return type
array k x 1
-
compute_persistence
(distances, spy=False)¶ Helper to compute persistent homology using the distance matrix by building a Rips filtration up to dimension 2(topological features to be observed are going to be 1 dimensional at max). First normalizes the distances before the computation.
- Parameters
distances (n x n array) – distance matrix. First output of the make_distance_matrix.
spy (bool, optional) – Take a peak at the persistence diagram
- Returns
diag – The birth and death times of the topological features in all dimensions.
- Return type
list
-
display
(n_size=15, labels=True)¶ Method to pass parameters into
nx.draw()
.- Parameters
n_size (int) – node sizes.
labels (bool) – node labels.
- Returns
- Return type
nx.draw(self.graph)
-
display_comm_sizes
(Q, labels)¶ Helper to visualize the size of the active nodes during the contagion. Shades are indicating the max and min values of the spread starting from different nodes.
- Parameters
Q (list, [n x T+1 array]) – Output of the make_distance_matrix appended in a list
labels (figure labels corresponding to every list element, threshold, network type, C etc...) –
-
excitation
(T, C, seed, threshold, refractory=False, ax=None, spy=False)¶ THE CORE FUNCTION OF THE NEURONAL CONTAGION MODEL.
In this model, a neuron fires if the ratio of it’s excited neighbors to the total number of neighbors is greater than the threshold. Let’s call the difference between this ratio and the threshold = F so that if F is positive, neuron is going to fire and it doesn’t fire when it’s negative. We add some stocasticity to the model by defining the sigmoid function so that the probability that the neuron is going to fire is not a step function, but a sigmoid function.
- Parameters
T (int) – Number of time steps contagions is going to be iterated.
C (int) – A positive constant for the sigmoid function, if C is too large(>100), jump from 0 to 1 is gonna be too quick i.e. model is going to be deterministic.
seed (int) – node id to start the contagion, in the first time step, we infect the neighbors of the seed with probablity 1 then enter the while loop below
threshold (float) – threshold to compare for a neuron’s neighbor input. threshold must be in (0,1).
refractory (bool) – if TRUE, sets the refractory period of 1 time step i.e. neuron cannot fire for 1 time step right after it fires. if FALSE, neuron stays active once its activated.
ax (matplotlib.axis, bool) – if spy is TRUE, there have to be an axis provided to plot the contagion spread.
spy (matplotlib.axis, bool) – if spy is TRUE, there have to be an axis provided to plot the contagion spread.
- Returns
activation_times (array) – An array of n x 1 keeping track of the first time step the corresponding node gets activated.
size_of_contagion (array) – An array of (T+1) x 1 keeping track of the number of active nodes at a given time(at t = 0, all neighbors of the seed is active)
-
make_distance_matrix
(T, C, threshold, Trials, refractory, spy_distance=False)¶ A shortcut to run all of the above functions in one function. This creates an activation matrix by running the contagion on starting from every node and encoding the first activation times of each node. Then, finding the euclidean distances between the columns of this matrix, creating a distance matrix so that the (i,j) entry corresponds to the average time(over the trials) that a contagion reaches node j starting from node i.
- Parameters
T (int) – Number of time steps contagions is going to be iterated.
C (int) – A positive constant for the sigmoid function, if C is too large(>100), jump from 0 to 1 is gonna be too quick i.e. model is going to be deterministic.
threshold (float) – threshold to compare for a neuron’s neighbor input. threshold must be in (0,1).
Trials (int) – Number of trials to run the contagion on the same network.
refractory (bool) – if TRUE, sets the refractory period of 1 time step i.e. neuron cannot fire for 1 time step right after it fires. if FALSE, neuron stays active once its activated.
spy_distance (bool) – Check True if you want to take a peak at the distance matrix.
- Returns
D (n x n array) – Distance matrix
Q (n x T+1 array) – Array carrying the size of the contagion at every time step.
-
one_d_Delta
(persistence)¶ Helper to compute the specific topological features.
- Parameters
persistences (list) – A list of birth and death times of the topological features or the output of the compute_persistence.
- Returns
Delta_min (float) – The difference between the life times of the longest and the second longest 1-cycle.
Delta_max (float) – The difference between the life times of the longest and the shorthes 1-cycle.
Delta_avg (float) – The average lifetime of all the 1-cycles.
-
run_excitation
(Trials, T, C, seed, threshold, refractory, ax=None)¶ Helper function to run the excitation over several trials.
- Parameters
Trials (int) – Number of trials to run the contagion on the same network.
T (int) – Number of time steps contagions is going to be iterated.
C (int) – A positive constant for the sigmoid function, if C is too large(>100), jump from 0 to 1 is gonna be too quick i.e. model is going to be deterministic.
seed (int) – node id to start the contagion, in the first time step.
threshold (float) – threshold to compare for a neuron’s neighbor input. threshold must be in (0,1).
refractory (bool) – if TRUE, sets the refractory period of 1 time step i.e. neuron cannot fire for 1 time step right after it fires. if FALSE, neuron stays active once its activated.
ax (matplotlib.axis, optional) – if not provided, contagion will not be shown.
- Returns
activation_times (array) – An array of n x Trials keeping track of the first time step the corresponding node gets activated for each trial.
size_of_contagion (array) – An array of (T+1) x Trials keeping track of the number of active nodes at a given time(at t = 0, all neighbors of the seed is active) for each trial.
-
spy_first_activation
(first_activation_times)¶ Helper function to visualize the first activation times.
- Parameters
first_activation_times (array of size n x Trials) – First output of the run_excitation showing the first time step that the contagion reaches to a given node.
-