Spaces:
Running
Running
| """ | |
| MODULE ALGORITHM - AI METRICS (LOUVAIN & PAGERANK) | |
| ================================================== | |
| Calcule et injecte les métriques directement dans les attributs des nœuds. | |
| """ | |
| import community.community_louvain as community_louvain | |
| import networkx as nx | |
| def apply_ai_algorithms(G: nx.Graph): | |
| ai_metadata = {} | |
| # 1. PAGERANK (Influence) | |
| try: | |
| pagerank_scores = nx.pagerank(G, alpha=0.85) | |
| nx.set_node_attributes(G, pagerank_scores, 'pagerank_score') | |
| # On ajoute le score dans le 'title' pour le survol souris | |
| for n, score in pagerank_scores.items(): | |
| current_title = G.nodes[n].get('title', '') | |
| G.nodes[n]['title'] = f"{current_title}\nInfluence (PR): {score:.4f}".strip() | |
| except Exception as e: | |
| print(f"Erreur PageRank: {e}") | |
| # 2. LOUVAIN (Communautés) | |
| try: | |
| # Louvain nécessite un graphe non-dirigé | |
| G_undirected = G.to_undirected() | |
| partition = community_louvain.best_partition(G_undirected) | |
| nx.set_node_attributes(G, partition, 'community_id') | |
| # Stats pour l'IA | |
| num_communities = len(set(partition.values())) | |
| ai_metadata["louvain_communities_count"] = num_communities | |
| # Ajout info au survol | |
| for n, cid in partition.items(): | |
| current_title = G.nodes[n].get('title', '') | |
| G.nodes[n]['title'] = f"{current_title}\nGroupe: #{cid}".strip() | |
| except Exception as e: | |
| print(f"Erreur Louvain: {e}") | |
| ai_metadata["louvain_communities_count"] = 0 | |
| return G, ai_metadata |