synthesizer.utils.util_funcs

A module containing general utility functions.

Example usage:

planck(frequency, temperature=10000 * K) rebin_1d(arr, 10, func=np.sum)

Functions

synthesizer.utils.util_funcs.combine_arrays(arr1, arr2, verbose=False)[source]

Combine two arrays into a single array.

This function is a helper used to combine two arrays of the same length into a single array while abstracting some checks and handling improper combinations.

If both arrays are None then None is returned. If one array is None and the other is not then None is returned along with a warning.

Parameters:
  • arr1 (np.ndarray) – The first array to combine.

  • arr2 (np.ndarray) – The second array to combine.

  • verbose (bool) – If True, print warnings for None arrays.

Returns:

The combined array.

Return type:

np.ndarray or None

synthesizer.utils.util_funcs.depluralize(word)[source]

Convert a plural word to its singular form based on simple rules.

Parameters:

word (str) – The word to depluralize.

Returns:

The depluralized word.

Return type:

str

synthesizer.utils.util_funcs.ensure_array_c_compatible_double(arr)[source]

Ensure that the input array is compatible with our C extensions.

Being “compatible” means that the numpy array is both C contiguous and is a double array for floating point numbers.

If we don’t do this then the C extensions will produce garbage due to the mismatch between the data types.

Parameters:

arr (np.ndarray) – The input array to be checked.

synthesizer.utils.util_funcs.ensure_double_precision(value)[source]

Ensure that the input value is a double precision float.

Parameters:

value (float or unyt_quantity) – The value to be converted.

Returns:

The input value as a double precision float.

Return type:

unyt_quantity

synthesizer.utils.util_funcs.get_attr_c_compatible_double(obj, attr)[source]

Ensure an attribute of an object is compatible with our C extensions.

This function checks if the attribute of the object is a numpy array and ensures that it is both C contiguous and of double precision. If the attribute is not compatible, it modifies it in place.

Parameters:
  • obj (object) – The object containing the attribute to be checked.

  • attr (str) – The name of the attribute to be checked.

synthesizer.utils.util_funcs.is_c_compatible_double(arr)[source]

Check if the input array is compatible with our C extensions.

Being “compatible” means that the numpy array is both C contiguous and is a double array for floating point numbers.

If we don’t do this then the C extensions will produce garbage due to the mismatch between the data types.

Parameters:

arr (np.ndarray) – The input array to be checked.

Returns:

True if the array is C contiguous and of double precision,

False otherwise.

Return type:

bool

synthesizer.utils.util_funcs.is_c_compatible_int(arr)[source]

Check if the input array is compatible with our C extensions.

Being “compatible” means that the numpy array is both C contiguous and is an int array for integer numbers.

If we don’t do this then the C extensions will produce garbage due to the mismatch between the data types.

Parameters:

arr (np.ndarray) – The input array to be checked.

Returns:

True if the array is C contiguous and of int type,

False otherwise.

Return type:

bool

synthesizer.utils.util_funcs.parse_grid_id(grid_id)[source]

Parse a grid name for the properties of the grid.

This is used for parsing a grid ID to return the SPS model, version, and IMF

Parameters:

grid_id (str) – string grid identifier

synthesizer.utils.util_funcs.planck(frequency, temperature)[source]

Compute the planck distribution for a given frequency and temperature.

This function computes the spectral radiance of a black body at a given frequency and temperature using Planck’s law. The spectral radiance is then converted to spectral luminosity density assuming a luminosity distance of 10 pc.

Parameters:
  • frequency (float or unyt_quantity) – Frequency of the radiation in Hz.

  • temperature (float or unyt_quantity) – Temperature in Kelvin.

Returns:

Spectral luminosity density in erg/s/Hz.

Return type:

unyt_quantity

synthesizer.utils.util_funcs.pluralize(word)[source]

Pluralize a singular word.

Parameters:

word (str) – The word to pluralize.

Returns:

The pluralized word.

Return type:

str

synthesizer.utils.util_funcs.rebin_1d(arr, resample_factor, func=<function sum>)[source]

Rebin a 1D array.

This function takes a 1D array and rebins it by a specified factor using a specified function (e.g. mean or sum).

Parameters:
  • arr (np.ndarray, list or unyt_array) – The input 1D array.

  • resample_factor (int) – The integer rebinning factor, i.e. how many bins to rebin by.

  • func (function) – The function to use (e.g. mean or sum).

Returns:

The input array resampled by i.

Return type:

array-like

synthesizer.utils.util_funcs.scalar_to_array(value)[source]

Convert a passed scalar to an array.

Parameters:

value (Any) – The value to wrapped into an array. If already an array-like object then it is returned as is.

Returns:

The scalar value wrapped in an array or the array-like object passed in.

Return type:

array-like/unyt_array

Raises:

InconsistentArguments – If the value is not a scalar or array-like object.

synthesizer.utils.util_funcs.wavelength_to_rgba(wavelength, gamma=0.8, fill_red=(0, 0, 0, 0.5), fill_blue=(0, 0, 0, 0.5), alpha=1.0)[source]

Convert wavelength float to RGBA tuple.

Taken from https://stackoverflow.com/questions/44959955/ matplotlib-color-under-curve-based-on-spectral-color

Who originally took it from http://www.noah.org/wiki/ Wavelength_to_RGB_in_Python

Parameters:
  • wavelength (float) – Wavelength in nm.

  • gamma (float) – Gamma value.

  • fill_red (bool or tuple) – The colour (RGBA) to use for wavelengths red of the visible. If None use final nearest visible colour.

  • fill_blue (bool or tuple) – The colour (RGBA) to use for wavelengths red of the visible. If None use final nearest visible colour.

  • alpha (float) – The value of the alpha channel (between 0 and 1).

Returns:

RGBA tuple.

Return type:

rgba (tuple)

synthesizer.utils.util_funcs.wavelengths_to_rgba(wavelengths, gamma=0.8)[source]

Convert wavelength array to RGBA list.

Parameters:
  • wavelengths (unyt_array) – The wavelengths to convert to RGBA tuples.

  • gamma (float) – The gamma value to use for the conversion.

Returns:

list of RGBA tuples.

Return type:

list