R internals¶
These wrappers back the optional R-powered functionality exposed through
soundscapy.spi and soundscapy.satp.
soundscapy.r_wrapper._r_wrapper
¶
Internal R integration for skew-normal and circumplex model calculations.
Wraps rpy2 to expose R's sn package and the bundled CircE BFGS scripts.
Session state is held in a single module-level :class:RSession dataclass
instance (_state). Call :func:get_r_session to obtain it; the session
is initialised lazily on first access.
Not intended for direct use — all public names are re-exported from
soundscapy.spi and soundscapy.satp.
| CLASS | DESCRIPTION |
|---|---|
RSession |
State container for the active R session. |
| FUNCTION | DESCRIPTION |
|---|---|
get_r_session |
Return the active R session, initialising lazily on first call. |
reset_r_session |
Reset all session state, forcing re-initialisation on the next call. |
install_r_packages |
.. deprecated:: |
sample_mtsn |
Sample from a multivariate truncated skew-normal distribution. |
dp2cp |
Convert Direct Parameters (DP) to Centred Parameters (CP). |
cp2dp |
Convert Centred Parameters (CP) to Direct Parameters (DP). |
bfgs_fit |
Fit a circumplex model and return extracted fit statistics. |
RSession
dataclass
¶
State container for the active R session.
A single module-level instance (_state) holds the loaded R package
objects. :func:get_r_session initialises it lazily on first call;
:func:reset_r_session clears it to force re-initialisation.
| ATTRIBUTE | DESCRIPTION |
|---|---|
sn |
Loaded
TYPE:
|
base |
Loaded
TYPE:
|
active |
TYPE:
|
get_r_session
¶
Return the active R session, initialising lazily on first call.
| RETURNS | DESCRIPTION |
|---|---|
RSession
|
The module-level |
| RAISES | DESCRIPTION |
|---|---|
ImportError
|
If R is not installed or its version is too old, if the |
RuntimeError
|
If session initialisation fails for any other reason. |
Source code in src/soundscapy/r_wrapper/_r_wrapper.py
reset_r_session
¶
Reset all session state, forcing re-initialisation on the next call.
Note: the R process itself continues running — rpy2 does not support terminating the embedded R interpreter.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/soundscapy/r_wrapper/_r_wrapper.py
install_r_packages
¶
.. deprecated::
This function is a no-op and will be removed in a future release.
Install the R sn package directly from an R session::
install.packages('sn')
Source code in src/soundscapy/r_wrapper/_r_wrapper.py
sample_mtsn
¶
sample_mtsn(
selm_model: RS4 | None = None,
xi: ndarray | None = None,
omega: ndarray | None = None,
alpha: ndarray | None = None,
a: float = -1,
b: float = 1,
n: int = 1000,
max_iter: int = 100000,
) -> np.ndarray
Sample from a multivariate truncated skew-normal distribution.
Uses rejection sampling to ensure samples fall within [a, b] for both
dimensions.
| PARAMETER | DESCRIPTION |
|---|---|
selm_model
|
Fitted SELM model from R's
TYPE:
|
xi
|
Location parameter (2×1 array).
TYPE:
|
omega
|
Scale matrix (2×2 array).
TYPE:
|
alpha
|
Skewness parameter (2×1 array).
TYPE:
|
a
|
Lower truncation bound for both dimensions.
TYPE:
|
b
|
Upper truncation bound for both dimensions.
TYPE:
|
n
|
Number of samples to generate.
TYPE:
|
max_iter
|
Maximum total candidate draws before raising
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ndarray
|
Array of samples (n × 2). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If neither |
RuntimeError
|
If |
Source code in src/soundscapy/r_wrapper/_r_wrapper.py
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 | |
dp2cp
¶
dp2cp(
xi: ndarray,
omega: ndarray,
alpha: ndarray,
family: Literal["SN", "ESN", "ST", "SC"] = "SN",
) -> tuple
Convert Direct Parameters (DP) to Centred Parameters (CP).
| PARAMETER | DESCRIPTION |
|---|---|
xi
|
Location parameter (2×1 array).
TYPE:
|
omega
|
Scale matrix (2×2 array).
TYPE:
|
alpha
|
Skewness parameter (2×1 array).
TYPE:
|
family
|
Distribution family.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
Tuple of centred parameters |
Source code in src/soundscapy/r_wrapper/_r_wrapper.py
cp2dp
¶
cp2dp(
mean: ndarray,
sigma: ndarray,
skew: ndarray,
family: Literal["SN", "ESN", "ST", "SC"] = "SN",
) -> tuple
Convert Centred Parameters (CP) to Direct Parameters (DP).
| PARAMETER | DESCRIPTION |
|---|---|
mean
|
Mean vector (2×1 array).
TYPE:
|
sigma
|
Covariance matrix (2×2 array).
TYPE:
|
skew
|
Skewness vector (2×1 array).
TYPE:
|
family
|
Distribution family.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple
|
Tuple of direct parameters |
Source code in src/soundscapy/r_wrapper/_r_wrapper.py
bfgs_fit
¶
bfgs_fit(
data_cor: DataFrame,
n: int,
scales: list[str] = PAQ_IDS,
m_val: int = 3,
*,
equal_ang: bool = True,
equal_com: bool = True,
) -> dict[str, Any]
Fit a circumplex model and return extracted fit statistics.
Calls the embedded CircE BFGS implementation and converts the result to a Python dict with scalar normalisation and a scipy-computed p-value.
| PARAMETER | DESCRIPTION |
|---|---|
data_cor
|
Correlation matrix of the data.
TYPE:
|
n
|
Number of observations used to compute
TYPE:
|
scales
|
List of scale names. |
m_val
|
Number of Fourier dimensions.
TYPE:
|
equal_ang
|
Whether to enforce equal-angles constraint.
TYPE:
|
equal_com
|
Whether to enforce equal-communalities constraint.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
Dictionary of fit statistics. |