FEAT: add Mermaid graph rendering - #347
Conversation
…tests and documentation
…mermaid in Jupyter notebooks - Updated the visualize_mermaid.ipynb to use asmermaid for generating Mermaid source directly. - Removed show_mermaid_markdown function and integrated its functionality into asmermaid. - Adjusted documentation and examples to reflect the new usage of asmermaid. - Cleaned up the io module by consolidating Mermaid-related functions and removing unnecessary code. - Enhanced tests to verify the new asmermaid functionality and removed tests related to the deprecated show_mermaid_markdown.
…ebook with few mermaid examples and fix references
…tic newline in visualize.ipynb
|
Found issue with rendering long lines eg. in graph edges. isospin_magnitude ∊ [0, 1/2, 1, 3/2 ] gets displayed as isospin_magnitude ∊ [0, 1/2, 1, 3/2 |
b024f39 to
4c59a19
Compare
redeboer
left a comment
There was a problem hiding this comment.
Good job! 💯
Some smaller comments below.
One general thought I had: it seems that Mermaid (as opposed to Graphviz) supports LaTeX, even if not for all backends. Rewriting the example of #159:
```mermaid
graph LR
A["$$J/\psi$$"] --> N0[ ]
N0 --> N1["$$f_0(980)$$"]
N0 --> 0[$$\gamma$$]
N1 --> 1[$$\pi^0$$]
N1 --> 2[$$\pi^0$$]
style A fill:#FFFFFF, stroke:#FFFFFF;
style N0 fill:#FFFFFF, stroke:#FFFFFF;
style N1 fill:#FFFFFF, stroke:#FFFFFF;
style 0 fill:#FFFFFF, stroke:#FFFFFF;
style 1 fill:#FFFFFF, stroke:#FFFFFF;
style 2 fill:#FFFFFF, stroke:#FFFFFF;
```Maybe worth posting a follow-up issue. Similarly to #348, one could select LaTeX as rendering with a flag, for instance.
Rendering on GitHub ❌
graph LR
A["$$J/\psi$$"] --> N0[ ]
N0 --> N1["$$f_0(980)$$"]
N0 --> 0[$$\gamma$$]
N1 --> 1[$$\pi^0$$]
N1 --> 2[$$\pi^0$$]
style A fill:#FFFFFF, stroke:#FFFFFF;
style N0 fill:#FFFFFF, stroke:#FFFFFF;
style N1 fill:#FFFFFF, stroke:#FFFFFF;
style 0 fill:#FFFFFF, stroke:#FFFFFF;
style 1 fill:#FFFFFF, stroke:#FFFFFF;
style 2 fill:#FFFFFF, stroke:#FFFFFF;
…bled and also switched edge style from arrows to lines
| def __init__( | ||
| self, | ||
| *, | ||
| render_node: bool | None = None, | ||
| render_final_state_id: bool = True, | ||
| render_resonance_id: bool = False, | ||
| render_initial_state_id: bool = False, | ||
| strip_spin: bool = False, | ||
| collapse_graphs: bool = False, | ||
| figure_style: dict[str, Any] | None = None, | ||
| edge_style: dict[str, Any] | None = None, | ||
| node_style: dict[str, Any] | None = None, | ||
| ) -> None: | ||
| self.render_node = render_node | ||
| self.render_final_state_id = render_final_state_id | ||
| self.render_resonance_id = render_resonance_id | ||
| self.render_initial_state_id = render_initial_state_id | ||
| self.strip_spin = strip_spin | ||
| self.collapse_graphs = collapse_graphs | ||
| self.figure_style = dict(figure_style) if figure_style else {} | ||
| self.edge_style = dict(edge_style) if edge_style else {} | ||
| self.node_style = dict(node_style) if node_style else {} |
There was a problem hiding this comment.
Better to declare this class with @attrs.define to reduce boilerplate code, just like GraphPrinter, which is declared with @define(on_setattr=_check_booleans).
There was a problem hiding this comment.
Does this definition satisfies this?
Also AI mentioned, that in the definition of the GraphvizPrinter class has the weakness, that it also accepts invalid constructor combinations.
Should this also be fixed? A shared attrs validator was a recommendation.
| def __extract_priority(description: str) -> str: | ||
| matches = re.match(r".* \- ([0-9]+|NA)$", description) | ||
| if matches is None: | ||
| msg = f"{description} does not contain a priority number" | ||
| raise ValueError(msg) | ||
| return matches[1] |
There was a problem hiding this comment.
- This returns a
str, and line 134 uses it directly assorted(..., key=..., reverse=True). This makes priority9sort above10, and"NA"outranks every numeric priority. The rule list in the rendered label ends up in the wrong order whenever any priority reaches double
digits.
Returning anintwith an explicit sentinel forNA(-1, orfloat("-inf")if negative priorities are possible) would fix it. - Minor problem: in the same regex, the
\-does not need escaping outside a character class.
Note that this exact same function exists under _dot.py!
There was a problem hiding this comment.
Should be fixed, but please review... I'm not exactly sure if the fix is what you asked for 🙃
…y into seperate _labels.py module



Closes #159