Skip to content

Commit

Permalink
Merge branch 'main' into lira_carlini
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Preen <[email protected]>
  • Loading branch information
rpreen authored Jun 10, 2024
2 parents da2e41b + d22383c commit d6f782a
Show file tree
Hide file tree
Showing 32 changed files with 832 additions and 1,191 deletions.
1 change: 1 addition & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
ignore:
- "setup.py"
- "aisdc/safemodel/classifiers/new_model_template.py"
- "aisdc/preprocessing"
...
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
9 changes: 6 additions & 3 deletions aisdc/attacks/likelihood_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,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 @@ -540,6 +540,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
)
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
44 changes: 44 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,47 @@ 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
]

[tool.ruff.per-file-ignores]
"tests/**/*" = ["S101"]
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d6f782a

Please sign in to comment.