Skip to content

Commit 43736ad

Browse files
MechaCritterclaudeCopilot
authored
Bugfix/fix ruff errors (#12)
* Fix ruff I001: sort imports in _base_classes.py Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff F401: declare OxfordFlowerDataset re-export via __all__ Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff B027: remove empty __init__ from FeatureExtractorBase Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff B028: add stacklevel=2 to warnings.warn calls Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff B904: chain exceptions with 'raise ... from e' in except clauses Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff B008: replace function-call defaults with None and lazy init RootSIFT() and vgg16(weights=...) were evaluated once at import time; vgg16 in particular triggered a model download on import. They are now instantiated per-call inside __init__ when the argument is omitted. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff B905: pass strict=True to zip() calls All zipped sequences are required to have equal lengths, so strict=True turns silent truncation on a length mismatch into an immediate ValueError. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix ruff F403/F405: replace star imports in example notebooks Replaces 'from pyvisim._utils import *' with explicit imports in the five example notebooks. This required restoring plot_image, cluster_and_return_labels, cluster_images_and_generate_statistics, plot_and_save_heatmap and plot_and_save_barplot to pyvisim/_utils.py: they were deleted as dead code in b2832a7, but the notebooks still call them (the dead-code scan did not cover notebooks). Restored versions are modernized to pass the current lint config, and the bogus check_is_image decorator was not restored. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * added ruff-check in pre commit * Potential fix for pull request finding fixed indentation in pre commit config file Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding in plot_image, detach if it's a torch tensor Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding in plot_and_save_heatmap, turn everything to numpy before plotting Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * ran ruff formatter again --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 0855bfe commit 43736ad

16 files changed

Lines changed: 279 additions & 107 deletions

.pre-commit-config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ repos:
1919
- repo: https://github.com/astral-sh/ruff-pre-commit
2020
rev: v0.15.2
2121
hooks:
22-
- id: ruff-format
23-
22+
- id: ruff-check
23+
args: [--fix]
24+
- id: ruff-format
2425
- repo: https://github.com/codespell-project/codespell
2526
rev: v2.4.1
2627
hooks:

examples/clustering_images_using_fv.ipynb

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,16 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 1,
23+
"execution_count": null,
2424
"id": "9d64d8aebd89bdcd",
2525
"metadata": {
2626
"ExecuteTime": {
2727
"end_time": "2025-01-18T23:05:14.526690Z",
2828
"start_time": "2025-01-18T23:05:00.750503Z"
2929
}
3030
},
31-
"outputs": [
32-
{
33-
"name": "stdout",
34-
"output_type": "stream",
35-
"text": [
36-
"2025-01-19 00:05:08,127 - numexpr.utils - INFO - Note: NumExpr detected 64 cores but \"NUMEXPR_MAX_THREADS\" not set, so enforcing safe limit of 8.\n",
37-
"2025-01-19 00:05:08,128 - numexpr.utils - INFO - NumExpr defaulting to 8 threads.\n"
38-
]
39-
}
40-
],
41-
"source": [
42-
"from torchvision.models import VGG16_Weights, vgg16\n",
43-
"\n",
44-
"from pyvisim._utils import *\n",
45-
"from pyvisim.datasets import OxfordFlowerDataset\n",
46-
"from pyvisim.encoders import FisherVectorEncoder, GMMWeights\n",
47-
"from pyvisim.features import DeepConvFeature"
48-
]
31+
"outputs": [],
32+
"source": "import numpy as np\nfrom torchvision.models import VGG16_Weights, vgg16\n\nfrom pyvisim._utils import (\n cluster_images_and_generate_statistics,\n cosine_similarity,\n plot_and_save_heatmap,\n)\nfrom pyvisim.datasets import OxfordFlowerDataset\nfrom pyvisim.encoders import FisherVectorEncoder, GMMWeights\nfrom pyvisim.features import DeepConvFeature"
4933
},
5034
{
5135
"cell_type": "markdown",
@@ -316,4 +300,4 @@
316300
},
317301
"nbformat": 4,
318302
"nbformat_minor": 5
319-
}
303+
}

