-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_cycles_randomized.py
59 lines (46 loc) · 1.69 KB
/
count_cycles_randomized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Calculate the mean number of directed cycles for each order
# in the randomized network
# -*- coding: utf-8 -*-
from networkx import Graph, DiGraph, simple_cycles, find_cycle, cycle_basis
import networkx as nx
import random
from collections import defaultdict
from lib import *
import csv
# Parameters
# ========================================
n_randomizations = 1000
kmax = 10 # Maximum cycle order
# Load data
# ========================================
# Get the information from both files
dictionary1 = csv2dict("sevaseviene2network.csv", delimiter=";")
# These function transform the dictionary to a directed graph networkx-style
DG1 = DiGraph(dictionary1)
# Remove nodes with out degree 0
remove = [node for node in DG1.nodes if DG1.out_degree(node) <= 0]
DG1.remove_nodes_from(remove)
# Array to store the number of directed cycles found for each order
# accumulated over randomizations
nksum_directed = np.zeros(kmax + 1, dtype=int)
for j_rand in range(n_randomizations):
# Randomize the network
edges = list(DG1.edges())
for edge in edges:
revert_edge = random.randint(0,1)
if revert_edge:
DG1.remove_edge(edge[0], edge[1])
DG1.add_edge(edge[1], edge[0])
# Find the cycles
directed_cycles = list(nx.simple_cycles(DG1))
# Store their distribution
for cycle in directed_cycles:
k = len(cycle)
if k <= kmax:
nksum_directed[k] += 1
# Calculate the mean number of cycles per order
nk_directed = nksum_directed.astype(float)/n_randomizations
# Save the results to file
data = np.vstack(
(np.arange(kmax + 1, dtype=int), nk_directed))
np.savetxt("cycledistribution_rand_sevaseviene.dat", data, fmt="%6d")