Skip to content

Commit

Permalink
style: Bumps ruff & black, removes isort & pydocstyle (#216)
Browse files Browse the repository at this point in the history
* chore: Updates ruff config

* style: Fixes ruff

* style: Updates ruff config

* style: Adds copyright check

* style: Adds rules to ruff

* style: Updates precommit

* style: Fixes ruff

* chore: Removes pydocstyle

* style: Fixes typing

* chore: Removes isort

* style: Fixes black

* style: Fixes bandit

* chore: Updates deps for mypy

* ci: Updates CI

* chore: Bumps mypy
  • Loading branch information
frgfm committed Jul 13, 2023
1 parent b533442 commit dee2e74
Show file tree
Hide file tree
Showing 26 changed files with 81 additions and 146 deletions.
4 changes: 2 additions & 2 deletions .github/collect_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_cudnn_version(run_lambda):
if not files:
return None
# Alphabetize the result because the order is non-deterministic otherwise
files = list(sorted(files))
files = sorted(files)
if len(files) == 1:
return files[0]
result = "\n".join(files)
Expand Down Expand Up @@ -292,7 +292,7 @@ def maybe_start_on_next_line(string):
"nvidia_gpu_models",
"nvidia_driver_version",
]
all_cuda_fields = dynamic_cuda_fields + ["cudnn_version"]
all_cuda_fields = [*dynamic_cuda_fields, "cudnn_version"]
all_dynamic_cuda_fields_missing = all(mutable_dict[field] is None for field in dynamic_cuda_fields)
if TORCH_AVAILABLE and not torch.cuda.is_available() and all_dynamic_cuda_fields_missing:
for field in all_cuda_fields:
Expand Down
6 changes: 5 additions & 1 deletion .github/verify_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@


def query_repo(cmd: str, *, accept) -> Any:
response = requests.get(f"https://api.github.com/repos/{GH_ORG}/{GH_REPO}/{cmd}", headers=dict(Accept=accept))
response = requests.get(
f"https://api.github.com/repos/{GH_ORG}/{GH_REPO}/{cmd}",
headers={"Accept": accept},
timeout=5,
)
return response.json()


Expand Down
43 changes: 2 additions & 41 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,6 @@ jobs:
ruff --version
ruff check --diff .
isort:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: [3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run isort
run: |
pip install isort
isort --version
isort .
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then exit 1; else echo "All clear"; fi
mypy:
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -68,31 +48,12 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -e . --upgrade
pip install mypy
pip install "mypy==1.4.1"
- name: Run mypy
run: |
mypy --version
mypy
pydocstyle:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
python: [3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: x64
- name: Run pydocstyle
run: |
pip install pydocstyle[toml]
pydocstyle --version
pydocstyle
black:
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -108,7 +69,7 @@ jobs:
architecture: x64
- name: Run black
run: |
pip install "black==22.3.0"
pip install "black==23.3.0"
black --version
black --check --diff .
Expand Down
24 changes: 11 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
default_language_version:
python: python3.9
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-ast
- id: check-yaml
exclude: .conda
- id: check-toml
- id: check-json
- id: check-added-large-files
- id: end-of-file-fixer
- id: trailing-whitespace
- id: debug-statements
language_version: python3
- id: check-ast
- id: check-json
- id: check-merge-conflict
- id: no-commit-to-branch
args: ['--branch', 'main']
- id: debug-statements
language_version: python3
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 23.3.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
exclude: "(__init__.py)$"
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.260'
rev: 'v0.0.278'
hooks:
- id: ruff
exclude: "(__init__.py)$"
args:
- --fix
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# this target runs checks on all files
quality:
isort . -c
ruff check .
mypy
pydocstyle
black --check .
bandit -r . -c pyproject.toml

# this target runs checks on all files and potentially modifies some of them
style:
isort .
black .
ruff --fix .

Expand Down
8 changes: 3 additions & 5 deletions demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"convnext_small",
]
LABEL_MAP = requests.get(
"https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
"https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json",
timeout=10,
).json()


def main():

# Wide mode
st.set_page_config(layout="wide")

Expand Down Expand Up @@ -91,19 +91,17 @@ def main():
)

