Skip to content

Commit

Permalink
Improve notebook version check script and added what_are_recipes_and_…
Browse files Browse the repository at this point in the history
…how_to_use to the list of checked notebooks
  • Loading branch information
BloodAxe committed Oct 30, 2023
1 parent c9b2b25 commit 60c244b
Show file tree
Hide file tree
Showing 5 changed files with 965 additions and 1,282 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sweeper_test:

# Here you define a list of notebooks we want to execute and convert to markdown files
# NOTEBOOKS = hellomake.ipynb hellofunc.ipynb helloclass.ipynb
NOTEBOOKS = src/super_gradients/examples/model_export/models_export.ipynb
NOTEBOOKS = src/super_gradients/examples/model_export/models_export.ipynb notebooks/what_are_recipes_and_how_to_use.ipynb

# This Makefile target runs notebooks listed below and converts them to markdown files in documentation/source/
run_and_convert_notebooks_to_docs: $(NOTEBOOKS)
Expand Down
2,183 changes: 908 additions & 1,275 deletions notebooks/what_are_recipes_and_how_to_use.ipynb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/deci_core_unit_test_suite_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from tests.unit_tests.test_deprecations import DeprecationsUnitTest
from tests.unit_tests.test_min_samples_single_node import TestMinSamplesSingleNode
from tests.unit_tests.test_train_with_torch_scheduler import TrainWithTorchSchedulerTest
from tests.unit_tests.test_version_check import TestVersionCheck
from tests.unit_tests.test_yolo_nas_pose import YoloNASPoseTests
from tests.unit_tests.train_with_intialized_param_args_test import TrainWithInitializedObjectsTest
from tests.unit_tests.pretrained_models_unit_test import PretrainedModelsUnitTest
Expand Down Expand Up @@ -166,6 +167,7 @@ def _add_modules_to_unit_tests_suite(self):
self.unit_tests_suite.addTest(self.test_loader.loadTestsFromModule(PoseEstimationSampleTest))
self.unit_tests_suite.addTest(self.test_loader.loadTestsFromModule(TestMixedPrecisionDisabled))
self.unit_tests_suite.addTest(self.test_loader.loadTestsFromModule(DynamicModelTests))
self.unit_tests_suite.addTest(self.test_loader.loadTestsFromModule(TestVersionCheck))

def _add_modules_to_end_to_end_tests_suite(self):
"""
Expand Down
34 changes: 34 additions & 0 deletions tests/unit_tests/test_version_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import unittest

from tests.verify_notebook_version import try_extract_super_gradients_version_from_pip_install_command


class TestVersionCheck(unittest.TestCase):
def test_pip_install_no_version(self):
self.assertIsNone(try_extract_super_gradients_version_from_pip_install_command("!pip install super-gradients"))

def test_pip_install_major_only(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("!pip install super-gradients==3"), "3")

def test_pip_install_major_minor(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("!pip install super-gradients==3.0"), "3.0")

def test_pip_install_major_patch(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("!pip install super-gradients==3.3.1"), "3.3.1")

def test_pip_install_with_extra_args(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("!pip install -q super-gradients==3.3.1"), "3.3.1")
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("!pip install super-gradients==3.3.1 --extra-index-url=foobar"), "3.3.1")

def test_pip_install_with_space(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("! pip install -q super-gradients==3.3.1"), "3.3.1")

def test_pip_install_with_stdout_redirect(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("! pip install -q super-gradients==3.3.1 &> /dev/null"), "3.3.1")

def test_pip_install_with_extra_packages(self):
self.assertEquals(try_extract_super_gradients_version_from_pip_install_command("! pip install super-gradients==3.3.1 torch==2.0 numpy>2"), "3.3.1")


if __name__ == "__main__":
unittest.main()
26 changes: 20 additions & 6 deletions tests/verify_notebook_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import re
import sys
from typing import Optional

import super_gradients
import nbformat

Expand All @@ -19,6 +21,22 @@ def get_first_cell_content(notebook_path):
return first_cell_content


def try_extract_super_gradients_version_from_pip_install_command(input: str) -> Optional[str]:
"""
Extracts the version of super_gradients from a string like `!pip install super_gradients=={version}` command.
A pip install may contain extra arguments, e.g. `!pip install -q super_gradients=={version} torch=={another version}`.
:param input: A string that contains a `!pip install super_gradients=={version}` command.
:return: The version of super_gradients.
"""
pattern = re.compile(r"pip\s+install.*?super-gradients==([0-9]+(?:\.[0-9]+)*(?:\.[0-9]+)?)")
match = re.search(pattern, input)
if match:
return match.group(1)
else:
return None


def main():
"""
This script is used to verify that the version of the SG package matches the version of SG installed in the notebook.
Expand All @@ -29,13 +47,9 @@ def main():
first_cell_content = get_first_cell_content(notebook_path)
print(first_cell_content)

# Check if the first cell contains "!pip install super_gradients=={version}" using regex and extract the version
pattern = re.compile(r"^!pip install super_gradients==([\d\.]+)")

for line in first_cell_content.splitlines():
match = re.search(pattern, line)
if match:
sg_version_in_notebook = match.group(1)
sg_version_in_notebook = try_extract_super_gradients_version_from_pip_install_command(line)
if sg_version_in_notebook is not None:
if sg_version_in_notebook == super_gradients.__version__:
return 0
else:
Expand Down

0 comments on commit 60c244b

Please sign in to comment.