-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleOnEcoFloridaNet.py
More file actions
182 lines (162 loc) · 6.03 KB
/
ExampleOnEcoFloridaNet.py
File metadata and controls
182 lines (162 loc) · 6.03 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 23 12:30:01 2025
@author: jafish
"""
from NetworkFragilityClean import fragile_net
from RunNetworkFragilityClean import run_sparse_fn
import networkx as nx
import numpy as np
from datetime import datetime
from NetworkDestruction import NetworkDestruction
import pymetis
#The follwing two codes (and a third necessary file) can be found at:
#https://github.com/PPNew1/Edge_Collective_Influence/tree/main
from EdgeCollectiveInfluence import *
from DualCompetitivePercolation import *
#NOTE MUST HAVE igraph installed using pip install igraph
import igraph as ig
#USEFUL (AND NECESSARY) FUNCTIONS
def nx_to_igraph(G_nx):
"""
Convert an undirected NetworkX graph to an igraph Graph.
Node IDs are stored in the 'name' attribute.
"""
# Get nodes and edges
nodes = list(G_nx.nodes())
edges = [(nodes.index(u), nodes.index(v)) for u, v in G_nx.edges()]
# Create igraph graph
G_ig = Graph(n=len(nodes), edges=edges, directed=False)
G_ig.vs["name"] = nodes # preserve original node IDs G_ig.vs["name"] = nodes # preserve original node IDs
return G_ig
def igraph_to_networkx(ig_graph):
# Choose directed/undirected
if ig_graph.is_directed():
nx_graph = nx.DiGraph()
else:
nx_graph = nx.Graph()
# --- Add nodes with attributes ---
for v in ig_graph.vs:
node_id = v.index
attrs = dict(v.attributes())
nx_graph.add_node(node_id, **attrs)
# --- Add edges with attributes ---
for e in ig_graph.es:
source, target = e.tuple
attrs = dict(e.attributes())
nx_graph.add_edge(source, target, **attrs)
return nx_graph
Today = datetime.today().strftime('%d-%m-%Y')
delta = 0.5
NetworkType = 'FloridaEcological'
#Replace the path with your own path to the file
path = 'C:/Users/jafish/Downloads/eco-florida/eco-florida.edges'
fh = open(path, "rb")
#Get the graph
G = nx.read_edgelist(fh,data=(("weight", float),))
fh.close()
#The graph has strings as node labels, which breaks things, so convert
#to integer labels
Gex = nx.convert_node_labels_to_integers(G, label_attribute="old_label")
#Start with pymetis NOTE MUST HAVE PYMETIS INSTALLED, can use
#pip install pymetis if you have not already
print("Calculating PyMETIS partition...")
A_list1 = []
for l in range(len(Gex)):
A_list1.append(np.array(list(Gex[l])))
#Find the number of edges that must be removed
NumEdges,PartLabels = pymetis.part_graph(2,A_list1)
#NumEdges represents the number of edges removed!
PyMETISFrac = NumEdges/len(Gex.edges())
FN = fragile_net()
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,PyMETISFrac)
Frag_pymetis = FN.return_Fragility()
print("PyMETIS Fragility: ", Frag_pymetis)
print("")
#Now for the Kernighan-Lin bipartition
print("Calculating Kernighan-Lin partition...")
ND = NetworkDestruction(Graph=Gex)
C,New,S1,S2 = ND.Kernighan_Lin_NumEdges()
#C represents the number of edges removed using the Kernighan-Lin algorithm
KLFrac = C/len(Gex.edges())
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,KLFrac)
Frag_KL = FN.return_Fragility()
print("Kernighan-Lin Fragility: ", Frag_KL)
print("")
i = 0
print("Calculating All Rewire Strategies...")
#Run for future iterative add back
Output = run_sparse_fn(Gex,delta,NetworkType+'Net_FullRemoval'+str(i))
#To save the results of this if you wish
np.savez(NetworkType+'Net_FullRemoval_' +str(i) + Today+ '.npz',Output)
#First compute Min degree strategy
G2 = nx.Graph(Output['MinDegree']['Graphs'][-1])
MinFrac = (len(Gex.edges())-len(G2.edges()))/len(Gex.edges())
FN = fragile_net()
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,MinFrac)
Frag_Min = FN.return_Fragility()
print("Min-Degree Fragility: ",Frag_Min)
print("")
FN.add_graph(Gex)
FN.add_graph_new(G2)
G3 = FN.sparse_iterative_add_back(len(Gex.nodes())//2)
MinDegRewFrac = (len(Gex.edges())-len(G3.edges()))/len(Gex.edges())
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,MinDegRewFrac)
Frag_MinRew = FN.return_Fragility()
#Now for edge betweenness
G2 = nx.Graph(Output['EdgeBetweenness']['Graphs'][-1])
EBFrac = (len(Gex.edges())-len(G2.edges()))/len(Gex.edges())
FN = fragile_net()
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,EBFrac)
Frag_EB = FN.return_Fragility()
print("Edge-Betweenness Fragility: ",Frag_EB)
print("")
FN.add_graph(Gex)
FN.add_graph_new(G2)
G3 = FN.sparse_iterative_add_back(len(Gex.nodes())//2)
EBRewFrac = (len(Gex.edges())-len(G3.edges()))/len(Gex.edges())
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,EBFrac)
Frag_EBRew = FN.return_Fragility()
#Now for edge sum
G2 = nx.Graph(Output['EdgeSum']['Graphs'][-1])
ESFrac = (len(Gex.edges())-len(G2.edges()))/len(Gex.edges())
FN = fragile_net()
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,MinFrac)
Frag_ES = FN.return_Fragility()
print("Edge-Sum Fragility: ",Frag_ES)
print("")
FN.add_graph(Gex)
FN.add_graph_new(G2)
G3 = FN.sparse_iterative_add_back(len(Gex.nodes())//2)
ESRewFrac = (len(Gex.edges())-len(G3.edges()))/len(Gex.edges())
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,ESRewFrac)
Frag_ESRew = FN.return_Fragility()
Frag_Rew = np.max([Frag_MinRew,Frag_EBRew, Frag_ESRew])
print("Rewire Fragility: ",Frag_Rew )
print("")
#Now run for ECI (edge collective influence)
g = nx_to_igraph(Gex)
res = IECIR(g,p=0.5)
NumEdges = len(res[2][2])
ECIFrac = NumEdges/len(Gex.edges())
FN = fragile_net()
FN.compute_Fragility(len(Gex.nodes()),len(Gex.nodes())//2,ECIFrac)
Frag_ECI = FN.return_Fragility()
print("ECI Fragility: ", Frag_ECI)
print("")
print("Full Results: ")
print("")
print("PyMETIS Fragility: ", Frag_pymetis)
print("")
print("Kernighan-Lin Fragility: ", Frag_KL)
print("")
print("Min-Degree Fragility: ",Frag_Min)
print("")
print("Edge-Betweenness Fragility: ",Frag_EB)
print("")
print("Edge-Sum Fragility: ",Frag_ES)
print("")
print("Rewire Fragility: ",Frag_Rew )
print("")
print("ECI Fragility: ", Frag_ECI)
print("")