34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
import networkx as nx
|
|
|
|
def exporter_graphe_filtre(G, liens_chemins):
|
|
"""Gère l'export du graphe filtré au format DOT"""
|
|
|
|
G_export = nx.DiGraph()
|
|
for u, v in liens_chemins:
|
|
G_export.add_node(u, **G.nodes[u])
|
|
G_export.add_node(v, **G.nodes[v])
|
|
data = G.get_edge_data(u, v)
|
|
if isinstance(data, dict) and all(isinstance(k, int) for k in data):
|
|
G_export.add_edge(u, v, **data[0])
|
|
elif isinstance(data, dict):
|
|
G_export.add_edge(u, v, **data)
|
|
else:
|
|
G_export.add_edge(u, v)
|
|
|
|
return(G_export)
|
|
|
|
def extraire_liens_filtres(chemins, niveaux, niveau_depart, niveau_arrivee, niveaux_speciaux):
|
|
"""Extrait les liens des chemins en respectant les niveaux"""
|
|
liens = set()
|
|
for chemin in chemins:
|
|
for i in range(len(chemin) - 1):
|
|
u, v = chemin[i], chemin[i + 1]
|
|
niveau_u = niveaux.get(u, 999)
|
|
niveau_v = niveaux.get(v, 999)
|
|
if (
|
|
(niveau_depart <= niveau_u <= niveau_arrivee or niveau_u in niveaux_speciaux)
|
|
and (niveau_depart <= niveau_v <= niveau_arrivee or niveau_v in niveaux_speciaux)
|
|
):
|
|
liens.add((u, v))
|
|
return liens
|