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 tools.solutions import get_solutions
from tools.models import get_transitions_from_model_file
from tools.solutions 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
Doc::
https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.component.html
https://networkx.github.io/documentation/stable/reference/algorithms/shortest_paths.html
average_shortest_path_length
https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.generic.average_shortest_path_length.html#networkx.algorithms.shortest_paths.generic.average_shortest_path_length
weakly_connected_component_subgraphs
https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.algorithms.components.weakly_connected.weakly_connected_component_subgraphs.html#networkx.algorithms.components.weakly_connected.weakly_connected_component_subgraphs
Measures
https://networkx.github.io/documentation/stable/reference/algorithms/index.html
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>
"""
info.update({
'graph_nodes': len(G.nodes()),
'graph_edges': len(G.edges()),
})
if not centralities:
return
# 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 = G.degree()
info.update({
'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",
}
})
LOGGER.info("%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