Skip to content

Commit

Permalink
Revert "refactor: renamed window_size to window_length"
Browse files Browse the repository at this point in the history
This reverts commit e6e8405.
  • Loading branch information
MothNik committed May 24, 2024
1 parent 4a1be7e commit 109be51
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
36 changes: 18 additions & 18 deletions chemotools/utils/_finite_differences.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def estimate_noise_stddev(
series: np.ndarray,
differences: int = 6,
diff_accuracy: int = 2,
window_length: Optional[int] = None,
window_size: Optional[int] = None,
extrapolator: Callable[..., np.ndarray] = np.pad,
extrapolator_args: Tuple[Any, ...] = ("reflect",),
extrapolator_kwargs: Optional[Dict[str, Any]] = None,
Expand Down Expand Up @@ -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_length : int or None, default=None
window_size : 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
Expand All @@ -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_length // 2)`` elements on each
``pad_width = (diff_kernel_size // 2) + (window_size // 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:
Expand All @@ -434,7 +434,7 @@ def estimate_noise_stddev(
)
```
If ``window_length`` is ``None``, only the central finite differences kernel is
If ``window_size`` 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
Expand Down Expand Up @@ -479,7 +479,7 @@ def estimate_noise_stddev(
ValueError
If ``diff_accuracy`` is not an even integer ``>= 2``.
ValueError
If ``window_length`` is below 1.
If ``window_size`` is below 1.
References
Expand Down Expand Up @@ -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_length is not None:
if window_size is not None:
check_scalar(
window_length,
name="window_length",
window_size,
name="window_size",
target_type=Integral,
min_val=1,
include_boundaries="left",
)
if window_length % 2 == 0:
if window_size % 2 == 0:
raise ValueError(
f"Got window_length = {window_length}, expected an odd integer."
f"Got window_size = {window_size}, expected an odd integer."
)

# power
Expand Down Expand Up @@ -560,10 +560,10 @@ def estimate_noise_stddev(
f"size)."
)

if window_length is not None:
if series.size < window_length:
if window_size is not None:
if series.size < window_size:
raise ValueError(
f"Got series.size = {series.size}, must be >= {window_length} (window "
f"Got series.size = {series.size}, must be >= {window_size} (window "
"size)."
)

Expand All @@ -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_length is None else window_length // 2
pad_width += 0 if window_size is None else window_size // 2
series_extrap = extrapolator(
series,
pad_width,
Expand All @@ -595,22 +595,22 @@ 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_length is None:
if window_size is None:
noise_stddev = np.full_like(
series,
fill_value=prefactor * np.median(abs_diff_series),
)

# Case 2: the local noise standard deviation is estimated
else:
half_window_length = window_length // 2
half_window_size = window_size // 2
noise_stddev = (
prefactor
* median_filter(
abs_diff_series,
size=window_length,
size=window_size,
mode="constant",
)[half_window_length : size_after_diff - half_window_length]
)[half_window_size : size_after_diff - half_window_size]
)

# the minimum-bounded noise standard deviation is raised to the power
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_length = int(row[0])
window_length = window_length if window_length > 0 else None
window_size = int(row[0])
window_size = window_size if window_size > 0 else None
noise_level_refs.append(
NoiseEstimationReference(
window_length=window_length,
window_size=window_size,
min_noise_level=row[1],
differences=round(row[2]),
accuracy=round(row[3]),
Expand Down
14 changes: 7 additions & 7 deletions tests/test_for_utils/test_finite_differences.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_squ_fw_fin_diff_mat_cho_banded_transpose_first(


@pytest.mark.parametrize(
"series, differences, accuracy, window_length, power, stddev_min",
"series, differences, accuracy, window_size, power, stddev_min",
[
( # Number 0 series is too small for difference kernel
np.arange(start=0, stop=5),
Expand Down Expand Up @@ -338,7 +338,7 @@ def test_estimate_noise_stddev_invalid_input(
series: np.ndarray,
differences: int,
accuracy: int,
window_length: Optional[int],
window_size: Optional[int],
power: int,
stddev_min: float,
) -> None:
Expand All @@ -363,7 +363,7 @@ def test_estimate_noise_stddev_invalid_input(
series=series,
differences=differences,
diff_accuracy=accuracy,
window_length=window_length,
window_size=window_size,
power=power, # type: ignore
stddev_min=stddev_min,
)
Expand All @@ -388,7 +388,7 @@ def test_noise_level_estimation(
series=noise_level_estimation_signal,
differences=ref.differences,
diff_accuracy=ref.accuracy,
window_length=ref.window_length,
window_size=ref.window_size,
stddev_min=ref.min_noise_level,
)
# then, the noise level itself is compared to the reference in a quite strict
Expand All @@ -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_length} given a minimum standard deviation of "
f"{ref.window_size} given a minimum standard deviation of "
f"{ref.min_noise_level}."
)

Expand All @@ -409,7 +409,7 @@ def test_noise_level_estimation(
series=noise_level_estimation_signal,
differences=ref.differences,
diff_accuracy=ref.accuracy,
window_length=ref.window_length,
window_size=ref.window_size,
stddev_min=ref.min_noise_level,
power=power,
)
Expand All @@ -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_length} given a minimum standard deviation of "
f"{ref.window_size} given a minimum standard deviation of "
f"{ref.min_noise_level} and a power of {power}."
)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_for_utils/utils_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NoiseEstimationReference:
"""

window_length: Optional[int]
window_size: Optional[int]
min_noise_level: float
differences: int
accuracy: int
Expand Down

0 comments on commit 109be51

Please sign in to comment.