petitRADTRANS.utils#

Stores useful generic functions.

Classes#

LockedDict

Derivative of dict with a lock.

Functions#

check_all_close(a, b, **kwargs)

class_init_args2class_args(string)

Convenience code-writing function to convert a series of arguments into lines of initialization 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.

copy_array_like(array)

Copy a numpy or masked array without recursive Python deepcopy overhead.

copy_mask_array(array)

Copy the boolean mask associated with a masked array-like object.

copy_parameter_value(value)

Copy nested parameter values while leaving immutable scalars untouched.

fill_object(array, value)

Fill a numpy object array with a value.

flatten_object(array)

Flatten a numpy object array.

hdf52dict(hdf5_file)

intersection_indices(array1, array2)

Get the indices of the intersection between two sorted arrays in increasing order.

list_str2str(list_str[, separator])

Convert a list of strings into a string in which each input string is quoted and separated by 'separator'.

load_csv(file, **kwargs)

remove_mask(data, data_uncertainties)

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

savez_compressed_record(file, numpy_record_array)

Apply numpy.savez_compressed on a record array.

topological_sort(source)

Perform topological sort on a dictionary.

tuple_init(arg)

user_input(→ str | int | None)

Module Contents#

class petitRADTRANS.utils.LockedDict#

Bases: dict

Derivative of dict with a lock. Can be used to ensure that no new key is added once the lock is on, to prevent errors due to key typos.

_locked = False#
__copy__()#

Override the copy.copy method. Necessary to allow locked LockedDict to be copied.

__deepcopy__(memo)#

Override the copy.deepcopy method. Necessary to allow locked LockedDict to be copied.

__setitem__(key, value)#

Prevent a key to be added if the lock is on.

classmethod build_and_lock(dictionary=None, **kwargs)#

Instantiate a LockedDict and lock it.

lock()#
update(__m=None, **kwargs) None#

Ensure that update takes the lock into account and do not remove keys.

unlock()#
tree_flatten()#
classmethod tree_unflatten(aux_data, children)#
petitRADTRANS.utils.check_all_close(a, b, **kwargs)#
petitRADTRANS.utils.class_init_args2class_args(string)#

Convenience code-writing function to convert a series of arguments into lines of initialization 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.copy_array_like(array)#

Copy a numpy or masked array without recursive Python deepcopy overhead.

petitRADTRANS.utils.copy_mask_array(array)#

Copy the boolean mask associated with a masked array-like object.

petitRADTRANS.utils.copy_parameter_value(value)#

Copy nested parameter values while leaving immutable scalars untouched.

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.hdf52dict(hdf5_file)#
petitRADTRANS.utils.intersection_indices(array1: numpy.typing.NDArray, array2: numpy.typing.NDArray)#

Get the indices of the intersection between two sorted arrays in increasing order.

For example, if array1 is [0.1, …, 0.3, …, 3] and array2 is interval [0.3, …, 3, …, 28], then the output indices will be the indices corresponding to [0.3, …, 3] in both arrays.

Args:

array1: the first array array2: the second array

Returns:

The indices of the intersection between both arrays, for the first and the second array.

petitRADTRANS.utils.list_str2str(list_str, separator: str = ', ')#

Convert a list of strings into a string in which each input string is quoted and separated by ‘separator’. Example:

>>> list_str2str(('AA', 'BBB', 'C'))
>>> "'AA', 'BBB', 'C'"
petitRADTRANS.utils.load_csv(file, **kwargs)#
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.savez_compressed_record(file, numpy_record_array)#

Apply numpy.savez_compressed on a record array.

petitRADTRANS.utils.topological_sort(source)#

Perform topological sort on a dictionary.

Source: https://stackoverflow.com/questions/11557241/python-sorting-a-dependency-list

Args:

source: dictionary of {name: [list of dependencies]} pairs

Returns:

list of names, with dependencies listed first

petitRADTRANS.utils.tuple_init(arg)#
petitRADTRANS.utils.user_input(introduction_message: str, input_message: str, failure_message: str, cancel_message: str, mode: str, max_attempts: int = 5, list_length: int = None) str | int | None#