Skip to content

Commit

Permalink
🔨 Lint: Update Ruff Config - Add Missing Copyright Headers (#2281)
Browse files Browse the repository at this point in the history
* pre-commit autoupdate

Signed-off-by: Samet Akcay <samet.akcay@intel.com>

* Enable preview feautures, and disable some of the updated features

* Add missing copyrights

Signed-off-by: Samet Akcay <samet.akcay@intel.com>

* Ignore copyrights in notebooks

---------

Signed-off-by: Samet Akcay <samet.akcay@intel.com>
  • Loading branch information
samet-akcay authored Aug 29, 2024
1 parent b7188db commit 4b1e08c
Show file tree
Hide file tree
Showing 27 changed files with 1,296 additions and 1,235 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:

# Ruff version.
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.5.1"
rev: "v0.6.2"
hooks:
# Run the linter.
- id: ruff
Expand All @@ -27,7 +27,7 @@ repos:

# python static type checking
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.10.1"
rev: "v1.11.2"
hooks:
- id: mypy
additional_dependencies: [types-PyYAML, types-setuptools]
Expand All @@ -43,7 +43,7 @@ repos:

# notebooks.
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
rev: 1.8.7
hooks:
- id: nbqa-ruff
# Ignore unsorted imports. This is because jupyter notebooks can import
Expand Down
3 changes: 3 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
"""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from __future__ import annotations

import sys
Expand Down
2 changes: 1 addition & 1 deletion notebooks/000_getting_started/001_getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
"from anomalib import TaskType\n",
"from anomalib.data import MVTec\n",
"from anomalib.data.utils import read_image\n",
"from anomalib.deploy import OpenVINOInferencer, ExportType\n",
"from anomalib.deploy import ExportType, OpenVINOInferencer\n",
"from anomalib.engine import Engine\n",
"from anomalib.models import Padim"
]
Expand Down
1,664 changes: 832 additions & 832 deletions notebooks/200_models/201_fastflow.ipynb

Large diffs are not rendered by default.

685 changes: 342 additions & 343 deletions notebooks/400_openvino/401_nncf.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
}
],
"source": [
"from anomalib.data import Folder\n",
"from anomalib import TaskType\n",
"from anomalib.data import Folder\n",
"\n",
"datamodule = Folder(\n",
" name=\"cubes\",\n",
Expand Down Expand Up @@ -646,9 +646,10 @@
}
],
"source": [
"from anomalib.utils.visualization.image import ImageVisualizer, VisualizationMode\n",
"from PIL import Image\n",
"\n",
"from anomalib.utils.visualization.image import ImageVisualizer, VisualizationMode\n",
"\n",
"visualizer = ImageVisualizer(mode=VisualizationMode.FULL, task=TaskType.CLASSIFICATION)\n",
"output_image = visualizer.visualize_image(predictions)\n",
"Image.fromarray(output_image)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@
"\n",
"# Anomalib imports\n",
"from __future__ import annotations\n",
"from typing import TYPE_CHECKING\n",
"\n",
"import sys\n",
"import time # time library\n",
"from datetime import datetime\n",
"from pathlib import Path\n",
"from threading import Thread\n",
"from typing import TYPE_CHECKING\n",
"\n",
"if TYPE_CHECKING:\n",
" import numpy as np\n",
"\n",
"# importing required libraries\n",
"import cv2 # OpenCV library\n",
"\n",
"from anomalib.deploy import OpenVINOInferencer"
]
},
Expand Down
16 changes: 9 additions & 7 deletions notebooks/600_loggers/601_mlflow_logging.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"metadata": {},
"outputs": [],
"source": [
"#!mlflow server"
"# !mlflow server"
]
},
{
Expand Down Expand Up @@ -175,15 +175,17 @@
"metadata": {},
"outputs": [],
"source": [
"from anomalib.data import MVTec\n",
"import warnings\n",
"\n",
"from lightning.pytorch.callbacks import EarlyStopping\n",
"\n",
"from anomalib import TaskType\n",
"from anomalib.callbacks.checkpoint import ModelCheckpoint\n",
"from lightning.pytorch.callbacks import EarlyStopping\n",
"from anomalib.models import Fastflow\n",
"from anomalib.loggers import AnomalibMLFlowLogger\n",
"from anomalib.data import MVTec\n",
"from anomalib.engine import Engine\n",
"from anomalib.loggers import AnomalibMLFlowLogger\n",
"from anomalib.models import Fastflow\n",
"\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
Expand Down Expand Up @@ -1044,7 +1046,7 @@
" pixel_metrics=\"AUROC\",\n",
" accelerator=\"auto\",\n",
" devices=1,\n",
" logger=mlflow_logger, # Logger is set here\n",
" logger=mlflow_logger, # Logger is set here\n",
" **kwargs,\n",
")"
]
Expand Down
38 changes: 38 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ version = { attr = "anomalib.__version__" }
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# RUFF CONFIGURATION #
[tool.ruff]
# Enable preview features
preview = true

# Enable rules
lint.select = [
"F", # Pyflakes (`F`)
Expand Down Expand Up @@ -158,6 +161,33 @@ lint.ignore = [
"PLR0912", # Too many branches
"PLR0915", # Too many statements

# NOTE: Disable the following rules for now.
"A004", # import is shadowing a Python built-in
"A005", # Module is shadowing a Python built-in
"B909", # Mutation to loop iterable during iteration
"PLR6301", # could be a function, class method or static method
"PLW1514", # Add explicit encoding argument
"PLR6201", # Convert to set
"PLC2701", # Private name import
"PLC0415", # import should be at the top of the file
"PLR0917", # Too many positional arguments
"E226", # Missing whitespace around arithmetic operator
"E266", # Too many leading `#` before block comment

"F822", # Undefined name `` in `__all__`

"PGH004", # Use specific rule codes when using 'ruff: noqa'
"PT001", # Use @pytest.fixture over @pytest.fixture()
"PLR6104", # Use `*=` to perform an augmented assignment directly
"PLR0914", # Too many local variables
"PLC0206", # Extracting value from dictionary without calling `.items()`
"PLC1901", # can be simplified

"RUF021", # Parenthesize the `and` subexpression
"RUF022", # Apply an isort-style sorting to '__all__'
"S404", # `subprocess` module is possibly insecure
# End of disable rules

# flake8-annotations
"ANN101", # Missing-type-self
"ANN002", # Missing type annotation for *args
Expand Down Expand Up @@ -225,6 +255,14 @@ max-complexity = 15
[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.flake8-copyright]
notice-rgx = """
# Copyright \\(C\\) (\\d{4}(-\\d{4})?) Intel Corporation
# SPDX-License-Identifier: Apache-2\\.0
"""

[tool.ruff.lint.per-file-ignores]
"notebooks/**/*" = ["CPY001"]

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# MYPY CONFIGURATION. #
Expand Down
2 changes: 1 addition & 1 deletion src/anomalib/callbacks/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ def on_test_end(self, trainer: Trainer, pl_module: LightningModule) -> None:
else:
test_data_loader = trainer.test_dataloaders[0]
output += f"(batch_size={test_data_loader.batch_size})"
output += f" : {self.num_images/testing_time} FPS"
output += f" : {self.num_images / testing_time} FPS"
logger.info(output)
3 changes: 3 additions & 0 deletions src/anomalib/loggers/mlflow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""MLFlow logger with add image interface."""

# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import os
from typing import Literal

Expand Down
3 changes: 3 additions & 0 deletions src/anomalib/models/components/classification/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Classification modules."""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from .kde_classifier import FeatureScalingMethod, KDEClassifier

__all__ = ["KDEClassifier", "FeatureScalingMethod"]
3 changes: 3 additions & 0 deletions src/anomalib/models/components/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Implements filters used by models."""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

from .blur import GaussianBlur2d

__all__ = ["GaussianBlur2d"]
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
"""PyTorch modules for Reverse Distillation."""

# Copyright (C) 2022-2024 Intel Corporation
#
# 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.
# SPDX-License-Identifier: Apache-2.0

from .bottleneck import get_bottleneck_layer
from .de_resnet import get_decoder
Expand Down
16 changes: 7 additions & 9 deletions src/anomalib/models/video/ai_vad/clip/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ def _convert_image_to_rgb(image):


def _transform(n_px):
return Compose(
[
Resize(n_px, interpolation=BICUBIC),
CenterCrop(n_px),
_convert_image_to_rgb,
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
]
)
return Compose([
Resize(n_px, interpolation=BICUBIC),
CenterCrop(n_px),
_convert_image_to_rgb,
ToTensor(),
Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)),
])