examples/clustering_images_using_pipeline.ipynb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 4,
25+
"execution_count": null,
2626
"id": "9d64d8aebd89bdcd",
2727
"metadata": {
2828
"ExecuteTime": {
@@ -31,20 +31,7 @@
3131
}
3232
},
3333
"outputs": [],
34-
"source": [
35-
"from torchvision.models import VGG16_Weights, vgg16\n",
36-
"\n",
37-
"from pyvisim._utils import *\n",
38-
"from pyvisim.datasets import OxfordFlowerDataset\n",
39-
"from pyvisim.encoders import (\n",
40-
" FisherVectorEncoder,\n",
41-
" GMMWeights,\n",
42-
" KMeansWeights,\n",
43-
" Pipeline,\n",
44-
" VLADEncoder,\n",
45-
")\n",
46-
"from pyvisim.features import DeepConvFeature"
47-
]
34+
"source": "import numpy as np\nfrom torchvision.models import VGG16_Weights, vgg16\n\nfrom pyvisim._utils import (\n cluster_images_and_generate_statistics,\n cosine_similarity,\n plot_and_save_heatmap,\n)\nfrom pyvisim.datasets import OxfordFlowerDataset\nfrom pyvisim.encoders import (\n FisherVectorEncoder,\n GMMWeights,\n KMeansWeights,\n Pipeline,\n VLADEncoder,\n)\nfrom pyvisim.features import DeepConvFeature"
4835
},
4936
{
5037
"cell_type": "markdown",
@@ -320,4 +307,4 @@
320307
},
321308
"nbformat": 4,
322309
"nbformat_minor": 5
323-
}
310+
}

examples/clustering_images_using_vlad.ipynb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
{
2222
"cell_type": "code",
23-
"execution_count": 8,
23+
"execution_count": null,
2424
"id": "9d64d8aebd89bdcd",
2525
"metadata": {
2626
"ExecuteTime": {
@@ -29,14 +29,7 @@
2929
}
3030
},
3131
"outputs": [],
32-
"source": [
33-
"from torchvision.models import VGG16_Weights, vgg16\n",
34-
"\n",
35-
"from pyvisim._utils import *\n",
36-
"from pyvisim.datasets import OxfordFlowerDataset\n",
37-
"from pyvisim.encoders import KMeansWeights, VLADEncoder\n",
38-
"from pyvisim.features import DeepConvFeature"
39-
]
32+
"source": "import numpy as np\nfrom torchvision.models import VGG16_Weights, vgg16\n\nfrom pyvisim._utils import (\n cluster_images_and_generate_statistics,\n cosine_similarity,\n plot_and_save_heatmap,\n)\nfrom pyvisim.datasets import OxfordFlowerDataset\nfrom pyvisim.encoders import KMeansWeights, VLADEncoder\nfrom pyvisim.features import DeepConvFeature"
4033
},
4134
{
4235
"cell_type": "markdown",
@@ -305,4 +298,4 @@
305298
},
306299
"nbformat": 4,
307300
"nbformat_minor": 5
308-
}
301+
}

