365 Render this task graph with matplotlib as a UML state diagram: one
366 rounded-rectangle state per task, a solid transition arrow from
367 every dependency to the task that depends on it. Tasks are laid
368 out top to bottom by dependency depth (a task without dependencies
369 sits at the top; everything that - directly or transitively -
370 depends on it sits below it), so following the transitions always
371 moves downwards, exactly like reading a state diagram's flow of
372 control. The tasks of one layer sit side by side: every task sits
373 straight below its leftmost dependency and is moved to the right
374 only where it would otherwise overlap another task.
376 A filled black circle - the UML initial pseudostate - transitions
377 into every task that has no dependencies; every task nothing else
378 depends on transitions into a bullseye - the UML final pseudostate.
379 A task with has_to_repeat set (i.e. not None) is additionally drawn
380 with a small self-transition loop right of its state, the standard UML
381 notation for "this state can re-trigger itself", labelled with the
382 task's has_to_repeat string itself.
384 Every action set attached to a task (@see Task.add_action_set) is
385 listed inside that task's own state box as a "do / <name>" line,
386 below a separator - the same internal-activity compartment a UML
387 state uses to list its entry/exit/do behaviours, as opposed to the
388 transition arrows between states, which describe order, not
389 ownership. The same compartment also lists the cell/face/vertex
390 submodels the task declares via use_cell()/use_face()/use_vertex()
391 as "cell / <name> [<Task.DataAccess>]" (and face/vertex
392 counterparts) lines, one per kind that is actually used.
394 Every state also carries a small coloured badge in its top-left
395 corner indicating which kind of grid entity (vertex/face/cell/
396 global) the task works on; a legend below the title spells out
397 what each colour means, restricted to the entity kinds actually
398 used in this particular graph.
400 Opens a blocking matplotlib window; close it to continue.
402 import matplotlib.pyplot
as plt
403 from matplotlib.lines
import Line2D
404 from matplotlib.patches
import Circle, FancyArrowPatch, FancyBboxPatch
405 from matplotlib.path
import Path
408 print(f
"Task graph '{title}' is empty - nothing to visualise.")
416 def depth(task: Task) -> int:
417 if task
not in depth_of_task:
418 depth_of_task[task] = (
419 0
if not task.dependencies
else 1 + max(depth(dependency)
for dependency
in task.dependencies)
421 return depth_of_task[task]
424 for task
in self.
tasks:
425 layers.setdefault(depth(task), []).
append(task)
428 Task.GridEntity.VERTEX:
"tab:blue",
429 Task.GridEntity.FACE:
"tab:orange",
430 Task.GridEntity.CELL:
"tab:green",
431 Task.GridEntity.GLOBAL:
"tab:purple",
432 Task.GridEntity.GLOBAL_START_OF_STEP:
"tab:red",
433 Task.GridEntity.GLOBAL_END_OF_STEP:
"tab:brown",
442 ACTIVITY_LINE_HEIGHT = 0.22
449 def activity_lines(task: Task):
450 lines = [f
"do / {action_set.get_action_set_name()}" for action_set
in task.action_sets]
451 for kind, submodels, access
in (
452 (
"cell", task.cell_data, task.cell_data_access),
453 (
"face", task.face_data, task.face_data_access),
454 (
"vertex", task.vertex_data, task.vertex_data_access),
459 +
", ".join(f
"{submodel.name} [{access[submodel].name}]" for submodel
in submodels)
463 width_of_task, height_of_task = {}, {}
464 for task
in self.
tasks:
465 lines = activity_lines(task)
466 longest_line = max([len(task.name)] + [len(line)
for line
in lines])
467 width_of_task[task] = max(MIN_WIDTH, CHAR_WIDTH * longest_line + 0.6)
468 height_of_task[task] = HEADER_HEIGHT + (
469 BODY_PADDING + len(lines) * ACTIVITY_LINE_HEIGHT + 0.5 * BODY_PADDING
if lines
else 0
472 SELF_LOOP_WIDTH = 0.4
473 SELF_LOOP_LABEL_CHAR_WIDTH = 0.065
474 PSEUDOSTATE_OFFSET = 0.6
478 def self_loop_extent(task: Task) -> float:
479 if not task.has_to_repeat:
481 return SELF_LOOP_WIDTH + 0.06 + SELF_LOOP_LABEL_CHAR_WIDTH * len(task.has_to_repeat)
489 position_of_task = {}
491 for layer
in sorted(layers):
492 def preferred_left_edge(task: Task) -> float:
493 if not task.dependencies:
496 position_of_task[dependency][0] - width_of_task[dependency] / 2
497 for dependency
in task.dependencies
500 tasks_in_layer = sorted(layers[layer], key=preferred_left_edge)
501 row_height = max(height_of_task[task]
for task
in tasks_in_layer)
502 next_free_x = float(
"-inf")
503 for task
in tasks_in_layer:
504 left_edge = max(preferred_left_edge(task), next_free_x)
505 position_of_task[task] = (left_edge + width_of_task[task] / 2, row_y - row_height / 2)
506 next_free_x = left_edge + width_of_task[task] + self_loop_extent(task) + HORIZONTAL_GAP
507 row_y -= row_height + VERTICAL_GAP
508 bottom_y = row_y + VERTICAL_GAP
510 root_tasks = [task
for task
in self.
tasks if not task.dependencies]
512 task
for task
in self.
tasks if not any(task
in other.dependencies
for other
in self.
tasks)
524 min_x = min(position_of_task[task][0] - width_of_task[task] / 2
for task
in self.
tasks)
526 position_of_task[task][0] + width_of_task[task] / 2 + self_loop_extent(task)
for task
in self.
tasks
531 max_y = PSEUDOSTATE_OFFSET + 0.09
533 min_y = bottom_y - PSEUDOSTATE_OFFSET - 0.11
534 min_x, max_x, min_y, max_y = min_x - PADDING, max_x + PADDING, min_y - PADDING, max_y + PADDING
543 LEFT_CHROME, RIGHT_CHROME, TOP_CHROME, BOTTOM_CHROME = 0.35, 0.1, 0.4 + LEGEND_HEIGHT, 0.15
544 data_width_inches = (max_x - min_x) * INCH_PER_UNIT
545 data_height_inches = (max_y - min_y) * INCH_PER_UNIT
546 figure_width = data_width_inches + LEFT_CHROME + RIGHT_CHROME
547 figure_height = data_height_inches + TOP_CHROME + BOTTOM_CHROME
549 figure = plt.figure(figsize=(figure_width, figure_height))
550 axes = figure.add_axes(
552 LEFT_CHROME / figure_width,
553 BOTTOM_CHROME / figure_height,
554 data_width_inches / figure_width,
555 data_height_inches / figure_height,
558 axes.set_xlim(min_x, max_x)
559 axes.set_ylim(min_y, max_y)
566 for task
in self.
tasks:
567 x, y = position_of_task[task]
568 w, h = width_of_task[task], height_of_task[task]
569 lines = activity_lines(task)
571 box = FancyBboxPatch(
572 (x - w / 2, y - h / 2),
575 boxstyle=
"round,pad=0,rounding_size=0.09",
582 state_patch[task] = box
586 (x - w / 2 + 0.10, y + h / 2 - 0.10),
588 facecolor=entity_colour.get(task.entity,
"tab:gray"),
594 header_center_y = y + h / 2 - HEADER_HEIGHT / 2
if lines
else y
597 (x, header_center_y + 0.08),
598 ha=
"center", va=
"center", zorder=3, fontsize=7.5, fontweight=
"bold",
601 f
"«{task.entity.name.lower()}»",
602 (x, header_center_y - 0.13),
603 ha=
"center", va=
"center", zorder=3, fontsize=6, style=
"italic", color=
"dimgray",
607 separator_y = y + h / 2 - HEADER_HEIGHT
609 [x - w / 2, x + w / 2], [separator_y, separator_y],
610 color=
"black", linewidth=0.8, zorder=3,
612 for index, line
in enumerate(lines):
613 line_y = separator_y - BODY_PADDING - index * ACTIVITY_LINE_HEIGHT - 0.08
616 (x - w / 2 + 0.12, line_y),
617 ha=
"left", va=
"center", zorder=3, fontsize=6.5, color=
"dimgray",
623 for task
in self.
tasks:
624 for dependency
in task.dependencies:
627 position_of_task[dependency],
628 position_of_task[task],
629 patchA=state_patch[dependency],
630 patchB=state_patch[task],
633 connectionstyle=
"arc3,rad=0.08",
640 if task.has_to_repeat:
641 x, y = position_of_task[task]
642 w, h = width_of_task[task], height_of_task[task]
644 upper_y, lower_y = y + 0.18 * h, y - 0.18 * h
648 (right_x + SELF_LOOP_WIDTH, upper_y),
649 (right_x + SELF_LOOP_WIDTH, lower_y),
652 [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4],
666 (right_x + SELF_LOOP_WIDTH + 0.06, y),
667 ha=
"left", va=
"center", zorder=3, fontsize=6, style=
"italic", color=
"crimson",
674 x = sum(position_of_task[task][0]
for task
in root_tasks) / len(root_tasks)
675 initial_pos = (x, PSEUDOSTATE_OFFSET)
676 initial_patch = Circle(initial_pos, 0.09, facecolor=
"black", edgecolor=
"black", zorder=3)
677 axes.add_patch(initial_patch)
678 for task
in root_tasks:
682 position_of_task[task],
683 patchA=initial_patch,
684 patchB=state_patch[task],
694 x = sum(position_of_task[task][0]
for task
in sink_tasks) / len(sink_tasks)
695 final_pos = (x, bottom_y - PSEUDOSTATE_OFFSET)
696 final_outer = Circle(final_pos, 0.11, facecolor=
"white", edgecolor=
"black", linewidth=1.3, zorder=3)
697 axes.add_patch(final_outer)
698 axes.add_patch(Circle(final_pos, 0.05, facecolor=
"black", edgecolor=
"none", zorder=4))
699 for task
in sink_tasks:
702 position_of_task[task],
704 patchA=state_patch[task],
719 title_height = TOP_CHROME - LEGEND_HEIGHT
720 figure.suptitle(title, y=1.0 - 0.5 * title_height / figure_height, fontsize=10)
722 used_entities = [entity
for entity
in Task.GridEntity if any(task.entity == entity
for task
in self.
tasks)]
726 marker=
"o", linestyle=
"none", markersize=6,
727 markerfacecolor=entity_colour.get(entity,
"tab:gray"), markeredgecolor=
"none",
728 label=f
"«{entity.name.lower()}»",
730 for entity
in used_entities
733 handles=legend_handles,
735 bbox_to_anchor=(0.5, 1.0 - (title_height + 0.5 * LEGEND_HEIGHT) / figure_height),
736 ncol=len(legend_handles),
743 axes.set_ylabel(
"Dependency depth")
746 axes.set_aspect(
"equal")
754 plt.show(block=
False)