Commit f6dbe2ac authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[cmd] Add get_solutions_graph_data to get graph data about a graph processed...

[cmd] Add get_solutions_graph_data to get graph data about a graph processed from solutions (not models)
parent b96339db
......@@ -81,7 +81,7 @@ from import get_solutions
from tools.models import get_transitions_from_model_file
from import load_solutions, convert_solutions_to_json, \
get_query_from_filename, get_mac_lines
from tools.graphs import export_graph, build_graph, merge_graphs
from tools.graphs import export_graph, build_graph, merge_graphs, get_solutions_graph_data
import cadbiom.commons as cm
......@@ -252,6 +252,76 @@ def build_graph(solution, steps, transitions):
return G, transition_nodes, all_nodes, edges_in_cond, edges
def get_solutions_graph_data(G, info, centralities):
"""Complete the given dictionary with information specific to the graph considered
By default the following information are added::
- graph_nodes: Number of nodes
- graph_edges: Number of edges
If centralities is True, the folliwing information are added to the a new
key named "centralities"::
- strongly_connected:
- weakly_connected
- max_degree
- min_degree
- average_degree
- degree
- connected_components_number
- connected_components
- average_shortest_paths
:param G: NetworkX directed graph
:param info: Dictionnary of data to be completed
:param centralities: Flag to activate the computation of centralities.
:type G: <networkx.classes.digraph.DiGraph>
:type info: <dict>
:type centralities: <boolean>
'graph_nodes': len(G.nodes()),
'graph_edges': len(G.edges()),
if not centralities:
# largest_connected_component = max(nx.connected_component_subgraphs(G), key=len)
connected_components = [g for g in nx.weakly_connected_component_subgraphs(G) if len(g) > 1]
shortest_paths = [nx.average_shortest_path_length(g) for g in connected_components]
degree =
'centralities': {
'strongly_connected': nx.is_strongly_connected(G),
'weakly_connected': nx.is_weakly_connected(G),
'max_degree': max(degree.values()),
'min_degree': min(degree.values()),
'average_degree': sum(degree.values()) / float(len(degree)),
'degree': degree,
'connected_components_number': len(shortest_paths),
'connected_components': [len(g) for g in connected_components],
'average_shortest_paths': sum(shortest_paths) / len(shortest_paths) if shortest_paths else "UKN",
})"%s", info)
def get_json_graph(G):
"""Translate Networkx graph into a dictionary ready to be dumped in a JSON file.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment