emic.output¶
Visualization and export utilities.
output
¶
Output and visualization for epsilon-machines.
This module provides functions for visualizing, exporting, and serializing epsilon-machines and analysis results.
Functions:
| Name | Description |
|---|---|
render_state_diagram |
Render machine as Graphviz diagram |
display_state_diagram |
Display diagram in Jupyter |
to_tikz |
Export machine as TikZ code |
to_mermaid |
Export machine as Mermaid diagram |
to_dot |
Export machine as DOT format |
to_json |
Serialize machine to JSON |
from_json |
Deserialize machine from JSON |
to_latex_table |
Format analysis results as LaTeX table |
Example
from emic.sources import GoldenMeanSource from emic.output import render_state_diagram, to_tikz machine = GoldenMeanSource(p=0.5).true_machine diagram = render_state_diagram(machine) _ = diagram.render('golden_mean', format='pdf') # doctest: +SKIP
DiagramStyle
dataclass
¶
DiagramStyle(
layout: Literal["dot", "neato", "circo", "fdp"] = "dot",
rankdir: Literal["LR", "TB", "RL", "BT"] = "LR",
node_shape: str = "circle",
node_color: str = "#4a90d9",
node_fontsize: int = 12,
node_width: float = 0.5,
edge_fontsize: int = 10,
edge_color: str = "#333333",
show_probabilities: bool = True,
probability_format: str = ".2f",
state_labels: dict[str, str] = dict(),
symbol_labels: dict[str, str] = dict(),
size: tuple[float, float] | None = None,
dpi: int = 150,
)
Configuration for state diagram rendering.
Attributes:
| Name | Type | Description |
|---|---|---|
layout |
Literal['dot', 'neato', 'circo', 'fdp']
|
Graphviz layout engine (dot, neato, circo, fdp). |
rankdir |
Literal['LR', 'TB', 'RL', 'BT']
|
Graph direction (LR=left-to-right, TB=top-to-bottom). |
node_shape |
str
|
Shape of state nodes (circle, doublecircle, box). |
node_color |
str
|
Fill color for nodes (hex or name). |
node_fontsize |
int
|
Font size for node labels. |
node_width |
float
|
Minimum width of nodes in inches. |
edge_fontsize |
int
|
Font size for edge labels. |
edge_color |
str
|
Color of edges. |
show_probabilities |
bool
|
Whether to show transition probabilities. |
probability_format |
str
|
Format string for probabilities. |
state_labels |
dict[str, str]
|
Custom labels for states (state_id -> label). |
symbol_labels |
dict[str, str]
|
Custom labels for symbols (str(symbol) -> label). |
size |
tuple[float, float] | None
|
Figure size in inches (width, height). |
dpi |
int
|
Resolution in dots per inch. |
render_state_diagram
¶
render_state_diagram(
machine: EpsilonMachine[A],
style: DiagramStyle | None = None,
) -> Digraph
Render an epsilon-machine as a state diagram.
Creates a Graphviz directed graph representing the epsilon-machine. States are shown as nodes and transitions as labeled edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
EpsilonMachine[A]
|
The epsilon-machine to visualize. |
required |
style
|
DiagramStyle | None
|
Rendering configuration. Uses defaults if not provided. |
None
|
Returns:
| Type | Description |
|---|---|
Digraph
|
A graphviz.Digraph object that can be rendered to various formats. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If graphviz is not installed. |
Example
from emic.sources import GoldenMeanSource from emic.output import render_state_diagram machine = GoldenMeanSource(p=0.5).true_machine diagram = render_state_diagram(machine) diagram.render('golden_mean', format='pdf') 'golden_mean.pdf'
Source code in src/emic/output/diagram.py
display_state_diagram
¶
display_state_diagram(
machine: EpsilonMachine[A],
style: DiagramStyle | None = None,
) -> None
Display state diagram in Jupyter notebook.
Renders the epsilon-machine as an inline SVG in Jupyter environments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
EpsilonMachine[A]
|
The epsilon-machine to visualize. |
required |
style
|
DiagramStyle | None
|
Rendering configuration. Uses defaults if not provided. |
None
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If graphviz or IPython is not installed. |
Example
from emic.sources import GoldenMeanSource from emic.output import display_state_diagram display_state_diagram(GoldenMeanSource().true_machine) # doctest: +SKIP
Source code in src/emic/output/diagram.py
to_tikz
¶
to_tikz(
machine: EpsilonMachine[A],
style: DiagramStyle | None = None,
) -> str
Generate TikZ code for a state diagram.
Creates LaTeX/TikZ code that can be included in documents to render a publication-quality state diagram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
EpsilonMachine[A]
|
The epsilon-machine to export. |
required |
style
|
DiagramStyle | None
|
Rendering configuration for labels and formatting. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
LaTeX/TikZ code as a string. |
Example
from emic.sources import GoldenMeanSource from emic.output import to_tikz machine = GoldenMeanSource(p=0.5).true_machine tikz = to_tikz(machine) print(tikz) # doctest: +ELLIPSIS \begin{tikzpicture}...
Source code in src/emic/output/latex.py
to_latex_table
¶
to_latex_table(
summaries: list[tuple[str, AnalysisSummary]],
measures: list[str] | None = None,
caption: str = "Epsilon-machine analysis",
label: str = "tab:analysis",
) -> str
Generate LaTeX table of analysis results.
Creates a publication-ready LaTeX table comparing complexity measures across multiple processes or machines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
summaries
|
list[tuple[str, AnalysisSummary]]
|
List of (name, summary) pairs to include. |
required |
measures
|
list[str] | None
|
Which measures to include. Defaults to common measures. |
None
|
caption
|
str
|
Table caption. |
'Epsilon-machine analysis'
|
label
|
str
|
LaTeX label for cross-referencing. |
'tab:analysis'
|
Returns:
| Type | Description |
|---|---|
str
|
LaTeX table code as a string. |
Example
from emic.sources import GoldenMeanSource, BiasedCoinSource from emic.analysis import analyze from emic.output import to_latex_table results = [ ... ("Golden Mean", analyze(GoldenMeanSource(p=0.5).true_machine)), ... ("Biased Coin", analyze(BiasedCoinSource(p=0.7).true_machine)), ... ] latex = to_latex_table(results) print(latex) # doctest: +ELLIPSIS \begin{table}...
Source code in src/emic/output/latex.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
to_dot
¶
to_dot(machine: EpsilonMachine[A]) -> str
Export epsilon-machine as DOT format.
Creates a DOT (Graphviz) representation that can be rendered by Graphviz tools or imported into other visualization software.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
EpsilonMachine[A]
|
The epsilon-machine to export. |
required |
Returns:
| Type | Description |
|---|---|
str
|
DOT format string. |
Example
from emic.sources import GoldenMeanSource from emic.output import to_dot machine = GoldenMeanSource(p=0.5).true_machine dot = to_dot(machine) print(dot) # doctest: +ELLIPSIS digraph { ...
Source code in src/emic/output/serialization.py
to_mermaid
¶
to_mermaid(machine: EpsilonMachine[A]) -> str
Export epsilon-machine as Mermaid diagram.
Creates a Mermaid state diagram that can be rendered in Markdown documents on GitHub, GitLab, and other platforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
EpsilonMachine[A]
|
The epsilon-machine to export. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Mermaid diagram code. |
Example
from emic.sources import GoldenMeanSource from emic.output import to_mermaid machine = GoldenMeanSource(p=0.5).true_machine mermaid = to_mermaid(machine) print(mermaid) # doctest: +ELLIPSIS stateDiagram-v2 ...
Source code in src/emic/output/serialization.py
to_json
¶
to_json(machine: EpsilonMachine[A]) -> str
Serialize an epsilon-machine to JSON.
Creates a JSON representation that can be saved to a file or
transmitted over a network. Use from_json to deserialize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
EpsilonMachine[A]
|
The epsilon-machine to serialize. |
required |
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation. |
Example
from emic.sources import GoldenMeanSource from emic.output import to_json, from_json machine = GoldenMeanSource(p=0.5).true_machine json_str = to_json(machine) restored = from_json(json_str) len(restored) == len(machine) True
Source code in src/emic/output/serialization.py
from_json
¶
from_json(json_str: str) -> EpsilonMachine[Any]
Deserialize an epsilon-machine from JSON.
Reconstructs an epsilon-machine from its JSON representation
created by to_json.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_str
|
str
|
JSON string representation. |
required |
Returns:
| Type | Description |
|---|---|
EpsilonMachine[Any]
|
The reconstructed epsilon-machine. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the JSON is malformed or missing required fields. |
JSONDecodeError
|
If the string is not valid JSON. |
Example
from emic.output import from_json json_str = '''{"alphabet": [0, 1], "start_state": "A", ... "states": [{"id": "A", "transitions": [ ... {"symbol": 0, "target": "A", "probability": 0.5}, ... {"symbol": 1, "target": "A", "probability": 0.5} ... ]}]}''' machine = from_json(json_str) len(machine) 1