-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Update perturb.py #5231
Merged
Merged
Update perturb.py #5231
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2bc47e0
Update perturb.py
stevehuang52 78acbec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bfb4bb7
update check and teest
stevehuang52 6977262
Merge branch 'main' into stevehuang52-patch-1
stevehuang52 b55ea31
fix test
stevehuang52 970eba1
Merge branch 'main' into stevehuang52-patch-1
stevehuang52 6430bd5
Merge branch 'main' into stevehuang52-patch-1
stevehuang52 b8ea99d
Merge branch 'main' into stevehuang52-patch-1
stevehuang52 c301a07
Merge branch 'main' into stevehuang52-patch-1
stevehuang52 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import os | ||
import tempfile | ||
from typing import List, Type, Union | ||
|
@@ -20,6 +21,7 @@ | |
import pytest | ||
import soundfile as sf | ||
|
||
from nemo.collections.asr.parts.preprocessing.perturb import NoisePerturbation | ||
from nemo.collections.asr.parts.preprocessing.segment import AudioSegment | ||
from nemo.collections.asr.parts.utils.audio_utils import select_channels | ||
|
||
|
@@ -120,3 +122,58 @@ def test_from_file(self, num_channels, channel_selector): | |
assert uut.duration == self.signal_duration_sec | ||
max_diff = np.max(np.abs(uut.samples - golden_samples)) | ||
assert max_diff < self.max_diff_tol | ||
|
||
@pytest.mark.unit | ||
@pytest.mark.parametrize("data_channels", [1, 4]) | ||
@pytest.mark.parametrize("noise_channels", [1, 4]) | ||
def test_noise_perturb_channels(self, data_channels, noise_channels): | ||
"""Test loading a signal from a file. | ||
""" | ||
with tempfile.TemporaryDirectory() as test_dir: | ||
# Prepare a wav file | ||
audio_file = os.path.join(test_dir, 'audio.wav') | ||
if data_channels == 1: | ||
# samples is a one-dimensional vector for single-channel signal | ||
samples = np.random.rand(self.num_samples) | ||
else: | ||
samples = np.random.rand(self.num_samples, data_channels) | ||
sf.write(audio_file, samples, self.sample_rate, 'float') | ||
|
||
noise_file = os.path.join(test_dir, 'noise.wav') | ||
if noise_channels == 1: | ||
# samples is a one-dimensional vector for single-channel signal | ||
samples = np.random.rand(self.num_samples) | ||
else: | ||
samples = np.random.rand(self.num_samples, data_channels) | ||
sf.write(noise_file, samples, self.sample_rate, 'float') | ||
|
||
manifest_file = os.path.join(test_dir, 'noise_manifest.json') | ||
with open(manifest_file, 'w') as fout: | ||
item = {'audio_filepath': os.path.abspath(noise_file), 'label': '-', 'duration': 0.1, 'offset': 0.0} | ||
fout.write(f'{json.dumps(item)}\n') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: consider using |
||
|
||
perturber = NoisePerturbation(manifest_file) | ||
audio = AudioSegment.from_file(audio_file) | ||
noise = AudioSegment.from_file(noise_file) | ||
|
||
if data_channels == noise_channels: | ||
try: | ||
_ = perturber.perturb_with_input_noise(audio, noise, ref_mic=0) | ||
except ValueError as e: | ||
assert False, "perturb_with_input_noise failed with ref_mic=0" | ||
|
||
with pytest.raises(ValueError): | ||
_ = perturber.perturb_with_input_noise(audio, noise, ref_mic=data_channels) | ||
|
||
try: | ||
_ = perturber.perturb_with_foreground_noise(audio, noise, ref_mic=0) | ||
except ValueError as e: | ||
assert False, "perturb_with_foreground_noise failed with ref_mic=0" | ||
|
||
with pytest.raises(ValueError): | ||
_ = perturber.perturb_with_foreground_noise(audio, noise, ref_mic=data_channels) | ||
else: | ||
with pytest.raises(ValueError): | ||
_ = perturber.perturb_with_input_noise(audio, noise) | ||
with pytest.raises(ValueError): | ||
_ = perturber.perturb_with_foreground_noise(audio, noise) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