Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pygraphviz with neo4j-viz for graph visualization #306

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

oskarhane
Copy link
Member

@oskarhane oskarhane commented Mar 14, 2025

Neo4j has its own viz library, let's use that!

  • Replace pygraphviz imports with neo4j-viz in pipeline.py
  • Update draw() method to save visualizations as HTML files
  • Implement get_neo4j_viz_graph() method for interactive visualizations
  • Update example in visualization.py to use HTML output
  • Update tests to work with neo4j-viz implementation
  • Add color coding for better visual distinction

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update
  • Project configuration change

How Has This Been Tested?

  • Unit tests
  • E2E tests
  • Manual tests

Checklist

The following requirements should have been met (depending on the changes in the branch):

  • Unit tests have been updated
  • Examples have been updated
  • CHANGELOG.md updated if appropriate

Sorry, something went wrong.

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane
- Replace pygraphviz imports with neo4j-viz in pipeline.py
- Update draw() method to save visualizations as HTML files
- Implement get_neo4j_viz_graph() method for interactive visualizations
- Update example in visualization.py to use HTML output
- Update tests to work with neo4j-viz implementation
- Add color coding for better visual distinction
@oskarhane oskarhane requested a review from a team as a code owner March 14, 2025 14:45

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane
…and Relationship

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane

Verified

This commit was signed with the committer’s verified signature. The key has expired.
oskarhane Oskar Hane
else:
# For other formats, we'll use the render method and save the image
G.render()
# Note: neo4j-viz doesn't support direct saving to image formats
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the comment saying that it can't save to PNG? Because code in the 'else' looks very similar to the one in the former 'if' block to me.

@@ -24,7 +24,9 @@
try:
from mistralai import Mistral
except ImportError:
Mistral = None # type: ignore
# Define placeholder type for type checking
class Mistral: # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better compared to the previous approach? (out of curiosity)

def get_pygraphviz_graph(self, hide_unused_outputs: bool = True) -> pgv.AGraph:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember why we haven't made this method private, but since we are removing it we should mention it in the changelog.

with open(path, "w") as f:
f.write(G.render()._repr_html_())

def get_neo4j_viz_graph(self, hide_unused_outputs: bool = True) -> NeoVizGraph:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe make this method private?

"Follow installation instruction in pygraphviz documentation "
"to get it up and running on your system."
"Could not import neo4j-viz. "
"Install it with 'pip install neo4j-viz'."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using extra groups instead reduces a bit dependency issues:

Suggested change
"Install it with 'pip install neo4j-viz'."
'Install it with 'pip install "neo4j-graphrag[experimental]".'

node_ids[n] = node_counter
nodes.append(
Node(
id=node_counter,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the doc, it looks like id can be a string? That could simplify the id and node matching part.

G.remove_node(n)
return G
used_nodes = set()
for rel in relationships:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a logic issue here: we do not want to filter out isolated nodes (this should not happen I think?), but node of type "output" with no outgoing relationship.

assert len(g.nodes()) == 3
g = pipe.get_pygraphviz_graph(hide_unused_outputs=False)
# - 2 outputs 'a.result' and 'b.result' (neo4j-viz implementation includes both)
assert len(g.nodes) == 4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to my comment above about logic, the test should not need to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants