Coverage for python / lsst / pipe / base / mermaid_tools.py: 12%
39 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-01 08:20 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-05-01 08:20 +0000
1# This file is part of pipe_base.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28"""Module defining few methods to generate Mermaid charts from pipelines or
29quantum graphs.
30"""
32from __future__ import annotations
34__all__ = ["graph2mermaid", "pipeline2mermaid"]
36from collections.abc import Iterable
37from typing import TYPE_CHECKING, Any, Literal
39from .pipeline import Pipeline
41if TYPE_CHECKING:
42 from .graph import QuantumGraph
43 from .pipeline import TaskDef
44 from .quantum_graph import PredictedQuantumGraph
47def graph2mermaid(qgraph: QuantumGraph | PredictedQuantumGraph, file: Any) -> None:
48 """Convert QuantumGraph into a Mermaid flowchart (top-down).
50 This method is mostly for documentation/presentation purposes.
52 Parameters
53 ----------
54 qgraph : `~lsst.pipe.base.QuantumGraph`
55 QuantumGraph instance.
56 file : `str` or file object
57 File where Mermaid flowchart is written, can be a file name or file
58 object.
60 Raises
61 ------
62 OSError
63 Raised if the output file cannot be opened.
64 ImportError
65 Raised if the task class cannot be imported.
66 """
67 from .quantum_graph import PredictedQuantumGraph, visualization
69 if not isinstance(qgraph, PredictedQuantumGraph):
70 qgraph = PredictedQuantumGraph.from_old_quantum_graph(qgraph)
72 # Open a file if needed.
73 close = False
74 if not hasattr(file, "write"):
75 file = open(file, "w")
76 close = True
78 v = visualization.QuantumGraphMermaidVisualizer()
79 v.write_bipartite(qgraph, file)
81 if close:
82 file.close()
85def pipeline2mermaid(
86 pipeline: Pipeline | Iterable[TaskDef],
87 file: Any,
88 show_dimensions: bool = True,
89 expand_dimensions: bool = False,
90 show_storage: bool = True,
91) -> None:
92 """Convert a Pipeline into a Mermaid flowchart diagram.
94 This function produces a Mermaid flowchart, representing tasks and their
95 inputs/outputs as dataset nodes. It uses a top-down layout.
97 This method is mostly for documentation/presentation purposes.
99 Parameters
100 ----------
101 pipeline : Pipeline or Iterable[TaskDef]
102 The pipeline or collection of tasks to represent.
103 file : str or file-like
104 The output file or file-like object into which the Mermaid code is
105 written.
106 show_dimensions : bool, optional
107 If True, display dimension information for tasks and datasets.
108 Default is True.
109 expand_dimensions : bool, optional
110 If True, expand dimension names to include all components. Default is
111 False.
112 show_storage : bool, optional
113 If True, display storage class information for datasets. Default is
114 True.
116 Raises
117 ------
118 OSError
119 Raised if the output file cannot be opened.
120 ImportError
121 Raised if the task class cannot be imported.
122 """
123 from .pipeline_graph import PipelineGraph, visualization
125 # Ensure that pipeline is iterable of task definitions.
126 if isinstance(pipeline, Pipeline):
127 pipeline = pipeline.to_graph()._iter_task_defs()
129 # Open file if needed.
130 close = False
131 if not hasattr(file, "write"):
132 file = open(file, "w")
133 close = True
135 if isinstance(pipeline, Pipeline):
136 pg = pipeline.to_graph(visualization_only=True)
137 else:
138 pg = PipelineGraph()
139 for task_def in pipeline:
140 pg.add_task(
141 task_def.label,
142 task_class=task_def.taskClass,
143 config=task_def.config,
144 connections=task_def.connections,
145 )
146 pg.resolve(visualization_only=True)
148 dimensions: Literal["full", "concise"] | None = None
149 if show_dimensions:
150 if expand_dimensions:
151 dimensions = "full"
152 else:
153 dimensions = "concise"
155 visualization.show_mermaid(
156 pg, stream=file, dataset_types=True, dimensions=dimensions, storage_classes=show_storage
157 )
159 if close:
160 file.close()