def available_models() -> List[str]:
Expand Down
30 changes: 13 additions & 17 deletions src/anomalib/models/video/ai_vad/clip/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ def __init__(self, inplanes, planes, stride=1):
if stride > 1 or inplanes != planes * Bottleneck.expansion:
# downsampling layer is prepended with an avgpool, and the subsequent convolution has stride 1
self.downsample = nn.Sequential(
OrderedDict(
[
("-1", nn.AvgPool2d(stride)),
("0", nn.Conv2d(inplanes, planes * self.expansion, 1, stride=1, bias=False)),
("1", nn.BatchNorm2d(planes * self.expansion)),
]
)
OrderedDict([
("-1", nn.AvgPool2d(stride)),
("0", nn.Conv2d(inplanes, planes * self.expansion, 1, stride=1, bias=False)),
("1", nn.BatchNorm2d(planes * self.expansion)),
])
)

def forward(self, x: torch.Tensor):
Expand Down Expand Up @@ -192,13 +190,11 @@ def __init__(self, d_model: int, n_head: int, attn_mask: torch.Tensor = None):
self.attn = nn.MultiheadAttention(d_model, n_head)
self.ln_1 = LayerNorm(d_model)
self.mlp = nn.Sequential(
OrderedDict(
[
("c_fc", nn.Linear(d_model, d_model * 4)),
("gelu", QuickGELU()),
("c_proj", nn.Linear(d_model * 4, d_model)),
]
)
OrderedDict([
("c_fc", nn.Linear(d_model, d_model * 4)),
("gelu", QuickGELU()),
("c_proj", nn.Linear(d_model * 4, d_model)),
])
)
self.ln_2 = LayerNorm(d_model)
self.attn_mask = attn_mask
Expand Down Expand Up @@ -430,9 +426,9 @@ def build_model(state_dict: dict):

