Don't exit 0 with linter errors (#26)

* Cosmetic: Make linter happy

* Fix test safe log test

Co-authored-by: Lukas Wutschitz <lukas.wutschitz@microsoft.com>
This commit is contained in:
Lukas Wutschitz 2021-11-17 13:59:32 +00:00 коммит произвёл GitHub
Родитель d83df3af60
Коммит 77783cae78
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 14 добавлений и 16 удалений

2
.github/workflows/test-package.yml поставляемый
Просмотреть файл

@ -34,7 +34,7 @@ jobs:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
flake8 . --count --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest

Просмотреть файл

@ -4,8 +4,6 @@
import numpy as np
from typing import Tuple
from scipy import optimize
from .other_accountants import RDP
from . import discretisers
from . import composers
@ -55,7 +53,7 @@ class Accountant:
mesh_size = 2*eps_error / np.sqrt(2*max_compositions*np.log(2/eta0))
prv = privacy_random_variables.PoissonSubsampledGaussianMechanism(sampling_probability, noise_multiplier)
rdp = RDP(prv=prv, delta=self.delta_error/4)
L = self.eps_error + rdp.compute_epsilon(max_compositions)[2]
rdp = RDP(prv=prv, delta=self.delta_error/8/max_compositions)

Просмотреть файл

@ -34,7 +34,7 @@ class DiscretePrivacyRandomVariable:
if i <= 0:
raise RuntimeError("Cannot compute epsilon")
return np.log((d1[i-1]-delta_target)/d2[i-1])
eps_upper = find_epsilon(delta - delta_error) + epsilon_error
eps_lower = find_epsilon(delta + delta_error) - epsilon_error
eps_estimate = find_epsilon(delta)

Просмотреть файл

@ -28,7 +28,7 @@ class RDP:
Compute bounds on epsilon.
This function is based on Google's TF Privacy:
https://github.com/tensorflow/privacy/blob/master/tensorflow_privacy/privacy/analysis/rdp_accountant.py
https://github.com/tensorflow/privacy/blob/master/tensorflow_privacy/privacy/analysis/rdp_accountant.py
"""
rdp_steps = self.rdp*num_compositions
orders_vec = np.atleast_1d(self.orders)

Просмотреть файл

@ -17,8 +17,8 @@ M_PI = np.pi
def log(x):
valid = (x > 0)
x_is_0 = (x == 0)
return np.where(valid, np.log(np.where(valid, x, 1)),
np.where(x_is_0, -np.inf, np.nan))
return np.where(valid, np.log(np.where(valid, x, 1)),
np.where(x_is_0, -np.inf, np.nan))
class PrivacyRandomVariable(ABC):
@ -43,7 +43,7 @@ class PrivacyRandomVariable(ABC):
"""
raise NotImplementedError()
class PrivacyRandomVariableTruncated:
def __init__(self, prv, t_min: float, t_max: float) -> None:
self.prv = prv
@ -102,7 +102,7 @@ class PoissonSubsampledGaussianMechanism(PrivacyRandomVariable):
def cdf(self, t):
sigma = self.sigma
p = self.p
z = np.where(t>0, log((p-1)/p + exp(t)/p), log((p-1)/p + exp(t)/p))
z = np.where(t > 0, log((p-1)/p + exp(t)/p), log((p-1)/p + exp(t)/p))
return np.where(t > log(1 - p), (
(1.0/2.0) * p * (-erfc(np.double((1.0/4.0)*M_SQRT2*(2*pow(sigma, 2)*z - 1)/sigma))) -
1.0/2.0*(p - 1) * (-erfc(np.double((1.0/4.0)*M_SQRT2*(2*pow(sigma, 2)*z + 1)/sigma))) + 1
@ -114,8 +114,8 @@ class PoissonSubsampledGaussianMechanism(PrivacyRandomVariable):
def rdp(self, alpha: float) -> float:
"""
Compute RDP of this mechanism of order alpha
Based on Google's TF Privacy: https://github.com/tensorflow/privacy/blob/master/tensorflow_privacy/privacy/analysis/rdp_accountant.py
Based on Google's TF Privacy: https://github.com/tensorflow/privacy/blob/master/tensorflow_privacy/privacy/analysis/rdp_accountant.py # noqa: E501
"""
if self.p == 0:
return 0
@ -130,7 +130,7 @@ class PoissonSubsampledGaussianMechanism(PrivacyRandomVariable):
# The following code is based on Google's TF Privacy
# https://github.com/tensorflow/privacy/blob/master/tensorflow_privacy/privacy/analysis/rdp_accountant.py
# https://github.com/tensorflow/privacy/blob/master/tensorflow_privacy/privacy/analysis/rdp_accountant.py
def _compute_log_a(q, sigma, alpha):
"""Compute log(A_alpha) for any positive finite alpha."""

Просмотреть файл

@ -1,7 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
from typing import AsyncContextManager
import scipy
import math
import pytest
@ -10,9 +9,10 @@ import sys
from prv_accountant.privacy_random_variables import PoissonSubsampledGaussianMechanism, log, PrivacyRandomVariableTruncated
def test_safe_log():
assert np.isnan(log(-1)) == True
assert np.isneginf(log(0)) == True
assert np.isnan(log(-1)) == True # noqa: E712
assert np.isneginf(log(0)) == True # noqa: E712
assert log(1) == pytest.approx(0)