-
Notifications
You must be signed in to change notification settings - Fork 33
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
Dependency graph #659
Draft
polinabinder1
wants to merge
2
commits into
main
Choose a base branch
from
polinabinder/package_dependencies
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−0
Draft
Dependency graph #659
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
# SPDX-License-Identifier: LicenseRef-Apache2 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import os | ||
from collections import defaultdict | ||
from pathlib import Path | ||
|
||
import matplotlib.pyplot as plt | ||
import networkx as nx | ||
import toml | ||
|
||
|
||
def find_pyproject_files(base_dir): | ||
"""Find all pyproject.toml files in subdirectories.""" | ||
pyproject_files = [] | ||
for root, _, files in os.walk(base_dir): | ||
if "pyproject.toml" in files: | ||
pyproject_files.append(os.path.join(root, "pyproject.toml")) | ||
return pyproject_files | ||
|
||
|
||
def parse_dependencies(pyproject_path): | ||
"""Parse dependencies from a pyproject.toml file.""" | ||
with open(pyproject_path, "r") as f: | ||
pyproject_data = toml.load(f) | ||
|
||
dependencies = [] | ||
package_name = None | ||
|
||
# Extract package name | ||
try: | ||
package_name = pyproject_data["project"]["name"] | ||
except KeyError: | ||
print(f"Warning: Could not find package name in {pyproject_path}") | ||
|
||
# Extract dependencies | ||
try: | ||
deps = pyproject_data["project"]["dependencies"] | ||
if isinstance(deps, dict): # If dependencies are a dictionary | ||
for dep, _ in deps.items(): | ||
if dep.startswith("bionemo-"): | ||
dependencies.append((dep)) | ||
elif isinstance(deps, list): # If dependencies are a list | ||
for dep in deps: | ||
if dep.startswith("bionemo-"): | ||
dependencies.append((dep)) | ||
except KeyError: | ||
print(f"Warning: Could not find dependencies in {pyproject_path}") | ||
|
||
return package_name, dependencies | ||
|
||
|
||
def build_dependency_graph(base_dir): | ||
"""Build a dependency graph for all sub-packages.""" | ||
pyproject_files = find_pyproject_files(base_dir) | ||
dependency_graph = defaultdict(list) | ||
|
||
for pyproject_file in pyproject_files: | ||
package_name, dependencies = parse_dependencies(pyproject_file) | ||
if package_name: | ||
for dep in dependencies: | ||
dependency_graph[f"{dep}"].append(package_name) | ||
|
||
return dependency_graph | ||
|
||
|
||
def visualize_dependency_graph(dependency_graph, filename): | ||
"""Visualize the dependency graph using NetworkX.""" | ||
G = nx.DiGraph() | ||
|
||
for package, dependents in dependency_graph.items(): | ||
for dep in dependents: | ||
G.add_edge(package, dep) | ||
|
||
plt.figure(figsize=(14, 10)) | ||
pos = nx.shell_layout(G) # Use shell layout for better visualization of hierarchical dependencies | ||
# pos = nx.spiral_layout(G) | ||
|
||
nx.draw( | ||
G, | ||
pos, | ||
with_labels=True, | ||
node_size=3000, | ||
node_color="lightblue", | ||
font_size=10, | ||
font_weight="bold", | ||
arrowsize=20, | ||
edge_color="gray", | ||
) | ||
plt.title("Dependency Graph", fontsize=16) | ||
plt.savefig(filename) | ||
|
||
|
||
if __name__ == "__main__": | ||
script_path = Path(__file__).resolve() | ||
|
||
# Get the parent directory | ||
parent_directory = script_path.parent.parent | ||
|
||
base_dir = parent_directory / "sub-packages" | ||
dependency_graph = build_dependency_graph(base_dir) | ||
visualize_dependency_graph(dependency_graph, "dependency_graph.png") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we could also parse tach.toml and possibly warn if the dependency graphs are different? The tach check actually enforces this separation during CI, so it's probably more accurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a check in place to ensure that the tach.toml and pyproject.toml files are up-to-date and valid in terms of dependencies? For example, does PyPI installation and importing all subpackages automatically verify this?
We could implement regular checks or enforcement in the CI pipeline. If the above method isn't sufficient, we can create a script that parses the project.toml files of the main project and its subpackages, extracts the import paths used in Python scripts under the src directory of each subpackage, and verifies all imports starting with
from bionemo
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dorota -- I suggest we constrain scope for now to just drawing the dependency graph.
I agree it's a good idea to do what you're describing, but it would increase the scope of this substantially and at the moment there's other stuff we gotta do :)