Skip to content

Commit

Permalink
Add ability to generate graphs from CLI for debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
saulshanabrook committed Jan 27, 2022
1 parent 2d6d3b0 commit d55dc5e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
31 changes: 24 additions & 7 deletions lineapy/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
import tempfile
from io import TextIOWrapper
from typing import List
from typing import List, Optional

import click
import nbformat
Expand Down Expand Up @@ -42,7 +42,17 @@ def linea_cli():
@click.argument("file", type=click.File())
@click.argument("artifact_name")
@click.argument("artifact_value", type=str)
def notebook(file: TextIOWrapper, artifact_name: str, artifact_value: str):
@click.option(
"--visualize-slice",
type=click.Path(dir_okay=False, path_type=pathlib.Path),
help="Create a visualization for the sliced code, save it to this path",
)
def notebook(
file: TextIOWrapper,
artifact_name: str,
artifact_value: str,
visualize_slice: Optional[pathlib.Path],
):
"""
Executes the notebook FILE, saves the value ARTIFACT_VALUE with name ARTIFACT_NAME, and prints the sliced code.
Expand All @@ -58,11 +68,18 @@ def notebook(file: TextIOWrapper, artifact_name: str, artifact_value: str):
notebook = nbformat.read(file, nbformat.NO_CONVERT)
notebook["cells"].append(
nbformat.v4.new_code_cell(
"import lineapy\n"
# Save to a new variable first, so that if artifact value is composite, the slice of creating it
# won't include the `lineapy.save` line.
f"linea_artifact_value = {artifact_value}\n"
f"lineapy.save(linea_artifact_value, {repr(artifact_name)})"
(
"import lineapy\n"
# Save to a new variable first, so that if artifact value is composite, the slice of creating it
# won't include the `lineapy.save` line.
f"linea_artifact_value = {artifact_value}\n"
f"linea_artifact = lineapy.save(linea_artifact_value, {repr(artifact_name)})\n"
)
+ (
f"linea_artifact.visualize({repr(str(visualize_slice.resolve()))})"
if visualize_slice
else ""
)
)
)

Expand Down
14 changes: 8 additions & 6 deletions lineapy/graph_reader/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,19 @@ def to_airflow(self, filename: Optional[str] = None) -> Path:
)
return path

def visualize(self) -> None:
def visualize(self, path: Optional[str]) -> None:
"""
Displays the graph for this artifact.
If a path is provided, will save it to that file instead.
"""
from lineapy.visualizer import Visualizer

display(
Visualizer.for_public_node(
self._graph, self.node_id
).ipython_display_object()
)
visualizer = Visualizer.for_public_node(self._graph, self.node_id)
if path:
visualizer.render_pdf_file(path)
else:
display(visualizer.ipython_display_object())


class LineaCatalog:
Expand Down
3 changes: 1 addition & 2 deletions lineapy/visualizer/optimize_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import subprocess
import tempfile

from path import Path
from pathlib import Path

# https://github.com/scour-project/scour#usage
OPTIONS = [
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def version(path):
"pyyaml",
"asttokens",
"isort",
"graphviz",
],
extras_require={
"dev": [
Expand All @@ -95,7 +96,6 @@ def version(path):
"nbval",
"coveralls",
"seaborn",
"graphviz",
"pre-commit",
"SQLAlchemy[mypy]>=1.4.0",
"sphinx",
Expand Down

0 comments on commit d55dc5e

Please sign in to comment.