Skip to content
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

add basic ruff linting/formatting #284

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,17 @@ repos:
.*\.ipynb
)$

# Autoremoves unused imports
- repo: https://github.com/hadialqattan/pycln
rev: "v2.4.0"
# Ruff, the Python auto-correcting linter/formatter written in Rust
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
hooks:
- id: pycln
stages: [manual]

# Sort includes
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade

# Upgrade old Python syntax
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black"]

# Black format Python and notebooks
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black-jupyter
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

# Format docstrings
- repo: https://github.com/DanielNoord/pydocstringformatter
rev: "v0.7.3"
rev: v0.7.3
hooks:
- id: pydocstringformatter
args: ["--style=numpydoc"]
Expand Down
4 changes: 1 addition & 3 deletions aisdc/attacks/attack_report_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ def __str__(self):
raise NotImplementedError()


class FinalRecommendationModule(
AnalysisModule
): # pylint: disable=too-many-instance-attributes
class FinalRecommendationModule(AnalysisModule): # pylint: disable=too-many-instance-attributes
"""Module that generates the first layer of a recommendation report."""

def __init__(self, report: dict):
Expand Down
12 changes: 6 additions & 6 deletions aisdc/attacks/likelihood_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ def _construct_metadata(self) -> None:
if auc_p <= self.p_thresh
else f"Not significant at p={self.p_thresh}"
)
self.metadata["global_metrics"][
"null_auc_3sd_range"
] = f"{0.5 - 3 * auc_std} -> {0.5 + 3 * auc_std}"
self.metadata["global_metrics"]["null_auc_3sd_range"] = (
f"{0.5 - 3 * auc_std} -> {0.5 + 3 * auc_std}"
)

self.metadata["attack"] = str(self)

Expand Down Expand Up @@ -498,9 +498,9 @@ def _get_attack_metrics_instances(self) -> dict:
attack_metrics_instances = {}

for rep, _ in enumerate(self.attack_metrics):
self.attack_metrics[rep][
"n_shadow_models_trained"
] = self.attack_failfast_shadow_models_trained
self.attack_metrics[rep]["n_shadow_models_trained"] = (
self.attack_failfast_shadow_models_trained
)
attack_metrics_instances["instance_" + str(rep)] = self.attack_metrics[rep]

attack_metrics_experiment["attack_instance_logger"] = attack_metrics_instances
Expand Down
8 changes: 2 additions & 6 deletions aisdc/attacks/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,14 @@ def title(pdf, text, border=BORDER, font_size=24, font_style="B"):
pdf.ln(h=5)


def subtitle(
pdf, text, indent=10, border=BORDER, font_size=12, font_style="B"
): # pylint: disable = too-many-arguments
def subtitle(pdf, text, indent=10, border=BORDER, font_size=12, font_style="B"): # pylint: disable = too-many-arguments
"""Write a subtitle block."""
pdf.cell(indent, border=border)
pdf.set_font("arial", font_style, font_size)
pdf.cell(75, 10, text, border, 1)


def line(
pdf, text, indent=0, border=BORDER, font_size=11, font_style="", font="arial"
): # pylint: disable = too-many-arguments
def line(pdf, text, indent=0, border=BORDER, font_size=11, font_style="", font="arial"): # pylint: disable = too-many-arguments
"""Write a standard block."""
if indent > 0:
pdf.cell(indent, border=border)
Expand Down
6 changes: 3 additions & 3 deletions aisdc/safemodel/classifiers/new_model_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""This is a template for implementing supplementary models
Obviously we have invented an sklearn ensemble called ModelToMakeSafer
Replace this with details of the model you wish to create a wrapper for
and then remove the comment which disables the pylint warning.
Obviously we have invented an sklearn ensemble called ModelToMakeSafer
Replace this with details of the model you wish to create a wrapper for
and then remove the comment which disables the pylint warning.
"""

# pylint: disable=duplicate-code
Expand Down
4 changes: 1 addition & 3 deletions aisdc/safemodel/classifiers/safedecisiontreeclassifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ def get_tree_k_anonymity(thetree: DecisionTreeClassifier, X: Any) -> int:
return k_anonymity


class SafeDecisionTreeClassifier(
SafeModel, DecisionTreeClassifier
): # pylint: disable=too-many-ancestors
class SafeDecisionTreeClassifier(SafeModel, DecisionTreeClassifier): # pylint: disable=too-many-ancestors
"""Privacy protected Decision Tree classifier."""

def __init__(self, **kwargs: Any) -> None:
Expand Down
14 changes: 6 additions & 8 deletions aisdc/safemodel/classifiers/safekeras.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""Safekeras.py:
Jim Smith, Andrew McCarty and Richard Preen
UWE 2022.
Jim Smith, Andrew McCarty and Richard Preen
UWE 2022.
"""

