diff --git a/chemotools/utils/_finite_differences.py b/chemotools/utils/_finite_differences.py index 93a4719..066776e 100644 --- a/chemotools/utils/_finite_differences.py +++ b/chemotools/utils/_finite_differences.py @@ -377,7 +377,7 @@ def estimate_noise_stddev( series: np.ndarray, differences: int = 6, diff_accuracy: int = 2, - window_size: Optional[int] = None, + window_length: Optional[int] = None, extrapolator: Callable[..., np.ndarray] = np.pad, extrapolator_args: Tuple[Any, ...] = ("reflect",), extrapolator_kwargs: Optional[Dict[str, Any]] = None, @@ -408,7 +408,7 @@ def estimate_noise_stddev( integer ``>= 2``. Higher values will enhance the effect of outliers that will corrupt the noise estimation of their neighborhood. - window_size : int or None, default=None + window_length : int or None, default=None The odd window size around a datapoint to estimate its local noise standard deviation. Higher values will lead to a smoother noise standard deviation estimate by @@ -420,7 +420,7 @@ def estimate_noise_stddev( extrapolator : callable, default=np.pad The extrapolator function that is used to pad the series before the finite differences and the median filter are applied. It will pad the signal with - ``pad_width = (diff_kernel_size // 2) + (window_size // 2)`` elements on each + ``pad_width = (diff_kernel_size // 2) + (window_length // 2)`` elements on each side where ``diff_kernel_size`` is the size of the central finite differences kernel (see the Notes for details). It has to be a callable with the following signature: @@ -434,7 +434,7 @@ def estimate_noise_stddev( ) ``` - If ``window_size`` is ``None``, only the central finite differences kernel is + If ``window_length`` is ``None``, only the central finite differences kernel is considered. By default, the signal is padded by reflecting ``series`` at the edges on either side, but of course the quality of the noise estimation can be improved by using @@ -479,7 +479,7 @@ def estimate_noise_stddev( ValueError If ``diff_accuracy`` is not an even integer ``>= 2``. ValueError - If ``window_size`` is below 1. + If ``window_length`` is below 1. References @@ -520,17 +520,17 @@ def estimate_noise_stddev( # NOTE: the difference order and accuracy are by the central finite differences # kernel function # window size - if window_size is not None: + if window_length is not None: check_scalar( - window_size, - name="window_size", + window_length, + name="window_length", target_type=Integral, min_val=1, include_boundaries="left", ) - if window_size % 2 == 0: + if window_length % 2 == 0: raise ValueError( - f"Got window_size = {window_size}, expected an odd integer." + f"Got window_length = {window_length}, expected an odd integer." ) # power @@ -560,10 +560,10 @@ def estimate_noise_stddev( f"size)." ) - if window_size is not None: - if series.size < window_size: + if window_length is not None: + if series.size < window_length: raise ValueError( - f"Got series.size = {series.size}, must be >= {window_size} (window " + f"Got series.size = {series.size}, must be >= {window_length} (window " "size)." ) @@ -578,7 +578,7 @@ def estimate_noise_stddev( # the signal is extrapolated to avoid edge effects pad_width = diff_kernel.size // 2 - pad_width += 0 if window_size is None else window_size // 2 + pad_width += 0 if window_length is None else window_length // 2 series_extrap = extrapolator( series, pad_width, @@ -595,7 +595,7 @@ def estimate_noise_stddev( # ... and the median filter is applied to theses differences prefactor = _MAD_PREFACTOR / np.linalg.norm(diff_kernel) # Case 1: the global noise standard deviation is estimated - if window_size is None: + if window_length is None: noise_stddev = np.full_like( series, fill_value=prefactor * np.median(abs_diff_series), @@ -603,14 +603,14 @@ def estimate_noise_stddev( # Case 2: the local noise standard deviation is estimated else: - half_window_size = window_size // 2 + half_window_length = window_length // 2 noise_stddev = ( prefactor * median_filter( abs_diff_series, - size=window_size, + size=window_length, mode="constant", - )[half_window_size : size_after_diff - half_window_size] + )[half_window_length : size_after_diff - half_window_length] ) # the minimum-bounded noise standard deviation is raised to the power diff --git a/tests/fixtures.py b/tests/fixtures.py index 8007ba4..81597c9 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -209,11 +209,11 @@ def noise_level_estimation_refs() -> List[NoiseEstimationReference]: row = data[row_idx, ::] # if the window size is 0, it is set to None because this indicates that the # global noise level is to be estimated rather than a local one - window_size = int(row[0]) - window_size = window_size if window_size > 0 else None + window_length = int(row[0]) + window_length = window_length if window_length > 0 else None noise_level_refs.append( NoiseEstimationReference( - window_size=window_size, + window_length=window_length, min_noise_level=row[1], differences=round(row[2]), accuracy=round(row[3]), diff --git a/tests/test_for_utils/test_finite_differences.py b/tests/test_for_utils/test_finite_differences.py index 040a39d..424ba13 100644 --- a/tests/test_for_utils/test_finite_differences.py +++ b/tests/test_for_utils/test_finite_differences.py @@ -194,7 +194,7 @@ def test_squ_fw_fin_diff_mat_cho_banded_transpose_first( @pytest.mark.parametrize( - "series, differences, accuracy, window_size, power, stddev_min", + "series, differences, accuracy, window_length, power, stddev_min", [ ( # Number 0 series is too small for difference kernel np.arange(start=0, stop=5), @@ -338,7 +338,7 @@ def test_estimate_noise_stddev_invalid_input( series: np.ndarray, differences: int, accuracy: int, - window_size: Optional[int], + window_length: Optional[int], power: int, stddev_min: float, ) -> None: @@ -363,7 +363,7 @@ def test_estimate_noise_stddev_invalid_input( series=series, differences=differences, diff_accuracy=accuracy, - window_size=window_size, + window_length=window_length, power=power, # type: ignore stddev_min=stddev_min, ) @@ -388,7 +388,7 @@ def test_noise_level_estimation( series=noise_level_estimation_signal, differences=ref.differences, diff_accuracy=ref.accuracy, - window_size=ref.window_size, + window_length=ref.window_length, stddev_min=ref.min_noise_level, ) # then, the noise level itself is compared to the reference in a quite strict @@ -398,7 +398,7 @@ def test_noise_level_estimation( assert np.allclose(noise_level, ref.noise_level, rtol=1e-12), ( f"Original noise level differs from reference noise for differences " f"{ref.differences} with accuracy {ref.accuracy} and window size " - f"{ref.window_size} given a minimum standard deviation of " + f"{ref.window_length} given a minimum standard deviation of " f"{ref.min_noise_level}." ) @@ -409,7 +409,7 @@ def test_noise_level_estimation( series=noise_level_estimation_signal, differences=ref.differences, diff_accuracy=ref.accuracy, - window_size=ref.window_size, + window_length=ref.window_length, stddev_min=ref.min_noise_level, power=power, ) @@ -420,7 +420,7 @@ def test_noise_level_estimation( ), ( f"Raised noise level differs from reference noise for differences " f"{ref.differences} with accuracy {ref.accuracy} and window size " - f"{ref.window_size} given a minimum standard deviation of " + f"{ref.window_length} given a minimum standard deviation of " f"{ref.min_noise_level} and a power of {power}." ) diff --git a/tests/test_for_utils/utils_models.py b/tests/test_for_utils/utils_models.py index 1a4c85b..cc51fcc 100644 --- a/tests/test_for_utils/utils_models.py +++ b/tests/test_for_utils/utils_models.py @@ -41,7 +41,7 @@ class NoiseEstimationReference: """ - window_size: Optional[int] + window_length: Optional[int] min_noise_level: float differences: int accuracy: int