Commit 043de8c5 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Work on the remove of matplotlib dependency; Fix regexp for event detection...

Work on the remove of matplotlib dependency; Fix regexp for event detection (less strict => allow letters)
parent ef25db19
......@@ -197,7 +197,7 @@ def get_transitions(file, all_places=False):
# and a node (__start__?) for this case.
trans.event = 'SCC-' + trans.ori.name
events = {trans.event: trans.condition}
elif re.match('_h_[0-9_\.]+', trans.event):
elif re.match('_h[\w]+', trans.event):
# 1 event (with 1 clock)
events = {trans.event: trans.condition}
else:
......@@ -652,8 +652,14 @@ def build_graph(solution, steps, transitions):
edges = list() # Normal transitions without condition
# Get all nodes in all transitions (origin & ext)
all_transitions = (transitions[step_event] for step_event in it.chain(*steps))
transitions_ori_ext = (tuple((trans[0], trans[1])) for trans in it.chain(*all_transitions))
try:
all_transitions = \
(transitions[step_event] for step_event in it.chain(*steps))
transitions_ori_ext = \
(tuple((trans[0], trans[1])) for trans in it.chain(*all_transitions))
except KeyError:
print("/!\One event is not in the given model file... Please check it.")
raise
all_nodes = set(it.chain(*transitions_ori_ext)) | set(frontier_places)
# Parse all conditions in transitions;
......@@ -702,6 +708,7 @@ def draw_graph(output_dir, solution, solution_index, G,
(used to distinguish exported filenames).
:param arg3: Networkx graph object.
:param arg4: Nodes corresponding to transitions with conditions.
List of tuples: event, node
:param arg5: All nodes in the model
:param arg6: Edges between transition node and nodes in condition
:param arg7: Normal transitions without condition
......@@ -714,6 +721,19 @@ def draw_graph(output_dir, solution, solution_index, G,
:type arg7: <list>
"""
creation_date = dt.datetime.now().strftime("%H-%M-%S")
# Save & show
if not matplotlib_export:
# Save the graph without matplotlib requirement
# PS: inhibitors will still have not the attribute 'color' = 'white'
nx.write_graphml(
G,
"{}{}_{}_{}.graphml".format(
output_dir, creation_date, solution_index, solution[:75]
)
)
return
# Drawing ##################################################################
# draw_circular(G, **kwargs) On a circle.
# draw_random(G, **kwargs) Uniformly at random in the unit square.
......@@ -721,19 +741,18 @@ def draw_graph(output_dir, solution, solution_index, G,
# draw_spring(G, **kwargs) Fruchterman-Reingold force-directed algorithm.
# draw_shell(G, **kwargs) Concentric circles.
# draw_graphviz(G[, prog]) Draw networkx graph with graphviz layout.
unzip = lambda l: list(zip(*l))
# Get a list of transition nodes (without dictionnary of attributes)
transition_nodes_names = [node[0] for node in transition_nodes]
pos = nx.circular_layout(G)
# Legend of conditions in transition nodes
if matplotlib_export:
f = plt.figure(1)
ax = f.add_subplot(1,1,1)
text = '\n'.join(
node_dict['label'] for node_dict in unzip(transition_nodes)[1]
)
ax.text(0, 0, text, style='italic', fontsize=10,
bbox={'facecolor':'white', 'alpha':0.5, 'pad':10})
f = plt.figure(1)
ax = f.add_subplot(1,1,1)
text = '\n'.join(transition_nodes_names)
ax.text(0, 0, text, style='italic', fontsize=10,
bbox={'facecolor': 'white', 'alpha': 0.5, 'pad': 10})
# Draw nodes:
# - red: frontier places (in solution variable),
......@@ -751,17 +770,11 @@ def draw_graph(output_dir, solution, solution_index, G,
else:
return 'white'
# Get a list of transition nodes (without dictionnary of attributes)
unziped_transition_nodes = unzip(transition_nodes)[0]
# Color nodes
colors = [color_map(node) for node in G.nodes_iter()]
if matplotlib_export:
nx.draw(G, pos=pos, with_labels=True,
node_color=colors, node_size=1000, alpha=0.5, ax=ax)
else:
nx.draw(G, pos=pos, with_labels=True,
node_color=colors, node_size=1000, alpha=0.5)
nx.draw(G, pos=pos, with_labels=True,
node_color=colors, node_size=1000, alpha=0.5,
ax=ax)
# Draw edges involved in transitions with conditions
edges_colors = [edge[2]['color'] for edge in edges_in_cond]
......@@ -774,20 +787,11 @@ def draw_graph(output_dir, solution, solution_index, G,
nx.draw_networkx_edge_labels(G, pos, edges_labels, label_pos=0.3)
# Save & show
date = dt.datetime.now().strftime("%H-%M-%S")
if matplotlib_export:
plt.legend()
plt.savefig(
output_dir + date + '_' + solution[:75] + ".svg",
format="svg")
plt.show()
nx.write_graphml(
G,
"{}{}_{}_{}.graphml".format(
output_dir, date, solution_index, solution[:75]
)
)
plt.legend()
plt.savefig(
output_dir + creation_date + '_' + solution[:75] + ".svg",
format="svg")
plt.show()
def process_solutions(output_dir, sol_steps, transitions):
......
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