Skip to content

Commit 4dc5d7d

Browse files
Update docs (#122)
1 parent 6d82183 commit 4dc5d7d

File tree

2 files changed

+97
-67
lines changed

2 files changed

+97
-67
lines changed

grape/fault_diagnosis.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ def fitness_iteration_parallel(self, out_queue, ichunk, chunk_length,
8181
index of the individual, the individual itself, and its fitness.
8282
8383
:param multiprocessing.queues.Queue out_queue: multiprocessing queue
84-
:param int ichunk: index of the chunk under consideration
84+
:param int ichunk: index of the chunk under consideration.
8585
:param int chunk_length: lengths of the chunks (the last chunk may
8686
be shorter due to non-even division of the number of generations by
87-
the number of processors)
87+
the number of processors).
8888
:param list individuals: list of individuals on which to perform
8989
fitness evaluation.
9090
:param list perturbed_nodes: nodes(s) involved in the perturbing event.
9191
:param dict initial_condition: initial status (boolean) for the graph
9292
switches.
93-
93+
:param origin: origin of the perturbation. Nodes can be resitant to
94+
different kinds of perturbations, default to None.
95+
:type origin: string, optional
9496
"""
9597

9698
for iter_ind in range(len(individuals)):
@@ -106,13 +108,16 @@ def fitness_evaluation_parallel(self, pop, perturbed_nodes,
106108
Wrapper for fitness evaluation. This methods spawns the processes for
107109
fitness evaluation and collects the results.
108110
109-
:param list pop: list of individuals
111+
:param list pop: list of individuals.
110112
:param list perturbed_nodes: nodes(s) involved in the perturbing event.
111113
:param dict initial_condition: initial status (boolean) for the graph
112114
switches.
115+
:param origin: origin of the perturbation. Nodes can be resitant to
116+
different kinds of perturbations, default to None.
117+
:type origin: string, optional
113118
114119
:return: list of tuples constituted by the index of the individual,
115-
the individual itself, and its fitness
120+
the individual itself, and its fitness.
116121
:rtype: list
117122
"""
118123

@@ -157,6 +162,9 @@ def fitness_evaluation(self, individual, perturbed_nodes,
157162
perturbing event.
158163
:param dict initial_condition: initial status (boolean) for the graph
159164
switches.
165+
:param origin: origin of the perturbation. Nodes can be resitant to
166+
different kinds of perturbations, default to None.
167+
:type origin: string, optional
160168
"""
161169

162170
acts = np.sum(np.not_equal(list(initial_condition.values()),
@@ -226,7 +234,10 @@ def optimizer(self, perturbed_nodes, initial_condition, params, weights,
226234
(default to -1.0)
227235
- 'w5': weight for service balance over users (default to 2.0)
228236
:param bool parallel: flag for parallel fitness evaluation of
229-
initial population
237+
initial populations.
238+
:param origin: origin of the perturbation. Nodes can be resitant to
239+
different kinds of perturbations, default to None.
240+
:type origin: string, optional
230241
"""
231242

232243
logging.getLogger().setLevel(logging.INFO)
@@ -263,28 +274,24 @@ def optimizer(self, perturbed_nodes, initial_condition, params, weights,
263274
for ind, f in zip(pop, fitnesses):
264275
ind.fitness.values = (np.dot(w, np.asarray(f)),)
265276

266-
# Variable keeping track of the number of generations
277+
# Generations
267278
g = 0
268-
269279
result = []
270-
# Begin the evolution
271280

272-
# Population is made by individuals and fitnesses
273281
pop = [list(couple) for couple in zip(pop, fitnesses)]
274282
pop.sort(key=lambda x: np.dot(w, np.asarray(x[1])))
275283

276284
while g < params['ngen']:
277285

278-
# A new generation
279286
g = g + 1
280287

281-
# Select the next generation individuals
288+
# Select next generation
282289
offspring = [x[0] for x in pop[:params['nsel']]]
283290
pop = pop[:params['nsel']]
284291

285292
invalid_ind = []
286293

287-
# Apply crossover and mutation on the offspring
294+
# Crossover and mutation on the offspring
288295
for parent1, parent2 in zip(offspring[::2], offspring[1::2]):
289296
if random.random() < params['tresh']:
290297
child1 = toolbox.clone(parent1)
@@ -401,11 +408,14 @@ def rm_nodes(self, node, graph, origin, visited=None, broken_nodes=None):
401408
402409
:param str node: the id of the node to remove.
403410
:param nx.DiGraph graph: graph on which to apply the node deletion
411+
:param origin: origin of the perturbation. Nodes can be resitant to
412+
different kinds of perturbations, default to None.
413+
:type origin: string, optional
404414
:param visited: nodes already visited, default to None.
405415
:type visited: set, optional
406416
:param broken_nodes: nodes that got broken along the perturbation,
407417
default to None.
408-
:type visited: list, optional
418+
:type broken_nodes: list, optional
409419
"""
410420

411421
if visited is None:
@@ -508,6 +518,9 @@ def delete_a_node(self, node, origin):
508518
Delete a node in the graph.
509519
510520
:param str node: the id of the node to remove.
521+
:param origin: origin of the perturbation. Nodes can be resitant to
522+
different kinds of perturbations, default to None.
523+
:type origin: string, optional
511524
512525
.. warning:: the node id must be contained in the graph.
513526
No check is done within this function.
@@ -555,10 +568,13 @@ def apply_perturbation(self, perturbed_nodes, params, weights, parallel,
555568
(default to -1.0)
556569
- 'w5': weight for service balance over users (default to 2.0)
557570
:param bool parallel: flag for parallel fitness evaluation of
558-
initial population
559-
:param bool verbose: flag for verbose printing
571+
initial population.
572+
:param bool verbose: flag for verbose printing.
573+
:param origin: origin of the perturbation. Nodes can be resitant to
574+
different kinds of perturbations, default to None.
575+
:type origin: string, optional
560576
:param str kind: type of simulation, used to label output files,
561-
default to 'element'
577+
default to 'element'.
562578
563579
.. note:: A perturbation, depending on the considered system,
564580
may spread in all directions starting from the damaged
@@ -671,10 +687,13 @@ def simulate_element_perturbation(self, perturbed_nodes,
671687
- 'w5': weight for service balance over users (default to 2.0)
672688
:type weights: dict, optional
673689
:param parallel: flag for parallel fitness evaluation of
674-
initial population, default to False
690+
initial population, default to False.
675691
:type parallel: bool, optional
676-
:param verbose: flag for verbose output, default to True
692+
:param verbose: flag for verbose output, default to True.
677693
:type verbose: bool, optional
694+
:param origin: origin of the perturbation. Nodes can be resitant to
695+
different kinds of perturbations, default to None.
696+
:type origin: string, optional
678697
679698
.. note:: A perturbation, depending on the considered system,
680699
may spread in all directions starting from the damaged
@@ -723,8 +742,11 @@ def simulate_area_perturbation(self, perturbed_areas, params={'npop': 300,
723742
- 'w5': weight for service balance over users (default to 2.0)
724743
:type weights: dict, optional
725744
:param parallel: flag for parallel fitness evaluation of
726-
initial population, default to False
745+
initial population, default to False.
727746
:type parallel: bool, optional
747+
:param origin: origin of the perturbation. Nodes can be resitant to
748+
different kinds of perturbations, default to None.
749+
:type origin: string, optional
728750
729751
.. note:: A perturbation, depending on the considered system,
730752
may spread in all directions starting from the damaged

grape/general_graph.py

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,8 @@ def floyd_warshall_kernel(self, distance, predecessor, init, stop,
632632
:param distance: matrix of distances.
633633
:type distance: numpy.ndarray or multiprocessing.sharedctypes.RawArray
634634
:param predecessor: matrix of predecessors.
635-
:type predecessor: numpy.ndarray or multiprocessing.sharedctypes.RawArray
635+
:type predecessor: numpy.ndarray or
636+
multiprocessing.sharedctypes.RawArray
636637
:param int init: starting column of numpy matrix slice.
637638
:param int stop: ending column of numpy matrix slice.
638639
:param multiprocessing.synchronize.Barrier barrier:
@@ -1199,8 +1200,7 @@ def compute_outdegree_centrality(self):
11991200
def compute_service(self):
12001201
"""
12011202
1202-
Compute service for every node,
1203-
together with edge splitting.
1203+
Compute service for every node, together with edge splitting.
12041204
12051205
:return: computed service computed for every node;
12061206
splitting computed for every edge.
@@ -1268,56 +1268,64 @@ def print_graph(self, radius=None, initial_pos=None, fixed_nodes=None,
12681268
:type radius: float, optional, default to 1/sqrt(n) where n is the
12691269
number of nodes in the graph
12701270
:param initial_pos: initial positions for nodes as a dictionary with
1271-
node as keys and values as a coordinate list or tuple. If None,
1272-
then use random initial positions.
1273-
:type initial_pos: dict, optional, default to None
1274-
:param fixed_nodes: nodes to keep fixed at initial position. ValueError
1275-
raised if `fixed_nodes` specified and `initial_pos` not.
1276-
:type fixed_nodes: list, optional, default to None
1271+
node as keys and values as a coordinate list or tuple, default to
1272+
None. If None, then use random initial positions.
1273+
:type initial_pos: dict, optional
1274+
:param fixed_nodes: nodes to keep fixed at initial position, default to
1275+
None. ValueError raised if `fixed_nodes` specified and
1276+
`initial_pos` not.
1277+
:type fixed_nodes: list, optional
12771278
:param n_iter: maximum number of iterations taken in spring layout
1278-
simulation.
1279-
:type n_iter: int, optional, default to 500
1279+
simulation, default to 500.
1280+
:type n_iter: int, optional
12801281
:param thresh: threshold for relative error in node position changes.
1281-
The iteration stops if the error is below this threshold.
1282-
:type thresh: float, optional, default to 0.0001
1283-
:param size: size of nodes.
1284-
:type size: int, optional, default to 800
1285-
:param border: color of node borders.
1286-
:type border: color, optional, default to 'black'
1287-
:param edge_width: width of edges.
1288-
:type edge_width: float, optional, default to 1.0
1289-
:param arrow_size: size of the arrow head’s length and width.
1290-
:type arrow_size: int, optional, default to 10
1291-
:param fsize: font size for text labels.
1292-
:type fsize: int, optional, default to 12
1293-
:param fcolor: font color string for labels.
1294-
:type fcolor: string, optional, default to 'k' (black)
1295-
:param ffamily: font family for labels.
1296-
:type ffamily: string, optional, default to 'sans-serif'
1297-
:param title: title for figure window.
1298-
:type title: string, optional, defaut to 'Graph'
1282+
The iteration stops if the error is below this threshold,
1283+
default to 0.0001.
1284+
:type thresh: float, optional
1285+
:param size: size of nodes, default to 800.
1286+
:type size: int, optional
1287+
:param border: color of node borders, default to 'black'.
1288+
:type border: color, optional
1289+
:param edge_width: width of edges, default to 1.0.
1290+
:type edge_width: float, optional
1291+
:param arrow_size: size of the arrow head’s length and width,
1292+
default to 10.
1293+
:type arrow_size: int, optional
1294+
:param fsize: font size for text labels, default to 12.
1295+
:type fsize: int, optional
1296+
:param fcolor: font color string for labels, default to 'k' (black).
1297+
:type fcolor: string, optional
1298+
:param ffamily: font family for labels, default to 'sans-serif'.
1299+
:type ffamily: string, optional
1300+
:param title: title for figure window, default to 'Graph'.
1301+
:type title: string, optional
12991302
:param input_cmap: colormap for coloring the different areas with
1300-
different colors. If None, all nodes are colored as white.
1301-
:type input_cmap: Matplotlib colormap, optional, default to None
1302-
:param legend: show the legend on/off.
1303-
:type legend: bool, optional, default to True
1304-
:param legend_loc: the location of the legend.
1305-
:type legend_loc: str, optional, default to 'upper right'
1306-
:param legend_ncol: the number of columns that the legend has.
1307-
:type legend_ncol: int, optional, default to 1
1303+
different colors, default to None.
1304+
If None, all nodes are colored as white.
1305+
:type input_cmap: Matplotlib colormap
1306+
:param legend: show the legend on/off, default to True.
1307+
:type legend: bool, optional
1308+
:param legend_loc: the location of the legend, default to 'upper right'.
1309+
:type legend_loc: str, optional
1310+
:param legend_ncol: the number of columns that the legend has,
1311+
default to 1.
1312+
:type legend_ncol: int, optional
13081313
:param legend_anchor: box that is used to position the legend in
1309-
conjunction with loc.
1310-
:type legend_anchor: 2-tuple, or 4-tuple of floats, optional,
1311-
defaults to axes.bbox (if called as a method to Axes.legend)
1312-
or figure.bbox (if Figure.legend).
1313-
This argument allows arbitrary placement of the legend
1314+
conjunction with loc, defaults to axes.bbox (if called as a method
1315+
to Axes.legend) or figure.bbox (if Figure.legend). This argument
1316+
allows arbitrary placement of the legend.
1317+
:type legend_anchor: 2-tuple, or 4-tuple of floats, optional
13141318
:param legend_fsize: the font size of the legend. The value must be
1315-
numeric, implying the size the absolute font size in points.
1316-
:type legend_fsize: int, optional, default to 12
1317-
:param save_to_file: name of the file where to save the graph drawing.
1318-
The extension is guesses from the filename.
1319+
numeric, implying the size the absolute font size in points,
1320+
default to 12.
1321+
:type legend_fsize: int, optional
1322+
:param save_to_file: name of the file where to save the graph drawing,
1323+
default to None. The extension is guesses from the filename.
13191324
Interactive window is rendered in any case.
1320-
:type save_to_file: string, optional, default to None
1325+
:type save_to_file: string, optional
1326+
:param status: include initial condition of switches in printed graph.
1327+
To activate it, set it to 'initial', default to None.
1328+
:type status: string, optional
13211329
13221330
:return: a dictionary of positions keyed by node.
13231331
:rtype: dict

0 commit comments

Comments
 (0)