Skip to content

Commit

Permalink
Update print graph on tutorials (#84)
Browse files Browse the repository at this point in the history
* Add print graph to tutorials

* Add more arguments to print graph

* Delete previous png input files for tutorial

* Avoid repeated definition of initial positions
  • Loading branch information
martinateruzzi authored May 26, 2021
1 parent 9be1c57 commit adecfb0
Show file tree
Hide file tree
Showing 10 changed files with 666 additions and 173 deletions.
98 changes: 84 additions & 14 deletions grape/general_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import pandas as pd
from matplotlib import cm
from matplotlib.colors import ListedColormap
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
import matplotlib.pyplot as plt

warnings.simplefilter(action='ignore', category=FutureWarning)
Expand Down Expand Up @@ -1230,9 +1232,10 @@ def compute_service(self):
return computed_service, splitting

def print_graph(self, radius=None, initial_pos=None, fixed_nodes=None,
n_iter=500, thresh=0.0001, size=800, border='black', fsize=12,
fcolor='k', family='sans-serif', title='Graph', input_cmap=None,
save_to_file=None):
n_iter=500, thresh=0.0001, size=800, border='black', edge_width=1.0,
arrow_size=10, fsize=12, fcolor='k', family='sans-serif', title='Graph',
input_cmap=None, legend_loc='upper right', legend_ncol=1,
legend_anchor=None, legend_fsize=12, save_to_file=None):
"""
Print the graph.
Expand All @@ -1256,7 +1259,7 @@ def print_graph(self, radius=None, initial_pos=None, fixed_nodes=None,
:type radius: float, optional, default to 1/sqrt(n) where n is the
number of nodes in the graph
:param initial_pos: initial positions for nodes as a dictionary with
node as jeys and values as a coordinate list or tuple. If None,
node as keys and values as a coordinate list or tuple. If None,
then use random initial positions.
:type initial_pos: dict, optional, default to None
:param fixed_nodes: nodes to keep fixed at initial position. ValueError
Expand All @@ -1272,6 +1275,10 @@ def print_graph(self, radius=None, initial_pos=None, fixed_nodes=None,
:type size: int, optional, default to 800
:param border: color of node borders.
:type border: color, optional, default to 'black'
:param edge_width: width of edges.
:type edge_width: float, optional, default to 1.0
:param arrow_size: size of the arrow head head’s length and width.
:type arrow_size: int, optional, default to 10
:param fsize: font size for text labels.
:type fsize: int, optional, default to 12
:param fcolor: font color string for labels.
Expand All @@ -1280,9 +1287,22 @@ def print_graph(self, radius=None, initial_pos=None, fixed_nodes=None,
:type ffamily: string, optional, default to 'sans-serif'
:param title: title for figure window.
:type title: string, optional, defaut to 'Graph'
:param cmap: colormap for coloring the different areas with different
colors. If None, all nodes are colored as white.
:type cmap: Matplotlib colormap, optional, default to None
:param input_cmap: colormap for coloring the different areas with
different colors. If None, all nodes are colored as white.
:type input_cmap: Matplotlib colormap, optional, default to None
:param legend_loc: the location of the legend.
:type legend_loc: str, optional, default to 'upper right'
:param legend_ncol: the number of columns that the legend has.
:type legend_ncol: int, optional, default to 1
:param legend_anchor: box that is used to position the legend in
conjunction with loc.
:type legend_anchor: 2-tuple, or 4-tuple of floats, optional,
defaults to axes.bbox (if called as a method to Axes.legend)
or figure.bbox (if Figure.legend).
This argument allows arbitrary placement of the legend
:param legend_fsize: the font size of the legend. The value must be
numeric, implying the size the absolute font size in points.
:type legend_fsize: int, optional, default to 12
:param save_to_file: name of the file where to save the graph drawing.
The extension is guesses from the filename.
Interactive window is rendered in any case.
Expand Down Expand Up @@ -1318,27 +1338,77 @@ def print_graph(self, radius=None, initial_pos=None, fixed_nodes=None,
node_shape=shapes[self.type[node]], node_size=size,
edgecolors=border)

pert_resistant = [node for node in self.perturbation_resistant.keys()
if self.perturbation_resistant[node] == 1]

for node in pert_resistant:
col = mymap(area_indices[self.area[node]])
col = np.array([col])
nx.draw_networkx_nodes(self, pos, nodelist=[node], node_color=col,
node_shape=shapes[self.type[node]], node_size=size,
edgecolors='red')

or_edges = [(u, v) for (u, v, d) in self.edges(data=True)
if d['father_condition'] == 'OR']
and_edges = [(u, v) for (u, v, d) in self.edges(data=True)
if d['father_condition'] == 'AND']
single_edges = [(u, v) for (u, v, d) in self.edges(data=True)
if d['father_condition'] == 'SINGLE']

nx.draw_networkx_edges(self, pos, edgelist=or_edges, width=3, alpha=0.9,
edge_color='brown', style='dashed', node_size=size)
nx.draw_networkx_edges(self, pos, edgelist=and_edges, width=3,
alpha=0.9, edge_color='violet', node_size=size)
nx.draw_networkx_edges(self, pos, edgelist=single_edges, width=3,
alpha=0.9, edge_color='black', node_size=size)
nx.draw_networkx_edges(self, pos, edgelist=or_edges, width=edge_width,
arrowsize=arrow_size, alpha=0.9, edge_color='brown', style='dashed',
node_size=size)
nx.draw_networkx_edges(self, pos, edgelist=and_edges, width=edge_width,
arrowsize=arrow_size, alpha=0.9, edge_color='violet',
node_size=size)
nx.draw_networkx_edges(self, pos, edgelist=single_edges,
width=edge_width, arrowsize=arrow_size, alpha=0.9,
edge_color='black', node_size=size)

nx.draw_networkx_labels(self, pos, labels=self.mark, font_size=fsize,
font_color=fcolor)

plt.get_current_fig_manager().canvas.set_window_title(title)
plt.tight_layout()
plt.axis('off')

handles = []
plt.rcParams.update({"text.usetex": True})

source_key = mlines.Line2D([], [], color='white', marker='v',
markeredgecolor='black', markersize=legend_fsize, label='SOURCE')
handles.append(source_key)
user_key = mlines.Line2D([], [], color='white', marker='^',
markeredgecolor='black', markersize=legend_fsize, label='USER')
handles.append(user_key)
hub_key = mlines.Line2D([], [], color='white', marker='o',
markeredgecolor='black', markersize=legend_fsize, label='HUB')
handles.append(hub_key)
switch_key = mlines.Line2D([], [], color='white', marker='X',
markeredgecolor='black', markersize=legend_fsize, label='SWITCH')
handles.append(switch_key)
pr_key = mlines.Line2D([], [], color='white', marker='o',
markeredgecolor='red', markersize=legend_fsize,
label='Perturbation Resistant')
handles.append(pr_key)

single_key = mlines.Line2D([], [], color='white',
marker=r'$\rightarrow$', markeredgecolor='black',
markersize=legend_fsize, label='SINGLE')
handles.append(single_key)
or_key = mlines.Line2D([], [], color='white',
marker=r'$-\rightarrow$', markeredgecolor='brown',
markersize=legend_fsize, label='OR')
handles.append(or_key)
and_key = mlines.Line2D([], [], color='white',
marker=r'$\rightarrow$', markeredgecolor='violet',
markersize=legend_fsize, label='AND')
handles.append(and_key)

plt.legend(handles=handles, loc=legend_loc, ncol=legend_ncol,
bbox_to_anchor=legend_anchor, fontsize=legend_fsize)

if save_to_file:
plt.savefig(save_to_file, orientation='landscape', transparent=True)
else:
plt.show()
plt.show()
221 changes: 167 additions & 54 deletions tutorials/tutorial01/01_element_perturbation.ipynb

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions tutorials/tutorial01/01_element_perturbation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@

F = FaultDiagnosis("./input_files/TOY_graph.csv")

initial = { '11': (2., 2.), '19': (6., 2.), '12': (10., 2.),
'14': (6., 10.), '13': (10., 10.), '18': (2., 10.), '5': (-2., 2.),
'3': (-4., 6.), '1': (-6., 10.), '2': (-8., 6.), '4': (-10., 2.),
'6': (-6., -2.), '7': (-10, -10.), '8': (-2, -10.), '10': (2., -2.),
'17': (10., -2.), '16': (10., -10.), '9': (2., -10.), '15': (6., -6.)}

F.G.print_graph(initial_pos=initial, size=800, edge_width=3., arrow_size=7,
fsize=12, fixed_nodes=list(F.G), title='TOY graph (integer)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=4,
legend_anchor=(0.5, 1.2), legend_fsize=12)

F.check_input_with_gephi()
F.simulate_element_perturbation(["1"])

F.G.print_graph(initial_pos=initial, size=800, edge_width=3., arrow_size=7,
fsize=12, fixed_nodes=list(F.G), title='TOY graph (perturbed)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=4,
legend_anchor=(0.5, 1.2), legend_fsize=12)
Binary file removed tutorials/tutorial01/input_files/TOY_graph.png
Binary file not shown.
211 changes: 161 additions & 50 deletions tutorials/tutorial02/02_area_perturbation.ipynb

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions tutorials/tutorial02/02_area_perturbation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@

F = FaultDiagnosis("./input_files/TOY_graph.csv")

initial = { '11': (2., 2.), '19': (6., 2.), '12': (10., 2.),
'14': (6., 10.), '13': (10., 10.), '18': (2., 10.), '5': (-2., 2.),
'3': (-4., 6.), '1': (-6., 10.), '2': (-8., 6.), '4': (-10., 2.),
'6': (-6., -2.), '7': (-10, -10.), '8': (-2, -10.), '10': (2., -2.),
'17': (10., -2.), '16': (10., -10.), '9': (2., -10.), '15': (6., -6.)}

F.G.print_graph(initial_pos=initial, size=800, edge_width=3., arrow_size=7,
fsize=12, fixed_nodes=list(F.G), title='TOY graph (integer)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=4,
legend_anchor=(0.5, 1.2), legend_fsize=12)

F.check_input_with_gephi()
F.simulate_area_perturbation(["area1"])

F.G.print_graph(initial_pos=initial, size=800, edge_width=3., arrow_size=7,
fsize=12, fixed_nodes=list(F.G), title='TOY graph (perturbed)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=4,
legend_anchor=(0.5, 1.2), legend_fsize=12)
Binary file removed tutorials/tutorial02/input_files/TOY_graph.png
Binary file not shown.
248 changes: 193 additions & 55 deletions tutorials/tutorial03/03_switch_activation.ipynb

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions tutorials/tutorial03/03_switch_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,41 @@

F = FaultDiagnosis("./input_files/switch_line.csv")
F.check_input_with_gephi()

initial = {'10': (0., 0.), '1': (0., 2.), 'A': (0., 4.),
'S1': (2., 2.), '2': (4., 2.), 'S2': (6., 2.), '3': (8., 2.),
'11': (8., 0.), 'S3': (10., 2.), '4': (12., 2.), 'S4': (14., 2.),
'5': (16., 2.), '12': (16., 0.), 'B': (16., 4.), 'S5': (18., 2.),
'6': (20., 2.), 'S6': (22., 2.), '7': (24., 2.), '13': (24., 0.),
'C': (24., 4.), 'S7': (26., 2.), '8': (28., 2.), 'S8': (30., 2.),
'9': (32., 2.), '14': (32., 0.)}

F.G.print_graph(initial_pos=initial, size=200, arrow_size=4, fsize=10,
fixed_nodes=list(F.G), title='Switch line (integer)', input_cmap='Accent',
legend_loc='upper center', legend_ncol=8, legend_anchor=(0.5, 1.05),
legend_fsize=7)

F.simulate_element_perturbation(["1"])
print("\nPredecessors of S1: ", list(F.G.predecessors('S1')))
print("\nSuccessors of S1: ", list(F.G.successors('S1')))

F.G.print_graph(initial_pos=initial, size=200, arrow_size=4, fsize=10,
fixed_nodes=list(F.G), title='Switch line (node 1 perturbed)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=8,
legend_anchor=(0.5, 1.05), legend_fsize=7)

D = FaultDiagnosis("./input_files/switch_line.csv")
D.simulate_element_perturbation(["2"])

D.G.print_graph(initial_pos=initial, size=200, arrow_size=4, fsize=10,
fixed_nodes=list(D.G), title='Switch line (node 2 perturbed)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=8,
legend_anchor=(0.5, 1.05), legend_fsize=7)

T = FaultDiagnosis("./input_files/switch_line.csv")
T.simulate_element_perturbation(["2", "3"])

T.G.print_graph(initial_pos=initial, size=200, arrow_size=4, fsize=10,
fixed_nodes=list(T.G), title='Switch line (nodes 2 and 3 perturbed)',
input_cmap='Accent', legend_loc='upper center', legend_ncol=8,
legend_anchor=(0.5, 1.05), legend_fsize=7)
Binary file removed tutorials/tutorial03/input_files/switch_line.png
Binary file not shown.

0 comments on commit adecfb0

Please sign in to comment.