|
1
|
|
|
from __future__ import annotations |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
import warnings |
|
4
|
|
|
from pathlib import Path |
|
5
|
|
|
from typing import AbstractSet |
|
6
|
|
|
|
|
7
|
|
|
from pocketutils.core.exceptions import NotConstructableError |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class WarningsConfig: |
|
11
|
|
|
""" |
|
12
|
|
|
Convenient API to add warning filters. |
|
13
|
|
|
Also provides :meth:`simplify_format`, which sets a less-verbose warning formatter. |
|
14
|
|
|
|
|
15
|
|
|
Example: |
|
16
|
|
|
>>> ( |
|
17
|
|
|
GlobalWarningUtils.simplify_format() |
|
18
|
|
|
.filter_common() |
|
19
|
|
|
.never("Number of features differ") |
|
20
|
|
|
) |
|
21
|
|
|
""" |
|
22
|
|
|
|
|
23
|
|
|
def __init__(self): |
|
24
|
|
|
raise NotConstructableError(f"Do not instantiate {self.__class__.__name__}") |
|
25
|
|
|
|
|
26
|
|
|
@classmethod |
|
27
|
|
|
def simplify_format(cls) -> __qualname__: |
|
|
|
|
|
|
28
|
|
|
""" |
|
29
|
|
|
Common initialization, including setting a better formatter that doesn't say "WARNING:py.warnings:". |
|
|
|
|
|
|
30
|
|
|
""" |
|
31
|
|
|
|
|
32
|
|
|
def new_formatter(message, category, filename, lineno, line=None): |
|
|
|
|
|
|
33
|
|
|
cat = category.__name__.replace("Warning", "") |
|
34
|
|
|
s = f"{Path(filename).name}:{lineno}: {cat}: {message}" |
|
|
|
|
|
|
35
|
|
|
return s.replace("WARNING:py.warnings:", "") |
|
36
|
|
|
|
|
37
|
|
|
warnings.formatwarning = new_formatter |
|
38
|
|
|
return cls |
|
39
|
|
|
|
|
40
|
|
|
@classmethod |
|
41
|
|
|
def filter(cls, **kwargs) -> __qualname__: |
|
42
|
|
|
"""Same as warnings.filterwarnings.""" |
|
43
|
|
|
warnings.filterwarnings(**kwargs) |
|
44
|
|
|
return cls |
|
45
|
|
|
|
|
46
|
|
|
@classmethod |
|
47
|
|
|
def never(cls, *substrings: str) -> __qualname__: |
|
48
|
|
|
"""Adds a filter containing this substring, never showing the warning.""" |
|
49
|
|
|
for substring in substrings: |
|
50
|
|
|
warnings.filterwarnings(message=".*?" + substring + ".*", action="ignore") |
|
51
|
|
|
return cls |
|
52
|
|
|
|
|
53
|
|
|
@classmethod |
|
54
|
|
|
def once(cls, *substrings: str) -> __qualname__: |
|
55
|
|
|
"""Adds a filter containing this substring, warning only once.""" |
|
56
|
|
|
for substring in substrings: |
|
57
|
|
|
warnings.filterwarnings(message=".*?" + substring + ".*", action="once") |
|
58
|
|
|
return cls |
|
59
|
|
|
|
|
60
|
|
|
@classmethod |
|
61
|
|
|
def filter_common(cls) -> __qualname__: |
|
62
|
|
|
""" |
|
63
|
|
|
Adds filters for common unavoidable warnings from numpy, pandas, scikit-learn, etc. |
|
64
|
|
|
|
|
65
|
|
|
See ``common_never_substrings`` and ``common_once_substrings``. |
|
66
|
|
|
""" |
|
67
|
|
|
return cls.never(*cls.common_never()).once(*cls.common_once()) |
|
68
|
|
|
|
|
69
|
|
|
@classmethod |
|
70
|
|
|
def common_never(cls) -> AbstractSet[str]: |
|
|
|
|
|
|
71
|
|
|
return { |
|
72
|
|
|
"libuv only supports millisecond timer resolution", |
|
73
|
|
|
"or '1type' as a synonym of type is deprecated", |
|
74
|
|
|
"Series.nonzero() is deprecated and will be removed in a future version", |
|
75
|
|
|
"Monkey-patching ssl after ssl has already been imported may lead to errors", |
|
76
|
|
|
"your performance may suffer as PyTables will pickle object types that it cannot map directly to c-types", |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
@classmethod |
|
80
|
|
|
def common_once(cls) -> AbstractSet[str]: |
|
|
|
|
|
|
81
|
|
|
return { |
|
82
|
|
|
"Trying to unpickle estimator", |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
__all__ = ["WarningsConfig"] |
|
87
|
|
|
|