# general imports


import os
import warnings

Expand Down Expand Up @@ -219,10 +218,7 @@ def load_safe_keras_model(name: str = "undefined") -> Tuple[bool, Any]:


class SafeKerasModel(KerasModel, SafeModel):
"""Privacy Protected Wrapper around tf.keras.Model class from tensorflow 2.8
disabling pylont warnings about number of instance attributes
as this is necessarily complex.
"""
"""Privacy Protected Wrapper around tf.keras.Model class from tensorflow 2.8."""

# pylint: disable=too-many-instance-attributes
def __init__(self, *args: Any, **kwargs: Any) -> None:
Expand All @@ -236,6 +232,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
##inputs = kwargs.get("inputs","notFound")
##if inputs=="notFound":
## inputs = args[0] if len(args) == 3 else None
inputs = None
if "inputs" in kwargs.keys(): # pylint: disable=consider-iterating-dictionary
inputs = the_kwargs["inputs"]
elif len(args) == 3: # defaults is for Model(input,outputs,names)
Expand All @@ -252,7 +249,8 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:

# call the keras super class first as this comes first in chain
super().__init__( # pylint: disable=unexpected-keyword-arg
inputs=inputs, outputs=outputs # pylint: disable=used-before-assignment
inputs=inputs,
outputs=outputs, # pylint: disable=used-before-assignment
)

# set values where the user has supplied them
Expand Down
6 changes: 3 additions & 3 deletions aisdc/safemodel/classifiers/safetf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Work in progress to allow use of the DPModel classes
Jim smith 2022
When ready, linting of the imports will be enabled.
Jim smith 2022
When ready, linting of the imports will be enabled.
"""

# pylint: disable=unused-import
Expand Down Expand Up @@ -28,7 +28,7 @@ def __init__(
noise_multiplier: float,
use_xla: bool,
*args: any,
**kwargs: any
**kwargs: any,
) -> None:
"""Creates model and applies constraints to parameters."""
# safemodel.__init__(self)
Expand Down
41 changes: 41 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,44 @@ min-public-methods = 2 # Minimum number of public methods for a class (see R090
[tool.pylint.format]
max-line-length = 100 # Maximum number of characters on a single line.
max-module-lines = 1000 # Maximum number of lines in a module.

[tool.ruff]
indent-width = 4
line-length = 88
target-version = "py39"

lint.select = [
# "ANN", # flake8-annotations
# "ARG", # flake8-unused-arguments
# "B", # flake8-bugbear
# "C4", # flake8-comprehensions
# "C90", # mccabe
# "D", # pydocstyle
# "DTZ", # flake8-datetimez
# "E", # pycodestyle
# "EM", # flake8-errmsg
# "ERA", # eradicate
# "F", # Pyflakes
# "I", # isort
# "ICN", # flake8-import-conventions
# "N", # pep8-naming
# "PD", # pandas-vet
# "PGH", # pygrep-hooks
# "PIE", # flake8-pie
# "PL", # Pylint
# "PLC", # Pylint
# "PLE", # Pylint
# "PLR", # Pylint
# "PLW", # Pylint
# "PT", # flake8-pytest-style
# "Q", # flake8-quotes
# "RET", # flake8-return
# "RUF100", # Ruff-specific
# "S", # flake8-bandit
# "SIM", # flake8-simplify
# "T20", # flake8-print
# "TID", # flake8-tidy-imports
# "UP", # pyupgrade
# "W", # pycodestyle
# "YTT", # flake8-2020
]
6 changes: 2 additions & 4 deletions tests/attacks/test_lira_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ def test_check_and_update_dataset(self):
)
unique_classes_pre = set(local_test_y)
n_test_examples_pre = len(local_test_y)
local_target = (
attack_obj._check_and_update_dataset( # pylint:disable=protected-access
local_target
)
local_target = attack_obj._check_and_update_dataset( # pylint:disable=protected-access
local_target
)

unique_classes_post = set(local_target.y_test)
Expand Down
4 changes: 1 addition & 3 deletions tests/safemodel/test_safemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ def get_data():
return x, y


class SafeDummyClassifier(
SafeModel, DummyClassifier
): # pylint:disable=too-many-instance-attributes
class SafeDummyClassifier(SafeModel, DummyClassifier): # pylint:disable=too-many-instance-attributes
"""Privacy protected dummy classifier."""

def __init__(self, **kwargs) -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/safemodel/test_safetf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Jim Smith 2022
Not currently implemented.
Not currently implemented.
"""

from __future__ import annotations
Expand Down