Commit 55d54406 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[cmd] Move merge_graphs() to graphs module

parent bbf2da9f
......@@ -74,7 +74,6 @@ import json
import os
import glob
import csv
import networkx as nx
# Library imports
......@@ -82,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
from tools.graphs import export_graph, build_graph, merge_graphs
import cadbiom.commons as cm
......@@ -396,40 +395,6 @@ def solutions_2_common_graph(output_dir, model_file, path):
LOGGER.info("Files processed: %s", file_number)
assert file_number != 0, "No *mac_complete.txt files found!"
def merge_graphs(graphs):
"""Merge graphs in the given iterable; count and add the weights to the edges
of the final graph
:param graphs: Networkx graph objects.
:type graphs: <generator <networkx.classes.digraph.DiGraph>>
:return: Networkx graph object.
:rtype: <networkx.classes.digraph.DiGraph>
"""
G = nx.DiGraph()
for graph in graphs:
missing_nodes = set(graph.nodes_iter()) - set(G.nodes_iter())
if missing_nodes:
# Add missing nodes in G from the current graph
# Note: This step is mandatory even if add_edge() automatically adds
# nodes to the graph, because some nodes can be included in any
# transition.
# Build a tuple (node_name, attrs)
G.add_nodes_from((node, graph.node[node]) for node in missing_nodes)
for ori, ext, data in graph.edges_iter(data=True):
if G.has_edge(ori, ext):
# Update the edge
G[ori][ext]['weight'] += 1
else:
# Add the missing edge
G.add_edge(ori, ext, attr_dict=data, weight=1)
return G
## Matrices of occurrences #####################################################
def solutions_2_occcurrences_matrix(output_dir, model_file, path,
......
......@@ -334,3 +334,37 @@ def export_graph(output_dir, solution, solution_index, G, *args):
# Save
# PS: inhibitors will still have not the attribute 'color' = 'white'
nx.write_graphml(G, filename + ".graphml")
def merge_graphs(graphs):
"""Merge graphs in the given iterable; count and add the weights to the edges
of the final graph
:param graphs: Networkx graph objects.
:type graphs: <generator <networkx.classes.digraph.DiGraph>>
:return: Networkx graph object.
:rtype: <networkx.classes.digraph.DiGraph>
"""
G = nx.DiGraph()
for graph in graphs:
missing_nodes = set(graph.nodes_iter()) - set(G.nodes_iter())
if missing_nodes:
# Add missing nodes in G from the current graph
# Note: This step is mandatory even if add_edge() automatically adds
# nodes to the graph, because some nodes can be included in any
# transition.
# Build a tuple (node_name, attrs)
G.add_nodes_from((node, graph.node[node]) for node in missing_nodes)
for ori, ext, data in graph.edges_iter(data=True):
if G.has_edge(ori, ext):
# Update the edge
G[ori][ext]['weight'] += 1
else:
# Add the missing edge
G.add_edge(ori, ext, attr_dict=data, weight=1)
return G
Markdown is supported
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