Commit 5ff1a797 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Matplotlib is now optional

parent e939fd2d
......@@ -30,9 +30,14 @@ from collections import defaultdict
import networkx as nx
import itertools as it
import re
import matplotlib.pyplot as plt
import json
from logging import DEBUG
# Remove matplotlib dependency
# It is used on demand during the drawing of a graph
try:
import matplotlib.pyplot as plt
except ImportError:
pass
# Library imports
from cadbiom.models.guard_transitions.translators.chart_xml \
......@@ -665,7 +670,8 @@ def build_graph(solution, steps, transitions):
def draw_graph(output_dir, solution, solution_index, G,
transition_nodes, all_nodes,
edges_in_cond, edges):
edges_in_cond, edges,
matplotlib_export=False):
"""Draw graph with colors and export it to graphml format.
.. note:: Legend:
......@@ -702,13 +708,14 @@ def draw_graph(output_dir, solution, solution_index, G,
pos = nx.circular_layout(G)
# Legend of conditions in transition nodes
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})
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})
# Draw nodes:
# - red: frontier places (in solution variable),
......@@ -731,8 +738,12 @@ def draw_graph(output_dir, solution, solution_index, G,
# Color nodes
colors = [color_map(node) for node in G.nodes_iter()]
nx.draw(G, pos=pos, with_labels=True,
node_color=colors, node_size=1000, alpha=0.5, ax=ax)
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)
# Draw edges involved in transitions with conditions
edges_colors = [edge[2]['color'] for edge in edges_in_cond]
......@@ -746,9 +757,12 @@ def draw_graph(output_dir, solution, solution_index, G,
# Save & show
date = dt.datetime.now().strftime("%H-%M-%S")
# plt.legend()
# plt.savefig(output_dir + date + '_' + solution[:75] + ".svg", format="svg")
# plt.show()
if matplotlib_export:
plt.legend()
plt.savefig(
output_dir + date + '_' + solution[:75] + ".svg",
format="svg")
plt.show()
nx.write_graphml(
G,
......
......@@ -92,7 +92,9 @@ setup(
'console_scripts': ['cadbiom_cmd = cadbiom_cmd.cadbiom_cmd:main'],
},
install_requires=deps + ["matplotlib<2", "cadbiom"],
install_requires=deps + ["cadbiom"],
# For graphs drawing manually
extra_requires = ["matplotlib<2"],
# Tests
tests_require=['pytest'],
......
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