class_choices = [f"{idx + 1} - {class_name}" for idx, class_name in enumerate(LABEL_MAP)]
class_selection = st.sidebar.selectbox("Class selection", ["Predicted class (argmax)"] + class_choices)
class_selection = st.sidebar.selectbox("Class selection", ["Predicted class (argmax)", *class_choices])

# For newline
st.sidebar.write("\n")

if st.sidebar.button("Compute CAM"):

if uploaded_file is None:
st.sidebar.error("Please upload an image first")

else:
with st.spinner("Analyzing..."):

# Preprocess image
img_tensor = normalize(to_tensor(resize(img, (224, 224))), [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]


# Add googleanalytics id
# ref: https://github.com/orenhecht/googleanalytics/blob/master/sphinxcontrib/googleanalytics.py
def add_ga_javascript(app, pagename, templatename, context, doctree):

metatags = context.get("metatags", "")
metatags += """
<!-- Global site tag (gtag.js) - Google Analytics -->
Expand Down
70 changes: 45 additions & 25 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ test = [
"torchvision>=0.4.0,<1.0.0",
]
quality = [
"ruff>=0.0.260,<1.0.0",
"isort>=5.7.0",
"mypy>=1.2.0",
"pydocstyle[toml]>=6.0.0",
"black==22.3.0",
"ruff>=0.0.273,<1.0.0",
"mypy==1.4.1",
"black==23.3.0",
"bandit[toml]>=1.7.0,<1.8.0",
"pre-commit>=2.17.0,<3.0.0",
]
Expand All @@ -75,11 +73,9 @@ dev = [
"requests>=2.20.0,<3.0.0",
"torchvision>=0.4.0,<1.0.0",
# style
"ruff>=0.0.260,<1.0.0",
"isort>=5.7.0",
"mypy>=1.2.0",
"pydocstyle[toml]>=6.0.0",
"black==22.3.0",
"ruff>=0.0.273,<1.0.0",
"mypy==1.4.1",
"black==23.3.0",
"bandit[toml]>=1.7.0,<1.8.0",
"pre-commit>=2.17.0,<3.0.0",
# docs
Expand Down Expand Up @@ -108,18 +104,52 @@ exclude = ["demo*", "docs*", "notebooks*", "scripts*", "tests*"]
source = ["torchcam"]

[tool.ruff]
ignore = ["E402", "F403", "E731"]
exclude = [".git", "venv*", "build", "docs"]
select = [
"E", # pycodestyle errors
"W", # pycodestyle warnings
"D101", "D103", # pydocstyle missing docstring in public function/class
"D201","D202","D207","D208","D214","D215","D300","D301","D417", "D419", # pydocstyle
"F", # pyflakes
"I", # isort
"C", # flake8-comprehensions
"B", # flake8-bugbear
"CPY001", # flake8-copyright
"ISC", # flake8-implicit-str-concat
"PYI", # flake8-pyi
"NPY", # numpy
"PERF", # perflint
"RUF", # ruff specific
]
ignore = [
"E501", # line too long, handled by black
"B008", # do not perform function calls in argument defaults
"B904", # raise from
"C901", # too complex
"F403", # star imports
"E731", # lambda assignment
"C416", # list comprehension to list()
]
exclude = [".git"]
line-length = 120
target-version = "py38"
target-version = "py39"

[tool.ruff.per-file-ignores]
"**/__init__.py" = ["F401"]
"**/__init__.py" = ["I001", "F401", "CPY001"]
"scripts/**.py" = ["D"]
".github/**.py" = ["D"]
"docs/**.py" = ["E402", "D103"]
"tests/**.py" = ["D103", "CPY001"]
"demo/**.py" = ["D103"]

[tool.ruff.flake8-quotes]
docstring-quotes = "double"

[tool.ruff.isort]
known-first-party = ["app"]
known-third-party = ["torch", "torchvision"]

[tool.mypy]
python_version = "3.8"
files = "torchcam/"
show_error_codes = true
pretty = true
Expand All @@ -138,19 +168,9 @@ module = [
]
ignore_missing_imports = true

[tool.isort]
line_length = 120
src_paths = ["torchcam", "tests", "scripts", "docs", "demo", ".github"]
skip_glob = "**/__init__.py"
known_third_party = ["torch", "torchvision"]

[tool.pydocstyle]
select = "D300,D301,D417"
match = ".*\\.py"

[tool.black]
line-length = 120
target-version = ['py38']
target-version = ['py39']

[tool.bandit]
exclude_dirs = [".github/collect_env.py"]
Expand Down
3 changes: 1 addition & 2 deletions scripts/cam_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@


def main(args):

if args.device is None:
args.device = "cuda:0" if torch.cuda.is_available() else "cpu"

Expand All @@ -37,7 +36,7 @@ def main(args):

# Image
if args.img.startswith("http"):
img_path = BytesIO(requests.get(args.img).content)
img_path = BytesIO(requests.get(args.img, timeout=5).content)
else:
img_path = args.img
pil_img = Image.open(img_path, mode="r").convert("RGB")
Expand Down
1 change: 0 additions & 1 deletion scripts/eval_latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


def main(args):

if args.device is None:
args.device = "cuda:0" if torch.cuda.is_available() else "cpu"

Expand Down
1 change: 0 additions & 1 deletion scripts/eval_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@


def main(args):

if args.device is None:
args.device = "cuda:0" if torch.cuda.is_available() else "cpu"

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


if __name__ == "__main__":

print(f"Building wheel {PKG_NAME}-{VERSION}")

# Dynamically set the __version__ attribute
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def mock_img_tensor():
try:
# Get a dog image
URL = "https://www.woopets.fr/assets/races/000/066/big-portrait/border-collie.jpg"
response = requests.get(URL)
response = requests.get(URL, timeout=5)

# Forward an image
pil_img = Image.open(BytesIO(response.content), mode="r").convert("RGB")
Expand Down
1 change: 0 additions & 1 deletion tests/test_methods_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def test_video_cams(cam_name, target_layer, num_samples, output_size, mock_video

# Hook the corresponding layer in the model
with activation.__dict__[cam_name](model, target_layer, **kwargs) as extractor:

with torch.no_grad():
scores = model(mock_video_tensor)
# Use the hooked data to compute activation map
Expand Down
1 change: 0 additions & 1 deletion tests/test_methods_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ def test_cam_repr(mock_img_model):


def test_fuse_cams():

with pytest.raises(TypeError):
core._CAM.fuse_cams(torch.zeros((3, 32, 32)))

Expand Down
2 changes: 0 additions & 2 deletions tests/test_methods_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def test_img_cams(cam_name, target_layer, output_size, batch_size, mock_img_tens
target_layer = target_layer(model) if callable(target_layer) else target_layer
# Hook the corresponding layer in the model
with gradient.__dict__[cam_name](model, target_layer) as extractor:

scores = model(mock_img_tensor.repeat((batch_size,) + (1,) * (mock_img_tensor.ndim - 1)))
# Use the hooked data to compute activation map
_verify_cam(extractor(scores[0].argmax().item(), scores, retain_graph=True)[0], (batch_size, *output_size))
Expand Down Expand Up @@ -88,7 +87,6 @@ def test_smoothgradcampp_repr():


def test_layercam_fuse_cams(mock_img_model):

with pytest.raises(TypeError):
gradient.LayerCAM.fuse_cams(torch.zeros((3, 32, 32)))

Expand Down
Loading

0 comments on commit dee2e74

Please sign in to comment.