Skip to content

Commit

Permalink
Fixes regression for visualization stripping path
Browse files Browse the repository at this point in the history
Viz would strip paths like `./foo/bar/dag.png` to just `dag.png`.

This fixes that.
  • Loading branch information
skrawcz authored and elijahbenizzy committed Feb 6, 2024
1 parent e570014 commit aa8af83
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 10 deletions.
Binary file removed examples/hello_world/a_path.dot.png
Binary file not shown.
Binary file added examples/hello_world/a_path.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed examples/hello_world/my_dag.dot.png
Binary file not shown.
Binary file added examples/hello_world/my_dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/hello_world/my_full_dag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions examples/hello_world/my_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
print(df.to_string())

# To visualize do `pip install "sf-hamilton[visualization]"` if you want these to work
dr.visualize_execution(output_columns, "./my_dag.dot", {"format": "png"})
dr.visualize_execution(output_columns, "./my_dag.png")
dr.visualize_path_between(
"spend_mean",
"spend_zero_mean_unit_variance",
"./a_path.dot",
{"format": "png"},
"./a_path.png",
strict_path_visualization=False,
)
# dr.display_all_functions("./my_full_dag.dot", {"format": "png"})
dr.display_all_functions("./my_full_dag.png")
6 changes: 2 additions & 4 deletions hamilton/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import inspect
import logging
import pathlib
import os.path
import uuid
from enum import Enum
from types import ModuleType
Expand Down Expand Up @@ -856,12 +856,10 @@ def display(
)
kwargs = {"view": False, "format": "png"} # default format = png
if output_file_path: # infer format from path
suffix = pathlib.Path(output_file_path).suffix
output_file_path, suffix = os.path.splitext(output_file_path)
if suffix != "":
inferred_format = suffix.partition(".")[-1]
kwargs.update(format=inferred_format)
# remove suffix if exist because dot.render() will append the format
output_file_path = str(pathlib.Path(output_file_path).stem)
if render_kwargs and isinstance(render_kwargs, dict): # accept explicit format
kwargs.update(render_kwargs)
if output_file_path:
Expand Down
6 changes: 4 additions & 2 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import os
import pathlib
import uuid
from itertools import permutations
Expand Down Expand Up @@ -815,7 +816,7 @@ def _styling_function(*, node, node_class):

@pytest.mark.parametrize("show_legend", [(True), (False)])
def test_function_graph_display_legend(show_legend: bool, tmp_path: pathlib.Path):
dot_file_path = tmp_path / "dag"
dot_file_path = tmp_path / "dag.png"
fg = graph.FunctionGraph.from_modules(tests.resources.dummy_functions, config={"b": 1, "c": 2})

fg.display(
Expand All @@ -824,7 +825,8 @@ def test_function_graph_display_legend(show_legend: bool, tmp_path: pathlib.Path
render_kwargs={"view": False},
show_legend=show_legend,
)
dot = dot_file_path.open("r").read()
dot_file = pathlib.Path(os.path.splitext(str(dot_file_path))[0])
dot = dot_file.open("r").read()

found_legend = "cluster__legend" in dot
assert found_legend is show_legend
Expand Down

0 comments on commit aa8af83

Please sign in to comment.