From 474853b55766e6e2c94c17a57c5c47c8932aee1d Mon Sep 17 00:00:00 2001 From: RobertSamoilescu Date: Wed, 11 Dec 2024 11:26:30 +0000 Subject: [PATCH] Fixed linting errors (#914) * Fixed mypy errors * Minor refactoring --- alibi_detect/cd/keops/learned_kernel.py | 6 +++--- alibi_detect/cd/keops/mmd.py | 2 +- alibi_detect/cd/pytorch/context_aware.py | 2 +- alibi_detect/cd/pytorch/learned_kernel.py | 6 +++--- alibi_detect/cd/pytorch/lsdd.py | 2 +- alibi_detect/cd/pytorch/mmd.py | 2 +- alibi_detect/models/pytorch/embedding.py | 8 +++++--- alibi_detect/models/pytorch/trainer.py | 4 ++-- alibi_detect/od/pytorch/svm.py | 6 ++---- alibi_detect/utils/perturbation.py | 2 +- 10 files changed, 20 insertions(+), 20 deletions(-) diff --git a/alibi_detect/cd/keops/learned_kernel.py b/alibi_detect/cd/keops/learned_kernel.py index a112113a9..d7ccbaead 100644 --- a/alibi_detect/cd/keops/learned_kernel.py +++ b/alibi_detect/cd/keops/learned_kernel.py @@ -241,7 +241,7 @@ def score(self, x: Union[np.ndarray, list]) -> Tuple[float, float, float]: idx_threshold = int(self.p_val * len(mmd2_permuted)) distance_threshold = torch.sort(mmd2_permuted, descending=True).values[idx_threshold] - return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy() + return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy().item() def _mmd2(self, x_all: Union[list, torch.Tensor], perms: List[torch.Tensor], m: int, n: int) \ -> Tuple[torch.Tensor, torch.Tensor]: @@ -341,5 +341,5 @@ def trainer( optimizer.step() # type: ignore if verbose == 1: loss_ma = loss_ma + (loss.item() - loss_ma) / (step + 1) - dl.set_description(f'Epoch {epoch + 1}/{epochs}') - dl.set_postfix(dict(loss=loss_ma)) + dl.set_description(f'Epoch {epoch + 1}/{epochs}') # type: ignore[union-attr] + dl.set_postfix(dict(loss=loss_ma)) # type: ignore[union-attr] diff --git a/alibi_detect/cd/keops/mmd.py b/alibi_detect/cd/keops/mmd.py index 83ca30a51..b0280d88e 100644 --- a/alibi_detect/cd/keops/mmd.py +++ b/alibi_detect/cd/keops/mmd.py @@ -182,4 +182,4 @@ def score(self, x: Union[np.ndarray, list]) -> Tuple[float, float, float]: # compute distance threshold idx_threshold = int(self.p_val * len(mmd2_permuted)) distance_threshold = torch.sort(mmd2_permuted, descending=True).values[idx_threshold] - return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy() + return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy().item() diff --git a/alibi_detect/cd/pytorch/context_aware.py b/alibi_detect/cd/pytorch/context_aware.py index d1b45b03d..728cec970 100644 --- a/alibi_detect/cd/pytorch/context_aware.py +++ b/alibi_detect/cd/pytorch/context_aware.py @@ -178,7 +178,7 @@ def score(self, # type: ignore[override] idx_threshold = int(self.p_val * len(permuted_stats)) distance_threshold = torch.sort(permuted_stats, descending=True).values[idx_threshold] - return p_val.numpy().item(), stat.numpy().item(), distance_threshold.numpy(), coupling + return p_val.numpy().item(), stat.numpy().item(), distance_threshold.numpy().item(), coupling def _cmmd(self, K: torch.Tensor, L: torch.Tensor, bools: torch.Tensor, L_held: torch.Tensor = None) \ -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: diff --git a/alibi_detect/cd/pytorch/learned_kernel.py b/alibi_detect/cd/pytorch/learned_kernel.py index a8785efe7..01ceee90a 100644 --- a/alibi_detect/cd/pytorch/learned_kernel.py +++ b/alibi_detect/cd/pytorch/learned_kernel.py @@ -221,7 +221,7 @@ def score(self, x: Union[np.ndarray, list]) -> Tuple[float, float, float]: idx_threshold = int(self.p_val * len(mmd2_permuted)) distance_threshold = torch.sort(mmd2_permuted, descending=True).values[idx_threshold] - return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy() + return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy().item() @staticmethod def trainer( @@ -256,5 +256,5 @@ def trainer( optimizer.step() # type: ignore if verbose == 1: loss_ma = loss_ma + (loss.item() - loss_ma) / (step + 1) - dl.set_description(f'Epoch {epoch + 1}/{epochs}') - dl.set_postfix(dict(loss=loss_ma)) + dl.set_description(f'Epoch {epoch + 1}/{epochs}') # type: ignore[union-attr] + dl.set_postfix(dict(loss=loss_ma)) # type: ignore[union-attr] diff --git a/alibi_detect/cd/pytorch/lsdd.py b/alibi_detect/cd/pytorch/lsdd.py index 37e4dd81a..193d0e872 100644 --- a/alibi_detect/cd/pytorch/lsdd.py +++ b/alibi_detect/cd/pytorch/lsdd.py @@ -176,4 +176,4 @@ def score(self, x: Union[np.ndarray, list]) -> Tuple[float, float, float]: idx_threshold = int(self.p_val * len(lsdd_permuted)) distance_threshold = torch.sort(lsdd_permuted, descending=True).values[idx_threshold] - return float(p_val.cpu()), float(lsdd.cpu().numpy()), distance_threshold.cpu().numpy() + return p_val.cpu().item(), lsdd.cpu().numpy().item(), distance_threshold.cpu().numpy().item() diff --git a/alibi_detect/cd/pytorch/mmd.py b/alibi_detect/cd/pytorch/mmd.py index ffacad8e6..afda215ff 100644 --- a/alibi_detect/cd/pytorch/mmd.py +++ b/alibi_detect/cd/pytorch/mmd.py @@ -143,4 +143,4 @@ def score(self, x: Union[np.ndarray, list]) -> Tuple[float, float, float]: # compute distance threshold idx_threshold = int(self.p_val * len(mmd2_permuted)) distance_threshold = torch.sort(mmd2_permuted, descending=True).values[idx_threshold] - return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy() + return p_val.numpy().item(), mmd2.numpy().item(), distance_threshold.numpy().item() diff --git a/alibi_detect/models/pytorch/embedding.py b/alibi_detect/models/pytorch/embedding.py index 753835938..62d8ddc5d 100644 --- a/alibi_detect/models/pytorch/embedding.py +++ b/alibi_detect/models/pytorch/embedding.py @@ -25,9 +25,11 @@ def hidden_state_embedding(hidden_states: torch.Tensor, layers: List[int], ------- Tensor with embeddings. """ - hs = [hidden_states[layer][:, 0:1, :] if use_cls else hidden_states[layer] for layer in layers] - hs = torch.cat(hs, dim=1) # type: ignore - y = hs.mean(dim=1) if reduce_mean else hs # type: ignore + hs = torch.cat( + [hidden_states[layer][:, 0:1, :] if use_cls else hidden_states[layer] for layer in layers], + dim=1 + ) + y = hs.mean(dim=1) if reduce_mean else hs return y diff --git a/alibi_detect/models/pytorch/trainer.py b/alibi_detect/models/pytorch/trainer.py index c9d004fed..c168520d1 100644 --- a/alibi_detect/models/pytorch/trainer.py +++ b/alibi_detect/models/pytorch/trainer.py @@ -59,5 +59,5 @@ def trainer( optimizer.step() # type: ignore if verbose == 1: loss_ma = loss_ma + (loss.item() - loss_ma) / (step + 1) - dl.set_description(f'Epoch {epoch + 1}/{epochs}') - dl.set_postfix(dict(loss_ma=loss_ma)) + dl.set_description(f'Epoch {epoch + 1}/{epochs}') # type: ignore[union-attr] + dl.set_postfix(dict(loss_ma=loss_ma)) # type: ignore[union-attr] diff --git a/alibi_detect/od/pytorch/svm.py b/alibi_detect/od/pytorch/svm.py index ab9adf003..4552b55dd 100644 --- a/alibi_detect/od/pytorch/svm.py +++ b/alibi_detect/od/pytorch/svm.py @@ -143,14 +143,13 @@ def fit( # type: ignore[override] - converged: `bool` indicating whether training converged. - n_iter: number of iterations performed. """ - x_nys = self.nystroem.fit(x_ref).transform(x_ref) + x_nys = self.nystroem.fit(x_ref).transform(x_ref).cpu().numpy() self.svm = SGDOneClassSVM( tol=tol, max_iter=max_iter, verbose=verbose, nu=self.nu ) - x_nys = x_nys.cpu().numpy() self.svm = self.svm.fit(x_nys) self._set_fitted() return { @@ -194,8 +193,7 @@ def score(self, x: torch.Tensor) -> torch.Tensor: Raised if method called and detector has not been fit. """ self.check_fitted() - x_nys = self.nystroem.transform(x) - x_nys = x_nys.cpu().numpy() + x_nys = self.nystroem.transform(x).cpu().numpy() coef_ = self.svm.coef_ / (self.svm.coef_ ** 2).sum() x_nys = self.svm._validate_data(x_nys, accept_sparse="csr", reset=False) result = safe_sparse_dot(x_nys, coef_.T, dense_output=True).ravel() diff --git a/alibi_detect/utils/perturbation.py b/alibi_detect/utils/perturbation.py index 348e9c2b8..48c89fd26 100644 --- a/alibi_detect/utils/perturbation.py +++ b/alibi_detect/utils/perturbation.py @@ -895,7 +895,7 @@ def jpeg_compression(x: np.ndarray, strength: float, xrange: tuple = None) -> np if xrange[0] != 0 or xrange[1] != 255: x = (x - xrange[0]) / (xrange[1] - xrange[0]) * 255 - x = Image.fromarray(x.astype('uint8'), mode='RGB') + x = Image.fromarray(x.astype('uint8'), mode='RGB') # type: ignore[assignment] output = BytesIO() x.save(output, 'JPEG', quality=strength) # type: ignore[attr-defined] # TODO: allow redefinition x_image = Image.open(output)