petitRADTRANS.utils

Stores useful generic functions.

Module Contents

Functions

box_car_conv(array, points)

calc_met(f)

calculate_chi2(data, model, uncertainties)

calculate_reduced_chi2(data, model, uncertainties[, ...])

calculate_uncertainty(derivatives, uncertainties[, ...])

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

class_init_args2class_args(string)

Convenience code-writing function to convert a series of arguments into lines of initialisation for a class.

class_init_args2dict(string)

Convenience code-writing function to convert a series of arguments into a dictionary.

class2hdf5(obj[, filename])

Convert an instance of a class into a HDF5 dataset.

dataset2obj(obj)

Convert a HDF5 dataset into a list of objects (float, int or str).

dict2hdf5(dictionary, hdf5_file[, group])

Convert a dictionary into a HDF5 dataset.

fill_object(array, value)

Fill a numpy object array with a value.

flatten_object(array)

Flatten a numpy object array.

gaussian_weights1d(sigma[, truncate])

Compute a 1D Gaussian convolution kernel.

gaussian_weights_running(sigmas[, truncate])

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

hdf52dict(hdf5_file)

mean_uncertainty(uncertainties)

Calculate the uncertainty of the mean of an array.

median_uncertainties(uncertainties)

Calculate the uncertainty of the median of an array.

read_abunds(path)

rebin_spectrum(input_wavelengths, input_spectrum, ...)

Re-bin the spectrum using the Fortran rebin_spectrum function, and catch errors occurring there.

remove_mask(data, data_uncertainties)

Remove masked values of 3D data and linked uncertainties. TODO generalize this

running_mean(x, n)

savez_compressed_record(file, numpy_record_array)

Apply numpy.savez_compressed on a record array.

Attributes

logs_g

logs_met

petitRADTRANS.utils.box_car_conv(array, points)
petitRADTRANS.utils.logs_g
petitRADTRANS.utils.logs_met
petitRADTRANS.utils.calc_met(f)
petitRADTRANS.utils.calculate_chi2(data, model, uncertainties)
petitRADTRANS.utils.calculate_reduced_chi2(data, model, uncertainties, degrees_of_freedom=0)
petitRADTRANS.utils.calculate_uncertainty(derivatives, uncertainties, covariance_matrix=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 variables (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.utils.class_init_args2class_args(string)

Convenience code-writing function to convert a series of arguments into lines of initialisation for a class. Useful to quickly write the __init__ function of a class from its arguments. Example:

>>> s = "arg1, arg2=0.3, arg3='a'"
>>> print(class_init_args2class_args(s))
output:
    self.arg1 = arg1
    self.arg2 = arg2
    self.arg3 = arg3
petitRADTRANS.utils.class_init_args2dict(string)

Convenience code-writing function to convert a series of arguments into a dictionary. Useful to quickly write a dictionary from a long list of arguments. Example:

>>> s = "arg1, arg2=0.3, arg3='a'"
>>> print(class_init_args2class_args(s))
output:
    {
        'arg1': ,
        'arg2': ,
        'arg3': ,
    }
petitRADTRANS.utils.class2hdf5(obj, filename=None)

Convert an instance of a class into a HDF5 dataset.

petitRADTRANS.utils.dataset2obj(obj)

Convert a HDF5 dataset into a list of objects (float, int or str).

petitRADTRANS.utils.dict2hdf5(dictionary, hdf5_file, group='/')

Convert a dictionary into a HDF5 dataset.

petitRADTRANS.utils.fill_object(array, value)

Fill a numpy object array with a value.

petitRADTRANS.utils.flatten_object(array)

Flatten a numpy object array.

petitRADTRANS.utils.gaussian_weights1d(sigma, truncate=4.0)

Compute a 1D Gaussian convolution kernel. To be used with scipy.ndimage.convolve1d.

Based on scipy.ndimage gaussian_filter1d and _gaussian_kernel1d.

Args:
sigma:

Standard deviation for Gaussian kernel.

truncate:

Truncate the filter at this many standard deviations.

Returns:

petitRADTRANS.utils.gaussian_weights_running(sigmas, truncate=4.0)

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

Based on scipy.ndimage gaussian_filter1d and _gaussian_kernel1d.

Args:
sigmas:

Standard deviations for Gaussian kernel.

truncate:

Truncate the filter at this many standard deviations.

Returns:

petitRADTRANS.utils.hdf52dict(hdf5_file)
petitRADTRANS.utils.mean_uncertainty(uncertainties)

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.utils.median_uncertainties(uncertainties)

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.utils.read_abunds(path)
petitRADTRANS.utils.rebin_spectrum(input_wavelengths, input_spectrum, rebinned_wavelengths)

Re-bin the spectrum using the Fortran rebin_spectrum function, and catch errors occurring there. The fortran rebin function raises non-blocking errors. In that case, the function outputs an array of -1.

Args:

input_wavelengths: wavelengths of the input spectrum input_spectrum: spectrum to re-bin rebinned_wavelengths: wavelengths to re-bin the spectrum to. Must be contained within input_wavelengths

Returns:

The re-binned spectrum on the re-binned wavelengths

petitRADTRANS.utils.remove_mask(data, data_uncertainties)

Remove masked values of 3D data and linked uncertainties. TODO generalize this An array of objects is created if the resulting array is jagged.

Args:

data: 3D masked array data_uncertainties: 3D masked array

Returns:

The data and errors without the data masked values, and the mask of the original data array.

petitRADTRANS.utils.running_mean(x, n)
petitRADTRANS.utils.savez_compressed_record(file, numpy_record_array)

Apply numpy.savez_compressed on a record array.