From 6e25e492829c6b582421f61462f534957b0f4a96 Mon Sep 17 00:00:00 2001 From: mrava87 Date: Mon, 21 Jul 2025 19:19:40 +0100 Subject: [PATCH 1/2] feat: improved used of logging library --- pylops/__init__.py | 5 +++ pylops/_torchoperator.py | 6 +-- pylops/avo/avo.py | 4 -- pylops/avo/poststack.py | 3 -- pylops/avo/prestack.py | 5 --- pylops/basicoperators/linearregression.py | 8 ++-- pylops/basicoperators/matrixmult.py | 9 ++-- pylops/basicoperators/regression.py | 5 --- pylops/basicoperators/restriction.py | 6 +-- pylops/basicoperators/spread.py | 6 +-- pylops/basicoperators/sum.py | 6 +-- pylops/linearoperator.py | 3 -- pylops/optimization/basesolver.py | 4 +- pylops/optimization/cls_leastsquares.py | 4 -- pylops/optimization/cls_sparsity.py | 10 ++--- pylops/signalprocessing/_baseffts.py | 2 - pylops/signalprocessing/bilinear.py | 6 +-- pylops/signalprocessing/chirpradon2d.py | 4 -- pylops/signalprocessing/chirpradon3d.py | 4 -- pylops/signalprocessing/dwt.py | 3 -- pylops/signalprocessing/dwt2d.py | 3 -- pylops/signalprocessing/dwtnd.py | 3 -- pylops/signalprocessing/fft.py | 4 +- pylops/signalprocessing/fft2d.py | 3 -- pylops/signalprocessing/fftnd.py | 3 -- pylops/signalprocessing/fourierradon2d.py | 3 -- pylops/signalprocessing/fourierradon3d.py | 3 -- pylops/signalprocessing/interp.py | 8 ++-- pylops/signalprocessing/patch2d.py | 37 ++++++++------- pylops/signalprocessing/patch3d.py | 45 +++++++++---------- pylops/signalprocessing/radon2d.py | 3 -- pylops/signalprocessing/radon3d.py | 3 -- pylops/signalprocessing/sliding1d.py | 29 ++++++------ pylops/signalprocessing/sliding2d.py | 29 ++++++------ pylops/signalprocessing/sliding3d.py | 37 ++++++++------- pylops/utils/describe.py | 5 +-- pylops/utils/wavelets.py | 2 +- pylops/waveeqprocessing/kirchhoff.py | 4 +- pylops/waveeqprocessing/lsm.py | 3 -- pylops/waveeqprocessing/marchenko.py | 21 +++------ pylops/waveeqprocessing/mdd.py | 6 ++- pylops/waveeqprocessing/oneway.py | 4 -- .../waveeqprocessing/seismicinterpolation.py | 3 -- pylops/waveeqprocessing/wavedecomposition.py | 3 -- 44 files changed, 141 insertions(+), 226 deletions(-) diff --git a/pylops/__init__.py b/pylops/__init__.py index b1ee04c82..bf6b924a3 100755 --- a/pylops/__init__.py +++ b/pylops/__init__.py @@ -45,6 +45,8 @@ """ +import logging + from .config import * from .linearoperator import * from .torchoperator import * @@ -69,6 +71,9 @@ from .utils.utils import * from .utils.wavelets import * +# Prevent no handler message if an application using PyLops does not configure logging +logging.getLogger(__name__).addHandler(logging.NullHandler()) + try: from .version import version as __version__ except ImportError: diff --git a/pylops/_torchoperator.py b/pylops/_torchoperator.py index fe27eb61c..55527eb90 100644 --- a/pylops/_torchoperator.py +++ b/pylops/_torchoperator.py @@ -1,4 +1,4 @@ -import logging +import warnings from pylops.utils import deps @@ -22,8 +22,8 @@ def forward(ctx, x, forw, adj, device, devicetorch): # check if data is moved to cpu and warn user if ctx.device == "cpu" and ctx.devicetorch != "cpu": - logging.warning( - "pylops operator will be applied on the cpu " + warnings.warn( + "PyLops operator will be applied on the cpu " "whilst the input torch vector is on " "%s, this may lead to poor performance" % ctx.devicetorch ) diff --git a/pylops/avo/avo.py b/pylops/avo/avo.py index 35e979930..5a65787d5 100644 --- a/pylops/avo/avo.py +++ b/pylops/avo/avo.py @@ -9,7 +9,6 @@ "AVOLinearModelling", ] -import logging from typing import List, Optional, Tuple, Union import numpy as np @@ -22,8 +21,6 @@ from pylops.utils.decorators import reshaped from pylops.utils.typing import DTypeLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def zoeppritz_scattering( vp1: float, @@ -659,7 +656,6 @@ def __init__( elif linearization == "ps": Gs = ps(theta, vsvp, n=self.nt0) else: - logging.error("%s not an available " "linearization...", linearization) raise NotImplementedError( "%s not an available linearization..." % linearization ) diff --git a/pylops/avo/poststack.py b/pylops/avo/poststack.py index 8a9001ac1..0a1a71c75 100644 --- a/pylops/avo/poststack.py +++ b/pylops/avo/poststack.py @@ -3,7 +3,6 @@ "PoststackInversion", ] -import logging from typing import Optional, Tuple, Union import numpy as np @@ -32,8 +31,6 @@ from pylops.utils.signalprocessing import convmtx, nonstationary_convmtx from pylops.utils.typing import NDArray, ShapeLike -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def _PoststackLinearModelling( wav, diff --git a/pylops/avo/prestack.py b/pylops/avo/prestack.py index 4cf6c4ebc..a98f953aa 100644 --- a/pylops/avo/prestack.py +++ b/pylops/avo/prestack.py @@ -4,7 +4,6 @@ "PrestackInversion", ] -import logging from typing import Optional, Tuple, Union import numpy as np @@ -36,8 +35,6 @@ from pylops.utils.signalprocessing import convmtx from pylops.utils.typing import NDArray, ShapeLike -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - _linearizations = {"akirich": 3, "fatti": 3, "ps": 3} @@ -167,7 +164,6 @@ def PrestackLinearModelling( elif callable(linearization): G = linearization(theta, vsvp, n=nt0) else: - logging.error("%s not an available linearization...", linearization) raise NotImplementedError( "%s not an available linearization..." % linearization ) @@ -326,7 +322,6 @@ def PrestackWaveletModelling( elif callable(linearization): G = linearization(theta, vsvp, n=nt0) else: - logging.error("%s not an available linearization...", linearization) raise NotImplementedError( "%s not an available linearization..." % linearization ) diff --git a/pylops/basicoperators/linearregression.py b/pylops/basicoperators/linearregression.py index cd179dd43..3eb7d5d66 100644 --- a/pylops/basicoperators/linearregression.py +++ b/pylops/basicoperators/linearregression.py @@ -1,14 +1,10 @@ __all__ = ["LinearRegression"] -import logging - import numpy.typing as npt from pylops.basicoperators import Regression from pylops.utils.typing import DTypeLike -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class LinearRegression(Regression): r"""Linear regression. @@ -76,5 +72,7 @@ class LinearRegression(Regression): ``order=1``. """ - def __init__(self, taxis: npt.ArrayLike, dtype: DTypeLike = "float64", name: str = 'L'): + def __init__( + self, taxis: npt.ArrayLike, dtype: DTypeLike = "float64", name: str = "L" + ): super().__init__(taxis=taxis, order=1, dtype=dtype, name=name) diff --git a/pylops/basicoperators/matrixmult.py b/pylops/basicoperators/matrixmult.py index 66b96c70a..f1dbbc5f6 100644 --- a/pylops/basicoperators/matrixmult.py +++ b/pylops/basicoperators/matrixmult.py @@ -1,6 +1,7 @@ __all__ = ["MatrixMult"] import logging +import warnings from typing import Optional, Union import numpy as np @@ -12,7 +13,7 @@ from pylops.utils.backend import get_array_module from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) class MatrixMult(LinearOperator): @@ -92,8 +93,8 @@ def __init__( # Check if forceflat is needed and set it back to None otherwise if otherdims is not None and forceflat is not None: - logging.warning( - "setting forceflat=None since otherdims!=None. " + logger.warning( + "Setting forceflat=None since otherdims!=None. " "PyLops will automatically detect whether to return " "a 1d or nd array based on the shape of the input " "array." @@ -102,7 +103,7 @@ def __init__( # Check dtype for correctness (upcast to complex when A is complex) if np.iscomplexobj(A) and not np.iscomplexobj(np.ones(1, dtype=dtype)): dtype = A.dtype - logging.warning("Matrix A is a complex object, dtype cast to %s" % dtype) + warnings.warn("Matrix A is a complex object, dtype cast to %s" % dtype) super().__init__( dtype=np.dtype(dtype), dims=dims, diff --git a/pylops/basicoperators/regression.py b/pylops/basicoperators/regression.py index 1160fe9b1..fafe48943 100644 --- a/pylops/basicoperators/regression.py +++ b/pylops/basicoperators/regression.py @@ -1,7 +1,5 @@ __all__ = ["Regression"] -import logging - import numpy as np import numpy.typing as npt @@ -9,8 +7,6 @@ from pylops.utils.backend import get_array_module from pylops.utils.typing import DTypeLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class Regression(LinearOperator): r"""Polynomial regression. @@ -92,7 +88,6 @@ def __init__( ) -> None: ncp = get_array_module(taxis) if not isinstance(taxis, ncp.ndarray): - logging.error("t must be ndarray...") raise TypeError("t must be ndarray...") else: self.taxis = taxis diff --git a/pylops/basicoperators/restriction.py b/pylops/basicoperators/restriction.py index e27610bf2..d1b3da34c 100644 --- a/pylops/basicoperators/restriction.py +++ b/pylops/basicoperators/restriction.py @@ -16,7 +16,7 @@ ) from pylops.utils.typing import DTypeLike, InputDimsLike, IntNDArray, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def _compute_iavamask(dims, axis, iava, ncp): @@ -123,8 +123,8 @@ def __init__( # check if forceflat is needed and set it back to None otherwise if len(dims) > 2: if forceflat is not None: - logging.warning( - f"setting forceflat=None since len(dims)={len(dims)}>2. " + logger.warning( + f"Setting forceflat=None since len(dims)={len(dims)}>2. " f"PyLops will automatically detect whether to return " f"a 1d or nd array based on the shape of the input" f"array." diff --git a/pylops/basicoperators/spread.py b/pylops/basicoperators/spread.py index 2244ffb6d..bfea56553 100644 --- a/pylops/basicoperators/spread.py +++ b/pylops/basicoperators/spread.py @@ -22,7 +22,7 @@ _rmatvec_numba_table, ) -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) class Spread(LinearOperator): @@ -184,7 +184,7 @@ def __init__( self.engine = "numba" else: if engine == "numba" and jit is not None: - logging.warning(jit_message) + logger.warning(jit_message) self.engine = "numpy" # axes @@ -222,7 +222,7 @@ def __init__( if len(fh(0, 0)) == 2: self.interp = True if interp is not None and self.interp != interp: - logging.warning("interp has been overridden to %r.", self.interp) + logger.warning("interp has been overridden to %r.", self.interp) def _matvec_numpy(self, x: NDArray) -> NDArray: y = np.zeros(self.dimsd, dtype=self.dtype) diff --git a/pylops/basicoperators/sum.py b/pylops/basicoperators/sum.py index 855cf521f..18493823e 100644 --- a/pylops/basicoperators/sum.py +++ b/pylops/basicoperators/sum.py @@ -10,7 +10,7 @@ from pylops.utils.decorators import reshaped from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) class Sum(LinearOperator): @@ -84,8 +84,8 @@ def __init__( dimsd.pop(self.axis) # check if forceflat is needed and set it back to None otherwise if len(dims) > 2 and forceflat is not None: - logging.warning( - f"setting forceflat=None since len(dims)={len(dims)}>2. " + logger.warning( + f"Setting forceflat=None since len(dims)={len(dims)}>2. " f"PyLops will automatically detect whether to return " f"a 1d or nd array based on the shape of the input" f"array." diff --git a/pylops/linearoperator.py b/pylops/linearoperator.py index ce1b0202b..98612cdcc 100644 --- a/pylops/linearoperator.py +++ b/pylops/linearoperator.py @@ -5,7 +5,6 @@ "aslinearoperator", ] -import logging from abc import ABC, abstractmethod import numpy as np @@ -37,8 +36,6 @@ from pylops.utils.estimators import trace_hutchinson, trace_hutchpp, trace_nahutchpp from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray, ShapeLike -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class _LinearOperator(ABC): """Meta-class for Linear operator""" diff --git a/pylops/optimization/basesolver.py b/pylops/optimization/basesolver.py index 490abcf3b..7e74ba25e 100644 --- a/pylops/optimization/basesolver.py +++ b/pylops/optimization/basesolver.py @@ -1,8 +1,8 @@ __all__ = ["Solver"] import functools -import logging import time +import warnings from abc import ABCMeta, abstractmethod from typing import TYPE_CHECKING, Any @@ -133,7 +133,7 @@ def _setpreallocate(self, preallocate: bool) -> None: self.preallocate = preallocate if not self.isjax else False if preallocate and self.isjax: - logging.warning( + warnings.warn( "Preallocation is not supported for JAX arrays. " "Setting preallocate to False." ) diff --git a/pylops/optimization/cls_leastsquares.py b/pylops/optimization/cls_leastsquares.py index 8d66867bd..557997a10 100644 --- a/pylops/optimization/cls_leastsquares.py +++ b/pylops/optimization/cls_leastsquares.py @@ -5,7 +5,6 @@ "PreconditionedInversion", ] -import logging from typing import TYPE_CHECKING, Optional, Sequence, Tuple import numpy as np @@ -22,9 +21,6 @@ from pylops.linearoperator import LinearOperator -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - - def _check_regularization_dims( Regs: Sequence["LinearOperator"], dataregs: Optional[Sequence[NDArray]] = None, diff --git a/pylops/optimization/cls_sparsity.py b/pylops/optimization/cls_sparsity.py index b0bff8638..093efc856 100644 --- a/pylops/optimization/cls_sparsity.py +++ b/pylops/optimization/cls_sparsity.py @@ -30,7 +30,7 @@ if spgl1_message is None: from spgl1 import spgl1 as ext_spgl1 -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def _hardthreshold(x: NDArray, thresh: float) -> NDArray: @@ -1825,9 +1825,7 @@ def run( x, xupdate = self.step(x, showstep) self.callback(x) if xupdate <= self.tol: - logging.warning( - "update smaller that tolerance for " "iteration %d", self.iiter - ) + logger.info("Update smaller that tolerance for iteration %d", self.iiter) return x def finalize(self, show: bool = False) -> None: @@ -2208,8 +2206,8 @@ def run( x, z, xupdate = self.step(x, z, showstep) self.callback(x) if xupdate <= self.tol: - logging.warning( - "update smaller that tolerance for " "iteration %d", self.iiter + logger.warning( + "Update smaller that tolerance for " "iteration %d", self.iiter ) return x diff --git a/pylops/signalprocessing/_baseffts.py b/pylops/signalprocessing/_baseffts.py index 18a4cce2c..4f7cc9093 100644 --- a/pylops/signalprocessing/_baseffts.py +++ b/pylops/signalprocessing/_baseffts.py @@ -18,8 +18,6 @@ ) from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class _FFTNorms(Enum): ORTHO = auto() diff --git a/pylops/signalprocessing/bilinear.py b/pylops/signalprocessing/bilinear.py index 6bc62e585..e8a061672 100644 --- a/pylops/signalprocessing/bilinear.py +++ b/pylops/signalprocessing/bilinear.py @@ -10,7 +10,7 @@ from pylops.utils.decorators import reshaped from pylops.utils.typing import DTypeLike, InputDimsLike, IntNDArray, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def _checkunique(iava: npt.ArrayLike) -> None: @@ -106,8 +106,8 @@ def __init__( # check if forceflat is needed and set it back to None otherwise if ndims > 2: if forceflat is not None: - logging.warning( - f"setting forceflat=None since len(dims)={len(dims)}>2. " + logger.warning( + f"Setting forceflat=None since len(dims)={len(dims)}>2. " f"PyLops will automatically detect whether to return " f"a 1d or nd array based on the shape of the input" f"array." diff --git a/pylops/signalprocessing/chirpradon2d.py b/pylops/signalprocessing/chirpradon2d.py index 3df8ae891..1831fe9de 100755 --- a/pylops/signalprocessing/chirpradon2d.py +++ b/pylops/signalprocessing/chirpradon2d.py @@ -1,7 +1,5 @@ __all__ = ["ChirpRadon2D"] -import logging - import numpy as np from pylops import LinearOperator @@ -10,8 +8,6 @@ from ._chirpradon2d import _chirp_radon_2d -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class ChirpRadon2D(LinearOperator): r"""2D Chirp Radon transform diff --git a/pylops/signalprocessing/chirpradon3d.py b/pylops/signalprocessing/chirpradon3d.py index 193d41be1..64779a995 100755 --- a/pylops/signalprocessing/chirpradon3d.py +++ b/pylops/signalprocessing/chirpradon3d.py @@ -1,7 +1,5 @@ __all__ = ["ChirpRadon3D"] -import logging - import numpy as np from pylops import LinearOperator @@ -16,8 +14,6 @@ if pyfftw_message is None: from ._chirpradon3d import _chirp_radon_3d_fftw -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class ChirpRadon3D(LinearOperator): r"""3D Chirp Radon transform diff --git a/pylops/signalprocessing/dwt.py b/pylops/signalprocessing/dwt.py index 8785f1acd..09c2bf37d 100644 --- a/pylops/signalprocessing/dwt.py +++ b/pylops/signalprocessing/dwt.py @@ -1,6 +1,5 @@ __all__ = ["DWT"] -import logging from math import ceil, log from typing import Union @@ -17,8 +16,6 @@ if pywt_message is None: import pywt -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def _checkwavelet(wavelet: str) -> None: """Check that wavelet belongs to pywt.wavelist""" diff --git a/pylops/signalprocessing/dwt2d.py b/pylops/signalprocessing/dwt2d.py index 3116cfdf8..88faf15f6 100644 --- a/pylops/signalprocessing/dwt2d.py +++ b/pylops/signalprocessing/dwt2d.py @@ -1,6 +1,5 @@ __all__ = ["DWT2D"] -import logging from math import ceil, log import numpy as np @@ -17,8 +16,6 @@ if pywt_message is None: import pywt -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class DWT2D(LinearOperator): """Two dimensional Wavelet operator. diff --git a/pylops/signalprocessing/dwtnd.py b/pylops/signalprocessing/dwtnd.py index af43bb0d8..f01c02136 100644 --- a/pylops/signalprocessing/dwtnd.py +++ b/pylops/signalprocessing/dwtnd.py @@ -1,6 +1,5 @@ __all__ = ["DWTND"] -import logging from math import ceil, log import numpy as np @@ -17,8 +16,6 @@ if pywt_message is None: import pywt -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class DWTND(LinearOperator): """N-dimensional Wavelet operator. diff --git a/pylops/signalprocessing/fft.py b/pylops/signalprocessing/fft.py index 1209106ef..fe853771d 100644 --- a/pylops/signalprocessing/fft.py +++ b/pylops/signalprocessing/fft.py @@ -20,7 +20,7 @@ if pyfftw_message is None: import pyfftw -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) class _FFT_numpy(_BaseFFT): @@ -581,7 +581,7 @@ def FFT( ) elif engine == "numpy" or (engine == "fftw" and pyfftw_message is not None): if engine == "fftw" and pyfftw_message is not None: - logging.warning(pyfftw_message) + logger.warning(pyfftw_message) f = _FFT_numpy( dims, axis=axis, diff --git a/pylops/signalprocessing/fft2d.py b/pylops/signalprocessing/fft2d.py index 7c4b0e951..5df5df34a 100644 --- a/pylops/signalprocessing/fft2d.py +++ b/pylops/signalprocessing/fft2d.py @@ -1,6 +1,5 @@ __all__ = ["FFT2D"] -import logging import warnings from typing import Dict, Optional, Sequence, Union @@ -13,8 +12,6 @@ from pylops.utils.decorators import reshaped from pylops.utils.typing import DTypeLike, InputDimsLike -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class _FFT2D_numpy(_BaseFFTND): """Two dimensional Fast-Fourier Transform using NumPy""" diff --git a/pylops/signalprocessing/fftnd.py b/pylops/signalprocessing/fftnd.py index 6379bcfb6..5608b8049 100644 --- a/pylops/signalprocessing/fftnd.py +++ b/pylops/signalprocessing/fftnd.py @@ -1,6 +1,5 @@ __all__ = ["FFTND"] -import logging import warnings from typing import Optional, Sequence, Union @@ -13,8 +12,6 @@ from pylops.utils.decorators import reshaped from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class _FFTND_numpy(_BaseFFTND): """N-dimensional Fast-Fourier Transform using NumPy""" diff --git a/pylops/signalprocessing/fourierradon2d.py b/pylops/signalprocessing/fourierradon2d.py index 37439dda3..3743fdfcf 100755 --- a/pylops/signalprocessing/fourierradon2d.py +++ b/pylops/signalprocessing/fourierradon2d.py @@ -1,6 +1,5 @@ __all__ = ["FourierRadon2D"] -import logging from typing import Optional, Tuple import numpy as np @@ -20,8 +19,6 @@ if jit_message is None and cupy_message is None: from ._fourierradon2d_cuda import _aradon_inner_2d_cuda, _radon_inner_2d_cuda -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class FourierRadon2D(LinearOperator): r"""2D Fourier Radon transform diff --git a/pylops/signalprocessing/fourierradon3d.py b/pylops/signalprocessing/fourierradon3d.py index 6b2c3c7d4..599d7d3c4 100755 --- a/pylops/signalprocessing/fourierradon3d.py +++ b/pylops/signalprocessing/fourierradon3d.py @@ -1,6 +1,5 @@ __all__ = ["FourierRadon3D"] -import logging from typing import Optional, Tuple import numpy as np @@ -20,8 +19,6 @@ if jit_message is None and cupy_message is None: from ._fourierradon3d_cuda import _aradon_inner_3d_cuda, _radon_inner_3d_cuda -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class FourierRadon3D(LinearOperator): r"""3D Fourier Radon transform diff --git a/pylops/signalprocessing/interp.py b/pylops/signalprocessing/interp.py index 1d60451fb..da2f6b374 100644 --- a/pylops/signalprocessing/interp.py +++ b/pylops/signalprocessing/interp.py @@ -1,6 +1,6 @@ __all__ = ["Interp"] -import logging +import warnings from typing import Tuple, Union import numpy as np @@ -12,8 +12,6 @@ from pylops.utils.backend import get_array_module from pylops.utils.typing import DTypeLike, InputDimsLike, IntNDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def _checkunique(iava: npt.ArrayLike) -> None: _, count = np.unique(iava, return_counts=True) @@ -54,8 +52,8 @@ def _linearinterp( # penultimate sample and raise a warning outside = iava >= lastsample - 1 if sum(outside) > 0: - logging.warning( - "at least one value is beyond penultimate sample, " + warnings.warn( + "At least one value is beyond the penultimate sample, " "forced to be at penultimate sample" ) iava[outside] = lastsample - 1 - 1e-10 diff --git a/pylops/signalprocessing/patch2d.py b/pylops/signalprocessing/patch2d.py index 86e496ec0..f7810319c 100644 --- a/pylops/signalprocessing/patch2d.py +++ b/pylops/signalprocessing/patch2d.py @@ -20,7 +20,7 @@ from pylops.utils.tapers import taper2d from pylops.utils.typing import InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def patch2d_design( @@ -53,8 +53,8 @@ def patch2d_design( nop : :obj:`tuple` Size of model in the transformed domain. verb : :obj:`bool`, optional - Verbosity flag. If ``verb==True``, print the data - and model windows start-end indices + *Deprecated*, will be removed in v3.0.0. Simply kept for + back-compatibility with previous implementation Returns ------- @@ -83,22 +83,21 @@ def patch2d_design( mwins_inends = ((mwin0_ins, mwin0_ends), (mwin1_ins, mwin1_ends)) # print information about patching - if verb: - logging.warning("%d-%d windows required...", nwins0, nwins1) - logging.warning( - "data wins - start:%s, end:%s / start:%s, end:%s", - dwin0_ins, - dwin0_ends, - dwin1_ins, - dwin1_ends, - ) - logging.warning( - "model wins - start:%s, end:%s / start:%s, end:%s", - mwin0_ins, - mwin0_ends, - mwin1_ins, - mwin1_ends, - ) + logger.info("%d-%d windows required...", nwins0, nwins1) + logger.info( + "Data wins - start:%s, end:%s / start:%s, end:%s", + dwin0_ins, + dwin0_ends, + dwin1_ins, + dwin1_ends, + ) + logger.info( + "Model wins - start:%s, end:%s / start:%s, end:%s", + mwin0_ins, + mwin0_ends, + mwin1_ins, + mwin1_ends, + ) return nwins, dims, mwins_inends, dwins_inends diff --git a/pylops/signalprocessing/patch3d.py b/pylops/signalprocessing/patch3d.py index ce3889d44..fb8b6d39f 100644 --- a/pylops/signalprocessing/patch3d.py +++ b/pylops/signalprocessing/patch3d.py @@ -20,7 +20,7 @@ from pylops.utils.tapers import tapernd from pylops.utils.typing import InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def patch3d_design( @@ -53,8 +53,8 @@ def patch3d_design( nop : :obj:`tuple` Size of model in the transformed domain. verb : :obj:`bool`, optional - Verbosity flag. If ``verb==True``, print the data - and model windows start-end indices + *Deprecated*, will be removed in v3.0.0. Simply kept for + back-compatibility with previous implementation Returns ------- @@ -94,26 +94,25 @@ def patch3d_design( ) # print information about patching - if verb: - logging.warning("%d-%d-%d windows required...", nwins0, nwins1, nwins2) - logging.warning( - "data wins - start:%s, end:%s / start:%s, end:%s / start:%s, end:%s", - dwin0_ins, - dwin0_ends, - dwin1_ins, - dwin1_ends, - dwin2_ins, - dwin2_ends, - ) - logging.warning( - "model wins - start:%s, end:%s / start:%s, end:%s / start:%s, end:%s", - mwin0_ins, - mwin0_ends, - mwin1_ins, - mwin1_ends, - mwin2_ins, - mwin2_ends, - ) + logger.info("%d-%d-%d windows required...", nwins0, nwins1, nwins2) + logger.info( + "Data wins - start:%s, end:%s / start:%s, end:%s / start:%s, end:%s", + dwin0_ins, + dwin0_ends, + dwin1_ins, + dwin1_ends, + dwin2_ins, + dwin2_ends, + ) + logger.info( + "Model wins - start:%s, end:%s / start:%s, end:%s / start:%s, end:%s", + mwin0_ins, + mwin0_ends, + mwin1_ins, + mwin1_ends, + mwin2_ins, + mwin2_ends, + ) return nwins, dims, mwins_inends, dwins_inends diff --git a/pylops/signalprocessing/radon2d.py b/pylops/signalprocessing/radon2d.py index a76696b44..e5cf5524d 100644 --- a/pylops/signalprocessing/radon2d.py +++ b/pylops/signalprocessing/radon2d.py @@ -1,6 +1,5 @@ __all__ = ["Radon2D"] -import logging from typing import Callable, Optional, Tuple import numpy as np @@ -22,8 +21,6 @@ _parabolic_numba, ) -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def _linear( x: NDArray, diff --git a/pylops/signalprocessing/radon3d.py b/pylops/signalprocessing/radon3d.py index 939c14bab..20ecc2da4 100644 --- a/pylops/signalprocessing/radon3d.py +++ b/pylops/signalprocessing/radon3d.py @@ -1,6 +1,5 @@ __all__ = ["Radon3D"] -import logging from typing import Callable, Optional, Tuple import numpy as np @@ -22,8 +21,6 @@ _parabolic_numba, ) -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def _linear( y: NDArray, diff --git a/pylops/signalprocessing/sliding1d.py b/pylops/signalprocessing/sliding1d.py index 1726615ad..21874c528 100644 --- a/pylops/signalprocessing/sliding1d.py +++ b/pylops/signalprocessing/sliding1d.py @@ -20,7 +20,7 @@ from pylops.utils.tapers import taper from pylops.utils.typing import InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def sliding1d_design( @@ -48,8 +48,8 @@ def sliding1d_design( nop : :obj:`tuple` Size of model in the transformed domain. verb : :obj:`bool`, optional - Verbosity flag. If ``verb==True``, print the data - and model windows start-end indices + *Deprecated*, will be removed in v3.0.0. Simply kept for + back-compatibility with previous implementation Returns ------- @@ -74,18 +74,17 @@ def sliding1d_design( mwins_inends = (mwin_ins, mwin_ends) # print information about patching - if verb: - logging.warning("%d windows required...", nwins) - logging.warning( - "data wins - start:%s, end:%s", - dwin_ins, - dwin_ends, - ) - logging.warning( - "model wins - start:%s, end:%s", - mwin_ins, - mwin_ends, - ) + logger.info("%d windows required...", nwins) + logger.info( + "Data wins - start:%s, end:%s", + dwin_ins, + dwin_ends, + ) + logger.info( + "Model wins - start:%s, end:%s", + mwin_ins, + mwin_ends, + ) return nwins, dim, mwins_inends, dwins_inends diff --git a/pylops/signalprocessing/sliding2d.py b/pylops/signalprocessing/sliding2d.py index f6cbce9c6..5c7b5cc23 100644 --- a/pylops/signalprocessing/sliding2d.py +++ b/pylops/signalprocessing/sliding2d.py @@ -19,7 +19,7 @@ from pylops.utils.tapers import taper2d from pylops.utils.typing import InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def _slidingsteps( @@ -80,8 +80,8 @@ def sliding2d_design( nop : :obj:`tuple` Size of model in the transformed domain. verb : :obj:`bool`, optional - Verbosity flag. If ``verb==True``, print the data - and model windows start-end indices + *Deprecated*, will be removed in v3.0.0. Simply kept for + back-compatibility with previous implementation Returns ------- @@ -106,18 +106,17 @@ def sliding2d_design( mwins_inends = (mwin_ins, mwin_ends) # print information about patching - if verb: - logging.warning("%d windows required...", nwins) - logging.warning( - "data wins - start:%s, end:%s", - dwin_ins, - dwin_ends, - ) - logging.warning( - "model wins - start:%s, end:%s", - mwin_ins, - mwin_ends, - ) + logger.info("%d windows required...", nwins) + logger.info( + "Data wins - start:%s, end:%s", + dwin_ins, + dwin_ends, + ) + logger.info( + "Model wins - start:%s, end:%s", + mwin_ins, + mwin_ends, + ) return nwins, dims, mwins_inends, dwins_inends diff --git a/pylops/signalprocessing/sliding3d.py b/pylops/signalprocessing/sliding3d.py index bf6b773d4..c0af2526a 100644 --- a/pylops/signalprocessing/sliding3d.py +++ b/pylops/signalprocessing/sliding3d.py @@ -20,7 +20,7 @@ from pylops.utils.tapers import taper3d from pylops.utils.typing import InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def sliding3d_design( @@ -53,8 +53,8 @@ def sliding3d_design( nop : :obj:`tuple` Size of model in the transformed domain. verb : :obj:`bool`, optional - Verbosity flag. If ``verb==True``, print the data - and model windows start-end indices + *Deprecated*, will be removed in v3.0.0. Simply kept for + back-compatibility with previous implementation Returns ------- @@ -83,22 +83,21 @@ def sliding3d_design( mwins_inends = ((mwin0_ins, mwin0_ends), (mwin1_ins, mwin1_ends)) # print information about patching - if verb: - logging.warning("%d-%d windows required...", nwins0, nwins1) - logging.warning( - "data wins - start:%s, end:%s / start:%s, end:%s", - dwin0_ins, - dwin0_ends, - dwin1_ins, - dwin1_ends, - ) - logging.warning( - "model wins - start:%s, end:%s / start:%s, end:%s", - mwin0_ins, - mwin0_ends, - mwin1_ins, - mwin1_ends, - ) + logger.info("%d-%d windows required...", nwins0, nwins1) + logger.info( + "Data wins - start:%s, end:%s / start:%s, end:%s", + dwin0_ins, + dwin0_ends, + dwin1_ins, + dwin1_ends, + ) + logger.info( + "Model wins - start:%s, end:%s / start:%s, end:%s", + mwin0_ins, + mwin0_ends, + mwin1_ins, + mwin1_ends, + ) return nwins, dims, mwins_inends, dwins_inends diff --git a/pylops/utils/describe.py b/pylops/utils/describe.py index 0b93d8b29..f1a68f413 100644 --- a/pylops/utils/describe.py +++ b/pylops/utils/describe.py @@ -3,7 +3,6 @@ import logging import random import string - from typing import List, Set, Union from pylops import LinearOperator @@ -35,7 +34,7 @@ BlockDiag, ) -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def _in_notebook() -> bool: @@ -95,7 +94,7 @@ def _assign_name(Op, Ops, names: List[str]) -> str: while proposedname in names: proposedname = random.choice(string.ascii_letters).upper() + suffix name = proposedname - logging.warning( + logger.warning( f"The user has used the same name {origname} for two distinct operators, " f"changing name of operator {type(Op).__name__} to {name}..." ) diff --git a/pylops/utils/wavelets.py b/pylops/utils/wavelets.py index d81f9ae8d..5ec505551 100644 --- a/pylops/utils/wavelets.py +++ b/pylops/utils/wavelets.py @@ -18,7 +18,7 @@ def _tcrop(t: npt.ArrayLike) -> npt.ArrayLike: """Crop time axis with even number of samples""" if len(t) % 2 == 0: t = t[:-1] - warnings.warn("one sample removed from time axis...") + warnings.warn("One sample removed from time axis...") return t diff --git a/pylops/waveeqprocessing/kirchhoff.py b/pylops/waveeqprocessing/kirchhoff.py index a3aa9a22b..66105bd95 100644 --- a/pylops/waveeqprocessing/kirchhoff.py +++ b/pylops/waveeqprocessing/kirchhoff.py @@ -34,7 +34,7 @@ else: prange = range -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) class Kirchhoff(LinearOperator): @@ -1034,7 +1034,7 @@ def _register_multiplications(self, engine: str) -> None: self._kirch_rmatvec = self.cuda_helper._rmatvec_cuda else: if engine == "numba" and jit_message is not None: - logging.warning(jit_message) + logger.warning(jit_message) if self.dynamic and self.travsrcrec: self._kirch_matvec = self._ampsrcrec_kirch_matvec self._kirch_rmatvec = self._ampsrcrec_kirch_rmatvec diff --git a/pylops/waveeqprocessing/lsm.py b/pylops/waveeqprocessing/lsm.py index b173997ff..62a60f880 100644 --- a/pylops/waveeqprocessing/lsm.py +++ b/pylops/waveeqprocessing/lsm.py @@ -1,6 +1,5 @@ __all__ = ["LSM"] -import logging from typing import Callable, Optional from scipy.sparse.linalg import lsqr @@ -10,8 +9,6 @@ from pylops.waveeqprocessing.kirchhoff import Kirchhoff from pylops.waveeqprocessing.twoway import AcousticWave2D -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class LSM: r"""Least-squares Migration (LSM). diff --git a/pylops/waveeqprocessing/marchenko.py b/pylops/waveeqprocessing/marchenko.py index d810c70b8..6def6d981 100644 --- a/pylops/waveeqprocessing/marchenko.py +++ b/pylops/waveeqprocessing/marchenko.py @@ -15,7 +15,7 @@ from pylops.utils.typing import DTypeLike, NDArray from pylops.waveeqprocessing.mdd import MDC -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) +logger = logging.getLogger(__name__) def directwave( @@ -168,8 +168,8 @@ class Marchenko: Raises ------ - TypeError - If ``t`` is not :obj:`numpy.ndarray`. + ValueError + If ``nt`` is not provided when ``R`` is in time domain. See Also -------- @@ -265,17 +265,18 @@ def __init__( self.ns, self.nr, self.nt = R.shape self.nfmax = nfmax else: + if nt is None: + raise ValueError("nt must be provided as R is in frequency") self.ns, self.nr, self.nfmax = R.shape self.nt = nt - if nt is None: - logging.error("nt must be provided as R is in frequency") + self.nt2 = int(2 * self.nt - 1) self.t = np.arange(self.nt) * self.dt # Fix nfmax to be at maximum equal to half of the size of fft samples if self.nfmax is None or self.nfmax > np.ceil((self.nt2 + 1) / 2): self.nfmax = int(np.ceil((self.nt2 + 1) / 2)) - logging.warning("nfmax set equal to (nt+1)/2=%d", self.nfmax) + logger.warning("nfmax set equal to (nt+1)/2=%d", self.nfmax) # Add negative time to reflection data and convert to frequency if not np.iscomplexobj(R): @@ -439,10 +440,6 @@ def apply_onepoint( ).T G0 = to_cupy_conditional(self.Rtwosided_fft, G0) else: - logging.error( - "wav and/or nfft are not provided. " - "Provide either G0 or wav and nfft..." - ) raise ValueError( "wav and/or nfft are not provided. " "Provide either G0 or wav and nfft..." @@ -657,10 +654,6 @@ def apply_multiplepoints( ).T G0 = to_cupy_conditional(self.Rtwosided_fft, G0) else: - logging.error( - "wav and/or nfft are not provided. " - "Provide either G0 or wav and nfft..." - ) raise ValueError( "wav and/or nfft are not provided. " "Provide either G0 or wav and nfft..." diff --git a/pylops/waveeqprocessing/mdd.py b/pylops/waveeqprocessing/mdd.py index 02f61b3d3..5eed9a7b8 100644 --- a/pylops/waveeqprocessing/mdd.py +++ b/pylops/waveeqprocessing/mdd.py @@ -23,6 +23,8 @@ ) from pylops.utils.typing import NDArray +logger = logging.getLogger(__name__) + def _MDC( G: NDArray, @@ -87,7 +89,7 @@ def _MDC( nfft = int(np.ceil((nt + 1) / 2)) if nfmax > nfft: nfmax = nfft - logging.warning("nfmax set equal to ceil[(nt+1)/2=%d]", nfmax) + logger.warning("nfmax set equal to ceil[(nt+1)/2=%d]", nfmax) Fop = _FFT( dims=(nt, nr, nv), @@ -401,7 +403,7 @@ def MDD( # Fix nfmax to be at maximum equal to half of the size of fft samples if nfmax is None or nfmax > nfmax_allowed: nfmax = nfmax_allowed - logging.warning("nfmax set equal to ceil[(nt+1)/2=%d]", nfmax) + logger.warning("nfmax set equal to ceil[(nt+1)/2=%d]", nfmax) # Add negative part to data and model if twosided and add_negative: diff --git a/pylops/waveeqprocessing/oneway.py b/pylops/waveeqprocessing/oneway.py index ae7aa3c92..b3f5bf229 100644 --- a/pylops/waveeqprocessing/oneway.py +++ b/pylops/waveeqprocessing/oneway.py @@ -3,8 +3,6 @@ "Deghosting", ] - -import logging from typing import Callable, Optional, Sequence, Tuple, Union import numpy as np @@ -17,8 +15,6 @@ from pylops.utils.tapers import taper2d, taper3d from pylops.utils.typing import DTypeLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - class _PhaseShift(LinearOperator): """Phase shift operator in frequency-wavenumber domain diff --git a/pylops/waveeqprocessing/seismicinterpolation.py b/pylops/waveeqprocessing/seismicinterpolation.py index 3d18187e9..23481c4e5 100644 --- a/pylops/waveeqprocessing/seismicinterpolation.py +++ b/pylops/waveeqprocessing/seismicinterpolation.py @@ -1,6 +1,5 @@ __all__ = ["SeismicInterpolation"] -import logging from typing import List, Optional, Sequence, Tuple, Union import numpy as np @@ -22,8 +21,6 @@ from pylops.utils.dottest import dottest as Dottest from pylops.utils.typing import InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def SeismicInterpolation( data: NDArray, diff --git a/pylops/waveeqprocessing/wavedecomposition.py b/pylops/waveeqprocessing/wavedecomposition.py index c6e229441..4556b49f9 100644 --- a/pylops/waveeqprocessing/wavedecomposition.py +++ b/pylops/waveeqprocessing/wavedecomposition.py @@ -5,7 +5,6 @@ "WavefieldDecomposition", ] -import logging from typing import Callable, Optional, Sequence, Tuple, Union import numpy as np @@ -18,8 +17,6 @@ from pylops.utils.backend import get_array_module, get_module, get_module_name from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray -logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING) - def _filter_obliquity( OBL: NDArray, From 8e86f69fd31fd2b98361b1f9d492ada9f40d4f9f Mon Sep 17 00:00:00 2001 From: mrava87 Date: Mon, 21 Jul 2025 19:29:10 +0100 Subject: [PATCH 2/2] minor: fix flake8 --- pylops/signalprocessing/_baseffts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pylops/signalprocessing/_baseffts.py b/pylops/signalprocessing/_baseffts.py index 4f7cc9093..367174131 100644 --- a/pylops/signalprocessing/_baseffts.py +++ b/pylops/signalprocessing/_baseffts.py @@ -1,4 +1,3 @@ -import logging import warnings from enum import Enum, auto from typing import Optional, Sequence, Union