if vit:
vision_width = state_dict["visual.conv1.weight"].shape[0]
vision_layers = len(
[k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")]
)
vision_layers = len([
k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")
])
vision_patch_size = state_dict["visual.conv1.weight"].shape[-1]
grid_size = round((state_dict["visual.positional_embedding"].shape[0] - 1) ** 0.5)
image_resolution = vision_patch_size * grid_size
Expand Down
2 changes: 1 addition & 1 deletion src/anomalib/pipelines/components/runners/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def run(self, args: dict, prev_stage_results: PREV_STAGE_RESULT = None) -> GATHE
"""Run the job in parallel."""
self.task_id = self.progress.add_task(self.generator.job_class.name, total=None)
self.progress.start()
self.processes = {i: None for i in range(self.n_jobs)}
self.processes = dict.fromkeys(range(self.n_jobs))

with ProcessPoolExecutor(max_workers=self.n_jobs, mp_context=multiprocessing.get_context("spawn")) as executor:
for job in self.generator(args, prev_stage_results):
Expand Down
2 changes: 1 addition & 1 deletion src/anomalib/utils/post_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def add_label(
img_height, img_width, _ = image.shape

font = cv2.FONT_HERSHEY_PLAIN
text = label_name if confidence is None else f"{label_name} ({confidence*100:.0f}%)"
text = label_name if confidence is None else f"{label_name} ({confidence * 100:.0f}%)"

# get font sizing
font_scale = min(img_width, img_height) * font_scale
Expand Down
6 changes: 3 additions & 3 deletions tests/helpers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def _generate_dummy_avenue_dataset(
train_path = self.dataset_root / train_dir
train_path.mkdir(exist_ok=True, parents=True)
for clip_idx in range(self.num_train):
clip_path = train_path / f"{clip_idx+1:02}.avi"
clip_path = train_path / f"{clip_idx + 1:02}.avi"
frames, _ = self.video_generator.generate_video(length=32, first_label=LabelName.NORMAL, p_state_switch=0)
fourcc = cv2.VideoWriter_fourcc("F", "M", "P", "4")
writer = cv2.VideoWriter(str(clip_path), fourcc, 30, self.frame_shape)
Expand All @@ -517,8 +517,8 @@ def _generate_dummy_avenue_dataset(
gt_path = self.dataset_root / ground_truth_dir / "testing_label_mask"

for clip_idx in range(self.num_test):
clip_path = test_path / f"{clip_idx+1:02}.avi"
mask_path = gt_path / f"{clip_idx+1}_label"
clip_path = test_path / f"{clip_idx + 1:02}.avi"
mask_path = gt_path / f"{clip_idx + 1}_label"
mask_path.mkdir(exist_ok=True, parents=True)
frames, masks = self.video_generator.generate_video(length=32, p_state_switch=0.2)
fourcc = cv2.VideoWriter_fourcc("F", "M", "P", "4")
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/data/utils/test_tiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Image Tiling Tests."""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

import pytest
import torch
from omegaconf import ListConfig
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/metrics/aupro/aupro_reference.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# REMARK: CODE WAS TAKEN FROM https://github.com/eliahuhorwitz/3D-ADS/blob/main/utils/au_pro_util.py

"""Utils for testing AUPRO metric.
Code based on the official MVTec 3D-AD evaluation code found at
Expand All @@ -9,6 +7,11 @@
The PRO curve can also be integrated up to a constant integration limit.
"""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

# Original code was taken from https://github.com/eliahuhorwitz/3D-ADS/blob/main/utils/au_pro_util.py

import logging
from bisect import bisect

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/models/components/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"""Test individual components."""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
3 changes: 3 additions & 0 deletions tests/unit/models/components/clustering/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"""Tests for clustering components."""

# Copyright (C) 2022-2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
Loading

0 comments on commit 4b1e08c

Please sign in to comment.