-
Notifications
You must be signed in to change notification settings - Fork 3
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
Display help menus more promptly #62
Open
thompsonmj
wants to merge
2
commits into
main
Choose a base branch
from
feat/fast-help
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.
Open
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 |
---|---|---|
@@ -1,6 +1,16 @@ | ||
# SPDX-FileCopyrightText: 2024-present John Bradley <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
from bioclip.predict import TreeOfLifeClassifier, Rank, CustomLabelsClassifier, CustomLabelsBinningClassifier | ||
|
||
__all__ = ["TreeOfLifeClassifier", "Rank", "CustomLabelsClassifier", "CustomLabelsBinningClassifier"] | ||
|
||
def __getattr__(name): | ||
if name in __all__: | ||
from bioclip.predict import ( | ||
TreeOfLifeClassifier, | ||
Rank, | ||
CustomLabelsClassifier, | ||
CustomLabelsBinningClassifier, | ||
) | ||
return locals()[name] | ||
raise AttributeError(f"module {__name__} has no attribute {name}") |
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
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 |
---|---|---|
@@ -1,11 +1,164 @@ | ||
import unittest | ||
from unittest.mock import mock_open, patch | ||
from unittest.mock import mock_open, patch, MagicMock | ||
import argparse | ||
import pandas as pd | ||
from bioclip.__main__ import parse_args, Rank, create_classes_str, main, parse_bins_csv | ||
from enum import Enum | ||
from bioclip.predict import Rank | ||
from bioclip.__main__ import parse_args, create_classes_str, main, parse_bins_csv | ||
|
||
|
||
class TestParser(unittest.TestCase): | ||
|
||
def test_parse_args_lazy_import(self): | ||
"""Test that Rank is only imported when needed""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of testing that certain classes are loaded or not could we test how long it takes to display help? |
||
# Should not import Rank | ||
with patch('bioclip.predict.Rank') as mock_rank: | ||
args = parse_args(['embed', 'image.jpg']) | ||
mock_rank.assert_not_called() | ||
|
||
# Should not import Rank when using --cls | ||
with patch('bioclip.predict.Rank') as mock_rank: | ||
args = parse_args(['predict', 'image.jpg', '--cls', 'class1,class2']) | ||
mock_rank.assert_not_called() | ||
|
||
# Should not import Rank when using --bins | ||
with patch('bioclip.predict.Rank') as mock_rank: | ||
args = parse_args(['predict', 'image.jpg', '--bins', 'bins.csv']) | ||
mock_rank.assert_not_called() | ||
|
||
# Should import Rank for tree-of-life prediction | ||
with patch('bioclip.predict.Rank', Rank) as mock_rank: | ||
args = parse_args(['predict', 'image.jpg']) | ||
self.assertEqual(args.rank, Rank.SPECIES) | ||
|
||
def test_list_models_lazy_import(self): | ||
"""Test that open_clip is only imported when the list-models command is used""" | ||
# Should import open_clip for list-models | ||
with patch('bioclip.__main__.open_clip', create=True) as mock_oc: | ||
mock_parse_args = MagicMock(return_value=argparse.Namespace( | ||
command='list-models', | ||
model=None | ||
)) | ||
with patch('bioclip.__main__.parse_args', mock_parse_args), \ | ||
patch('builtins.print'): # prevent actual printing | ||
main() | ||
mock_oc.list_models.assert_called_once() | ||
|
||
# Should call list_pretrained_tags when model specified | ||
with patch('bioclip.__main__.open_clip', create=True) as mock_oc: | ||
mock_parse_args = MagicMock(return_value=argparse.Namespace( | ||
command='list-models', | ||
model='somemodel' | ||
)) | ||
with patch('bioclip.__main__.parse_args', mock_parse_args), \ | ||
patch('builtins.print'): # prevent actual printing | ||
main() | ||
mock_oc.list_pretrained_tags_by_model.assert_called_once_with('somemodel') | ||
|
||
def test_predict_lazy_imports(self): | ||
"""Test that classifier classes are only imported when needed""" | ||
# For cls_str path | ||
with patch('bioclip.predict.TreeOfLifeClassifier') as mock_tree, \ | ||
patch('bioclip.predict.CustomLabelsClassifier') as mock_custom, \ | ||
patch('bioclip.predict.CustomLabelsBinningClassifier') as mock_binning: | ||
mock_parse_args = MagicMock(return_value=argparse.Namespace( | ||
command='predict', | ||
image_file=['image.jpg'], | ||
format='csv', | ||
output='stdout', | ||
cls='cat,dog', | ||
bins=None, | ||
device='cpu', | ||
model=None, | ||
pretrained=None, | ||
k=5, | ||
rank=None | ||
)) | ||
with patch('bioclip.__main__.parse_args', mock_parse_args): | ||
with patch('bioclip.__main__.write_results'): # Prevent actual write | ||
main() | ||
mock_custom.assert_called() | ||
mock_tree.assert_not_called() | ||
mock_binning.assert_not_called() | ||
|
||
# For bins path | ||
with patch('bioclip.predict.TreeOfLifeClassifier') as mock_tree, \ | ||
patch('bioclip.predict.CustomLabelsClassifier') as mock_custom, \ | ||
patch('bioclip.predict.CustomLabelsBinningClassifier') as mock_binning: | ||
mock_parse_args = MagicMock(return_value=argparse.Namespace( | ||
command='predict', | ||
image_file=['image.jpg'], | ||
format='csv', | ||
output='stdout', | ||
cls=None, | ||
bins='bins.csv', | ||
device='cpu', | ||
model=None, | ||
pretrained=None, | ||
k=5, | ||
rank=None | ||
)) | ||
with patch('bioclip.__main__.parse_args', mock_parse_args), \ | ||
patch('bioclip.__main__.parse_bins_csv', return_value={}), \ | ||
patch('bioclip.__main__.write_results'): | ||
main() | ||
mock_binning.assert_called() | ||
mock_tree.assert_not_called() | ||
mock_custom.assert_not_called() | ||
|
||
# For default (TreeOfLifeClassifier) path | ||
with patch('bioclip.predict.TreeOfLifeClassifier') as mock_tree, \ | ||
patch('bioclip.predict.CustomLabelsClassifier') as mock_custom, \ | ||
patch('bioclip.predict.CustomLabelsBinningClassifier') as mock_binning: | ||
mock_parse_args = MagicMock(return_value=argparse.Namespace( | ||
command='predict', | ||
image_file=['image.jpg'], | ||
format='csv', | ||
output='stdout', | ||
cls=None, | ||
bins=None, | ||
device='cpu', | ||
model=None, | ||
pretrained=None, | ||
k=5, | ||
rank=Rank.SPECIES | ||
)) | ||
with patch('bioclip.__main__.parse_args', mock_parse_args), \ | ||
patch('bioclip.__main__.write_results'): | ||
main() | ||
mock_tree.assert_called() | ||
mock_custom.assert_not_called() | ||
mock_binning.assert_not_called() | ||
|
||
def test_embed_lazy_imports(self): | ||
"""Test that TreeOfLifeClassifier is only imported for embed command""" | ||
class MockTensor: | ||
def tolist(self): | ||
return [1.0, 2.0, 3.0] | ||
|
||
with patch('bioclip.predict.TreeOfLifeClassifier') as mock_clf: | ||
# Mock the classifier instance | ||
mock_clf_instance = MagicMock() | ||
mock_clf.return_value = mock_clf_instance | ||
|
||
# Make create_image_features_for_image return our mock tensor | ||
mock_clf_instance.create_image_features_for_image.return_value = MockTensor() | ||
mock_clf_instance.model_str = "test-model" | ||
|
||
mock_parse_args = MagicMock(return_value=argparse.Namespace( | ||
command='embed', | ||
image_file=['image.jpg'], | ||
output='stdout', | ||
device='cpu', | ||
model=None, | ||
pretrained=None | ||
)) | ||
with patch('bioclip.__main__.parse_args', mock_parse_args), \ | ||
patch('builtins.print'): # prevent actual printing to stdout | ||
main() | ||
mock_clf.assert_called_once() | ||
mock_clf_instance.create_image_features_for_image.assert_called_once() | ||
|
||
def test_parse_args(self): | ||
|
||
args = parse_args(['predict', 'image.jpg']) | ||
|
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.
This duplicates the value from here:
pybioclip/src/bioclip/predict.py
Line 19 in b3ac523