Skip to content

Commit cca7184

Browse files
authored
Merge pull request #687 from mrava87/feat-logging
feat: improved used of logging library
2 parents a94ea8e + 8e86f69 commit cca7184

44 files changed

Lines changed: 141 additions & 227 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pylops/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
4646
"""
4747

48+
import logging
49+
4850
from .config import *
4951
from .linearoperator import *
5052
from .torchoperator import *
@@ -69,6 +71,9 @@
6971
from .utils.utils import *
7072
from .utils.wavelets import *
7173

74+
# Prevent no handler message if an application using PyLops does not configure logging
75+
logging.getLogger(__name__).addHandler(logging.NullHandler())
76+
7277
try:
7378
from .version import version as __version__
7479
except ImportError:

pylops/_torchoperator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import logging
1+
import warnings
22

33
from pylops.utils import deps
44

@@ -22,8 +22,8 @@ def forward(ctx, x, forw, adj, device, devicetorch):
2222

2323
# check if data is moved to cpu and warn user
2424
if ctx.device == "cpu" and ctx.devicetorch != "cpu":
25-
logging.warning(
26-
"pylops operator will be applied on the cpu "
25+
warnings.warn(
26+
"PyLops operator will be applied on the cpu "
2727
"whilst the input torch vector is on "
2828
"%s, this may lead to poor performance" % ctx.devicetorch
2929
)

pylops/avo/avo.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"AVOLinearModelling",
1010
]
1111

12-
import logging
1312
from typing import List, Optional, Tuple, Union
1413

1514
import numpy as np
@@ -22,8 +21,6 @@
2221
from pylops.utils.decorators import reshaped
2322
from pylops.utils.typing import DTypeLike, NDArray
2423

25-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
26-
2724

2825
def zoeppritz_scattering(
2926
vp1: float,
@@ -659,7 +656,6 @@ def __init__(
659656
elif linearization == "ps":
660657
Gs = ps(theta, vsvp, n=self.nt0)
661658
else:
662-
logging.error("%s not an available " "linearization...", linearization)
663659
raise NotImplementedError(
664660
"%s not an available linearization..." % linearization
665661
)

pylops/avo/poststack.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"PoststackInversion",
44
]
55

6-
import logging
76
from typing import Optional, Tuple, Union
87

98
import numpy as np
@@ -32,8 +31,6 @@
3231
from pylops.utils.signalprocessing import convmtx, nonstationary_convmtx
3332
from pylops.utils.typing import NDArray, ShapeLike
3433

35-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
36-
3734

3835
def _PoststackLinearModelling(
3936
wav,

pylops/avo/prestack.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"PrestackInversion",
55
]
66

7-
import logging
87
from typing import Optional, Tuple, Union
98

109
import numpy as np
@@ -36,8 +35,6 @@
3635
from pylops.utils.signalprocessing import convmtx
3736
from pylops.utils.typing import NDArray, ShapeLike
3837

39-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
40-
4138
_linearizations = {"akirich": 3, "fatti": 3, "ps": 3}
4239

4340

@@ -167,7 +164,6 @@ def PrestackLinearModelling(
167164
elif callable(linearization):
168165
G = linearization(theta, vsvp, n=nt0)
169166
else:
170-
logging.error("%s not an available linearization...", linearization)
171167
raise NotImplementedError(
172168
"%s not an available linearization..." % linearization
173169
)
@@ -326,7 +322,6 @@ def PrestackWaveletModelling(
326322
elif callable(linearization):
327323
G = linearization(theta, vsvp, n=nt0)
328324
else:
329-
logging.error("%s not an available linearization...", linearization)
330325
raise NotImplementedError(
331326
"%s not an available linearization..." % linearization
332327
)

pylops/basicoperators/linearregression.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
__all__ = ["LinearRegression"]
22

3-
import logging
4-
53
import numpy.typing as npt
64

75
from pylops.basicoperators import Regression
86
from pylops.utils.typing import DTypeLike
97

10-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
11-
128

139
class LinearRegression(Regression):
1410
r"""Linear regression.
@@ -76,5 +72,7 @@ class LinearRegression(Regression):
7672
``order=1``.
7773
"""
7874

79-
def __init__(self, taxis: npt.ArrayLike, dtype: DTypeLike = "float64", name: str = 'L'):
75+
def __init__(
76+
self, taxis: npt.ArrayLike, dtype: DTypeLike = "float64", name: str = "L"
77+
):
8078
super().__init__(taxis=taxis, order=1, dtype=dtype, name=name)

pylops/basicoperators/matrixmult.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__all__ = ["MatrixMult"]
22

33
import logging
4+
import warnings
45
from typing import Optional, Union
56

67
import numpy as np
@@ -12,7 +13,7 @@
1213
from pylops.utils.backend import get_array_module
1314
from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray
1415

15-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
16+
logger = logging.getLogger(__name__)
1617

1718

1819
class MatrixMult(LinearOperator):
@@ -92,8 +93,8 @@ def __init__(
9293

9394
# Check if forceflat is needed and set it back to None otherwise
9495
if otherdims is not None and forceflat is not None:
95-
logging.warning(
96-
"setting forceflat=None since otherdims!=None. "
96+
logger.warning(
97+
"Setting forceflat=None since otherdims!=None. "
9798
"PyLops will automatically detect whether to return "
9899
"a 1d or nd array based on the shape of the input "
99100
"array."
@@ -102,7 +103,7 @@ def __init__(
102103
# Check dtype for correctness (upcast to complex when A is complex)
103104
if np.iscomplexobj(A) and not np.iscomplexobj(np.ones(1, dtype=dtype)):
104105
dtype = A.dtype
105-
logging.warning("Matrix A is a complex object, dtype cast to %s" % dtype)
106+
warnings.warn("Matrix A is a complex object, dtype cast to %s" % dtype)
106107
super().__init__(
107108
dtype=np.dtype(dtype),
108109
dims=dims,

pylops/basicoperators/regression.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
__all__ = ["Regression"]
22

3-
import logging
4-
53
import numpy as np
64
import numpy.typing as npt
75

86
from pylops import LinearOperator
97
from pylops.utils.backend import get_array_module
108
from pylops.utils.typing import DTypeLike, NDArray
119

12-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
13-
1410

1511
class Regression(LinearOperator):
1612
r"""Polynomial regression.
@@ -92,7 +88,6 @@ def __init__(
9288
) -> None:
9389
ncp = get_array_module(taxis)
9490
if not isinstance(taxis, ncp.ndarray):
95-
logging.error("t must be ndarray...")
9691
raise TypeError("t must be ndarray...")
9792
else:
9893
self.taxis = taxis

