From 7b5cf2d476bd54ad54b9fe43f3fce0a392d00a84 Mon Sep 17 00:00:00 2001 From: Emiliy Feldman Date: Wed, 13 Dec 2023 15:01:19 +0100 Subject: [PATCH] Warnings handling (#76) - Ignored some warnings in code - Checked presence of some warnings in tests - Ignored other warnings in tests --- rectools/dataset/identifiers.py | 7 ++++++- rectools/models/dssm.py | 9 ++++++-- setup.cfg | 2 ++ tests/model_selection/test_cross_validate.py | 22 ++++++++++++-------- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/rectools/dataset/identifiers.py b/rectools/dataset/identifiers.py index e9d04d92..d460afb9 100644 --- a/rectools/dataset/identifiers.py +++ b/rectools/dataset/identifiers.py @@ -15,6 +15,7 @@ """Mapping between external and internal ids.""" import typing as tp +import warnings import attr import numpy as np @@ -80,7 +81,11 @@ def from_dict(cls, mapping: tp.Dict[ExternalId, InternalId]) -> "IdMap": order = np.argsort(internal_ids) internal_ids_sorted = internal_ids[order] - internals_incorrect = internal_ids_sorted != np.arange(internal_ids_sorted.size) + with warnings.catch_warnings(): + # When comparing numeric vs. non-numeric array returns scalar, will change in the future + warnings.simplefilter("ignore", FutureWarning) + internals_incorrect = internal_ids_sorted != np.arange(internal_ids_sorted.size) + if internals_incorrect is True or internals_incorrect.any(): raise ValueError("Internal ids must be integers from 0 to n_objects-1") diff --git a/rectools/models/dssm.py b/rectools/models/dssm.py index a8d658fa..bb08c2a1 100644 --- a/rectools/models/dssm.py +++ b/rectools/models/dssm.py @@ -19,13 +19,18 @@ from __future__ import annotations import typing as tp +import warnings from copy import deepcopy import numpy as np import torch import torch.nn.functional as F -from pytorch_lightning import Callback, LightningModule, Trainer -from pytorch_lightning.loggers import Logger + +with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + from pytorch_lightning import Callback, LightningModule, Trainer + from pytorch_lightning.loggers import Logger + from torch import nn from torch.utils.data import DataLoader from torch.utils.data import Dataset as TorchDataset diff --git a/setup.cfg b/setup.cfg index 8d797a96..0720b967 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,6 +6,8 @@ doctest_optionflags = DONT_ACCEPT_TRUE_FOR_1 NORMALIZE_WHITESPACE filterwarnings = ignore:LightFM was compiled without OpenMP support ignore:distutils Version classes are deprecated + ignore:Converting sparse features to dense array may cause MemoryError + ignore:OpenBLAS is configured to use [coverage:run] # the name of the data file to use for storing or reporting coverage. diff --git a/tests/model_selection/test_cross_validate.py b/tests/model_selection/test_cross_validate.py index 560e12e3..6c889045 100644 --- a/tests/model_selection/test_cross_validate.py +++ b/tests/model_selection/test_cross_validate.py @@ -1,6 +1,7 @@ # pylint: disable=attribute-defined-outside-init import typing as tp +import warnings import numpy as np import pandas as pd @@ -251,12 +252,15 @@ def test_happy_path_with_features(self) -> None: def test_fail_with_cold_users(self) -> None: splitter = LastNSplitter(n=1, n_splits=2, filter_cold_users=False) - with pytest.raises(KeyError): - cross_validate( - dataset=self.dataset, - splitter=splitter, - metrics=self.metrics, - models=self.models, - k=2, - filter_viewed=False, - ) + with warnings.catch_warnings(record=True) as w: + with pytest.raises(KeyError): + cross_validate( + dataset=self.dataset, + splitter=splitter, + metrics=self.metrics, + models=self.models, + k=2, + filter_viewed=False, + ) + assert len(w) == 1 + assert "Currently models do not support recommendations for cold users" in str(w[-1].message)