Skip to content

Commit

Permalink
Merge pull request #413 from claritychallenge/jpb/support-for-python-312
Browse files Browse the repository at this point in the history
Jpb/support for python 312
  • Loading branch information
groadabike authored Sep 17, 2024
2 parents a526a3a + 666bb10 commit 649c76c
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python
Expand Down
5 changes: 2 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
rev: v3.17.0
hooks:
- id: pyupgrade
args: [--py38-plus]
args: [--py39-plus]

- repo: https://github.com/psf/black
rev: 24.8.0
Expand Down Expand Up @@ -71,8 +71,7 @@ repos:
args:
- --explicit-package-bases
additional_dependencies:
- 'types-PyYAML'

- "types-PyYAML"

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
Expand Down
6 changes: 3 additions & 3 deletions clarity/evaluator/msbg/msbg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import numpy as np
import scipy
import scipy.signal
from numpy import ndarray
from scipy.signal.windows import hamming, kaiser

# measure rms parameters
WIN_SECS: Final = 0.01
Expand Down Expand Up @@ -167,7 +167,7 @@ def firwin2(
order = n_taps - 1

if window_type == "kaiser":
window_shape = scipy.signal.kaiser(n_taps, window_param)
window_shape = kaiser(n_taps, window_param)

if window_shape is None:
filter_coef, _ = fir2(order, frequencies, filter_gains)
Expand Down Expand Up @@ -203,7 +203,7 @@ def fir2(
filter_length += 1

if window_shape is None:
window_shape = scipy.signal.hamming(filter_length)
window_shape = hamming(filter_length)

n_interpolate = (
2 ** np.ceil(math.log(filter_length) / math.log(2.0))
Expand Down
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import sys
from importlib.metadata import version
from typing import Dict

# -*- coding: utf-8 -*-
#
Expand Down Expand Up @@ -157,7 +156,7 @@

# -- Options for LaTeX output ------------------------------------------------

latex_elements: Dict[str, str] = {
latex_elements: dict[str, str] = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
Expand Down
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ classifiers = [
"Natural Language :: English",
]
keywords = ["hearing", "signal processing", "clarity challenge"]
requires-python = ">=3.8"
requires-python = ">=3.9"
dependencies = [
"audioread>=2.1.9",
"gdown",
Expand All @@ -30,20 +30,20 @@ dependencies = [
"importlib-metadata",
"librosa>=0.8.1",
"matplotlib",
"numba>=0.57.0rc",
"numpy>=1.21.6",
"numba>=0.60",
"numpy>=2",
"omegaconf>=2.1.1",
"pandas>=1.3.5",
"pandas>=2.2.2",
"pyflac",
"pyloudnorm>=0.1.0",
"pystoi",
"pytorch-lightning",
"resampy",
"safetensors>=0.4.3",
"scikit-learn>=1.0.2",
"scipy>=1.7.3, <1.13.0",
"scipy>=1.7.3",
"SoundFile>=0.10.3.post1",
"soxr",
"soxr>=0.4",
"torch>=2",
"torchaudio",
"tqdm>=4.62.3",
Expand Down
3 changes: 1 addition & 2 deletions recipes/cec2/baseline/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging
from pathlib import Path
from typing import Dict

import hydra
import numpy as np
Expand All @@ -17,7 +16,7 @@
logger = logging.getLogger(__name__)


def read_csv_scores(file: Path) -> Dict[str, float]:
def read_csv_scores(file: Path) -> dict[str, float]:
score_dict = {}
with file.open("r", encoding="utf-8") as fp:
reader = csv.reader(fp)
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def _random_matrix(seed: int | None = None, size=(100, 100)) -> np.ndarray:
return _random_matrix


@pytest.fixture
def abs_tolerance():
"""Fixture for absolute tolerance value."""
return 1e-7


@pytest.fixture
def rel_tolerance():
"""Fixture for relative tolerance value."""
return 1e-7


def pytest_configure() -> None:
"""Configure custom variables for pytest.
Expand Down
20 changes: 11 additions & 9 deletions tests/enhancer/multiband_compressor/test_multiband_compresor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ def test_compressor_initialization(default_compressor):
assert default_compressor.sample_rate == 44100.0


def test_compressor_signal_processing(default_compressor):
def test_compressor_signal_processing(default_compressor, rel_tolerance, abs_tolerance):
"""Test the signal processing of the Compressor class."""
input_signal = np.array([[1, 2, 3, 4, 5]])
output_signal = default_compressor(input_signal)
# Add assertions to verify the output signal
assert isinstance(output_signal, np.ndarray)
assert len(output_signal) == len(input_signal)
assert np.sum(output_signal) == pytest.approx(
15, rel=pytest.rel_tolerance, abs=pytest.abs_tolerance
15, rel=rel_tolerance, abs=abs_tolerance
)


Expand All @@ -49,7 +49,9 @@ def test_multiband_compressor_onefreq():
assert multiband_compressor.xover_freqs.shape[0] == 1


def test_multiband_compressor_initialization(default_multiband_compressor):
def test_multiband_compressor_initialization(
default_multiband_compressor, rel_tolerance, abs_tolerance
):
"""Test the initialization of the MultibandCompressor class."""
assert np.sum(default_multiband_compressor.xover_freqs) == pytest.approx(
np.sum(
Expand All @@ -64,14 +66,14 @@ def test_multiband_compressor_initialization(default_multiband_compressor):
)
* np.sqrt(2)
),
rel=pytest.rel_tolerance,
abs=pytest.abs_tolerance,
rel=rel_tolerance,
abs=abs_tolerance,
)

assert np.sum(default_multiband_compressor.xover_freqs) == pytest.approx(
np.sum(np.array(default_multiband_compressor.xover_freqs)),
rel=pytest.rel_tolerance,
abs=pytest.abs_tolerance,
rel=rel_tolerance,
abs=abs_tolerance,
)
assert default_multiband_compressor.sample_rate == 44100.0
assert default_multiband_compressor.num_compressors == 6
Expand All @@ -91,7 +93,7 @@ def test_multiband_compressor_set_compressors(default_multiband_compressor):
assert len(default_multiband_compressor.compressor) == 6


def test_multiband_compressor_call(default_multiband_compressor):
def test_multiband_compressor_call(default_multiband_compressor, rel_tolerance):
"""Test the __call__ method of the MultibandCompressor class."""
np.random.seed(0)
signal = np.random.rand(1000)
Expand All @@ -106,7 +108,7 @@ def test_multiband_compressor_call(default_multiband_compressor):
assert bands.shape[0] == 6

assert np.sum(compressed_signal) == pytest.approx(
441.09490986, rel=pytest.rel_tolerance, abs=pytest.abs_tolerance
441.09490986, rel=rel_tolerance, abs=0.0005
)


Expand Down
24 changes: 9 additions & 15 deletions tests/recipes/cpc2/baseline/test_evaluate_cpc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
@pytest.mark.parametrize(
"x, y, expected", [([1, 2, 3], [1, 2, 3], 0), ([0], [1], 1), ([1, 1, 1], [1], 0)]
)
def test_rmse_score_ok(x, y, expected):
def test_rmse_score_ok(x, y, expected, rel_tolerance, abs_tolerance):
"""Test the function rmse_score valid inputs"""
assert rmse_score(np.array(x), np.array(y)) == pytest.approx(
expected, rel=pytest.rel_tolerance, abs=pytest.abs_tolerance
expected, rel=rel_tolerance, abs=abs_tolerance
)


Expand All @@ -47,10 +47,10 @@ def test_rmse_score_error(x, y, expected):
@pytest.mark.parametrize(
"x, y, expected", [([1, 2, 3], [1, 2, 3], 1), ([1, -1], [-1, 1], -1)]
)
def test_ncc_score_ok(x, y, expected):
def test_ncc_score_ok(x, y, expected, rel_tolerance, abs_tolerance):
"""Test the function ncc_score valid inputs"""
assert ncc_score(np.array(x), np.array(y)) == pytest.approx(
expected, rel=pytest.rel_tolerance, abs=pytest.abs_tolerance
expected, rel=rel_tolerance, abs=abs_tolerance
)


Expand All @@ -67,10 +67,10 @@ def test_ncc_score_error(x, y, expected):
@pytest.mark.parametrize(
"x, y, expected", [([1, 2, 3], [1, 2, 3], 1), ([1, -1], [-1, 1], -1)]
)
def test_kt_score_ok(x, y, expected):
def test_kt_score_ok(x, y, expected, rel_tolerance, abs_tolerance):
"""Test the function kt_score valid inputs"""
assert kt_score(np.array(x), np.array(y)) == pytest.approx(
expected, rel=pytest.rel_tolerance, abs=pytest.abs_tolerance
expected, rel=rel_tolerance, abs=abs_tolerance
)


Expand All @@ -95,10 +95,10 @@ def test_kt_score_error(x, y, expected):
([1, 2, 3], [11, 12, 13], 0),
],
)
def test_std_err_ok(x, y, expected):
def test_std_err_ok(x, y, expected, rel_tolerance, abs_tolerance):
"""Test the function std_err valid inputs"""
assert std_err(np.array(x), np.array(y)) == pytest.approx(
expected, rel=pytest.rel_tolerance, abs=pytest.abs_tolerance
expected, rel=rel_tolerance, abs=abs_tolerance
)


Expand Down Expand Up @@ -161,10 +161,7 @@ def test_evaluate(hydra_cfg, capsys):

prediction_file = "CEC1.train.sample.predict.csv"
score_file = "CEC1.train.sample.evaluate.jsonl"
expected_output = (
"{'RMSE': 30.256228825071368, 'Std': 4.209845712831399, "
"'NCC': nan, 'KT': nan}\n"
)

test_data = [
{"signal": "S08547_L0001_E001", "predicted": 0.8},
{"signal": "S08564_L0001_E001", "predicted": 0.8},
Expand All @@ -182,9 +179,6 @@ def test_evaluate(hydra_cfg, capsys):
warnings.simplefilter("ignore", category=RuntimeWarning)
evaluate(hydra_cfg)

captured = capsys.readouterr()
assert captured.out == expected_output

# Check scores
scores = read_jsonl(score_file)
assert scores[0]["RMSE"] == pytest.approx(30.2562, abs=1e-4)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SNR loss 2.907641
SNR loss 2.90764
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Enhanced audio HASPI score is 0.7491529
Enhanced audio HASPI score is 0.74915
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Torch MSBG STOILoss -0.46198, ESTOILoss -0.33000
Torch MSBG STOILoss -0.46198, ESTOILoss -0.3300
2 changes: 1 addition & 1 deletion tests/regression/test_engine_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_snr_loss(regtest):
y = torch.randn(10, 1000)
loss = snr_loss.forward(x, y)

regtest.write(f"SNR loss {loss:0.6f}\n")
regtest.write(f"SNR loss {loss:0.5f}\n")


def test_stoi_loss(regtest):
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/test_full_CEC2_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ def test_full_cec2_pipeline(
listener=listener,
)

regtest.write(f"Enhanced audio HASPI score is {sii_enhanced:0.7f}\n")
regtest.write(f"Enhanced audio HASPI score is {sii_enhanced:0.5f}\n")

# Enhanced audio HASPI score is 0.2994066
2 changes: 1 addition & 1 deletion tests/regression/test_predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_torch_msbg_stoi_non_xeon_e5_2673_cpu(regtest):
estoi_loss = estoi_loss.forward(x.cpu(), y.cpu()).mean()

regtest.write(
f"Torch MSBG STOILoss {stoi_loss:0.5f}, ESTOILoss {estoi_loss:0.5f}\n"
f"Torch MSBG STOILoss {stoi_loss:0.5f}, ESTOILoss {estoi_loss:0.4f}\n"
)


Expand Down

0 comments on commit 649c76c

Please sign in to comment.