Skip to content

Instantly share code, notes, and snippets.

@briatte
Last active Nov 17, 2021
Embed
What would you like to do?
from networkx (Python) to igraph (R)
import networkx as nx
arcs = [(0,43), (0,56), (1,2), (1,6), (2,5), (2,14), (3,4), (3,17),(4,5), (4,16), (5,15), (5,14), (6,7), (6,14), (7,8), (7,14), (8,9), (8,11), (10,11), (10,24), (11,12), (11,24), (11,25), (12,13), (12,23), (13,14), (13,22), (14,21), (15,21), (15,18), (16,17), (16,18), (17,19), (18,20), (19,20), (20,21), (20,35), (20,36), (21,33), (22,26), (22,27), (23,25), (23,26), (24,25), (25,30), (26,31), (27,32), (27,33), (28,29), (28,30), (29,30), (29,43), (30,31), (32,42), (33,34), (35,38), (36,37), (36,38), (37,39), (38,39), (38,41), (38,40), (39,40), (39,47), (40,46), (34,41), (42,43), (42,44), (43,44), (44,55), (45,55), (45,52), (46,51), (47,48), (47,50), (48,49), (48,50), (49,50), (50,51), (51,52), (52,53), (53,54), (53,55), (55,56), (34,42), (34,45), (35,36)]
villes = ["Bayonne","Calais", "Lille", "Strasbourg", "Metz", "Reims", "Amiens", "Rouen", "Caen", "Cherbourg", "Brest", "Rennes", "Le Mans", "Chartres", "Paris", "Troyes", "Nancy", "Mulhouse", "Langres", "Besancon", "Dijon", "Sens", "Orleans", "Angers", "Vannes", "Nantes", "Tours", "Vierzon", "La Rochelle", "Saintes", "Niort", "Poitiers", "Limoges", "Vichy", "Clermont-Fd", "Macon", "Bourg-en-Bresse", "Geneve", "Lyon", "Grenoble", "Valence", "St-Etienne", "Brive", "Bordeaux", "Montauban", "Millau", "Avignon", "Digne-les-bains", "Nice", "Toulon", "Marseille", "Nimes", "Montpellier", "Narbonne", "Perpignan", "Toulouse", "Pau"]
g = nx.Graph()
g.add_edges_from(arcs)
library(igraph)
g = graph_from_literal(0--43, 0--56, 1--2, 1--6, 2--5, 2--14, 3--4, 3--17,4--5, 4--16, 5--15, 5--14, 6--7, 6--14, 7--8, 7--14, 8--9, 8--11, 10--11, 10--24, 11--12, 11--24, 11--25, 12--13, 12--23, 13--14, 13--22, 14--21, 15--21, 15--18, 16--17, 16--18, 17--19, 18--20, 19--20, 20--21, 20--35, 20--36, 21--33, 22--26, 22--27, 23--25, 23--26, 24--25, 25--30, 26--31, 27--32, 27--33, 28--29, 28--30, 29--30, 29--43, 30--31, 32--42, 33--34, 35--38, 36--37, 36--38, 37--39, 38--39, 38--41, 38--40, 39--40, 39--47, 40--46, 34--41, 42--43, 42--44, 43--44, 44--55, 45--55, 45--52, 46--51, 47--48, 47--50, 48--49, 48--50, 49--50, 50--51, 51--52, 52--53, 53--54, 53--55, 55--56, 34--42, 34--45, 35--36)
V(g)$name = c("Bayonne","Calais", "Lille", "Strasbourg", "Metz", "Reims", "Amiens", "Rouen", "Caen", "Cherbourg", "Brest", "Rennes", "Le Mans", "Chartres", "Paris", "Troyes", "Nancy", "Mulhouse", "Langres", "Besancon", "Dijon", "Sens", "Orleans", "Angers", "Vannes", "Nantes", "Tours", "Vierzon", "La Rochelle", "Saintes", "Niort", "Poitiers", "Limoges", "Vichy", "Clermont-Fd", "Macon", "Bourg-en-Bresse", "Geneve", "Lyon", "Grenoble", "Valence", "St-Etienne", "Brive", "Bordeaux", "Montauban", "Millau", "Avignon", "Digne-les-bains", "Nice", "Toulon", "Marseille", "Nimes", "Montpellier", "Narbonne", "Perpignan", "Toulouse", "Pau")[ as.integer(V(g)$name) + 1 ]
import operator
deg = nx.degree(g)
bet = nx.betweenness_centrality(g)
tri = sorted(bet.items(), reverse = True, key = operator.itemgetter(1))
for i in range(len(tri)):
print(" " + str(i + 1) + " " + villes[ int(tri[i][0]) ])
V(g)$degree = degree(g, normalized = FALSE)
V(g)$betweenness = betweenness(g, normalized = TRUE, weights = NULL)
# cities ranked by decreasing betweenness
V(g)$name[ order(V(g)$betweenness, decreasing = TRUE) ]
library(ggmap)
geo = geocode(paste(V(g)$name, "France"), output = "latlona")
plot(g, layout = as.matrix(geo[, 1:2]),
vertex.color = NA, vertex.size = 10, vertex.frame.color = NA,
vertex.label.family = "Helvetica", vertex.label.color = "black",
vertex.label.cex = V(g)$degree / 5)
aspl = nx.average_shortest_path_length(g)
vul = []
for i in range(len(g.nodes())):
if (i != 8 and i !=53): # exceptions
g2 = nx.Graph()
g2.add_edges_from(arcs)
g2.remove_node(i)
vul = vul + [(i, nx.average_shortest_path_length(g2) - aspl)]
trivul = sorted(vul, reverse = True, key = operator.itemgetter(1))
for i in range(len(trivul)):
print(" " + str(i + 1) + " " + villes[ int(trivul[i][0]) ])
aspl = mean_distance(g)
V(g)$vulnerability = NA
for (i in 1:vcount(g)) {
g2 = delete_vertices(g, i)
if (all(degree(g2) > 0)) { V(g)$vulnerability[i] = mean_distance(g2) - aspl }
}
# cities ranked by decreasing vulnerability
V(g)$name[ order(V(g)$vulnerability, decreasing = TRUE, na.last = NA) ]
vulnerability = function(g) {
d = mean_distance(g)
v = sapply(1:vcount(g), function(v) {
g2 = delete_vertices(g, v)
if (all(degree(g2) > 0)) { mean_distance(g2) - d } else { NA }
})
names(v) = V(g)$name
v
}
# sanity check
all(V(g)$vulnerability == vulnerability(g), na.rm = TRUE)
# named output
vulnerability(g)
import numpy
vul2 = nx.closeness_vitality(g)
mvul2 = list(vul2.items())
avul2 = numpy.array(mvul2)
x = avul2[:, 1]
mbet = list(bet.items())
abet = numpy.array(mbet)
y = abet[:, 1]
from scipy import stats
slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
r2 = r_value * r_value
print r2
closeness_vitality = function(g) {
a = sum(igraph::distances(g))
v = sapply(1:igraph::vcount(g), function(v) {
d = igraph::distances(igraph::delete_vertices(g, v))
a - sum(d[ !is.infinite(d) ])
})
names(v) = V(g)$name
v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment