From e1eb52e1abdf90fbb4e4e6543ee8c4f4b8c17d74 Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Tue, 15 Aug 2023 11:22:46 +0800 Subject: [PATCH] [xdoctest] reformat example code with google style in No.12-No.15 (#56210) * fix sample codes * fix bug * Update tess.py --- python/paddle/audio/backends/init_backend.py | 108 ++++++++++--------- python/paddle/audio/backends/wave_backend.py | 70 ++++++------ python/paddle/audio/datasets/esc50.py | 49 +++++---- python/paddle/audio/datasets/tess.py | 38 +++---- 4 files changed, 141 insertions(+), 124 deletions(-) diff --git a/python/paddle/audio/backends/init_backend.py b/python/paddle/audio/backends/init_backend.py index 66420cb01fd..e9793bcd973 100644 --- a/python/paddle/audio/backends/init_backend.py +++ b/python/paddle/audio/backends/init_backend.py @@ -43,25 +43,27 @@ def list_available_backends() -> List[str]: Examples: .. code-block:: python - import paddle - - sample_rate = 16000 - wav_duration = 0.5 - num_channels = 1 - num_frames = sample_rate * wav_duration - wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 - waveform = wav_data.tile([num_channels, 1]) - wav_path = "./test.wav" - - current_backend = paddle.audio.backends.get_current_backend() - print(current_backend) # wave_backend, the default backend. - backends = paddle.audio.backends.list_available_backends() - # default backends is ['wave_backend'] - # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 - if 'soundfile' in backends: - paddle.audio.backends.set_backend('soundfile') - - paddle.audio.save(wav_path, waveform, sample_rate) + >>> import paddle + + >>> sample_rate = 16000 + >>> wav_duration = 0.5 + >>> num_channels = 1 + >>> num_frames = sample_rate * wav_duration + >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 + >>> waveform = wav_data.tile([num_channels, 1]) + >>> wav_path = "./test.wav" + + >>> current_backend = paddle.audio.backends.get_current_backend() + >>> print(current_backend) + wave_backend + + >>> backends = paddle.audio.backends.list_available_backends() + >>> # default backends is ['wave_backend'] + >>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 + >>> if 'soundfile' in backends: + ... paddle.audio.backends.set_backend('soundfile') + ... + >>> paddle.audio.save(wav_path, waveform, sample_rate) """ backends = [] @@ -100,26 +102,28 @@ def get_current_backend() -> str: Examples: .. code-block:: python - import paddle + >>> import paddle - sample_rate = 16000 - wav_duration = 0.5 - num_channels = 1 - num_frames = sample_rate * wav_duration - wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 - waveform = wav_data.tile([num_channels, 1]) - wav_path = "./test.wav" + >>> sample_rate = 16000 + >>> wav_duration = 0.5 + >>> num_channels = 1 + >>> num_frames = sample_rate * wav_duration + >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 + >>> waveform = wav_data.tile([num_channels, 1]) + >>> wav_path = "./test.wav" - current_backend = paddle.audio.backends.get_current_backend() - print(current_backend) # wave_backend, the default backend. - backends = paddle.audio.backends.list_available_backends() - # default backends is ['wave_backend'] - # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 + >>> current_backend = paddle.audio.backends.get_current_backend() + >>> print(current_backend) + wave_backend - if 'soundfile' in backends: - paddle.audio.backends.set_backend('soundfile') + >>> backends = paddle.audio.backends.list_available_backends() + >>> # default backends is ['wave_backend'] + >>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 - paddle.audio.save(wav_path, waveform, sample_rate) + >>> if 'soundfile' in backends: + ... paddle.audio.backends.set_backend('soundfile') + ... + >>> paddle.audio.save(wav_path, waveform, sample_rate) """ current_backend = None @@ -144,26 +148,28 @@ def set_backend(backend_name: str): Examples: .. code-block:: python - import paddle + >>> import paddle - sample_rate = 16000 - wav_duration = 0.5 - num_channels = 1 - num_frames = sample_rate * wav_duration - wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 - waveform = wav_data.tile([num_channels, 1]) - wav_path = "./test.wav" + >>> sample_rate = 16000 + >>> wav_duration = 0.5 + >>> num_channels = 1 + >>> num_frames = sample_rate * wav_duration + >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 + >>> waveform = wav_data.tile([num_channels, 1]) + >>> wav_path = "./test.wav" - current_backend = paddle.audio.backends.get_current_backend() - print(current_backend) # wave_backend, the default backend. - backends = paddle.audio.backends.list_available_backends() - # default backends is ['wave_backend'] - # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 + >>> current_backend = paddle.audio.backends.get_current_backend() + >>> print(current_backend) + wave_backend - if 'soundfile' in backends: - paddle.audio.backends.set_backend('soundfile') + >>> backends = paddle.audio.backends.list_available_backends() + >>> # default backends is ['wave_backend'] + >>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 - paddle.audio.save(wav_path, waveform, sample_rate) + >>> if 'soundfile' in backends: + ... paddle.audio.backends.set_backend('soundfile') + ... + >>> paddle.audio.save(wav_path, waveform, sample_rate) """ if backend_name not in list_available_backends(): diff --git a/python/paddle/audio/backends/wave_backend.py b/python/paddle/audio/backends/wave_backend.py index 956820acbf9..1dcd48e1917 100644 --- a/python/paddle/audio/backends/wave_backend.py +++ b/python/paddle/audio/backends/wave_backend.py @@ -46,20 +46,20 @@ def info(filepath: str) -> AudioInfo: Example: .. code-block:: python - import os - import paddle - - sample_rate = 16000 - wav_duration = 0.5 - num_channels = 1 - num_frames = sample_rate * wav_duration - wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 - waveform = wav_data.tile([num_channels, 1]) - base_dir = os.getcwd() - filepath = os.path.join(base_dir, "test.wav") - - paddle.audio.save(filepath, waveform, sample_rate) - wav_info = paddle.audio.info(filepath) + >>> import os + >>> import paddle + + >>> sample_rate = 16000 + >>> wav_duration = 0.5 + >>> num_channels = 1 + >>> num_frames = sample_rate * wav_duration + >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 + >>> waveform = wav_data.tile([num_channels, 1]) + >>> base_dir = os.getcwd() + >>> filepath = os.path.join(base_dir, "test.wav") + + >>> paddle.audio.save(filepath, waveform, sample_rate) + >>> wav_info = paddle.audio.info(filepath) """ if hasattr(filepath, 'read'): @@ -111,20 +111,20 @@ def load( Examples: .. code-block:: python - import os - import paddle + >>> import os + >>> import paddle - sample_rate = 16000 - wav_duration = 0.5 - num_channels = 1 - num_frames = sample_rate * wav_duration - wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 - waveform = wav_data.tile([num_channels, 1]) - base_dir = os.getcwd() - filepath = os.path.join(base_dir, "test.wav") + >>> sample_rate = 16000 + >>> wav_duration = 0.5 + >>> num_channels = 1 + >>> num_frames = sample_rate * wav_duration + >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 + >>> waveform = wav_data.tile([num_channels, 1]) + >>> base_dir = os.getcwd() + >>> filepath = os.path.join(base_dir, "test.wav") - paddle.audio.save(filepath, waveform, sample_rate) - wav_data_read, sr = paddle.audio.load(filepath) + >>> paddle.audio.save(filepath, waveform, sample_rate) + >>> wav_data_read, sr = paddle.audio.load(filepath) """ if hasattr(filepath, 'read'): file_obj = filepath @@ -192,17 +192,17 @@ def save( Examples: .. code-block:: python - import paddle + >>> import paddle - sample_rate = 16000 - wav_duration = 0.5 - num_channels = 1 - num_frames = sample_rate * wav_duration - wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 - waveform = wav_data.tile([num_channels, 1]) - filepath = "./test.wav" + >>> sample_rate = 16000 + >>> wav_duration = 0.5 + >>> num_channels = 1 + >>> num_frames = sample_rate * wav_duration + >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 + >>> waveform = wav_data.tile([num_channels, 1]) + >>> filepath = "./test.wav" - paddle.audio.save(filepath, waveform, sample_rate) + >>> paddle.audio.save(filepath, waveform, sample_rate) """ assert src.ndim == 2, "Expected 2D tensor" diff --git a/python/paddle/audio/datasets/esc50.py b/python/paddle/audio/datasets/esc50.py index 3f2da64df42..48d4a05aaf4 100644 --- a/python/paddle/audio/datasets/esc50.py +++ b/python/paddle/audio/datasets/esc50.py @@ -47,25 +47,36 @@ class ESC50(AudioClassificationDataset): .. code-block:: python - import paddle - - mode = 'dev' - esc50_dataset = paddle.audio.datasets.ESC50(mode=mode, - feat_type='raw') - for idx in range(5): - audio, label = esc50_dataset[idx] - # do something with audio, label - print(audio.shape, label) - # [audio_data_length] , label_id - - esc50_dataset = paddle.audio.datasets.ESC50(mode=mode, - feat_type='mfcc', - n_mfcc=40) - for idx in range(5): - audio, label = esc50_dataset[idx] - # do something with mfcc feature, label - print(audio.shape, label) - # [feature_dim, length] , label_id + >>> import paddle + + >>> mode = 'dev' + >>> esc50_dataset = paddle.audio.datasets.ESC50(mode=mode, + ... feat_type='raw') + >>> for idx in range(5): + ... audio, label = esc50_dataset[idx] + ... # do something with audio, label + ... print(audio.shape, label) + ... # [audio_data_length] , label_id + [220500] 0 + [220500] 14 + [220500] 36 + [220500] 36 + [220500] 19 + + >>> esc50_dataset = paddle.audio.datasets.ESC50(mode=mode, + ... feat_type='mfcc', + ... n_mfcc=40) + >>> for idx in range(5): + ... audio, label = esc50_dataset[idx] + ... # do something with mfcc feature, label + ... print(audio.shape, label) + ... # [feature_dim, length] , label_id + [40, 1723] 0 + [40, 1723] 14 + [40, 1723] 36 + [40, 1723] 36 + [40, 1723] 19 + """ archive = { diff --git a/python/paddle/audio/datasets/tess.py b/python/paddle/audio/datasets/tess.py index 4c150c8c210..7d4ff9aafec 100644 --- a/python/paddle/audio/datasets/tess.py +++ b/python/paddle/audio/datasets/tess.py @@ -49,25 +49,25 @@ class TESS(AudioClassificationDataset): .. code-block:: python - import paddle - - mode = 'dev' - tess_dataset = paddle.audio.datasets.TESS(mode=mode, - feat_type='raw') - for idx in range(5): - audio, label = tess_dataset[idx] - # do something with audio, label - print(audio.shape, label) - # [audio_data_length] , label_id - - tess_dataset = paddle.audio.datasets.TESS(mode=mode, - feat_type='mfcc', - n_mfcc=40) - for idx in range(5): - audio, label = tess_dataset[idx] - # do something with mfcc feature, label - print(audio.shape, label) - # [feature_dim, num_frames] , label_id + >>> import paddle + + >>> mode = 'dev' + >>> tess_dataset = paddle.audio.datasets.TESS(mode=mode, + ... feat_type='raw') + >>> for idx in range(5): + ... audio, label = tess_dataset[idx] + ... # do something with audio, label + ... print(audio.shape, label) + ... # [audio_data_length] , label_id + + >>> tess_dataset = paddle.audio.datasets.TESS(mode=mode, + ... feat_type='mfcc', + ... n_mfcc=40) + >>> for idx in range(5): + ... audio, label = tess_dataset[idx] + ... # do something with mfcc feature, label + ... print(audio.shape, label) + ... # [feature_dim, num_frames] , label_id """ archive = { -- GitLab