Surveys¶
soundscapy.surveys
¶
Soundscapy Surveys Package.
This package handles the processing and analysis of soundscape surveys, including PAQ (Perceived Affective Quality) data and ISO coordinate calculations.
| MODULE | DESCRIPTION |
|---|---|
processing |
Soundscape survey data processing module. |
survey_utils |
Core utility functions for processing soundscape survey data. |
| FUNCTION | DESCRIPTION |
|---|---|
add_iso_coords |
Calculate and add ISO coordinates as new columns in the DataFrame. |
calculate_iso_coords |
Calculate the projected ISOPleasant and ISOEventful coordinates. |
ipsatize |
Participant-level ipsatization for circumplex analysis. |
return_paqs |
Return only the PAQ columns from a DataFrame. |
simulation |
Generate random PAQ responses for simulation purposes. |
rename_paqs |
Rename the PAQ columns in a DataFrame to standard PAQ IDs. |
add_iso_coords
¶
add_iso_coords(
data: DataFrame,
val_range: tuple[int, int] = (1, 5),
names: tuple[str, str] = ("ISOPleasant", "ISOEventful"),
angles: tuple[int, ...] = EQUAL_ANGLES,
*,
overwrite: bool = False,
) -> pd.DataFrame
Calculate and add ISO coordinates as new columns in the DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Input DataFrame containing PAQ data
TYPE:
|
val_range
|
(min, max) range of original PAQ responses, by default (1, 5) |
names
|
Names for new coordinate columns, by default ("ISOPleasant", "ISOEventful")
TYPE:
|
angles
|
Angles for each PAQ in degrees, by default EQUAL_ANGLES |
overwrite
|
Whether to overwrite existing ISO coordinate columns, by default False
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with new ISO coordinate columns added |
| RAISES | DESCRIPTION |
|---|---|
Warning
|
If ISO coordinate columns already exist and overwrite is False |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'PAQ1': [4, 2], 'PAQ2': [3, 5], 'PAQ3': [2, 4], 'PAQ4': [1, 3],
... 'PAQ5': [5, 1], 'PAQ6': [3, 2], 'PAQ7': [4, 3], 'PAQ8': [2, 5]
... })
>>> df_with_iso = add_iso_coords(df)
>>> df_with_iso[['ISOPleasant', 'ISOEventful']].round(2)
ISOPleasant ISOEventful
0 -0.03 -0.28
1 0.47 0.18
Source code in src/soundscapy/surveys/processing.py
calculate_iso_coords
¶
calculate_iso_coords(
results_df: DataFrame,
val_range: tuple[int, int] = (5, 1),
angles: tuple[int, ...] = EQUAL_ANGLES,
) -> tuple[pd.Series, pd.Series]
Calculate the projected ISOPleasant and ISOEventful coordinates.
| PARAMETER | DESCRIPTION |
|---|---|
results_df
|
DataFrame containing PAQ data.
TYPE:
|
val_range
|
(max, min) range of original PAQ responses, by default (5, 1) |
angles
|
Angles for each PAQ in degrees, by default EQUAL_ANGLES |
| RETURNS | DESCRIPTION |
|---|---|
tuple[Series, Series]
|
ISOPleasant and ISOEventful coordinate values |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'PAQ1': [4, 2], 'PAQ2': [3, 5], 'PAQ3': [2, 4], 'PAQ4': [1, 3],
... 'PAQ5': [5, 1], 'PAQ6': [3, 2], 'PAQ7': [4, 3], 'PAQ8': [2, 5]
... })
>>> iso_pleasant, iso_eventful = calculate_iso_coords(df)
>>> iso_pleasant.round(2)
0 -0.03
1 0.47
dtype: float64
>>> iso_eventful.round(2)
0 -0.28
1 0.18
dtype: float64
Source code in src/soundscapy/surveys/processing.py
ipsatize
¶
ipsatize(
data: DataFrame,
method: Literal[
"grand_mean", "column_wise", "row_wise"
] = "grand_mean",
participant_col: str = "participant",
scales: list[str] | None = None,
) -> pd.DataFrame
Participant-level ipsatization for circumplex analysis.
Removes systematic response biases before computing a correlation matrix. The choice of method depends on the study design and the type of bias being corrected.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
DataFrame containing PAQ scale columns and (for participant-level methods) a grouping column.
TYPE:
|
method
|
Centering strategy:
TYPE:
|
participant_col
|
Column used to group observations by participant. Required for
TYPE:
|
scales
|
PAQ column names to centre. Defaults to :data: |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame containing only the scale columns with centred values.
The |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If |
Examples:
>>> import pandas as pd
>>> data = pd.DataFrame({
... 'PAQ1': [50., 60., 40., 30.], 'PAQ2': [50., 60., 40., 30.],
... 'PAQ3': [50., 60., 40., 30.], 'PAQ4': [50., 60., 40., 30.],
... 'PAQ5': [50., 60., 40., 30.], 'PAQ6': [50., 60., 40., 30.],
... 'PAQ7': [50., 60., 40., 30.], 'PAQ8': [50., 60., 40., 30.],
... 'participant': ['A', 'A', 'B', 'B'],
... })
>>> result = ipsatize(data, method="grand_mean")
>>> result['PAQ1'].tolist()
[-5.0, 5.0, 5.0, -5.0]
Source code in src/soundscapy/surveys/processing.py
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 718 719 720 | |
return_paqs
¶
return_paqs(
df: DataFrame,
other_cols: list[str] | None = None,
*,
incl_ids: bool = True,
) -> pd.DataFrame
Return only the PAQ columns from a DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing PAQ data.
TYPE:
|
other_cols
|
Other columns to include in the output, by default None. |
incl_ids
|
Whether to include ID columns (RecordID, GroupID, etc.), by default True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame containing only the PAQ columns and optionally ID and other specified columns. |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'RecordID': [1, 2],
... 'PAQ1': [4, 3],
... 'PAQ2': [2, 5],
... 'PAQ3': [1, 2],
... 'PAQ4': [3, 4],
... 'PAQ5': [5, 1],
... 'PAQ6': [2, 3],
... 'PAQ7': [4, 5],
... 'PAQ8': [1, 2],
... 'OtherCol': ['A', 'B']
... })
>>> return_paqs(df)
RecordID PAQ1 PAQ2 PAQ3 PAQ4 PAQ5 PAQ6 PAQ7 PAQ8
0 1 4 2 1 3 5 2 4 1
1 2 3 5 2 4 1 3 5 2
>>> return_paqs(df, incl_ids=False, other_cols=['OtherCol'])
PAQ1 PAQ2 PAQ3 PAQ4 PAQ5 PAQ6 PAQ7 PAQ8 OtherCol
0 4 2 1 3 5 2 4 1 A
1 3 5 2 4 1 3 5 2 B
Source code in src/soundscapy/surveys/survey_utils.py
simulation
¶
simulation(
n: int = 3000,
val_range: tuple[int, int] = (1, 5),
*,
seed: int | None = None,
incl_iso_coords: bool = False,
**coord_kwargs: Unpack[_AddISOCoordsKwargs],
) -> pd.DataFrame
Generate random PAQ responses for simulation purposes.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of samples to simulate, by default 3000
TYPE:
|
val_range
|
Range of values for PAQ responses, by default (1, 5) |
seed
|
Optional random seed for deterministic output, by default None
TYPE:
|
incl_iso_coords
|
Whether to add calculated ISO coordinates, by default False
TYPE:
|
**coord_kwargs
|
Optional keyword arguments passed directly to the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame of randomly generated PAQ responses |
Examples:
>>> data = simulation(n=5,incl_iso_coords=True)
>>> data.shape
(5, 10)
>>> list(data.columns)
['PAQ1', 'PAQ2', 'PAQ3', 'PAQ4', 'PAQ5', 'PAQ6', 'PAQ7', 'PAQ8', 'ISOPleasant', 'ISOEventful']
Source code in src/soundscapy/surveys/processing.py
rename_paqs
¶
Rename the PAQ columns in a DataFrame to standard PAQ IDs.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing PAQ data.
TYPE:
|
paq_aliases
|
Specify which PAQs are to be renamed. If None, will check if the column names are in pre-defined options. If a tuple, the order must match PAQ_IDS. If a dict, keys are current names and values are desired PAQ IDs. |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with renamed PAQ columns. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If paq_aliases is not a tuple, list, or dictionary. |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'pleasant': [4, 3],
... 'vibrant': [2, 5],
... 'other_col': [1, 2]
... })
>>> rename_paqs(df)
PAQ1 PAQ2 other_col
0 4 2 1
1 3 5 2
>>> df_custom = pd.DataFrame({
... 'pl': [4, 3],
... 'vb': [2, 5],
... })
>>> rename_paqs(df_custom, paq_aliases={'pl': 'PAQ1', 'vb': 'PAQ2'})
PAQ1 PAQ2
0 4 2
1 3 5
Source code in src/soundscapy/surveys/survey_utils.py
Processing¶
soundscapy.surveys.processing
¶
Soundscape survey data processing module.
This module contains functions for processing and analyzing soundscape survey data, including ISO coordinate calculations, data quality checks, and SSM metrics.
Notes
The functions in this module are designed to be fairly general and can be used with any dataset in a similar format to the ISD. The key to this is using a simple dataframe/sheet with the following columns:
- Index columns: e.g. LocationID, RecordID, GroupID, SessionID
- Perceptual attributes: PAQ1, PAQ2, ..., PAQ8
- Independent variables: e.g. Laeq, N5, Sharpness, etc.
The key functions of this module are designed to clean/validate datasets, calculate ISO
coordinate values or SSM metrics, filter on index columns. Functions and operations
which are specific to a particular dataset are located in their own
modules under soundscape.databases.
| CLASS | DESCRIPTION |
|---|---|
ISOCoordinates |
Dataclass for storing ISO coordinates. |
SSMMetrics |
Dataclass for storing Structural Summary Method (SSM) metrics. |
| FUNCTION | DESCRIPTION |
|---|---|
calculate_iso_coords |
Calculate the projected ISOPleasant and ISOEventful coordinates. |
add_iso_coords |
Calculate and add ISO coordinates as new columns in the DataFrame. |
likert_data_quality |
Perform basic quality checks on PAQ (Likert scale) data. |
simulation |
Generate random PAQ responses for simulation purposes. |
ssm_metrics |
Calculate the Structural Summary Method (SSM) metrics for each response. |
ssm_cosine_fit |
Fit a cosine model to the PAQ data for SSM analysis. |
ipsatize |
Participant-level ipsatization for circumplex analysis. |
ISOCoordinates
dataclass
¶
Dataclass for storing ISO coordinates.
SSMMetrics
dataclass
¶
SSMMetrics(
amplitude: float,
angle: float,
elevation: float,
displacement: float,
r_squared: float,
)
Dataclass for storing Structural Summary Method (SSM) metrics.
| METHOD | DESCRIPTION |
|---|---|
table |
Generate a pandas Series containing specific attributes of the instance. |
table
¶
Generate a pandas Series containing specific attributes of the instance.
This method collects the values of the instance attributes related to amplitude, angle, elevation, displacement, and r_squared, and organizes them into a pandas Series. It is useful for presenting the data in a structured format suitable for further processing or analysis.
| RETURNS | DESCRIPTION |
|---|---|
Series
|
A pandas Series containing the following key-value pairs:
|
Source code in src/soundscapy/surveys/processing.py
calculate_iso_coords
¶
calculate_iso_coords(
results_df: DataFrame,
val_range: tuple[int, int] = (5, 1),
angles: tuple[int, ...] = EQUAL_ANGLES,
) -> tuple[pd.Series, pd.Series]
Calculate the projected ISOPleasant and ISOEventful coordinates.
| PARAMETER | DESCRIPTION |
|---|---|
results_df
|
DataFrame containing PAQ data.
TYPE:
|
val_range
|
(max, min) range of original PAQ responses, by default (5, 1) |
angles
|
Angles for each PAQ in degrees, by default EQUAL_ANGLES |
| RETURNS | DESCRIPTION |
|---|---|
tuple[Series, Series]
|
ISOPleasant and ISOEventful coordinate values |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'PAQ1': [4, 2], 'PAQ2': [3, 5], 'PAQ3': [2, 4], 'PAQ4': [1, 3],
... 'PAQ5': [5, 1], 'PAQ6': [3, 2], 'PAQ7': [4, 3], 'PAQ8': [2, 5]
... })
>>> iso_pleasant, iso_eventful = calculate_iso_coords(df)
>>> iso_pleasant.round(2)
0 -0.03
1 0.47
dtype: float64
>>> iso_eventful.round(2)
0 -0.28
1 0.18
dtype: float64
Source code in src/soundscapy/surveys/processing.py
add_iso_coords
¶
add_iso_coords(
data: DataFrame,
val_range: tuple[int, int] = (1, 5),
names: tuple[str, str] = ("ISOPleasant", "ISOEventful"),
angles: tuple[int, ...] = EQUAL_ANGLES,
*,
overwrite: bool = False,
) -> pd.DataFrame
Calculate and add ISO coordinates as new columns in the DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Input DataFrame containing PAQ data
TYPE:
|
val_range
|
(min, max) range of original PAQ responses, by default (1, 5) |
names
|
Names for new coordinate columns, by default ("ISOPleasant", "ISOEventful")
TYPE:
|
angles
|
Angles for each PAQ in degrees, by default EQUAL_ANGLES |
overwrite
|
Whether to overwrite existing ISO coordinate columns, by default False
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with new ISO coordinate columns added |
| RAISES | DESCRIPTION |
|---|---|
Warning
|
If ISO coordinate columns already exist and overwrite is False |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'PAQ1': [4, 2], 'PAQ2': [3, 5], 'PAQ3': [2, 4], 'PAQ4': [1, 3],
... 'PAQ5': [5, 1], 'PAQ6': [3, 2], 'PAQ7': [4, 3], 'PAQ8': [2, 5]
... })
>>> df_with_iso = add_iso_coords(df)
>>> df_with_iso[['ISOPleasant', 'ISOEventful']].round(2)
ISOPleasant ISOEventful
0 -0.03 -0.28
1 0.47 0.18
Source code in src/soundscapy/surveys/processing.py
likert_data_quality
¶
likert_data_quality(
df: DataFrame,
val_range: tuple[int, int] = (1, 5),
*,
allow_na: bool = False,
) -> list[int] | None
Perform basic quality checks on PAQ (Likert scale) data.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
DataFrame containing PAQ data
TYPE:
|
allow_na
|
Whether to allow NaN values in PAQ data, by default False
TYPE:
|
val_range
|
Valid range for PAQ values, by default (1, 5) |
| RETURNS | DESCRIPTION |
|---|---|
list[int] | None
|
|
Examples:
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame({
... 'PAQ1': [np.nan, 2, 3, 3], 'PAQ2': [3, 2, 6, 3], 'PAQ3': [2, 2, 3, 3],
... 'PAQ4': [1, 2, 3, 3], 'PAQ5': [5, 2, 3, 3], 'PAQ6': [3, 2, 3, 3],
... 'PAQ7': [4, 2, 3, 3], 'PAQ8': [2, 2, 3, 3]
... })
>>> likert_data_quality(df)
[0, 1, 2]
>>> likert_data_quality(df,allow_na=True)
[1, 2]
Source code in src/soundscapy/surveys/processing.py
simulation
¶
simulation(
n: int = 3000,
val_range: tuple[int, int] = (1, 5),
*,
seed: int | None = None,
incl_iso_coords: bool = False,
**coord_kwargs: Unpack[_AddISOCoordsKwargs],
) -> pd.DataFrame
Generate random PAQ responses for simulation purposes.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of samples to simulate, by default 3000
TYPE:
|
val_range
|
Range of values for PAQ responses, by default (1, 5) |
seed
|
Optional random seed for deterministic output, by default None
TYPE:
|
incl_iso_coords
|
Whether to add calculated ISO coordinates, by default False
TYPE:
|
**coord_kwargs
|
Optional keyword arguments passed directly to the
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame of randomly generated PAQ responses |
Examples:
>>> data = simulation(n=5,incl_iso_coords=True)
>>> data.shape
(5, 10)
>>> list(data.columns)
['PAQ1', 'PAQ2', 'PAQ3', 'PAQ4', 'PAQ5', 'PAQ6', 'PAQ7', 'PAQ8', 'ISOPleasant', 'ISOEventful']
Source code in src/soundscapy/surveys/processing.py
ssm_metrics
¶
ssm_metrics(
df: DataFrame,
paq_cols: list[str] = PAQ_IDS,
method: str = "cosine",
val_range: tuple[int, int] = (5, 1),
angles: tuple[int, ...] = EQUAL_ANGLES,
) -> pd.DataFrame
Calculate the Structural Summary Method (SSM) metrics for each response.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
DataFrame containing PAQ data
TYPE:
|
paq_cols
|
List of PAQ column names, by default PAQ_IDS |
method
|
Method to calculate SSM metrics, either "cosine" or "polar", by default "cosine"
TYPE:
|
val_range
|
Range of values for PAQ responses, by default (5, 1) |
angles
|
Angles for each PAQ in degrees, by default EQUAL_ANGLES |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame containing the SSM metrics |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If PAQ columns are not present in the DataFrame or if an invalid method is specified |
Examples:
>>>
>>> import pandas as pd
>>> data = pd.DataFrame({
... 'PAQ1': [4, 2], 'PAQ2': [3, 5], 'PAQ3': [2, 4], 'PAQ4': [1, 3],
... 'PAQ5': [5, 1], 'PAQ6': [3, 2], 'PAQ7': [4, 3], 'PAQ8': [2, 5]
... })
>>> ssm_metrics(data).round(2)
amplitude angle elevation displacement r_squared
0 0.68 263.82 10.57 -7.57 0.15
1 1.21 20.63 0.01 3.11 0.39
Source code in src/soundscapy/surveys/processing.py
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 | |
ssm_cosine_fit
¶
ssm_cosine_fit(
y: Series,
angles: tuple[int, ...] | ndarray = EQUAL_ANGLES,
bounds: tuple[list[float], list[float]] = (
[0, 0, 0, -np.inf],
[np.inf, 360, np.inf, np.inf],
),
) -> SSMMetrics
Fit a cosine model to the PAQ data for SSM analysis.
| PARAMETER | DESCRIPTION |
|---|---|
y
|
Series of PAQ values
TYPE:
|
angles
|
Angles for each PAQ in degrees, by default EQUAL_ANGLES |
bounds
|
Bounds for the optimization parameters, by default ([0, 0, 0, -np.inf], [np.inf, 360, np.inf, np.inf])
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SSMMetrics
|
Calculated SSM metrics |
Examples:
>>>
>>> import pandas as pd
>>> y = pd.Series([4, 3, 2, 1, 5, 3, 4, 2])
>>> metrics = ssm_cosine_fit(y)
>>> [round(v, 2) if isinstance(v, float) else v for v in metrics.table()]
[0.68, 263.82, 10.57, -7.57, 0.15]
Source code in src/soundscapy/surveys/processing.py
ipsatize
¶
ipsatize(
data: DataFrame,
method: Literal[
"grand_mean", "column_wise", "row_wise"
] = "grand_mean",
participant_col: str = "participant",
scales: list[str] | None = None,
) -> pd.DataFrame
Participant-level ipsatization for circumplex analysis.
Removes systematic response biases before computing a correlation matrix. The choice of method depends on the study design and the type of bias being corrected.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
DataFrame containing PAQ scale columns and (for participant-level methods) a grouping column.
TYPE:
|
method
|
Centering strategy:
TYPE:
|
participant_col
|
Column used to group observations by participant. Required for
TYPE:
|
scales
|
PAQ column names to centre. Defaults to :data: |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame containing only the scale columns with centred values.
The |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If |
Examples:
>>> import pandas as pd
>>> data = pd.DataFrame({
... 'PAQ1': [50., 60., 40., 30.], 'PAQ2': [50., 60., 40., 30.],
... 'PAQ3': [50., 60., 40., 30.], 'PAQ4': [50., 60., 40., 30.],
... 'PAQ5': [50., 60., 40., 30.], 'PAQ6': [50., 60., 40., 30.],
... 'PAQ7': [50., 60., 40., 30.], 'PAQ8': [50., 60., 40., 30.],
... 'participant': ['A', 'A', 'B', 'B'],
... })
>>> result = ipsatize(data, method="grand_mean")
>>> result['PAQ1'].tolist()
[-5.0, 5.0, 5.0, -5.0]
Source code in src/soundscapy/surveys/processing.py
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 718 719 720 | |
Survey utilities¶
soundscapy.surveys.survey_utils
¶
Core utility functions for processing soundscape survey data.
This module contains fundamental functions and constants used across the soundscapy package for handling and analyzing soundscape survey data.
| CLASS | DESCRIPTION |
|---|---|
PAQ |
Enumeration of Perceptual Attribute Questions (PAQ) names and IDs. |
PAQDfSchema |
Pandera schema for validating PAQ (Perceptual Attribute Questions) DataFrames. |
LikertScale |
Contains different Likert scale options for survey questions. |
| FUNCTION | DESCRIPTION |
|---|---|
return_paqs |
Return only the PAQ columns from a DataFrame. |
rename_paqs |
Rename the PAQ columns in a DataFrame to standard PAQ IDs. |
mean_responses |
Calculate the mean responses for each PAQ group. |
PAQ
¶
Bases: Enum
Enumeration of Perceptual Attribute Questions (PAQ) names and IDs.
Initialize a PAQ enum member.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The descriptive label for the PAQ (e.g., 'pleasant').
TYPE:
|
id
|
The standard identifier for the PAQ (e.g., 'PAQ1').
TYPE:
|
Source code in src/soundscapy/surveys/survey_utils.py
PAQDfSchema
¶
Bases: DataFrameModel
Pandera schema for validating PAQ (Perceptual Attribute Questions) DataFrames.
This schema defines the expected structure and data types for DataFrames containing soundscape survey data with PAQ responses and associated metadata. It includes automatic column name coercion to standardize various input formats.
| ATTRIBUTE | DESCRIPTION |
|---|---|
PAQ1-PAQ8 |
Perceptual Attribute Question responses (1-8) on a Likert scale. Nullable to allow for missing responses.
TYPE:
|
language |
Language code for the survey responses. Optional field.
TYPE:
|
location_id |
Identifier for the survey location. Optional field.
TYPE:
|
session_id |
Identifier for the survey session. Optional field.
TYPE:
|
group_id |
Identifier for the survey group. Optional field.
TYPE:
|
record_id |
Unique identifier for each survey record. Optional field.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
column_name_coercion |
Coerce column names to standardized format for PAQ data. |
column_name_coercion
¶
Coerce column names to standardized format for PAQ data.
This parser automatically renames columns to match the expected schema:
- PAQ label names (e.g., 'pleasant') to PAQ IDs (e.g., 'PAQ1')
- Legacy ID column names to lowercase snake_case format
| PARAMETER | DESCRIPTION |
|---|---|
cls
|
The schema class (automatically passed by pandera).
|
df
|
Input DataFrame with potentially non-standard column names.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with standardized column names. |
Source code in src/soundscapy/surveys/survey_utils.py
LikertScale
dataclass
¶
LikertScale(
paq: Scale = (
lambda: [
"Strongly disagree",
"Somewhat disagree",
"Neutral",
"Somewhat agree",
"Strongly agree",
]
)(),
source: Scale = (
lambda: [
"Not at all",
"A little",
"Moderately",
"A lot",
"Dominates completely",
]
)(),
overall: Scale = (
lambda: [
"Very bad",
"Bad",
"Neither bad nor good",
"Good",
"Very good",
]
)(),
appropriate: Scale = (
lambda: [
"Not at all",
"A little",
"Moderately",
"A lot",
"Perfectly",
]
)(),
loud: Scale = (
lambda: [
"Not at all",
"A little",
"Moderately",
"Very",
"Extremely",
]
)(),
often: Scale = (
lambda: [
"Never / This is my first time here",
"Rarely",
"Sometimes",
"Often",
"Very often",
]
)(),
visit: Scale = (
lambda: [
"Never",
"Rarely",
"Sometimes",
"Often",
"Very often",
]
)(),
)
Contains different Likert scale options for survey questions.
This class provides standardized 5-point Likert scales questions commonly used in acoustic and soundscape surveys.
| ATTRIBUTE | DESCRIPTION |
|---|---|
PAQ |
Agreement scale from "Strongly disagree" to "Strongly agree"
|
SOURCE |
Source perception scale from "Not at all" to "Dominates completely"
|
OVERALL |
Quality assessment scale from "Very bad" to "Very good"
|
APPROPRIATE |
Appropriateness scale from "Not at all" to "Perfectly"
|
LOUD |
Loudness perception scale from "Not at all" to "Extremely"
|
OFTEN |
Frequency scale with first-time option from "Never / This is my first time here" to "Very often"
|
VISIT |
Standard frequency scale from "Never" to "Very often"
|
return_paqs
¶
return_paqs(
df: DataFrame,
other_cols: list[str] | None = None,
*,
incl_ids: bool = True,
) -> pd.DataFrame
Return only the PAQ columns from a DataFrame.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing PAQ data.
TYPE:
|
other_cols
|
Other columns to include in the output, by default None. |
incl_ids
|
Whether to include ID columns (RecordID, GroupID, etc.), by default True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame containing only the PAQ columns and optionally ID and other specified columns. |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'RecordID': [1, 2],
... 'PAQ1': [4, 3],
... 'PAQ2': [2, 5],
... 'PAQ3': [1, 2],
... 'PAQ4': [3, 4],
... 'PAQ5': [5, 1],
... 'PAQ6': [2, 3],
... 'PAQ7': [4, 5],
... 'PAQ8': [1, 2],
... 'OtherCol': ['A', 'B']
... })
>>> return_paqs(df)
RecordID PAQ1 PAQ2 PAQ3 PAQ4 PAQ5 PAQ6 PAQ7 PAQ8
0 1 4 2 1 3 5 2 4 1
1 2 3 5 2 4 1 3 5 2
>>> return_paqs(df, incl_ids=False, other_cols=['OtherCol'])
PAQ1 PAQ2 PAQ3 PAQ4 PAQ5 PAQ6 PAQ7 PAQ8 OtherCol
0 4 2 1 3 5 2 4 1 A
1 3 5 2 4 1 3 5 2 B
Source code in src/soundscapy/surveys/survey_utils.py
rename_paqs
¶
Rename the PAQ columns in a DataFrame to standard PAQ IDs.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing PAQ data.
TYPE:
|
paq_aliases
|
Specify which PAQs are to be renamed. If None, will check if the column names are in pre-defined options. If a tuple, the order must match PAQ_IDS. If a dict, keys are current names and values are desired PAQ IDs. |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with renamed PAQ columns. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If paq_aliases is not a tuple, list, or dictionary. |
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({
... 'pleasant': [4, 3],
... 'vibrant': [2, 5],
... 'other_col': [1, 2]
... })
>>> rename_paqs(df)
PAQ1 PAQ2 other_col
0 4 2 1
1 3 5 2
>>> df_custom = pd.DataFrame({
... 'pl': [4, 3],
... 'vb': [2, 5],
... })
>>> rename_paqs(df_custom, paq_aliases={'pl': 'PAQ1', 'vb': 'PAQ2'})
PAQ1 PAQ2
0 4 2
1 3 5
Source code in src/soundscapy/surveys/survey_utils.py
mean_responses
¶
Calculate the mean responses for each PAQ group.
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Input DataFrame containing PAQ data.
TYPE:
|
group
|
Column name to group by.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
DataFrame with mean responses for each PAQ group. |