Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

bennaaym/graphviz_support: bug fixed | README updated with examples #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/graphviz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ jobs:
pip install extensions/graphviz_support
- name: Run graphviz_support tests
run: |
python -m extensions.graphviz_support.graphviz_support.tests.test_lg_graphviz_api
python -m extensions.graphviz_support.graphviz_support.tests.test_lg_graphviz_api
python -m extensions.graphviz_support.graphviz_support.examples.simple_viz
python -m extensions.graphviz_support.graphviz_support.examples.graph_w_logging
40 changes: 31 additions & 9 deletions extensions/graphviz_support/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# Graphviz for LabGraph graphs
# Graphviz for LabGraph Graphs

This extension provides an API to generate a graphviz visualization of the LabGraph topology.
This extension provides an API to generate a graphviz visualization of the LabGraph topology.

## Quick Start

### Method 1 - building from source code

**Prerequisites**:
* Python3\
Supported python version(s)
* [Python3.6](https://www.python.org/downloads/)
* [Python3.8](https://www.python.org/downloads/) (**RECOMMENDED**)
* Make sure to install [labgraph](https://github.com/facebookresearch/labgraph) before proceeding
* Make sure to install [graphviz](https://graphviz.org/download/) on your main OS before proceeding

- Python3\
Supported python version(s)
_ [Python3.6](https://www.python.org/downloads/)
_ [Python3.8](https://www.python.org/downloads/) (**RECOMMENDED**)
- Make sure to install [labgraph](https://github.com/facebookresearch/labgraph) before proceeding
- Make sure to install [graphviz](https://graphviz.org/download/) on your main OS before proceeding

```
cd labgraph/extensions/graphviz_support
Expand All @@ -24,21 +25,42 @@ python setup.py install
To make sure things are working:

1- Move to the root of the LabGraph directory:

```
labgraph\extensions\graphviz_support> cd ../..
labgraph>
```

2- Run the following test

```
python -m extensions.graphviz_support.graphviz_support.tests.test_lg_graphviz_api
```

**The output of the file for this test can be found at:**\
extensions\graphviz_support\graphviz_support\tests\output

### Generating a graphviz file

To generate a graph visualization just call 'generate_graphviz' function and pass the appropriate parameters

```
from extensions/graphviz_support/graphviz_support/generate_graphviz/generate_graphviz.py import generate_graphviz.py
from graphviz_support import generate_graphviz

generate_graphviz(graph, output_file)
```

### Examples:

To get a better understanding of Graphviz API, please check the following examples

```
python -m extensions.graphviz_support.graphviz_support.examples.simple_viz
```

```
python -m extensions.graphviz_support.graphviz_support.examples.graph_w_logging
```

**(!) The outputs of the above examples can be found under the following folder**
extensions\graphviz_support\graphviz_support\examples\output
7 changes: 7 additions & 0 deletions extensions/graphviz_support/graphviz_support/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserved.


from .generate_graphviz.generate_graphviz import generate_graphviz

__all__ = ['generate_graphviz']
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserve

from labgraph.examples.graph_w_logging import Demo as GraphWLogging
from graphviz_support import generate_graphviz
import pathlib


if __name__ == '__main__':
graph_w_logging = GraphWLogging()
out_dir: str = pathlib.Path(__file__).parent.absolute()
generate_graphviz(
graph_w_logging,
f'{out_dir}/output/graph_w_logging.svg'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserve

from labgraph.examples.simple_viz import Demo as SimpleVizGraph
from graphviz_support import generate_graphviz
import pathlib


if __name__ == '__main__':
simple_viz_graph = SimpleVizGraph()
out_dir: str = pathlib.Path(__file__).parent.absolute()
generate_graphviz(
simple_viz_graph,
f'{out_dir}/output/simple_viz.svg'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserved.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserve

import os
import labgraph as lg
from labgraph.graphs.stream import Stream
from graphviz import Digraph
Expand Down Expand Up @@ -79,7 +80,7 @@ def in_out_edge_mapper(streams: Stream) -> Dict[str, str]:
difference = set(stream.topic_paths).difference(GraphVizNode.in_edges)

if difference:
upstream_edge = difference.pop()
upstream_edge = max(difference, key=len)
for edge in stream.topic_paths:
if edge != upstream_edge:
in_out_edge_map[edge] = upstream_edge
Expand Down Expand Up @@ -171,7 +172,8 @@ def generate_graphviz(graph: lg.Graph, output_file: str) -> None:
raise GenerateGraphvizError(
"Parameter 'output_file' cannot be null or empty string."
)
filename, format = output_file.split('.')

filename, format = os.path.splitext(output_file)

# Local variables
nodes: List[GraphVizNode] = []
Expand All @@ -187,5 +189,5 @@ def generate_graphviz(graph: lg.Graph, output_file: str) -> None:
type(graph).__name__,
nodes,
filename,
format
format[1:]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python3
# Copyright 2004-present Facebook. All Rights Reserved.
3 changes: 2 additions & 1 deletion extensions/graphviz_support/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from setuptools import find_packages, setup


setup(
name="graphviz_support",
version="1.0.0",
description="LabGraph Monitor Improvement - Graphviz for LabGraph graphs",
description="Graphviz Extension For LabGraph",
packages=find_packages(),
python_requires=">=3.6",
install_requires=[
Expand Down