Utilities Module / ユーティリティモジュール¶
The wandas.utils module provides various utility functions used in the Wandas library.
wandas.utils モジュールは、Wandasライブラリで使用される様々なユーティリティ機能を提供します。
Frame Dataset / フレームデータセット¶
Provides dataset utilities for managing multiple data frames. 複数のデータフレームを管理するためのデータセットユーティリティを提供します。
Overview / 概要¶
The FrameDataset classes enable efficient batch processing of audio files in a folder. Key features include:
FrameDataset クラスは、フォルダ内の音声ファイルの効率的なバッチ処理を可能にします。主な機能:
- Lazy Loading: Load files only when accessed, reducing memory usage. 遅延読み込み: アクセス時のみファイルを読み込み、メモリ使用量を削減。
- Transformation Chaining: Apply multiple processing operations efficiently. 変換のチェーン: 複数の処理操作を効率的に適用。
- Sampling: Extract random subsets for testing or analysis. サンプリング: テストや分析のためにランダムなサブセットを抽出。
- Metadata Tracking: Keep track of dataset properties and processing history. メタデータ追跡: データセットのプロパティと処理履歴を記録。
Main Classes / 主なクラス¶
ChannelFrameDataset: For time-domain data (WAV, FLAC, OGG, AIFF, SND, and CSV files).ChannelFrameDataset: 時間領域データ用(WAV、FLAC、OGG、AIFF、SND、CSVファイル)。SpectrogramFrameDataset: For time-frequency domain data (typically created from STFT).SpectrogramFrameDataset: 時間周波数領域データ用(通常はSTFTから作成)。
Basic Usage / 基本的な使用方法¶
from wandas.utils.frame_dataset import ChannelFrameDataset
# Create a dataset from a folder
# フォルダからデータセットを作成
dataset = ChannelFrameDataset.from_folder(
folder_path="path/to/audio/files",
sampling_rate=16000, # Optional: resample all files to this rate / オプション: すべてのファイルをこのレートにリサンプリング
file_extensions=[".wav", ".flac"], # File types to include / 含めるファイルタイプ
recursive=True, # Search subdirectories / サブディレクトリを検索
lazy_loading=True # Load files on demand (recommended) / オンデマンドでファイルを読み込む(推奨)
)
# Access individual files
# 個別のファイルにアクセス
first_file = dataset[0]
print(f"File: {first_file.label}")
print(f"Duration: {first_file.duration}s")
# Get dataset information
# データセット情報を取得
metadata = dataset.get_metadata()
print(f"Total files: {metadata['file_count']}")
print(f"Loaded files: {metadata['loaded_count']}")
Sampling / サンプリング¶
Extract random subsets of the dataset for testing or analysis: テストや分析のためにデータセットのランダムなサブセットを抽出:
# Sample by number of files
# ファイル数でサンプリング
sampled = dataset.sample(n=10, seed=42)
# Sample by ratio
# 比率でサンプリング
sampled = dataset.sample(ratio=0.1, seed=42)
# Default: 10% or minimum 1 file
# デフォルト: 10% または最低1ファイル
sampled = dataset.sample(seed=42)
Metadata-driven file selection / メタデータ駆動のファイル選択¶
Path-derived partitions / パス由来のパーティション¶
For common partitioned folders, set path_metadata=True instead of writing a resolver. Wandas inspects only each discovered relative path, so discovery remains lazy and does not open audio files.
一般的なパーティションフォルダでは resolver を書かずに path_metadata=True を指定できます。Wandas は探索した相対パスだけを調べるため、探索は遅延性を保ち、音声ファイルを開きません。
dataset = wd.from_folder(
"recordings/",
recursive=True,
path_metadata=True,
)
selected = dataset.select(partition_0="group_a", partition_1="batch_01")
The naming follows AWS Glue crawler conventions: 命名は AWS Glue crawler の規則に従います。
- Plain parent segments use their zero-based path positions:
group_a/batch_01/file.wavbecomes{"partition_0": "group_a", "partition_1": "batch_01"}. / 通常の親セグメントにはゼロ始まりのパス位置を使います。 - Hive-style segments use their keys:
group=group_a/batch=batch_01/file.wavbecomes{"group": "group_a", "batch": "batch_01"}. / Hive 形式のセグメントにはそのキーを使います。 - The root folder and filename are excluded. Files at uneven depths receive only the keys present in their own parent path; a missing key does not match a
select()criterion. / ルートフォルダとファイル名は除外します。深さが異なる場合は各ファイルに存在するキーだけを持ち、不足キーはselect()条件に一致しません。 - Duplicate Hive keys, the reserved Hive key
_source_file, and Hive keys in the generatedpartition_<number>namespace raiseValueErrorinstead of overwriting or impersonating metadata. / Hive キーの重複、予約済み Hive キー_source_file、生成用partition_<number>名前空間の Hive キーは、メタデータの上書きや偽装をせずValueErrorになります。
path_metadata=True and metadata_resolver are mutually exclusive because combining two metadata sources would make precedence ambiguous.
2つのメタデータ源の優先順位が曖昧になるため、path_metadata=True と metadata_resolver は同時に指定できません。
Custom metadata resolver / カスタムメタデータ resolver¶
metadata_resolver receives each path relative to folder_path once during file discovery. This makes it possible to select files without reading audio headers or waveform samples. Multiple select() criteria use exact-match AND semantics.
metadata_resolver はファイル探索時に、folder_path からの相対パスを各ファイルにつき一度受け取ります。音声ヘッダーや波形サンプルを読まずにファイルを選択でき、複数の select() 条件は完全一致の AND として扱われます。
Use a custom resolver when metadata comes from a project-specific filename rule rather than parent folders. For ordinary partition folders, prefer path_metadata=True above.
親フォルダではなくプロジェクト固有のファイル名規則からメタデータを得る場合に custom resolver を使います。通常のパーティションフォルダでは、上記の path_metadata=True を優先してください。
from pathlib import Path
import wandas as wd
def resolve_recording_metadata(path: Path):
filename = path.name
recording_id, status = filename.removesuffix(".wav").split("__")
return {
"recording_id": recording_id,
"status": status,
}
dataset = wd.from_folder(
"recordings/",
recursive=True,
file_extensions=[".wav"],
metadata_resolver=resolve_recording_metadata,
)
selected = dataset.select(status="approved")
Sidecar CSV data remains application-owned in v1. Convert it to a lookup and reference that lookup from the same resolver contract. Specify WAV explicitly because reading a signal CSV currently materializes the whole CSV during header inspection. v1 では sidecar CSV は利用側で管理します。lookup に変換し、同じ resolver 契約から参照してください。信号 CSV のヘッダー確認では現在 CSV 全体を実体化するため、WAV を明示的に指定します。
import pandas as pd
recordings = pd.read_csv("recordings.csv")
lookup = recordings.set_index("path")[["condition", "priority"]].to_dict(orient="index")
dataset = wd.from_folder(
"recordings/",
recursive=True,
file_extensions=[".wav"],
metadata_resolver=lambda path: lookup[path.as_posix()],
)
selected = dataset.select(condition="reference", priority=1)
Indexing the lookup is recommended because a WAV missing from the CSV fails during dataset construction. Use lookup.get(path.as_posix(), {}) only when files without CSV metadata are intentionally allowed.
CSVにないWAVをDataset構築時のエラーにできるため、lookupの添字アクセスを推奨します。CSVメタデータのないファイルも意図的に許可する場合だけ lookup.get(path.as_posix(), {}) を使用します。
Loading stages / 読み込みの3段階¶
from_folder(): discover paths and resolve metadata; no audio headers or waveform samples are read. / パス探索とメタデータ解決。音声ヘッダーと波形は未読。dataset[i]: inspect the audio header and create a Frame; waveform samples remain Dask-lazy. / 音声ヘッダーを確認してFrameを作成。波形はDask遅延のまま。frame.data: materialize waveform samples. / 波形サンプルを実体化。
With lazy_loading=False, stage 2 runs for every file during construction, while stage 3 remains lazy.
lazy_loading=False では構築時に全ファイルの段階2を実行しますが、段階3は引き続き遅延します。
Resolver and selection contracts / resolverと選択の契約¶
- The resolver receives a root-relative
Pathonce per discovered file and must return aMapping[str, object]. Wandas deep-copies the result. / resolverは探索した各ファイルにつき一度、ルート相対のPathを受け取り、Mapping[str, object]を返します。結果はディープコピーされます。 - Resolver exceptions, non-mapping results, non-string keys, and the reserved
_source_filekey fail during dataset construction with the affected path. / resolver例外、Mapping以外の戻り値、文字列以外のキー、予約キー_source_fileは対象パスを含む構築時エラーになります。 - Multiple
select()criteria use exact-match AND semantics.select()with no criteria returns an independent all-file view. / 複数条件は完全一致のANDです。条件なしのselect()は全ファイルを保持する独立ビューです。 - An unknown key raises
KeyError; no matches produce a valid empty Dataset. / 未知キーはKeyError、一致なしは有効な空Datasetです。
Transformations / 変換¶
Apply processing operations to all files in the dataset: データセット内のすべてのファイルに処理操作を適用:
# Built-in transformations
# 組み込みの変換
resampled = dataset.resample(target_sr=8000)
trimmed = dataset.trim(start=0.5, end=2.0)
# Chain multiple transformations
# 複数の変換をチェーン
processed = (
dataset
.resample(target_sr=8000)
.trim(start=0.5, end=2.0)
)
# Custom transformation
# カスタム変換
def custom_filter(frame):
return frame.low_pass_filter(cutoff=1000)
filtered = dataset.apply(custom_filter)
STFT - Spectrogram Generation / STFT - スペクトログラム生成¶
Convert time-domain data to spectrograms: 時間領域データをスペクトログラムに変換:
# Create spectrogram dataset
# スペクトログラムデータセットを作成
spec_dataset = dataset.stft(
n_fft=2048,
hop_length=512,
window="hann"
)
# Access a spectrogram
# スペクトログラムにアクセス
spec_frame = spec_dataset[0]
spec_frame.plot()
Iteration / 反復処理¶
Process all files in the dataset: データセット内のすべてのファイルを処理:
for i in range(len(dataset)):
frame = dataset[i]
if frame is not None:
# Process the frame
# フレームを処理
print(f"Processing {frame.label}...")
Key Parameters / 主なパラメータ¶
folder_path (str): Path to the folder containing audio files. 音声ファイルを含むフォルダへのパス。
sampling_rate (Optional[int]): Target sampling rate. Files will be resampled if different from this rate. ターゲットサンプリングレート。このレートと異なる場合、ファイルはリサンプリングされます。
file_extensions (Optional[list[str]]): List of file extensions to include. By default, all registered reader extensions from wd.supported_formats() are used: [".aif", ".aiff", ".csv", ".flac", ".ogg", ".snd", ".wav"]. Audio format availability follows the libsndfile library bundled or installed with SoundFile. MP3 is not a registered Wandas reader format.
含めるファイル拡張子のリスト。デフォルトでは、wd.supported_formats() が返す登録済み reader 拡張子 [".aif", ".aiff", ".csv", ".flac", ".ogg", ".snd", ".wav"] をすべて使用します。音声形式の利用可否は SoundFile に同梱またはインストールされた libsndfile に従います。MP3 は Wandas の登録済み reader 形式ではありません。
lazy_loading (bool): If True, files are loaded only when accessed. Default: True. Trueの場合、ファイルはアクセス時にのみ読み込まれます。デフォルト: True。
recursive (bool): If True, search subdirectories recursively. Default: False. Trueの場合、サブディレクトリを再帰的に検索します。デフォルト: False。
path_metadata (bool): If True, infer AWS Glue-style metadata from relative parent folders. Prefer this option for ordinary partitioned folders. Default: False. Trueの場合、相対親フォルダからAWS Glue形式のメタデータを推論します。通常のパーティションフォルダではこの方法を優先します。デフォルト: False。
metadata_resolver (Callable[[Path], Mapping[str, object]] | None): Resolve file metadata from each root-relative path during discovery. Default: None.
探索時に各ルート相対パスからファイルメタデータを解決します。デフォルト: None。
Examples / 使用例¶
For detailed examples, see the learning-path/ directory and the tutorial marimo apps listed in the Tutorial section.
詳細な例については、learning-path/ ディレクトリとチュートリアル marimo アプリを参照してください。
API Reference / APIリファレンス¶
wandas.utils.frame_dataset
¶
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
FrameType = ChannelFrame | SpectrogramFrame
module-attribute
¶
F = TypeVar('F', bound=FrameType)
module-attribute
¶
F_out = TypeVar('F_out', bound=FrameType)
module-attribute
¶
MetadataResolver = Callable[[Path], Mapping[str, object]]
module-attribute
¶
Classes¶
LazyFrame
dataclass
¶
Bases: Generic[F]
A class that encapsulates a frame and its loading state.
Attributes:
| Name | Type | Description |
|---|---|---|
file_path |
Path
|
File path associated with the frame |
frame |
F | None
|
Loaded frame object (None if not loaded) |
is_loaded |
bool
|
Flag indicating if the frame is loaded |
load_attempted |
bool
|
Flag indicating if loading was attempted (for error detection) |
Source code in wandas/utils/frame_dataset.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |
Attributes¶
file_path
instance-attribute
¶
metadata = field(default_factory=dict)
class-attribute
instance-attribute
¶
frame = None
class-attribute
instance-attribute
¶
is_loaded = False
class-attribute
instance-attribute
¶
load_attempted = False
class-attribute
instance-attribute
¶
Functions¶
__init__(file_path, metadata=dict(), frame=None, is_loaded=False, load_attempted=False)
¶
ensure_loaded(loader)
¶
Ensures the frame is loaded, loading it if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
Callable[[Path], F | None]
|
Function to load a frame from a file path |
required |
Returns:
| Type | Description |
|---|---|
F | None
|
The loaded frame, or None if loading failed |
Source code in wandas/utils/frame_dataset.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
reset()
¶
Reset the frame state.
Source code in wandas/utils/frame_dataset.py
79 80 81 82 83 84 85 | |
FrameDataset
¶
Bases: Generic[F], ABC
Abstract base dataset class for processing files in a folder. Includes lazy loading capability to efficiently handle large datasets. Subclasses handle specific frame types (ChannelFrame, SpectrogramFrame, etc.).
Source code in wandas/utils/frame_dataset.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
Attributes¶
folder_path = Path(folder_path)
instance-attribute
¶
sampling_rate = sampling_rate
instance-attribute
¶
signal_length = signal_length
instance-attribute
¶
file_extensions = file_extensions or ['.wav']
instance-attribute
¶
Functions¶
__init__(folder_path, sampling_rate=None, signal_length=None, file_extensions=None, lazy_loading=True, recursive=False, source_dataset=None, transform=None, metadata_resolver=None, path_metadata=False)
¶
Source code in wandas/utils/frame_dataset.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
__len__()
¶
Return the number of files in the dataset.
Source code in wandas/utils/frame_dataset.py
307 308 309 | |
get_by_label(label)
¶
Get a frame by its label (filename).
Parameters¶
label : str The filename (label) to search for (e.g., 'sample_1.wav').
Returns¶
F | None The frame if found, otherwise None.
Examples¶
frame = dataset.get_by_label("sample_1.wav") if frame: ... print(frame.label)
Source code in wandas/utils/frame_dataset.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
get_all_by_label(label)
¶
Get all frames matching the given label (filename).
Parameters¶
label : str The filename (label) to search for (e.g., 'sample_1.wav').
Returns¶
list[F] A list of frames matching the label. If none are found, returns an empty list.
Notes¶
- Search is performed against the filename portion only (i.e. Path.name).
- Each matched frame will be loaded (triggering lazy load) via
_ensure_loaded.
Source code in wandas/utils/frame_dataset.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
__getitem__(key)
¶
__getitem__(key: int) -> F | None
__getitem__(key: str) -> list[F]
Get the frame by index (int) or label (str).
Parameters¶
key : int or str Index (int) or filename/label (str).
Returns¶
F | None or list[F]
If key is an int, returns the frame or None. If key is a str,
returns a list of matching frames (may be empty).
Examples¶
frame = dataset[0] # by index frames = dataset["sample_1.wav"] # list of matches by filename
Source code in wandas/utils/frame_dataset.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
apply(func)
¶
apply(func: Callable[[F], F_out | None]) -> FrameDataset[F_out]
apply(func: Callable[[F], Any | None]) -> FrameDataset[Any]
Apply a function to the entire dataset to create a new dataset.
Source code in wandas/utils/frame_dataset.py
411 412 413 414 415 416 417 418 419 420 421 422 423 | |
save(output_folder, filename_prefix='')
¶
Save processed frames to files.
Source code in wandas/utils/frame_dataset.py
425 426 427 | |
sample(n=None, ratio=None, seed=None)
¶
Get a sample from the dataset.
Source code in wandas/utils/frame_dataset.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
select(**criteria)
¶
Select files by exact-match resolver metadata without loading frames.
Source code in wandas/utils/frame_dataset.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | |
get_metadata()
¶
Get metadata for the dataset.
Source code in wandas/utils/frame_dataset.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
ChannelFrameDataset
¶
Bases: FrameDataset[ChannelFrame]
Dataset class for handling audio files as ChannelFrames in a folder.
Source code in wandas/utils/frame_dataset.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
Functions¶
__init__(folder_path, sampling_rate=None, signal_length=None, file_extensions=None, lazy_loading=True, recursive=False, source_dataset=None, transform=None, metadata_resolver=None, path_metadata=False)
¶
Source code in wandas/utils/frame_dataset.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
select(**criteria)
¶
Select files while preserving ChannelFrameDataset processing methods.
Source code in wandas/utils/frame_dataset.py
686 687 688 | |
resample(target_sr)
¶
Resample all frames in the dataset.
Source code in wandas/utils/frame_dataset.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 | |
trim(start, end)
¶
Trim all frames in the dataset.
Source code in wandas/utils/frame_dataset.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
normalize(**kwargs)
¶
Normalize all frames in the dataset.
Source code in wandas/utils/frame_dataset.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 | |
stft(n_fft=2048, hop_length=None, win_length=None, window='hann')
¶
Apply STFT to all frames in the dataset.
Source code in wandas/utils/frame_dataset.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 | |
from_folder(folder_path, sampling_rate=None, file_extensions=None, recursive=False, lazy_loading=True, metadata_resolver=None, path_metadata=False)
classmethod
¶
Create a dataset, optionally inferring metadata from parent paths.
Source code in wandas/utils/frame_dataset.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
SpectrogramFrameDataset
¶
Bases: FrameDataset[SpectrogramFrame]
Dataset class for handling spectrogram data as SpectrogramFrames. Expected to be generated mainly as a result of ChannelFrameDataset.stft().
Source code in wandas/utils/frame_dataset.py
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
Functions¶
__init__(folder_path, sampling_rate=None, signal_length=None, file_extensions=None, lazy_loading=True, recursive=False, source_dataset=None, transform=None)
¶
Source code in wandas/utils/frame_dataset.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
plot(index, **kwargs)
¶
Plot the spectrogram at the specified index.
Source code in wandas/utils/frame_dataset.py
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 | |
Functions¶
Sample Generation / サンプル生成¶
Provides functions for generating sample data for testing. テスト用のサンプルデータを生成する機能を提供します。
wandas.utils.generate_sample
¶
Classes¶
Functions¶
generate_sin(freqs=1000, sampling_rate=16000, duration=1.0, label=None)
¶
Generate sample sine wave signals.
Parameters¶
freqs : float or list of float, default=1000 Frequency of the sine wave(s) in Hz. If multiple frequencies are specified, multiple channels will be created. sampling_rate : int, default=16000 Sampling rate in Hz. duration : float, default=1.0 Duration of the signal in seconds. label : str, optional Label for the entire signal.
Returns¶
ChannelFrame ChannelFrame object containing the sine wave(s).
Source code in wandas/utils/generate_sample.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
generate_sin_lazy(freqs=1000, sampling_rate=16000, duration=1.0, label=None)
¶
Generate sample sine wave signals using lazy computation.
Parameters¶
freqs : float or list of float, default=1000 Frequency of the sine wave(s) in Hz. If multiple frequencies are specified, multiple channels will be created. sampling_rate : int, default=16000 Sampling rate in Hz. duration : float, default=1.0 Duration of the signal in seconds. label : str, optional Label for the entire signal.
Returns¶
ChannelFrame Lazy ChannelFrame object containing the sine wave(s).
Source code in wandas/utils/generate_sample.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
Type Definitions / 型定義¶
Provides type definitions used in Wandas. Wandasで使用される型定義を提供します。
wandas.utils.types
¶
General Utilities / 一般ユーティリティ¶
Provides other general utility functions. その他の一般的なユーティリティ機能を提供します。
wandas.utils.util
¶
Attributes¶
DB_FLOOR = 1e-12
module-attribute
¶
PA_REFERENCE = 2e-05
module-attribute
¶
DB_AMIN = 1e-15
module-attribute
¶
Functions¶
ref_weighted_dB(data, channel_metadata, ndim)
¶
Compute dB level relative to per-channel reference values.
Parameters¶
data : NDArrayReal
Non-negative amplitude data (already absolute-valued if complex).
channel_metadata : list
Objects with a .ref attribute (one per channel).
ndim : int
Number of dimensions in the underlying dask array.
Returns¶
NDArrayReal
Decibel values: 20 * log10(max(data / ref, DB_FLOOR))
Source code in wandas/utils/util.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
validate_sampling_rate(sampling_rate, param_name='sampling_rate')
¶
Validate that sampling rate is positive.
Parameters¶
sampling_rate : float Sampling rate in Hz to validate. param_name : str, default="sampling_rate" Name of the parameter being validated (for error messages).
Raises¶
ValueError If sampling_rate is not positive (i.e., <= 0).
Examples¶
validate_sampling_rate(44100) # No error validate_sampling_rate(0) # Raises ValueError validate_sampling_rate(-100) # Raises ValueError
Source code in wandas/utils/util.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
unit_to_ref(unit)
¶
Convert unit to reference value.
Parameters¶
unit : str Unit string.
Returns¶
float Reference value for the unit. For 'Pa', returns 2e-5 (20 μPa). For other units, returns 1.0.
Source code in wandas/utils/util.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
calculate_rms(wave)
¶
Calculate the root mean square of the wave.
Parameters¶
wave : NDArrayReal Input waveform data. Can be multi-channel (shape: [channels, samples]) or single channel (shape: [samples]).
Returns¶
Union[float, NDArray[np.float64]] RMS value(s). For multi-channel input, returns an array of RMS values, one per channel. For single-channel input, returns a single RMS value.
Source code in wandas/utils/util.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
calculate_desired_noise_rms(clean_rms, snr)
¶
Calculate the desired noise RMS based on clean signal RMS and target SNR.
Parameters¶
clean_rms : "NDArrayReal" RMS value(s) of the clean signal. Can be a single value or an array for multi-channel. snr : float Target Signal-to-Noise Ratio in dB.
Returns¶
"NDArrayReal" Desired noise RMS value(s) to achieve the target SNR.
Source code in wandas/utils/util.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
amplitude_to_db(amplitude, ref)
¶
Convert amplitude to decibel.
Parameters¶
amplitude : NDArrayReal Input amplitude data. ref : float Reference value for conversion.
Returns¶
NDArrayReal Amplitude data converted to decibels.
Source code in wandas/utils/util.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
level_trigger(data, level, offset=0, hold=1)
¶
Find points where the signal crosses the specified level from below.
Parameters¶
data : NDArrayReal Input signal data. level : float Threshold level for triggering. offset : int, default=0 Offset to add to trigger points. hold : int, default=1 Minimum number of samples between successive trigger points.
Returns¶
list of int List of sample indices where the signal crosses the level.
Source code in wandas/utils/util.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
cut_sig(data, point_list, cut_len, taper_rate=0, dc_cut=False)
¶
Cut segments from signal at specified points.
Parameters¶
data : NDArrayReal Input signal data. point_list : list of int List of starting points for cutting. cut_len : int Length of each segment to cut. taper_rate : float, default=0 Taper rate for Tukey window applied to segments. A value of 0 means no tapering, 1 means full tapering. dc_cut : bool, default=False Whether to remove DC component (mean) from segments.
Returns¶
NDArrayReal Array containing cut segments with shape (n_segments, cut_len).
Source code in wandas/utils/util.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |