petitRADTRANS.math#

Stores useful mathematical functions.

Functions#

bayes_factor2sigma(→ [float, jax.typing.ArrayLike])

Convert a Bayes factor, or "evidence", into a sigma significance. For Bayes factor higher than exp(25), the function

box_car_conv(→ jax.typing.ArrayLike)

calculate_uncertainty(→ [jax.typing.ArrayLike, None])

Calculate the uncertainty of a function f(x, y, ...) with uncertainties on x, y, ... and Pearson's correlation

compute_resolving_power(→ float)

Compute the mean resolving power of an array.

feature_scaling(→ jax.typing.ArrayLike)

Bring all values of array between a min and max value.

gaussian_kernel_1d(→ jaxtyping.Float[jaxtyping.Array, 2*])

JAX version of the gaussian kernel to be used in the gaussian_filter1d function.

gaussian_weights_running(→ jax.typing.ArrayLike)

Compute 1D Gaussian convolution kernels for an array of standard deviations.

longitude2phase(longitude)

median_filter(spectrum[, size, mode])

JAX-compatible implementation of scipy.ndimage.median_filter for 1D arrays.

mean_uncertainty(→ float)

Calculate the uncertainty of the mean of an array.

median_uncertainties(→ float)

Calculate the uncertainty of the median of an array.

normalize(→ jax.typing.ArrayLike)

phase2longitude(phase[, rad2deg])

prt_resolving_space(→ jax.typing.ArrayLike)

Return numbers evenly spaced at the specified resolving power.

resolving_space(start, stop, resolving_power)

running_mean(→ jax.typing.ArrayLike)

savgol_filter(spectrum[, window_length, polyorder, mode])

JAX-compatible Savitzky-Golay filter for 1D signals.

sigma2bayes_factor(→ float)

Convert a sigma significance into a Bayes factor, or "evidence".

Module Contents#

petitRADTRANS.math.bayes_factor2sigma(bayes_factor: float) [float, jax.typing.ArrayLike]#

Convert a Bayes factor, or “evidence”, into a sigma significance. For Bayes factor higher than exp(25), the function is approximated with a square root function. Note: sometimes algorithms return the “log-evidence”, or ln(z). The Bayes factor is z. Source: Benneke et al. 2013 https://iopscience.iop.org/article/10.1088/0004-637X/778/2/153 Molliere part by Paul Mollière. :param bayes_factor: Bayes factor (aka “evidence”) :return: sigma significance

petitRADTRANS.math.box_car_conv(array: jax.typing.ArrayLike, points: jax.typing.ArrayLike) jax.typing.ArrayLike#
petitRADTRANS.math.calculate_uncertainty(derivatives: jax.typing.ArrayLike, uncertainties: jax.typing.ArrayLike, covariance_matrix: jax.typing.ArrayLike = None) [jax.typing.ArrayLike, None]#

Calculate the uncertainty of a function f(x, y, …) with uncertainties on x, y, … and Pearson’s correlation coefficients between x, y, … The function must be (approximately) linear with its variables within the uncertainties of said variables. For independent variables, set the covariance matrix to identity. Uncertainties can be asymmetric, in that case for N variables, use a (N, 2) array for the uncertainties. Asymmetric uncertainties are handled the wrong way (see source 2), but it is better than nothing.

Sources:
  1. https://en.wikipedia.org/wiki/Propagation_of_uncertainty

  2. https://phas.ubc.ca/~oser/p509/Lec_10.pdf

  3. http://math.jacobs-university.de/oliver/teaching/jacobs/fall2015/esm106/handouts/error-propagation.pdf

Args:
derivatives:

Partial derivatives of the function with respect to each variable (df/dx, df/dy, …)

uncertainties:

Uncertainties of each variable (either a 1D-array or a 2D-array containing - and + unc.)

covariance_matrix:

Covariance matrix between the variables, by default set to the identity matrix

Returns:

A size-2 array containing the - and + uncertainties of the function

petitRADTRANS.math.compute_resolving_power(array: jax.typing.ArrayLike) float#

Compute the mean resolving power of an array.

Args:
array:

A 1-D array.

Returns:

The mean resolving power of the array.

petitRADTRANS.math.feature_scaling(array: jax.typing.ArrayLike, min_value: float = 0.0, max_value: float = 1.0) jax.typing.ArrayLike#

Bring all values of array between a min and max value.

Args:

array: array to normalize min_value: target minimum value max_value: target maximum value

Returns:

The normalized array with values between min_value and max_value.

petitRADTRANS.math.gaussian_kernel_1d(sigma: float, radius: int) jaxtyping.Float[jaxtyping.Array,  2*{radius}]#