examples/m_ap_and_top_k_accuracy.ipynb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
},
7272
{
7373
"cell_type": "code",
74-
"execution_count": 8,
74+
"execution_count": null,
7575
"id": "45cf34f1532e800e",
7676
"metadata": {
7777
"ExecuteTime": {
@@ -81,8 +81,12 @@
8181
},
8282
"outputs": [],
8383
"source": [
84-
"train_imgs, train_labels = zip(*[(img, label) for img, label, _ in train_dataset])\n",
85-
"val_imgs, val_labels = zip(*[(img, label) for img, label, _ in val_dataset])"
84+
"train_imgs, train_labels = zip(\n",
85+
" *[(img, label) for img, label, _ in train_dataset], strict=True\n",
86+
")\n",
87+
"val_imgs, val_labels = zip(\n",
88+
" *[(img, label) for img, label, _ in val_dataset], strict=True\n",
89+
")"
8690
]
8791
},
8892
{
@@ -170,7 +174,7 @@
170174
},
171175
{
172176
"cell_type": "code",
173-
"execution_count": 11,
177+
"execution_count": null,
174178
"id": "b41f8223d09379ac",
175179
"metadata": {
176180
"ExecuteTime": {
@@ -180,11 +184,13 @@
180184
},
181185
"outputs": [],
182186
"source": [
183-
"train_paths, train_labels = zip(*[(path, label) for _, label, path in train_dataset])\n",
187+
"train_paths, train_labels = zip(\n",
188+
" *[(path, label) for _, label, path in train_dataset], strict=True\n",
189+
")\n",
184190
"encodings_vlad = vlad_encoder.generate_encoding_map(train_paths)\n",
185191
"encodings_fisher = fisher_vector_encoder.generate_encoding_map(train_paths)\n",
186192
"encodings_pipeline = pipeline_with_pca.generate_encoding_map(train_paths)\n",
187-
"dataset_labels_dict = dict(zip(train_paths, train_labels))"
193+
"dataset_labels_dict = dict(zip(train_paths, train_labels, strict=True))"
188194
]
189195
},
190196
{

examples/pipeline.ipynb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
{
2525
"cell_type": "code",
26-
"execution_count": 1,
26+
"execution_count": null,
2727
"id": "3cfeb663c836d2a9",
2828
"metadata": {
2929
"ExecuteTime": {
@@ -32,20 +32,7 @@
3232
}
3333
},
3434
"outputs": [],
35-
"source": [
36-
"from torchvision.models import VGG16_Weights, vgg16\n",
37-
"\n",
38-
"from pyvisim._utils import *\n",
39-
"from pyvisim.datasets import OxfordFlowerDataset # or any dataset you have\n",
40-
"from pyvisim.encoders import (\n",
41-
" FisherVectorEncoder,\n",
42-
" GMMWeights,\n",
43-
" KMeansWeights,\n",
44-
" Pipeline,\n",
45-
" VLADEncoder,\n",
46-
")\n",
47-
"from pyvisim.features import DeepConvFeature"
48-
]
35+
"source": "import os\n\nimport numpy as np\nimport torch\nfrom torchvision.models import VGG16_Weights, vgg16\n\nfrom pyvisim._utils import cosine_similarity, plot_and_save_heatmap, plot_image\nfrom pyvisim.datasets import OxfordFlowerDataset # or any dataset you have\nfrom pyvisim.encoders import (\n FisherVectorEncoder,\n GMMWeights,\n KMeansWeights,\n Pipeline,\n VLADEncoder,\n)\nfrom pyvisim.features import DeepConvFeature"
4936
},
5037
{
5138
"cell_type": "markdown",
@@ -553,4 +540,4 @@
553540
},
554541
"nbformat": 4,
555542
"nbformat_minor": 5
556-
}
543+
}

examples/vlad_and_fisher_with_vgg16_deep_features.ipynb

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
{
3636
"cell_type": "code",
37-
"execution_count": 1,
37+
"execution_count": null,
3838
"id": "169b9b052dc0e9b4",
3939
"metadata": {
4040
"ExecuteTime": {
@@ -43,19 +43,7 @@
4343
}
4444
},
4545
"outputs": [],
46-
"source": [
47-
"from torchvision.models import VGG16_Weights, vgg16\n",
48-
"\n",
49-
"from pyvisim._utils import *\n",
50-
"from pyvisim.datasets import OxfordFlowerDataset # We'll create this in a moment\n",
51-
"from pyvisim.encoders._base_encoder import GMMWeights, KMeansWeights\n",
52-
"from pyvisim.encoders.fisher_vector import FisherVectorEncoder\n",
53-
"from pyvisim.encoders.vlad import VLADEncoder\n",
54-
"from pyvisim.eval import retrieve_top_k_similar\n",
55-
"\n",
56-
"# Our library imports\n",
57-
"from pyvisim.features import DeepConvFeature"
58-
]
46+
"source": "import os\n\nimport cv2\nimport matplotlib.pyplot as plt\nfrom torchvision.models import VGG16_Weights, vgg16\n\nfrom pyvisim._utils import plot_image\nfrom pyvisim.datasets import OxfordFlowerDataset # We'll create this in a moment\nfrom pyvisim.encoders._base_encoder import GMMWeights, KMeansWeights\nfrom pyvisim.encoders.fisher_vector import FisherVectorEncoder\nfrom pyvisim.encoders.vlad import VLADEncoder\nfrom pyvisim.eval import retrieve_top_k_similar\n\n# Our library imports\nfrom pyvisim.features import DeepConvFeature"
5947
},
6048
{
6149
"cell_type": "markdown",
@@ -1093,4 +1081,4 @@
10931081
},
10941082
"nbformat": 4,
10951083
"nbformat_minor": 5
1096-
}
1084+
}

pyvisim/_base_classes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import numpy as np
66

77

8-
98
class SimilarityMetric(abc.ABC):
109
"""
1110
Abstract base for all similarity encoders.
@@ -39,9 +38,6 @@ class FeatureExtractorBase(abc.ABC):
3938

4039
_logger = logging.getLogger("Feature_Extractor")
4140

42-
def __init__(self):
43-
pass
44-
4541
@abc.abstractmethod
4642
def __call__(self, image: np.ndarray):
4743
"""

0 commit comments

Comments
 (0)