pylops/basicoperators/restriction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
from pylops.utils.typing import DTypeLike, InputDimsLike, IntNDArray, NDArray
1818

19-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
19+
logger = logging.getLogger(__name__)
2020

2121

2222
def _compute_iavamask(dims, axis, iava, ncp):
@@ -123,8 +123,8 @@ def __init__(
123123
# check if forceflat is needed and set it back to None otherwise
124124
if len(dims) > 2:
125125
if forceflat is not None:
126-
logging.warning(
127-
f"setting forceflat=None since len(dims)={len(dims)}>2. "
126+
logger.warning(
127+
f"Setting forceflat=None since len(dims)={len(dims)}>2. "
128128
f"PyLops will automatically detect whether to return "
129129
f"a 1d or nd array based on the shape of the input"
130130
f"array."

pylops/basicoperators/spread.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
_rmatvec_numba_table,
2323
)
2424

25-
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
25+
logger = logging.getLogger(__name__)
2626

2727

2828
class Spread(LinearOperator):
@@ -184,7 +184,7 @@ def __init__(
184184
self.engine = "numba"
185185
else:
186186
if engine == "numba" and jit is not None:
187-
logging.warning(jit_message)
187+
logger.warning(jit_message)
188188
self.engine = "numpy"
189189

190190
# axes
@@ -222,7 +222,7 @@ def __init__(
222222
if len(fh(0, 0)) == 2:
223223
self.interp = True
224224
if interp is not None and self.interp != interp:
225-
logging.warning("interp has been overridden to %r.", self.interp)
225+
logger.warning("interp has been overridden to %r.", self.interp)
226226

227227
def _matvec_numpy(self, x: NDArray) -> NDArray:
228228
y = np.zeros(self.dimsd, dtype=self.dtype)

0 commit comments

Comments
 (0)