Skip to content

Replace tests comparing functionality to sox with hard coded results. #4017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: kaldi_mock
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 12 additions & 3 deletions test/torchaudio_unittest/common_utils/sox_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import subprocess
import sys
import warnings

import shutil
from pathlib import Path
import os

def get_encoding(dtype):
encodings = {
Expand Down Expand Up @@ -103,8 +105,13 @@ def _flattern(effects):
return [item for sublist in effects for item in sublist]


def run_sox_effect(input_file, output_file, effect, *, output_sample_rate=None, output_bitdepth=None):
"""Run sox effects"""
def run_sox_effect(request, input_file, output_file, effect, *, output_sample_rate=None, output_bitdepth=None):
"""Save or load the result of running sox effects for the test-id `request`."""

path = Path(f"torchaudio_unittest/assets/sox_expected_results/{request}.wav")
if os.path.exists(path):
shutil.copyfile(path, output_file)

Copy link
Member

@NicolasHug NicolasHug Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC the end state we want is for if os.path.exists(path): to be True, right? If it's false it means we forgot to check-in a file? If that's the case, let's not have the if part and always do

        shutil.copyfile(path, output_file)
        return

unconditionally.

I agree with your previous point that the file generation should still exist for reference, but for safety it might be best to comment it out, so that in never gets executed.

In the current state, as I understand it, if sox is available and the file doesn't exist for whatever reason, then we are effectively checking against sox's output instead of checking against a file which should exist - so we're not testing what we think we're testing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your understanding is correct! I was planning to address your point by just removing the sox dependency from the dependencies installed on CI, so that if the file was missing we'd see an error about importing the library. But I can certainly comment the generation part out if you think that's clearer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll be safer to do all of those:

  • remove dep from CI
  • remove if condition
  • comment-out file-generation logic

effect = _flattern(effect)
command = ["sox", "-V", "--no-dither", input_file]
if output_bitdepth:
Expand All @@ -114,3 +121,5 @@ def run_sox_effect(input_file, output_file, effect, *, output_sample_rate=None,
command += ["rate", str(output_sample_rate)]
print(" ".join(command))
subprocess.run(command, check=True)
path.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(output_file, path)
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
sox_utils,
TempDirMixin,
TorchaudioTestCase,
RequestMixin
)


@skipIfNoSox
@skipIfNoExec("sox")
class TestFunctionalFiltering(TempDirMixin, TorchaudioTestCase):
class TestFunctionalFiltering(TempDirMixin, TorchaudioTestCase, RequestMixin):
def run_sox_effect(self, input_file, effect):
output_file = self.get_temp_path("expected.wav")
sox_utils.run_sox_effect(input_file, output_file, [str(e) for e in effect])
sox_utils.run_sox_effect(self.request, input_file, output_file, [str(e) for e in effect])
return load_wav(output_file)

def assert_sox_effect(self, result, input_path, effects, atol=1e-04, rtol=1e-5):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
sox_utils,
TempDirMixin,
TorchaudioTestCase,
RequestMixin
)


@skipIfNoSox
@skipIfNoExec("sox")
class TestFunctionalFiltering(TempDirMixin, TorchaudioTestCase):
class TestFunctionalFiltering(TempDirMixin, TorchaudioTestCase, RequestMixin):
def run_sox_effect(self, input_file, effect):
output_file = self.get_temp_path("expected.wav")
sox_utils.run_sox_effect(input_file, output_file, [str(e) for e in effect])
sox_utils.run_sox_effect(self.request, input_file, output_file, [str(e) for e in effect])
return load_wav(output_file)

def assert_sox_effect(self, result, input_path, effects, atol=1e-04, rtol=1e-5):
Expand Down
Loading