Utils¶
Package¶
utils
¶
utils¶
Shared utility functions and helpers for psyphy.
This subpackage provides: - candidates : functions for generating candidate stimulus pools. - math : mathematical utilities (basis functions, distances, kernels). - rng : random number handling for reproducibility.
MVP implementation
- candidates: grid, Sobol, custom pools.
- math: Chebyshev basis, Mahalanobis distance, RBF kernel.
- rng: seed() and split() for JAX PRNG keys.
Full WPPM mode
- candidates: adaptive refinement around posterior uncertainty.
- math: richer kernels and basis expansions for Wishart processes.
- rng: experiment-wide RNG registry.
Functions:
Name | Description |
---|---|
chebyshev_basis |
Construct the Chebyshev polynomial basis matrix T_0..T_degree evaluated at x. |
custom_candidates |
Wrap a user-defined list of probes into candidate pairs. |
grid_candidates |
Generate grid-based candidate probes around a reference. |
mahalanobis_distance |
Compute squared Mahalanobis distance between x and mean. |
rbf_kernel |
Radial Basis Function (RBF) kernel between two sets of points. |
seed |
Create a new PRNG key from an integer seed. |
sobol_candidates |
Generate Sobol quasi-random candidates within bounds. |
split |
Split a PRNG key into multiple independent keys. |
chebyshev_basis
¶
chebyshev_basis(x: ndarray, degree: int) -> ndarray
Construct the Chebyshev polynomial basis matrix T_0..T_degree evaluated at x.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Input points of shape (N,). For best numerical properties, values should lie in [-1, 1]. |
required |
degree
|
int
|
Maximum polynomial degree (>= 0). The output includes columns for T_0 through T_degree. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Array of shape (N, degree + 1) where column j contains T_j(x). |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Notes
Uses the three-term recurrence: T_0(x) = 1 T_1(x) = x T_{n+1}(x) = 2 x T_n(x) - T_{n-1}(x) The Chebyshev polynomials are orthogonal on [-1, 1] with weight (1 / sqrt(1 - x^2)).
Examples:
Source code in src/psyphy/utils/math.py
custom_candidates
¶
Wrap a user-defined list of probes into candidate pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
(ndarray, shape(D))
|
Reference stimulus. |
required |
probe_list
|
list of jnp.ndarray
|
Explicitly chosen probe vectors. |
required |
Returns:
Type | Description |
---|---|
list of Stimulus
|
Candidate (reference, probe) pairs. |
Notes
- Useful when hardware constraints (monitor gamut, auditory frequencies) restrict the set of valid stimuli.
- Full WPPM mode: this pool could be pruned or expanded dynamically depending on posterior fit quality.
Source code in src/psyphy/utils/candidates.py
grid_candidates
¶
Generate grid-based candidate probes around a reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
(ndarray, shape(D))
|
Reference stimulus in model space. |
required |
radii
|
list of float
|
Distances from reference to probe. |
required |
directions
|
int
|
Number of angular directions. |
16
|
Returns:
Type | Description |
---|---|
list of Stimulus
|
Candidate (reference, probe) pairs. |
Notes
- MVP: probes lie on concentric circles around reference.
- Full WPPM mode: could adaptively refine grid around regions of high posterior uncertainty.
Source code in src/psyphy/utils/candidates.py
mahalanobis_distance
¶
Compute squared Mahalanobis distance between x and mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Data vector, shape (D,). |
required |
mean
|
ndarray
|
Mean vector, shape (D,). |
required |
cov_inv
|
ndarray
|
Inverse covariance matrix, shape (D, D). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Scalar squared Mahalanobis distance. |
Notes
- Formula: d^2 = (x - mean)^T Σ^{-1} (x - mean)
- Used in WPPM discriminability calculations.
Source code in src/psyphy/utils/math.py
rbf_kernel
¶
rbf_kernel(
x1: ndarray, x2: ndarray, lengthscale: float = 1.0
) -> ndarray
Radial Basis Function (RBF) kernel between two sets of points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
ndarray
|
First set of points, shape (N, D). |
required |
x2
|
ndarray
|
Second set of points, shape (M, D). |
required |
lengthscale
|
float
|
Length-scale parameter controlling smoothness. |
1.0
|
Returns:
Type | Description |
---|---|
ndarray
|
Kernel matrix of shape (N, M). |
Notes
- RBF kernel: k(x, x') = exp(-||x - x'||^2 / (2 * lengthscale^2))
- Default used for Gaussian processes for smooth covariance priors in Full WPPM mode.
Source code in src/psyphy/utils/math.py
seed
¶
seed(seed_value: int) -> KeyArray
Create a new PRNG key from an integer seed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed_value
|
int
|
Seed for random number generation. |
required |
Returns:
Type | Description |
---|---|
KeyArray
|
New PRNG key. |
Source code in src/psyphy/utils/rng.py
sobol_candidates
¶
sobol_candidates(
reference: ndarray,
n: int,
bounds: List[Tuple[float, float]],
seed: int = 0,
) -> List[Stimulus]
Generate Sobol quasi-random candidates within bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
(ndarray, shape(D))
|
Reference stimulus. |
required |
n
|
int
|
Number of candidates to generate. |
required |
bounds
|
list of (low, high)
|
Bounds per dimension. |
required |
seed
|
int
|
Random seed. |
0
|
Returns:
Type | Description |
---|---|
list of Stimulus
|
Candidate (reference, probe) pairs. |
Notes
- MVP: uniform coverage of space using low-discrepancy Sobol sequence.
- Full WPPM mode: Sobol could be used for initialization, then hand off to posterior-aware strategies.
Source code in src/psyphy/utils/candidates.py
split
¶
Split a PRNG key into multiple independent keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
KeyArray
|
RNG key to split. |
required |
num
|
int
|
Number of new keys to return. |
2
|
Returns:
Type | Description |
---|---|
tuple of jax.random.KeyArray
|
Independent new PRNG keys. |
Source code in src/psyphy/utils/rng.py
RNG¶
rng
¶
rng.py
Random number utilities for psyphy.
This module standardizes RNG handling across the package, especially important when mixing NumPy and JAX.
MVP implementation: - Wrappers around JAX PRNG keys. - Helpers for reproducibility.
Future extensions: - Experiment-wide RNG registry. - Splitting strategies for parallel adaptive placement.
Examples:
Functions:
Name | Description |
---|---|
seed |
Create a new PRNG key from an integer seed. |
split |
Split a PRNG key into multiple independent keys. |
seed
¶
seed(seed_value: int) -> KeyArray
Create a new PRNG key from an integer seed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed_value
|
int
|
Seed for random number generation. |
required |
Returns:
Type | Description |
---|---|
KeyArray
|
New PRNG key. |
Source code in src/psyphy/utils/rng.py
split
¶
Split a PRNG key into multiple independent keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
KeyArray
|
RNG key to split. |
required |
num
|
int
|
Number of new keys to return. |
2
|
Returns:
Type | Description |
---|---|
tuple of jax.random.KeyArray
|
Independent new PRNG keys. |
Source code in src/psyphy/utils/rng.py
Math¶
math
¶
math.py
Math utilities for psyphy.
Includes: - chebyshev_basis : compute Chebyshev polynomial basis. - mahalanobis_distance : discriminability metric used in WPPM MVP. - rbf_kernel : kernel function, useful in Full WPPM mode covariance priors.
All functions use JAX (jax.numpy) for compatibility with autodiff.
Notes
- math.chebyshev_basis is relevant when implementing Full WPPM mode, where covariance fields are expressed in a basis expansion.
- math.mahalanobis_distance is directly used in WPPM MVP discriminability.
- math.rbf_kernel is a placeholder for Gaussian-process-style covariance priors.
Examples:
Functions:
Name | Description |
---|---|
chebyshev_basis |
Construct the Chebyshev polynomial basis matrix T_0..T_degree evaluated at x. |
mahalanobis_distance |
Compute squared Mahalanobis distance between x and mean. |
rbf_kernel |
Radial Basis Function (RBF) kernel between two sets of points. |
chebyshev_basis
¶
chebyshev_basis(x: ndarray, degree: int) -> ndarray
Construct the Chebyshev polynomial basis matrix T_0..T_degree evaluated at x.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Input points of shape (N,). For best numerical properties, values should lie in [-1, 1]. |
required |
degree
|
int
|
Maximum polynomial degree (>= 0). The output includes columns for T_0 through T_degree. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Array of shape (N, degree + 1) where column j contains T_j(x). |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Notes
Uses the three-term recurrence: T_0(x) = 1 T_1(x) = x T_{n+1}(x) = 2 x T_n(x) - T_{n-1}(x) The Chebyshev polynomials are orthogonal on [-1, 1] with weight (1 / sqrt(1 - x^2)).
Examples:
Source code in src/psyphy/utils/math.py
mahalanobis_distance
¶
Compute squared Mahalanobis distance between x and mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
ndarray
|
Data vector, shape (D,). |
required |
mean
|
ndarray
|
Mean vector, shape (D,). |
required |
cov_inv
|
ndarray
|
Inverse covariance matrix, shape (D, D). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Scalar squared Mahalanobis distance. |
Notes
- Formula: d^2 = (x - mean)^T Σ^{-1} (x - mean)
- Used in WPPM discriminability calculations.
Source code in src/psyphy/utils/math.py
rbf_kernel
¶
rbf_kernel(
x1: ndarray, x2: ndarray, lengthscale: float = 1.0
) -> ndarray
Radial Basis Function (RBF) kernel between two sets of points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x1
|
ndarray
|
First set of points, shape (N, D). |
required |
x2
|
ndarray
|
Second set of points, shape (M, D). |
required |
lengthscale
|
float
|
Length-scale parameter controlling smoothness. |
1.0
|
Returns:
Type | Description |
---|---|
ndarray
|
Kernel matrix of shape (N, M). |
Notes
- RBF kernel: k(x, x') = exp(-||x - x'||^2 / (2 * lengthscale^2))
- Default used for Gaussian processes for smooth covariance priors in Full WPPM mode.
Source code in src/psyphy/utils/math.py
Stimulus candidates¶
candidates
¶
candidates.py
Utilities for generating candidate stimulus pools.
Definition
A candidate pool is the set of all possible (reference, probe) pairs that an adaptive placement strategy may select from.
Separation of concerns
- Candidate generation (this module) defines what stimuli are possible.
- Trial placement strategies (e.g., GreedyMAPPlacement, InfoGainPlacement) define which of those candidates to present next.
Why this matters
- Researchers: think of the candidate pool as the "menu" of allowable trials.
- Developers: placement strategies should not generate candidates but only select from a given pool.
MVP implementation
- Grid-based candidates (probes on circles around a reference).
- Sobol sequence candidates (low-discrepancy exploration).
- Custom user-defined candidate pools.
Full WPPM mode
- Candidate generation could adaptively refine itself based on posterior uncertainty (e.g., dynamic grids).
- Candidate pools could be constrained by device gamut or subject-specific calibration.
Functions:
Name | Description |
---|---|
custom_candidates |
Wrap a user-defined list of probes into candidate pairs. |
grid_candidates |
Generate grid-based candidate probes around a reference. |
sobol_candidates |
Generate Sobol quasi-random candidates within bounds. |
Attributes:
Name | Type | Description |
---|---|---|
Stimulus |
|
custom_candidates
¶
Wrap a user-defined list of probes into candidate pairs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
(ndarray, shape(D))
|
Reference stimulus. |
required |
probe_list
|
list of jnp.ndarray
|
Explicitly chosen probe vectors. |
required |
Returns:
Type | Description |
---|---|
list of Stimulus
|
Candidate (reference, probe) pairs. |
Notes
- Useful when hardware constraints (monitor gamut, auditory frequencies) restrict the set of valid stimuli.
- Full WPPM mode: this pool could be pruned or expanded dynamically depending on posterior fit quality.
Source code in src/psyphy/utils/candidates.py
grid_candidates
¶
Generate grid-based candidate probes around a reference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
(ndarray, shape(D))
|
Reference stimulus in model space. |
required |
radii
|
list of float
|
Distances from reference to probe. |
required |
directions
|
int
|
Number of angular directions. |
16
|
Returns:
Type | Description |
---|---|
list of Stimulus
|
Candidate (reference, probe) pairs. |
Notes
- MVP: probes lie on concentric circles around reference.
- Full WPPM mode: could adaptively refine grid around regions of high posterior uncertainty.
Source code in src/psyphy/utils/candidates.py
sobol_candidates
¶
sobol_candidates(
reference: ndarray,
n: int,
bounds: List[Tuple[float, float]],
seed: int = 0,
) -> List[Stimulus]
Generate Sobol quasi-random candidates within bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reference
|
(ndarray, shape(D))
|
Reference stimulus. |
required |
n
|
int
|
Number of candidates to generate. |
required |
bounds
|
list of (low, high)
|
Bounds per dimension. |
required |
seed
|
int
|
Random seed. |
0
|
Returns:
Type | Description |
---|---|
list of Stimulus
|
Candidate (reference, probe) pairs. |
Notes
- MVP: uniform coverage of space using low-discrepancy Sobol sequence.
- Full WPPM mode: Sobol could be used for initialization, then hand off to posterior-aware strategies.