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

Dependency graph #659

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
115 changes: 115 additions & 0 deletions scripts/dependency_graph.py
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):
Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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 :)

"""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")
Loading