Metrics¶
soundscapy.audio.metrics
¶
Functions for calculating various acoustic and psychoacoustic metrics for audio signals.
It includes implementations for single-channel and two-channel signals, as well as wrapper functions for different libraries such as Acoustic Toolbox, MoSQITo, and scikit-maad.
| FUNCTION | DESCRIPTION |
|---|---|
mosqito_metric_1ch |
Calculate a MoSQITo psychoacoustic metric for a single channel signal. |
maad_metric_1ch |
Run a metric from the scikit-maad library (or suite of indices) on a single channel. |
pyacoustics_metric_1ch |
Deprecated |
acoustics_metric_1ch |
Run a metric from the acoustic_toolbox library on a single channel object. |
pyacoustics_metric_2ch |
Deprecated |
acoustics_metric_2ch |
Run a metric from the Acoustic Toolbox library on a Binaural object. |
mosqito_metric_2ch |
Calculate metrics from MoSQITo for a two-channel signal with parallel processing. |
maad_metric_2ch |
Run a metric from scikit-maad library (or suite of indices) on a binaural signal. |
prep_multiindex_df |
Prepare a MultiIndex dataframe from a dictionary of results. |
add_results |
Add results to MultiIndex dataframe. |
process_all_metrics |
Process all metrics specified in the analysis settings for a binaural signal. |
mosqito_metric_1ch
¶
mosqito_metric_1ch(
s: Signal,
metric: Literal[
"loudness_zwtv",
"roughness_dw",
"sharpness_din_from_loudness",
"sharpness_din_perseg",
"sharpness_din_tv",
],
statistics: tuple[int | str, ...] = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
*,
as_df: bool = False,
return_time_series: bool = False,
**kwargs: Unpack[_MosqitoMetricParams],
) -> dict | pd.DataFrame
Calculate a MoSQITo psychoacoustic metric for a single channel signal.
| PARAMETER | DESCRIPTION |
|---|---|
s
|
Single channel signal object to analyze.
TYPE:
|
metric
|
Name of the metric to calculate. Options are "loudness_zwtv", "roughness_dw", "sharpness_din_from_loudness", "sharpness_din_perseg", or "sharpness_din_tv".
TYPE:
|
statistics
|
Statistics to calculate on the metric results.
TYPE:
|
label
|
Label to use for the metric in the results. If None, uses a default label.
TYPE:
|
as_df
|
If True, return results as a pandas DataFrame. Otherwise, return a dictionary.
TYPE:
|
return_time_series
|
If True, include the full time series in the results.
TYPE:
|
**kwargs
|
Additional arguments to pass to the underlying MoSQITo function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Results of the metric calculation and statistics. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input signal is not single-channel or if an unrecognized metric is specified. |
Examples:
>>>
>>> from soundscapy.audio import Binaural
>>> signal = Binaural.from_wav("audio.wav", resample=480000)
>>> results = mosqito_metric_1ch(signal[0], "loudness_zwtv", as_df=True)
Source code in src/soundscapy/audio/metrics.py
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 | |
maad_metric_1ch
¶
maad_metric_1ch(
s: Signal | Binaural,
metric: Literal[
"all_temporal_alpha_indices",
"all_spectral_alpha_indices",
],
as_df: bool = False,
func_args: dict | None = None,
) -> Any
Run a metric from the scikit-maad library (or suite of indices) on a single channel.
Currently only supports running all the alpha indices at once.
| PARAMETER | DESCRIPTION |
|---|---|
s
|
Single channel signal to calculate the alpha indices for.
TYPE:
|
metric
|
Metric to calculate.
TYPE:
|
as_df
|
Whether to return a pandas DataFrame, by default False. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
func_args
|
Additional keyword arguments to pass to the metric function, by default {}.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the signal is not single-channel or if an unrecognized metric is specified. |
See Also
maad.features.all_spectral_alpha_indices maad.features.all_temporal_alpha_indices
Source code in src/soundscapy/audio/metrics.py
pyacoustics_metric_1ch
¶
pyacoustics_metric_1ch(
s: Signal | Binaural,
metric: str,
statistics: list[int | str] = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
as_df: bool = False,
return_time_series: bool = False,
func_args={},
)
Deprecated
pyacoustics is deprecated. Use acoustics_metric_1ch instead.
Source code in src/soundscapy/audio/metrics.py
acoustics_metric_1ch
¶
acoustics_metric_1ch(
s: Signal | Binaural,
metric: str,
statistics: list[int | str] = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
as_df: bool = False,
return_time_series: bool = False,
func_args: dict | None = None,
) -> dict | pd.DataFrame
Run a metric from the acoustic_toolbox library on a single channel object.
| PARAMETER | DESCRIPTION |
|---|---|
s
|
Single channel signal to calculate the metric for.
TYPE:
|
metric
|
The metric to run.
TYPE:
|
statistics
|
List of level statistics to calculate (e.g. L_5, L_90, etc).
TYPE:
|
label
|
Label to use for the metric in the results dictionary. If None, will pull from default label for that metric given in DEFAULT_LABELS.
TYPE:
|
as_df
|
Whether to return a pandas DataFrame, by default False. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
return_time_series
|
Whether to return the time series of the metric, by default False. Cannot return time series if as_df is True.
TYPE:
|
func_args
|
Additional keyword arguments to pass to the metric function, by default {}.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Dictionary of the calculated statistics or a pandas DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the signal is not single-channel or if an unrecognized metric is specified. |
See Also
acoustic_toolbox
Source code in src/soundscapy/audio/metrics.py
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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
pyacoustics_metric_2ch
¶
pyacoustics_metric_2ch(
b: Binaural,
metric: str,
statistics: tuple | list = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
channel_names: tuple[str, str] = ("Left", "Right"),
as_df: bool = False,
return_time_series: bool = False,
func_args={},
)
Deprecated
pyacoustics is deprecated. Use acoustics_metric_2ch instead.
Source code in src/soundscapy/audio/metrics.py
acoustics_metric_2ch
¶
acoustics_metric_2ch(
b: Binaural,
metric: str,
statistics: tuple | list = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
channel_names: tuple[str, str] = ("Left", "Right"),
as_df: bool = False,
return_time_series: bool = False,
func_args: dict | None = None,
) -> dict | pd.DataFrame
Run a metric from the Acoustic Toolbox library on a Binaural object.
| PARAMETER | DESCRIPTION |
|---|---|
b
|
Binaural signal to calculate the metric for.
TYPE:
|
metric
|
The metric to run.
TYPE:
|
statistics
|
List of level statistics to calculate (e.g. L_5, L_90, etc).
TYPE:
|
label
|
Label to use for the metric in the results dictionary. If None, will pull from default label for that metric given in DEFAULT_LABELS.
TYPE:
|
channel_names
|
Custom names for the channels, by default ("Left", "Right"). |
as_df
|
Whether to return a pandas DataFrame, by default False. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
return_time_series
|
Whether to return the time series of the metric, by default False. Cannot return time series if as_df is True.
TYPE:
|
func_args
|
Arguments to pass to the metric function, by default {}.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input signal is not 2-channel. |
See Also
acoustics_metric_1ch
Source code in src/soundscapy/audio/metrics.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 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 | |
mosqito_metric_2ch
¶
mosqito_metric_2ch(
b: Binaural,
metric: Literal[
"loudness_zwtv",
"sharpness_din_from_loudness",
"sharpness_din_perseg",
"sharpness_din_tv",
"roughness_dw",
],
statistics: tuple | list = (
5,
10,
50,
90,
95,
"avg",
"max",
"min",
"kurt",
"skew",
),
label: str | None = None,
channel_names: tuple[str, str] = ("Left", "Right"),
as_df: bool = False,
return_time_series: bool = False,
parallel: bool = True,
func_args: dict | None = None,
) -> dict | pd.DataFrame
Calculate metrics from MoSQITo for a two-channel signal with parallel processing.
| PARAMETER | DESCRIPTION |
|---|---|
b
|
Binaural signal to calculate the sound quality indices for.
TYPE:
|
metric
|
Metric to calculate.
TYPE:
|
statistics
|
List of level statistics to calculate (e.g. L_5, L_90, etc.).
TYPE:
|
label
|
Label to use for the metric in the results dictionary. If None, will pull from default label for that metric given in DEFAULT_LABELS.
TYPE:
|
channel_names
|
Custom names for the channels, by default ("Left", "Right"). |
as_df
|
Whether to return a pandas DataFrame, by default False. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
return_time_series
|
Whether to return the time series of the metric, by default False. Only works for metrics that return a time series array. Cannot be returned in a dataframe.
TYPE:
|
parallel
|
Whether to process channels in parallel, by default True.
TYPE:
|
func_args
|
Additional arguments to pass to the metric function, by default {}.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input signal is not 2-channel. |
Source code in src/soundscapy/audio/metrics.py
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 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
maad_metric_2ch
¶
maad_metric_2ch(
b: Binaural,
metric: Literal[
"all_temporal_alpha_indices",
"all_spectral_alpha_indices",
],
channel_names: tuple[str, str] = ("Left", "Right"),
as_df: bool = False,
func_args: dict | None = None,
) -> dict | pd.DataFrame
Run a metric from scikit-maad library (or suite of indices) on a binaural signal.
Currently only supports running all the alpha indices at once.
| PARAMETER | DESCRIPTION |
|---|---|
b
|
Binaural signal to calculate the alpha indices for.
TYPE:
|
metric
|
Metric to calculate.
TYPE:
|
channel_names
|
Custom names for the channels, by default ("Left", "Right"). |
as_df
|
Whether to return a pandas DataFrame, by default False. If True, returns a MultiIndex Dataframe with ("Recording", "Channel") as the index.
TYPE:
|
func_args
|
Additional arguments to pass to the metric function, by default {}.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | DataFrame
|
Dictionary of results if as_df is False, otherwise a pandas DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input signal is not 2-channel or if an unrecognized metric is specified. |
See Also
scikit-maad library
maad_metric_1ch
Source code in src/soundscapy/audio/metrics.py
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 | |
prep_multiindex_df
¶
prep_multiindex_df(
dictionary: dict,
label: str = "Leq",
incl_metric: bool = True,
) -> pd.DataFrame
Prepare a MultiIndex dataframe from a dictionary of results.
| PARAMETER | DESCRIPTION |
|---|---|
dictionary
|
Dict of results with recording name as key, channels {"Left", "Right"} as second key, and Leq metric as value.
TYPE:
|
label
|
Name of metric included, by default "Leq".
TYPE:
|
incl_metric
|
Whether to include the metric value in the resulting dataframe, by default True. If False, will only set up the DataFrame with the proper MultiIndex.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Index includes "Recording" and "Channel" with a column for each index
if |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input dictionary is not in the expected format. |
Source code in src/soundscapy/audio/metrics.py
add_results
¶
Add results to MultiIndex dataframe.
| PARAMETER | DESCRIPTION |
|---|---|
results_df
|
MultiIndex dataframe to add results to.
TYPE:
|
metric_results
|
MultiIndex dataframe of results to add.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Index includes "Recording" and "Channel" with a column for each index. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input DataFrames are not in the expected format. |
Source code in src/soundscapy/audio/metrics.py
process_all_metrics
¶
process_all_metrics(
b: Binaural,
analysis_settings: AnalysisSettings,
parallel: bool = True,
) -> pd.DataFrame
Process all metrics specified in the analysis settings for a binaural signal.
This function runs through all enabled metrics in the provided analysis settings, computes them for the given binaural signal, and compiles the results into a single DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
b
|
Binaural signal object to process.
TYPE:
|
analysis_settings
|
Configuration object specifying which metrics to run and their parameters.
TYPE:
|
parallel
|
If True, run applicable calculations in parallel. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
A MultiIndex DataFrame containing results from all processed metrics. The index includes "Recording" and "Channel" levels. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If there's an error processing any of the metrics. |
Notes
The parallel option primarily affects the MoSQITo metrics. Other metrics may not benefit from parallelization.
Examples:
>>>
>>> from soundscapy.audio import Binaural
>>> from soundscapy import AnalysisSettings
>>> signal = Binaural.from_wav("audio.wav", resample=480000)
>>> settings = AnalysisSettings.from_yaml("settings.yaml")
>>> results = process_all_metrics(signal,settings)
Source code in src/soundscapy/audio/metrics.py
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 | |