Skip to content
Extraits de code Groupes Projets
Valider c07dd5c6 rédigé par Thomas MESLIN's avatar Thomas MESLIN
Parcourir les fichiers

Bon

parent e1544154
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Impossible d'afficher diff de source : il est trop volumineux. Options pour résoudre ce problème : voir le blob.
Impossible d'afficher diff de source : il est trop volumineux. Options pour résoudre ce problème : voir le blob.
import csv import csv
import networkx as nx import networkx as nx
from networkx.algorithms.community.louvain import louvain_communities from networkx.algorithms.community import louvain_communities
from pyvis.network import Network from pyvis.network import Network
import networkx as nx import random
G = nx.Graph()
path_node = "/home/agryos/Documents/Hackiie/graphe_association/res_script/nodes_avec_fantome.csv"
total_noeud = [] def centrality_to_color(value):
with (open(path_node, newline='') as csvfile): """Convertit une valeur de centralité (0 à 1) en couleur hex (bleu → rouge)."""
r = int(value * 255)
g = 0
b = int(255 - value * 255)
return f'#{r:02x}{g:02x}{b:02x}'
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader: # 1. Lecture du graphe biparti
B = nx.Graph()
path_node = "/home/agryos/Documents/Hackiie/graphe_association/res_script/nodes_sans_fantome.csv"
with open(path_node, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
if row[0] == "Id": if row[0] == "Id":
continue continue
id_node = int(row[0]) node_id = int(row[0])
label_node = row[1] label = row[1]
categorie = row[2] categorie = row[2] # "Asso" ou "Eleve"
color = "red" if categorie == "Asso" else "blue" B.add_node(node_id, label=label, bipartite=categorie)
node_to_add = (id_node,{"label":label_node, "categorie":categorie,"color":color})
total_noeud.append(node_to_add)
G.add_nodes_from(total_noeud) # 2. Lecture des arêtes
path_edge = "/home/agryos/Documents/Hackiie/graphe_association/res_script/edges_sans_fantome.csv"
maxi = 0
total_edge = []
path_edge = "/home/agryos/Documents/Hackiie/graphe_association/res_script/edges_avec_fantome.csv"
with open(path_edge, newline='') as csvfile: with open(path_edge, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') for row in reader:
for row in spamreader:
if row[0] == "Source": if row[0] == "Source":
continue continue
edge_source = int(row[0]) src = int(row[0])
edge_target = int(row[1]) tgt = int(row[1])
edge_weight = float(row[4]) weight = float(row[4])
maxi = max(maxi,edge_weight) B.add_edge(src, tgt, weight=weight)
edge_to_add = (edge_source,edge_target,edge_weight)
total_edge.append(edge_to_add)
total_edge_normalized = []
min_w = 1
max_w = 10
for (x1,x2,w) in total_edge:
total_edge_normalized.append((x1,x2,(max_w - min_w)*w/maxi + min_w))
G.add_weighted_edges_from(total_edge_normalized)
options = str({
"physics": {
"forceAtlas2Based": {
"theta": 0.55,
"gravitationalConstant": -23,
"centralGravity": 0.02,
"springLength": 160,
"springConstant": 0.005,
"damping": 0.31,
"avoidOverlap": 1
},
"maxVelocity": 33,
"minVelocity": 0.66,
"solver": "forceAtlas2Based",
"timestep": 0.25
}
}).replace('\'','"')
print(options)
communaute = louvain_communities(G,"weight",0.5) # 3. Projection sur les élèves uniquement
print(communaute) eleves = [n for n, d in B.nodes(data=True) if d["bipartite"] != "Asso"]
import random G_proj = nx.bipartite.weighted_projected_graph(B, eleves)
for edge in G.edges: positions = nx.fruchterman_reingold_layout(G_proj, seed=42, k=1,scale=6) # k ajuste l'espacement
G.ed = "brown"
for s in communaute: # 4. Calcul de la centralité
col = ["#"+''.join([random.choice('ABCDEF0123456789') for i in range(6)])][0] centrality = nx.betweenness_centrality(G_proj,weight="weight") # ou nx.betweenness_centrality(G_proj, weight="weight")
print(col)
for idx in s:
if len(s) == 1:
G.nodes[idx][color] = "grey"
nt = Network('500px', '500px') # 6. Affichage avec PyVis
nt.from_nx(G) net = Network("900px", "900px")
nt.force_atlas_2based(-23,0.02,160,0.015,0.31,1) net.set_options('''
nt.save_graph("python_truc.html") {"physics": {
"enabled": false
},
"nodes": {
nt.show_buttons(filter_=True) "highlight": {
nt.show('nx.html',notebook=False) "border": {
"width": 3,
"color": {
"highlight": "red"
}
},
"color": {
"highlight": {
"background": "yellow",
"border": "red"
}
}
}
},
"edges": {
"highlight": {
"color": {
"highlight": "#e1ccc8"
},
"width": 3
}
}
}
''')
maxi = 0
for node, pos in positions.items():
cent_value = centrality.get(node, 0)
maxi = max(cent_value,maxi)
# 6. Ajout des nœuds avec couleur selon centralité
for node, pos in positions.items():
label = B.nodes[node].get("label", str(node))
cent_value = centrality.get(node, 0)
color = centrality_to_color(cent_value/maxi)
x = pos[0] * 1000
y = pos[1] * 1000
net.add_node(
node,
label=label,
color=color,
borderWidth=2,
x=x,
y=y,
fixed=True,
title=f"{label}<br>Centralité : {cent_value:.3f}"
)
# 7. Ajout des arêtes
for src, tgt, data in G_proj.edges(data=True):
net.add_edge(src, tgt, value=data["weight"])
net.save_graph("chaatgpt_truc.html")
net.show("projection_communautes.html",notebook=False)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter