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

Add option to save graph as PNG #523

Merged
merged 6 commits into from
Dec 17, 2024
Merged

Add option to save graph as PNG #523

merged 6 commits into from
Dec 17, 2024

Conversation

jan-janssen
Copy link
Member

@jan-janssen jan-janssen commented Dec 17, 2024

Summary by CodeRabbit

  • New Features

    • Added optional parameter to specify filename for saving dependency graphs in the Executor and related classes.
    • Introduced a new group for optional dependencies related to graph notebooks.
  • Bug Fixes

    • Updated tests to conditionally skip plotting tests if the required library is not installed.
  • Chores

    • Removed matplotlib dependency from various environment configuration files and optional dependencies section.

@jan-janssen jan-janssen marked this pull request as draft December 17, 2024 00:57
Copy link
Contributor

coderabbitai bot commented Dec 17, 2024

Walkthrough

This pull request focuses on removing the matplotlib dependency from various environment configuration files and enhancing the dependency graph plotting functionality in the executorlib module. The changes include updating environment YAML files, modifying the Executor and ExecutorWithDependencies classes, and extending the draw function to support saving dependency graphs to files. The modifications aim to improve the flexibility of dependency graph visualization and streamline the project's dependency management.

Changes

File Change Summary
.ci_support/environment-*.yml Removed matplotlib dependency (version 3.5.3 or 3.10.0) from multiple environment configuration files
executorlib/__init__.py Added optional plot_dependency_graph_filename parameter to Executor class methods
executorlib/interactive/executor.py Updated ExecutorWithDependencies class to support new filename-based dependency graph plotting
executorlib/standalone/plot.py Enhanced draw function to save dependency graphs to specified files
pyproject.toml Removed matplotlib from optional dependencies, added networkx, created new graphnotebook dependency group
tests/test_dependencies_executor.py Added new test method test_executor_dependency_plot_filename

Sequence Diagram

sequenceDiagram
    participant User
    participant Executor
    participant ExecutorWithDependencies
    participant PlotModule

    User->>Executor: Create with plot_dependency_graph_filename
    Executor->>ExecutorWithDependencies: Pass filename
    ExecutorWithDependencies-->>PlotModule: Draw and save graph
    PlotModule-->>User: Graph saved to specified file
Loading

Possibly related PRs

Poem

🐰 Hop, hop, dependencies dance away,
Matplotlib fades, new graphs come to play,
Networkx weaves its magical thread,
Plotting secrets now easily spread,
A rabbit's code, clean and bright! 🎨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e439e64 and 7e1877b.

📒 Files selected for processing (1)
  • tests/test_dependencies_executor.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_dependencies_executor.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@jan-janssen jan-janssen marked this pull request as ready for review December 17, 2024 02:58
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (3)
executorlib/standalone/plot.py (1)

110-118: Improve docstring for the filename parameter

The parameter description should include that it's optional and specify supported file formats.

-        filename (str): Name of the file to store the plotted graph in.
+        filename (Optional[str], optional): Path to save the graph visualization. 
+            Supported formats: png, svg, pdf. Defaults to None for display output.
executorlib/interactive/executor.py (2)

45-45: Fix inconsistent documentation for _generate_dependency_graph attribute.

The attribute description is duplicated with conflicting types (bool vs str). This could lead to confusion.

Remove line 53 and update line 45 to accurately reflect the attribute's purpose:

-        _generate_dependency_graph (bool): Whether to generate the dependency graph.
-        _generate_dependency_graph (str): Name of the file to store the plotted graph in.
+        _generate_dependency_graph (bool): Whether to generate and save the dependency graph.

Also applies to: 53-53


81-85: Consider simplifying the graph generation logic.

The current implementation can be made more concise while maintaining the same behavior.

-        self._plot_dependency_graph_filename = plot_dependency_graph_filename
-        if plot_dependency_graph_filename is None:
-            self._generate_dependency_graph = plot_dependency_graph
-        else:
-            self._generate_dependency_graph = True
+        self._plot_dependency_graph_filename = plot_dependency_graph_filename
+        self._generate_dependency_graph = bool(plot_dependency_graph_filename) or plot_dependency_graph
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between baec2a1 and e439e64.