JAX version of the gaussian kernel to be used in the gaussian_filter1d function.

Note that the implementation parallels that of scipy.ndimage at scipy/scipy.

petitRADTRANS.math.gaussian_weights_running(sigmas: jax.typing.ArrayLike, truncate: float = 4.0, max_radius: int = 20) jax.typing.ArrayLike#

Compute 1D Gaussian convolution kernels for an array of standard deviations.

This version supports JIT compilation by avoiding arrays whose shape depends on values computed at runtime. To guarantee static shapes when JIT-compiling, pass an integer max_radius (half-kernel size). If max_radius is not provided the radius will be computed in Python (non-JIT use).

Args:
sigmas:

1-D array of standard deviations for each position (shape: (N,)).

truncate:

Truncate the filter at this many standard deviations (used only when max_radius is None).

max_radius:

Optional integer radius to force a static kernel size of 2*max_radius+1. Recommended for JIT compilation.

Returns:
weights: array of shape (N, 2*radius+1) containing the normalized

Gaussian kernel for each sigma.

petitRADTRANS.math.longitude2phase(longitude: float)#
petitRADTRANS.math.median_filter(spectrum, size=10, mode='nearest')#

JAX-compatible implementation of scipy.ndimage.median_filter for 1D arrays. Applies a median filter with a sliding window of specified size.

Args:

spectrum: 1D array to filter size: Window size (will be made odd if even) mode: Padding mode (‘nearest’, ‘reflect’, ‘constant’, etc.)

  • ‘nearest’: pad with edge values

  • ‘reflect’: mirror values at boundaries

  • ‘constant’: pad with zeros

  • ‘edge’: same as ‘nearest’

Returns:

Filtered spectrum with same shape as input

petitRADTRANS.math.mean_uncertainty(uncertainties: jax.typing.ArrayLike) float#

Calculate the uncertainty of the mean of an array.

Args:

uncertainties: individual uncertainties of the averaged array

Returns:

The uncertainty of the mean of the array

petitRADTRANS.math.median_uncertainties(uncertainties: jax.typing.ArrayLike) float#

Calculate the uncertainty of the median of an array.

Demonstration:

uncertainty ~ standard deviation = sqrt(variance) = sqrt(V) V_mean / V_median = 2 * (N - 1) / (pi * N); (see source) => V_median = V_mean * pi * N / (2 * (N - 1)) => uncertainty_median = uncertainty_mean * sqrt(pi * N / (2 * (N - 1)))

Source:

https://mathworld.wolfram.com/StatisticalMedian.html

Args:

uncertainties: individual uncertainties of the median of the array

Returns:

The uncertainty of the median of the array

petitRADTRANS.math.normalize(array: jax.typing.ArrayLike, axis: int = None) jax.typing.ArrayLike#
petitRADTRANS.math.phase2longitude(phase: float, rad2deg: bool = False)#
petitRADTRANS.math.prt_resolving_space(start, stop, resolving_power) jax.typing.ArrayLike#

Return numbers evenly spaced at the specified resolving power.

Args:
start:

The starting value of the sequence.

stop:

The end value of the sequence.

resolving_power:

Resolving power of the sample

Returns:

Samples spaced following the specified resolving power.

petitRADTRANS.math.resolving_space(start, stop, resolving_power)#
petitRADTRANS.math.running_mean(x: jax.typing.ArrayLike, n: int) jax.typing.ArrayLike#
petitRADTRANS.math.savgol_filter(spectrum, window_length=11, polyorder=3, mode='nearest')#

JAX-compatible Savitzky-Golay filter for 1D signals. Fits a polynomial to a sliding window and evaluates it at the center.

Note: Edge behavior differs slightly from scipy due to JAX padding mode limitations. The interior points (away from edges) match scipy’s output to machine precision.

Args:

spectrum: 1D input array window_length: Length of the filter window (will be made odd if even) polyorder: Order of the polynomial to fit (must be < window_length) mode: Padding mode (‘nearest’→’edge’, ‘reflect’, ‘constant’, etc.)

At edges: JAX uses ‘edge’ padding which may differ slightly from scipy’s ‘nearest’

Returns:

Filtered spectrum (same shape as input)

petitRADTRANS.math.sigma2bayes_factor(sigma: float) float#

Convert a sigma significance into a Bayes factor, or “evidence”. Note: sometimes algorithms return the “log-evidence”, or ln(z). The Bayes factor is z. Source: Benneke et al. 2013 https://iopscience.iop.org/article/10.1088/0004-637X/778/2/153 :param sigma: sigma significance :return: Bayes factor (aka “evidence”)