SPI¶
soundscapy.spi
¶
Multi-skew normal model¶
soundscapy.spi.msn
¶
Module for handling Multi-dimensional Skewed Normal (MSN) distributions.
Provides classes and functions for defining, fitting, sampling, and analyzing MSN distributions, often used in soundscape analysis for modeling ISOPleasant and ISOEventful ratings.
| CLASS | DESCRIPTION |
|---|---|
DirectParams |
Container for direct parameters (xi, omega, alpha) of a skew-normal distribution. |
CentredParams |
Container for centred parameters (mean, sigma, skew) of a skew-normal distribution. |
MultiSkewNorm |
High-level interface for fitting, sampling, and scoring a 2-D skew-normal model. |
| FUNCTION | DESCRIPTION |
|---|---|
dp2cp |
Convert a :class: |
cp2dp |
Convert a :class: |
spi_score |
Soundscape Perception Index: |
DirectParams
¶
Represents a set of direct parameters for a statistical model.
Direct parameters are the parameters that are directly used in the model. They are the parameters that are used to define the distribution of the data. In the case of a skew normal distribution, the direct parameters are the xi, omega, and alpha values.
| PARAMETER | DESCRIPTION |
|---|---|
xi
|
The location of the distribution in 2D space, represented as a 2x1 array with the x and y coordinates.
TYPE:
|
omega
|
The covariance matrix of the distribution, represented as a 2x2 array. The covariance matrix represents the measure of the relationship between different variables. It provides information about how changes in one variable are associated with changes in other variables.
TYPE:
|
alpha
|
The shape parameters for the x and y dimensions, controlling the shape (skewness) of the distribution. It is represented as a 2x1 array.
TYPE:
|
Initialize DirectParams instance.
| METHOD | DESCRIPTION |
|---|---|
__repr__ |
Return a string representation of the DirectParams object. |
__str__ |
Return a user-friendly string representation of the DirectParams object. |
validate |
Validate the direct parameters. |
from_cp |
Convert a CentredParams object to a DirectParams object. |
Source code in src/soundscapy/spi/msn.py
__repr__
¶
__str__
¶
Return a user-friendly string representation of the DirectParams object.
validate
¶
Validate the direct parameters.
In a skew normal distribution, the covariance matrix, often denoted as Ω (Omega), represents the measure of the relationship between different variables. It provides information about how changes in one variable are associated with changes in other variables. The covariance matrix must be positive definite and symmetric.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the direct parameters are not valid. |
| RETURNS | DESCRIPTION |
|---|---|
None
|
|
Source code in src/soundscapy/spi/msn.py
from_cp
classmethod
¶
Convert a CentredParams object to a DirectParams object.
| PARAMETER | DESCRIPTION |
|---|---|
cp
|
The CentredParams object to convert.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DirectParams
|
A new DirectParams object with the converted parameters. |
Source code in src/soundscapy/spi/msn.py
CentredParams
¶
Represents the centered parameters of a distribution.
| PARAMETER | DESCRIPTION |
|---|---|
mean
|
The mean of the distribution.
TYPE:
|
sigma
|
The standard deviation of the distribution.
TYPE:
|
skew
|
The skewness of the distribution.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
mean |
The mean of the distribution.
|
sigma |
The standard deviation of the distribution.
|
skew |
The skewness of the distribution.
|
| METHOD | DESCRIPTION |
|---|---|
from_dp |
Converts DirectParams object to CentredParams object. |
Initialize CentredParams instance.
Source code in src/soundscapy/spi/msn.py
__repr__
¶
__str__
¶
Return a user-friendly string representation of the CentredParams object.
Source code in src/soundscapy/spi/msn.py
from_dp
classmethod
¶
Convert a DirectParams object to a CentredParams object.
| PARAMETER | DESCRIPTION |
|---|---|
dp
|
The DirectParams object to convert.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CentredParams
|
A new CentredParams object with the converted parameters. |
Source code in src/soundscapy/spi/msn.py
MultiSkewNorm
¶
A class representing a multi-dimensional skewed normal distribution.
| ATTRIBUTE | DESCRIPTION |
|---|---|
cp |
The centred parameters of the fitted model.
|
dp |
The direct parameters of the fitted model.
|
sample_data |
The generated sample data from the fitted model.
|
data |
The input data used for fitting the model.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
summary |
Prints a summary of the fitted model. |
fit |
Fits the model to the provided data. |
define_dp |
Defines the direct parameters of the model. |
sample |
Generates an unrestricted sample from the fitted model. |
sample_mtsn |
Generates a truncated sample (rejection sampling within [a, b]). |
sspy_plot |
Plots the joint distribution of the generated sample. |
ks2d2s |
Computes the two-sample, two-dimensional Kolmogorov-Smirnov statistic. |
spi_score |
Computes the Soundscape Perception Index (SPI). |
Initialize the MultiSkewNorm object.
Source code in src/soundscapy/spi/msn.py
__repr__
¶
Return a string representation of the MultiSkewNorm object.
summary
¶
Provide a summary of the fitted MultiSkewNorm model.
| RETURNS | DESCRIPTION |
|---|---|
str
|
indicating the model is not fitted. |
Source code in src/soundscapy/spi/msn.py
fit
¶
fit(
data: DataFrame | ndarray | None = None,
x: ndarray | Series | None = None,
y: ndarray | Series | None = None,
) -> None
Fit the multi-dimensional skewed normal model to the provided data.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The input data as a pandas DataFrame or numpy array.
TYPE:
|
x
|
The x-values of the input data as a numpy array or pandas Series.
TYPE:
|
y
|
The y-values of the input data as a numpy array or pandas Series.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If neither |
Source code in src/soundscapy/spi/msn.py
define_dp
¶
Initiate a distribution from the direct parameters.
| PARAMETER | DESCRIPTION |
|---|---|
xi
|
The xi values of the direct parameters.
TYPE:
|
omega
|
The omega values of the direct parameters.
TYPE:
|
alpha
|
The alpha values of the direct parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MultiSkewNorm
|
|
Source code in src/soundscapy/spi/msn.py
from_params
classmethod
¶
from_params(
params: DirectParams | CentredParams | None = None,
*,
xi: ndarray | None = None,
omega: ndarray | None = None,
alpha: ndarray | None = None,
mean: ndarray | None = None,
sigma: ndarray | None = None,
skew: ndarray | None = None,
) -> MultiSkewNorm
Create a MultiSkewNorm instance from direct parameters.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
The direct parameters to initialize the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MultiSkewNorm
|
A new instance of MultiSkewNorm initialized with the provided parameters. |
Source code in src/soundscapy/spi/msn.py
sample
¶
Generate a sample from the fitted model.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
The number of samples to generate, by default 1000.
TYPE:
|
return_sample
|
Whether to return the generated sample as an np.ndarray, by default False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None | ndarray
|
The generated sample if |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the model is not fitted (i.e., |
Source code in src/soundscapy/spi/msn.py
sample_mtsn
¶
sample_mtsn(
n: int = 1000,
a: float = -1,
b: float = 1,
*,
return_sample: bool = False,
) -> None | np.ndarray
Generate a sample from the multi-dimensional truncated skew-normal distribution.
Uses rejection sampling to ensure that the samples are within the bounds [a, b] for both dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
The number of samples to generate, by default 1000.
TYPE:
|
a
|
Lower truncation bound for both dimensions, by default -1.
TYPE:
|
b
|
Upper truncation bound for both dimensions, by default 1.
TYPE:
|
return_sample
|
Whether to return the generated sample as an np.ndarray, by default False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None | ndarray
|
The generated sample if |
Source code in src/soundscapy/spi/msn.py
sspy_plot
¶
Plot the joint distribution of the generated sample using soundscapy.
| PARAMETER | DESCRIPTION |
|---|---|
color
|
Color for the density plot, by default "blue".
TYPE:
|
title
|
Title for the plot, by default None.
TYPE:
|
n
|
Number of samples to generate if
TYPE:
|
Source code in src/soundscapy/spi/msn.py
ks2d2s
¶
Compute the two-sample, two-dimensional Kolmogorov-Smirnov statistic.
| PARAMETER | DESCRIPTION |
|---|---|
test
|
The test data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float]
|
The KS2D statistic and p-value. |
Source code in src/soundscapy/spi/msn.py
spi_score
¶
Compute the Soundscape Perception Index (SPI).
Calculates the SPI for the test data against the target distribution represented by this MultiSkewNorm instance.
| PARAMETER | DESCRIPTION |
|---|---|
test
|
The test data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The Soundscape Perception Index (SPI), ranging from 0 to 100. |
Source code in src/soundscapy/spi/msn.py
spi_score
¶
Compute the Soundscape Perception Index (SPI).
Calculates the SPI for the test data against the target distribution represented by the sample data.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
The sample data representing the target distribution.
TYPE:
|
test
|
The test data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The Soundscape Perception Index (SPI), ranging from 0 to 100. |
Source code in src/soundscapy/spi/msn.py
ks2d
¶
Compute the two-sample, two-dimensional Kolmogorov-Smirnov statistic.
| PARAMETER | DESCRIPTION |
|---|---|
target
|
The sample data representing the target distribution.
TYPE:
|
test
|
The test data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float]
|
The KS2D statistic and p-value. |
Source code in src/soundscapy/spi/msn.py
cp2dp
¶
Convert centred parameters to direct parameters.
| PARAMETER | DESCRIPTION |
|---|---|
cp
|
The centred parameters object.
TYPE:
|
family
|
The distribution family, by default "SN" (Skew Normal).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DirectParams
|
The corresponding direct parameters object. |
Source code in src/soundscapy/spi/msn.py
dp2cp
¶
Convert direct parameters to centred parameters.
| PARAMETER | DESCRIPTION |
|---|---|
dp
|
The direct parameters object.
TYPE:
|
family
|
The distribution family, by default "SN" (Skew Normal).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CentredParams
|
The corresponding centred parameters object. |
Source code in src/soundscapy/spi/msn.py
KS2D utilities¶
soundscapy.spi.ks2d
¶
| FUNCTION | DESCRIPTION |
|---|---|
CountQuads |
Compute probabilities by counting points in quadrants. |
FuncQuads |
Compute probabilities by integrating a density function in quadrants. |
Qks |
Compute the Kolmogorov-Smirnov probability function Q(lambda). |
ks2d2s |
Perform the 2-dimensional, 2-sample Kolmogorov-Smirnov test. |
ks2d1s |
Perform the 2-dimensional, 1-sample Kolmogorov-Smirnov test. |
CountQuads
¶
Compute probabilities by counting points in quadrants.
Computes the probabilities of finding points in each of the 4 quadrants
defined by a vertical and horizontal line crossing the given point.
The probabilities are determined by counting the proportion of points
from Arr2D that fall into each quadrant.
| PARAMETER | DESCRIPTION |
|---|---|
Arr2D
|
Array of 2D points (shape N x 2) to be counted.
TYPE:
|
point
|
A 1D array or list with 2 elements representing the center (x, y) of the 4 quadrants.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float, float, float]
|
A tuple containing four floats (fpp, fnp, fpn, fnn), representing the normalized fractions (probabilities) of points in each quadrant:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/soundscapy/spi/ks2d.py
FuncQuads
¶
Compute probabilities by integrating a density function in quadrants.
Computes the probabilities of finding points in each of the 4 quadrants
defined by a vertical and horizontal line crossing the given point.
The probabilities are determined by numerically integrating the 2D density
function func2D over each quadrant within the specified limits.
| PARAMETER | DESCRIPTION |
|---|---|
func2D
|
A 2D density function that accepts two arguments (x, y).
TYPE:
|
point
|
A 1D array or list with 2 elements representing the center (x, y) of the 4 quadrants.
TYPE:
|
xlim
|
A list or array with 2 elements defining the integration limits for x.
TYPE:
|
ylim
|
A list or array with 2 elements defining the integration limits for y.
TYPE:
|
rounddig
|
Number of decimal digits to round the resulting probabilities to, by default 4.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float, float, float]
|
A tuple containing four floats (fpp, fnp, fpn, fnn), representing the integrated probabilities in each quadrant, normalized by the total integral:
|
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/soundscapy/spi/ks2d.py
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 | |
Qks
¶
Compute the Kolmogorov-Smirnov probability function Q(lambda).
Calculates the significance level for a given KS statistic alam (D).
This function is based on the approximation given in Numerical Recipes in C,
page 623. It represents the probability that the KS statistic will exceed
the observed value alam under the null hypothesis.
| PARAMETER | DESCRIPTION |
|---|---|
alam
|
The KS statistic D (or a related value, often D * sqrt(N_eff)).
TYPE:
|
iter
|
Maximum number of iterations for the series summation, by default 100.
TYPE:
|
prec
|
Convergence precision. The summation stops if the absolute value of
the term to add is less than
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The significance level P(D > observed) associated with |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/soundscapy/spi/ks2d.py
ks2d2s
¶
Perform the 2-dimensional, 2-sample Kolmogorov-Smirnov test.
Tests the null hypothesis that two independent 2D samples, Arr2D1 and
Arr2D2, are drawn from the same underlying probability distribution.
This implementation is based on the methods described by Peacock (1983)
and Fasano & Franceschini (1987).
| PARAMETER | DESCRIPTION |
|---|---|
Arr2D1
|
First 2D sample array (shape N1 x 2).
TYPE:
|
Arr2D2
|
Second 2D sample array (shape N2 x 2).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float]
|
d : float
The 2D KS statistic, representing the maximum difference found
between the cumulative distributions in any of the four quadrants,
evaluated at all data points.
prob : float
The significance level (p-value) of the observed statistic |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/soundscapy/spi/ks2d.py
ks2d1s
¶
Perform the 2-dimensional, 1-sample Kolmogorov-Smirnov test.
Tests the null hypothesis that a 2D sample Arr2D is drawn from a
given 2D probability density distribution func2D.
| PARAMETER | DESCRIPTION |
|---|---|
Arr2D
|
The 2D sample array (shape N x 2).
TYPE:
|
func2D
|
The theoretical 2D probability density function func(x, y).
TYPE:
|
xlim
|
Integration limits for the x-dimension. If empty, defaults are
calculated based on the range of
TYPE:
|
ylim
|
Integration limits for the y-dimension. If empty, defaults are
calculated based on the range of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[float, float]
|
d : float
The 2D KS statistic, representing the maximum difference between
the empirical distribution (from |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If |
Source code in src/soundscapy/spi/ks2d.py
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 | |