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

Fix E721 linting errors #846

Merged
merged 2 commits into from
Aug 1, 2023
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
2 changes: 1 addition & 1 deletion alibi_detect/od/mahalanobis.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def score(self, X: np.ndarray) -> np.ndarray:
proj_x = np.matmul(X, eigvects)
proj_x_clip = np.matmul(X_clip, eigvects)
proj_means = np.matmul(new_means_offset, eigvects)
if type(self.C) == int and self.C == 0:
if isinstance(self.C, int) and self.C == 0:
proj_cov = np.diag(np.zeros(n_components))
else:
proj_cov = np.matmul(eigvects.transpose(), np.matmul(self.C, eigvects))
Expand Down
4 changes: 2 additions & 2 deletions alibi_detect/saving/tests/test_saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def test_save_kernel(kernel, backend, tmp_path): # noqa: F811
kernel_loaded(X, X)

# Final checks
assert type(kernel_loaded) == type(kernel)
assert type(kernel_loaded) == type(kernel) # noqa: E721
if backend == 'tensorflow':
np.testing.assert_array_almost_equal(np.array(kernel_loaded.sigma), np.array(kernel.sigma), 5)
else:
Expand Down Expand Up @@ -1320,7 +1320,7 @@ def test_registry_get():
"""
for k, v in REGISTERED_OBJECTS.items():
obj = registry.get(k)
assert type(obj) == type(v)
assert type(obj) == type(v) # noqa: E721


def test_set_dtypes(backend):
Expand Down
2 changes: 1 addition & 1 deletion alibi_detect/utils/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def ord2num(data: np.ndarray, dist: dict) -> np.ndarray:
for k, v in dist.items():
cat_col = X[:, k].copy()
cat_col = np.array([v[int(cat_col[i])] for i in range(rng)])
if type(X) == np.matrix:
if isinstance(X, np.matrix):
X[:, k] = cat_col.reshape(-1, 1)
else:
X[:, k] = cat_col
Expand Down
2 changes: 1 addition & 1 deletion alibi_detect/utils/pytorch/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def batch_compute_kernel_matrix(
"""
if device is None:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if type(x) != type(y):
if type(x) != type(y): # noqa: E721
raise ValueError("x and y should be of the same type")

if isinstance(x, np.ndarray):
Expand Down
2 changes: 1 addition & 1 deletion alibi_detect/utils/state/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Union, Tuple
import numpy as np
from alibi_detect.utils.frameworks import Framework
from alibi_detect.utils.state._pytorch import save_state_dict as _save_state_dict_pt,\
from alibi_detect.utils.state._pytorch import save_state_dict as _save_state_dict_pt, \
load_state_dict as _load_state_dict_pt

logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion alibi_detect/utils/tensorflow/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def batch_compute_kernel_matrix(
-------
Kernel matrix in the form of a tensorflow tensor
"""
if type(x) != type(y):
if type(x) != type(y): # noqa: E721
raise ValueError("x and y should be of the same type")

n_x, n_y = len(x), len(y)
Expand Down
30 changes: 15 additions & 15 deletions alibi_detect/utils/tests/test_saving_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,81 +166,81 @@ def test_save_load(select_detector):
if type(det_load) in [OutlierVAE, OutlierVAEGMM]:
assert det_load.samples == det.samples == samples

if type(det_load) == AdversarialAE or type(det_load) == ModelDistillation:
if isinstance(det_load, (AdversarialAE, ModelDistillation)):
for layer in det_load.model.layers:
assert not layer.trainable

if type(det_load) == OutlierAEGMM:
if isinstance(det_load, OutlierAEGMM):
assert isinstance(det_load.aegmm.encoder, tf.keras.Sequential)
assert isinstance(det_load.aegmm.decoder, tf.keras.Sequential)
assert isinstance(det_load.aegmm.gmm_density, tf.keras.Sequential)
assert isinstance(det_load.aegmm, tf.keras.Model)
assert det_load.aegmm.n_gmm == n_gmm
elif type(det_load) == OutlierVAEGMM:
elif isinstance(det_load, OutlierVAEGMM):
assert isinstance(det_load.vaegmm.encoder.encoder_net, tf.keras.Sequential)
assert isinstance(det_load.vaegmm.decoder, tf.keras.Sequential)
assert isinstance(det_load.vaegmm.gmm_density, tf.keras.Sequential)
assert isinstance(det_load.vaegmm, tf.keras.Model)
assert det_load.vaegmm.latent_dim == latent_dim
assert det_load.vaegmm.n_gmm == n_gmm
elif type(det_load) in [AdversarialAE, OutlierAE]:
elif isinstance(det_load, (AdversarialAE, OutlierAE)):
assert isinstance(det_load.ae.encoder.encoder_net, tf.keras.Sequential)
assert isinstance(det_load.ae.decoder.decoder_net, tf.keras.Sequential)
assert isinstance(det_load.ae, tf.keras.Model)
elif type(det_load) == ModelDistillation:
elif isinstance(det_load, ModelDistillation):
assert isinstance(det_load.model, tf.keras.Sequential) or isinstance(det_load.model, tf.keras.Model)
assert (isinstance(det_load.distilled_model, tf.keras.Sequential) or
isinstance(det_load.distilled_model, tf.keras.Model))
elif type(det_load) == OutlierVAE:
elif isinstance(det_load, OutlierVAE):
assert isinstance(det_load.vae.encoder.encoder_net, tf.keras.Sequential)
assert isinstance(det_load.vae.decoder.decoder_net, tf.keras.Sequential)
assert isinstance(det_load.vae, tf.keras.Model)
assert det_load.vae.latent_dim == latent_dim
elif type(det_load) == Mahalanobis:
elif isinstance(det_load, Mahalanobis):
assert det_load.clip is None
assert det_load.mean == det_load.C == det_load.n == 0
assert det_load.meta['detector_type'] == 'outlier'
assert det_load.meta['online']
elif type(det_load) == OutlierProphet:
elif isinstance(det_load, OutlierProphet):
assert det_load.model.interval_width == .7
assert det_load.model.growth == 'logistic'
assert det_load.meta['data_type'] == 'time-series'
elif type(det_load) == SpectralResidual:
elif isinstance(det_load, SpectralResidual):
assert det_load.window_amp == 10
assert det_load.window_local == 10
elif type(det_load) == OutlierSeq2Seq:
elif isinstance(det_load, OutlierSeq2Seq):
assert isinstance(det_load.seq2seq, tf.keras.Model)
assert isinstance(det_load.seq2seq.threshold_net, tf.keras.Sequential)
assert isinstance(det_load.seq2seq.encoder, EncoderLSTM)
assert isinstance(det_load.seq2seq.decoder, DecoderLSTM)
assert det_load.latent_dim == latent_dim
assert det_load.threshold == threshold
assert det_load.shape == (-1, seq_len, input_dim)
elif type(det_load) == KSDrift:
elif isinstance(det_load, KSDrift):
assert det_load.n_features == latent_dim
assert det_load.p_val == p_val
assert (det_load.x_ref == X_ref).all()
assert isinstance(det_load.preprocess_fn, Callable)
assert det_load.preprocess_fn.func.__name__ == 'preprocess_drift'
elif type(det_load) in [ChiSquareDrift, TabularDrift]:
elif isinstance(det_load, (ChiSquareDrift, TabularDrift)):
assert isinstance(det_load.x_ref_categories, dict)
assert det_load.p_val == p_val
x = X_ref_cat.copy() if isinstance(det_load, ChiSquareDrift) else X_ref_mix.copy()
assert (det_load.x_ref == x).all()
elif type(det_load) == MMDDrift:
elif isinstance(det_load, MMDDrift):
assert not det_load._detector.infer_sigma
assert det_load._detector.n_permutations == n_permutations
assert det_load._detector.p_val == p_val
assert (det_load._detector.x_ref == X_ref).all()
assert isinstance(det_load._detector.preprocess_fn, Callable)
assert det_load._detector.preprocess_fn.func.__name__ == 'preprocess_drift'
elif type(det_load) == ClassifierDrift:
elif isinstance(det_load, ClassifierDrift):
assert det_load._detector.p_val == p_val
assert (det_load._detector.x_ref == X_ref).all()
assert isinstance(det_load._detector.skf, StratifiedKFold)
assert isinstance(det_load._detector.train_kwargs, dict)
assert isinstance(det_load._detector.model, tf.keras.Model)
elif type(det_load) == LLR:
elif isinstance(det_load, LLR):
assert isinstance(det_load.dist_s, tf.keras.Model)
assert isinstance(det_load.dist_b, tf.keras.Model)
assert not det_load.sequential
Expand Down