📒 Files selected for processing (9)
  • .ci_support/environment-mpich.yml (0 hunks)
  • .ci_support/environment-old.yml (0 hunks)
  • .ci_support/environment-openmpi.yml (0 hunks)
  • .ci_support/environment-win.yml (0 hunks)
  • executorlib/__init__.py (5 hunks)
  • executorlib/interactive/executor.py (4 hunks)
  • executorlib/standalone/plot.py (2 hunks)
  • pyproject.toml (1 hunks)
  • tests/test_dependencies_executor.py (2 hunks)
💤 Files with no reviewable changes (4)
  • .ci_support/environment-openmpi.yml
  • .ci_support/environment-old.yml
  • .ci_support/environment-mpich.yml
  • .ci_support/environment-win.yml
🔇 Additional comments (3)
pyproject.toml (2)

42-43: LGTM: Good choice using networkx for graph visualization

The addition of networkx is appropriate for graph visualization capabilities, and the version is properly pinned.


44-48: LGTM: Well-structured dependency group

Good separation of notebook-specific dependencies into a new group. This allows users to install only what they need.

executorlib/standalone/plot.py (1)

1-4: LGTM: Clean import organization

Imports are well-organized and the Optional type is correctly imported for the new parameter.

Comment on lines +126 to +129
if filename is not None:
file_format = os.path.splitext(filename)[-1][1:]
with open(filename, "wb") as f:
f.write(nx.nx_agraph.to_agraph(graph).draw(prog="dot", format=file_format))
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for file operations

The file operations should include error handling for common issues like permission errors or invalid paths.

     if filename is not None:
         file_format = os.path.splitext(filename)[-1][1:]
+        if not file_format:
+            raise ValueError("Filename must have an extension (e.g., .png, .svg, .pdf)")
+        if file_format not in ['png', 'svg', 'pdf']:
+            raise ValueError(f"Unsupported file format: {file_format}")
+        try:
             with open(filename, "wb") as f:
                 f.write(nx.nx_agraph.to_agraph(graph).draw(prog="dot", format=file_format))
+        except (OSError, IOError) as e:
+            raise IOError(f"Failed to save graph to {filename}: {str(e)}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if filename is not None:
file_format = os.path.splitext(filename)[-1][1:]
with open(filename, "wb") as f:
f.write(nx.nx_agraph.to_agraph(graph).draw(prog="dot", format=file_format))
if filename is not None:
file_format = os.path.splitext(filename)[-1][1:]
if not file_format:
raise ValueError("Filename must have an extension (e.g., .png, .svg, .pdf)")
if file_format not in ['png', 'svg', 'pdf']:
raise ValueError(f"Unsupported file format: {file_format}")
try:
with open(filename, "wb") as f:
f.write(nx.nx_agraph.to_agraph(graph).draw(prog="dot", format=file_format))
except (OSError, IOError) as e:
raise IOError(f"Failed to save graph to {filename}: {str(e)}")

Comment on lines 77 to 96
@unittest.skipIf(
skip_graphviz_test,
"graphviz is not installed, so the plot_dependency_graph tests are skipped.",
)
def test_executor_dependency_plot_filename(self):
graph_file = os.path.join(os.path.dirname(__file__), "test.png")
with Executor(
max_cores=1,
backend="local",
plot_dependency_graph=False,
plot_dependency_graph_filename=graph_file,
) as exe:
cloudpickle_register(ind=1)
future_1 = exe.submit(add_function, 1, parameter_2=2)
future_2 = exe.submit(add_function, 1, parameter_2=future_1)
self.assertTrue(future_1.done())
self.assertTrue(future_2.done())
self.assertTrue(os.path.exists(graph_file))
# os.remove(graph_file)

Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage for graph file output

The test case needs improvements:

  1. The commented cleanup code should be uncommented
  2. Add tests for other file formats
  3. Add negative test cases for invalid formats/paths
     def test_executor_dependency_plot_filename(self):
-        graph_file = os.path.join(os.path.dirname(__file__), "test.png")
-        with Executor(
-            max_cores=1,
-            backend="local",
-            plot_dependency_graph=False,
-            plot_dependency_graph_filename=graph_file,
-        ) as exe:
-            cloudpickle_register(ind=1)
-            future_1 = exe.submit(add_function, 1, parameter_2=2)
-            future_2 = exe.submit(add_function, 1, parameter_2=future_1)
-            self.assertTrue(future_1.done())
-            self.assertTrue(future_2.done())
-        self.assertTrue(os.path.exists(graph_file))
-        # os.remove(graph_file)
+        test_files = {
+            'png': os.path.join(os.path.dirname(__file__), "test.png"),
+            'svg': os.path.join(os.path.dirname(__file__), "test.svg"),
+            'pdf': os.path.join(os.path.dirname(__file__), "test.pdf")
+        }
+        
+        try:
+            # Test valid formats
+            for fmt, graph_file in test_files.items():
+                with Executor(
+                    max_cores=1,
+                    backend="local",
+                    plot_dependency_graph=False,
+                    plot_dependency_graph_filename=graph_file,
+                ) as exe:
+                    cloudpickle_register(ind=1)
+                    future_1 = exe.submit(add_function, 1, parameter_2=2)
+                    future_2 = exe.submit(add_function, 1, parameter_2=future_1)
+                    self.assertTrue(future_1.done())
+                    self.assertTrue(future_2.done())
+                self.assertTrue(os.path.exists(graph_file))
+
+            # Test invalid format
+            with self.assertRaises(ValueError):
+                with Executor(
+                    max_cores=1,
+                    backend="local",
+                    plot_dependency_graph_filename="test.invalid"
+                ) as exe:
+                    future_1 = exe.submit(add_function, 1, parameter_2=2)
+
+            # Test invalid path
+            with self.assertRaises(IOError):
+                with Executor(
+                    max_cores=1,
+                    backend="local",
+                    plot_dependency_graph_filename="/invalid/path/test.png"
+                ) as exe:
+                    future_1 = exe.submit(add_function, 1, parameter_2=2)
+
+        finally:
+            # Cleanup
+            for graph_file in test_files.values():
+                if os.path.exists(graph_file):
+                    os.remove(graph_file)

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +152 to +156
return draw(
node_lst=node_lst,
edge_lst=edge_lst,
filename=self._plot_dependency_graph_filename,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for file operations.

The draw function call should handle potential file operation errors when saving the graph.

-            return draw(
-                node_lst=node_lst,
-                edge_lst=edge_lst,
-                filename=self._plot_dependency_graph_filename,
-            )
+            try:
+                return draw(
+                    node_lst=node_lst,
+                    edge_lst=edge_lst,
+                    filename=self._plot_dependency_graph_filename,
+                )
+            except IOError as e:
+                raise IOError(f"Failed to save dependency graph: {e}") from e
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return draw(
node_lst=node_lst,
edge_lst=edge_lst,
filename=self._plot_dependency_graph_filename,
)
try:
return draw(
node_lst=node_lst,
edge_lst=edge_lst,
filename=self._plot_dependency_graph_filename,
)
except IOError as e:
raise IOError(f"Failed to save dependency graph: {e}") from e

@@ -216,6 +220,7 @@ def __new__(
init_function=init_function,
refresh_rate=refresh_rate,
plot_dependency_graph=plot_dependency_graph,
plot_dependency_graph_filename=plot_dependency_graph_filename,
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add input validation for the filename parameter.

The plot_dependency_graph_filename parameter should be validated before being passed to _ExecutorWithDependencies.

Add a validation check similar to other parameters:

def _check_plot_dependency_graph_filename(filename: Optional[str]) -> None:
    if filename is not None and not isinstance(filename, str):
        raise TypeError("plot_dependency_graph_filename must be a string or None")

Then use it before the _ExecutorWithDependencies instantiation:

+            _check_plot_dependency_graph_filename(plot_dependency_graph_filename)
             return _ExecutorWithDependencies(
                 max_workers=max_workers,
                 ...

@jan-janssen jan-janssen merged commit c3a0ae7 into main Dec 17, 2024
27 checks passed
@jan-janssen jan-janssen deleted the graph_as_png branch December 17, 2024 03:20
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.

1 participant