ACID_code_v2.utils

Utility functions for the ACID package. Some functions may not be useful to the user.

Functions

auto_window(taus[, c])

Automated windowing procedure following Sokal (1989) in emcee documentation.

autocorr_func_1d(x[, norm])

Autocorrelation estimate using FFT from the emcee tutorial.

autocorr_gw2010(y[, c])

autocorr_new(y[, c])

calc_deltav(wavelengths)

Calculates velocity pixel size

configure_mp_environ(os)

Configures the multiprocessing environment variables for optimal performance.

drop_invalid(wavelengths, flux, errors[, ...])

Drops any pixels where the wavelength, flux, or error is infinite or <= 0.

findfiles(directory, file_type)

flux_to_od([flux, errors, linelist])

Converts flux, errors, and linelist to optical depth.

get_normalisation_coeffs(wl)

Calculates normalization coefficients for wavelength array

guess_SNR(frame_wavelengths, frame_flux, ...)

Estimates S/N for each frame.

mask_invalid(wavelengths, flux, errors[, ...])

Masks any pixels where the wavelength, flux, or error is infinite or <= 0.

next_pow_2(n)

Calculates the next power of 2 greater than or equal to n.

od_to_flux([od, errors, linelist])

Converts optical depth to flux, errors, and linelist.

robust_mean(data[, nsig, axis])

Calculates the robust mean of the input data by excluding outliers beyond a specified number of standard deviations from the median.

set_dict_defaults(input_dict, default_dict)

Sets default values in a dictionary if they are not already present.

validate_args(x, i[, allow_none, sn])

Validates the input arguments.

ACID_code_v2.utils.validate_args(x, i, allow_none=False, sn=False)[source]

Validates the input arguments. This function can be used to ensure inputs to Acid are of the correct type and shape. It is performed automatically in Acid.

Parameters:
  • x (array_like) – array, list, or int to be validated

  • i (int) – position of the input argument

  • allow_none (bool, optional) – Whether None is allowed as a valid input, by default False

  • sn (bool, optional) – Whether the input is a signal-to-noise ratio array, by default False

Returns:

The validated and converted numpy array.

Return type:

array

Raises:

TypeError – If any of the conditions on inputs are not met.

ACID_code_v2.utils.mask_invalid(wavelengths, flux, errors, return_mask=False, verbose=0)[source]

Masks any pixels where the wavelength, flux, or error is infinite or <= 0. Replaces bad pixels with NaN, which ACID can handle.

Parameters:
  • wavelengths (array_like) – The wavelength values of the spectrum.

  • flux (array_like) – The flux values of the spectrum.

  • errors (array_like) – The error values associated with the spectrum.

Returns:

A tuple containing the cleaned wavelength, flux, and error arrays.

Return type:

tuple

ACID_code_v2.utils.drop_invalid(wavelengths, flux, errors, return_mask=False, verbose=0)[source]

Drops any pixels where the wavelength, flux, or error is infinite or <= 0.

Parameters:
  • wavelengths (array_like) – The wavelength values of the spectrum.

  • flux (array_like) – The flux values of the spectrum.

  • errors (array_like) – The error values associated with the spectrum.

Returns:

A tuple containing the cleaned wavelength, flux, and error arrays.

Return type:

tuple

ACID_code_v2.utils.calc_deltav(wavelengths: ndarray) float[source]

Calculates velocity pixel size

Calculates the velocity pixel size for the LSD velocity grid based off the spectral wavelengths.

Parameters:

wavelengths (np.ndarray) – Wavelengths for Acid input spectrum (in Angstroms), must be a 1D array of positive values.

Returns:

Velocity pixel size in km/s

Return type:

float

ACID_code_v2.utils.guess_SNR(frame_wavelengths: ndarray | list, frame_flux: ndarray | list, frame_errors: ndarray | list) ndarray[source]

Estimates S/N for each frame. Takes the median S/N in the central third of the wavelength range. Fully vectorized so that all the frames can be passed at once.

Parameters:
  • frame_wavelengths (np.ndarray | list) – Array/list of wavelengths for each frame.

  • frame_flux (np.ndarray | list) – Array/list of flux values for each frame.

  • frame_errors (np.ndarray | list) – Array/list of error values for each frame.

Returns:

Array of estimated signal-to-noise ratios for each frame.

Return type:

np.ndarray

ACID_code_v2.utils.get_normalisation_coeffs(wl)[source]

Calculates normalization coefficients for wavelength array

Parameters:

wl (array_like) – Wavelength array for which normalization coefficients are calculated.

Returns:

A tuple containing the normalization coefficients (a, b).

Return type:

tuple

ACID_code_v2.utils.set_dict_defaults(input_dict, default_dict)[source]

Sets default values in a dictionary if they are not already present.

Parameters:
  • input_dict (dict | None) – The dictionary to set defaults in (or none if not provided).

  • default_dict (dict) – The dictionary containing default key-value pairs.

Returns:

The updated dictionary with defaults set.

Return type:

dict

ACID_code_v2.utils.findfiles(directory, file_type)[source]
ACID_code_v2.utils.robust_mean(data: ndarray, nsig: int | float = 1, axis: int = 0) ndarray | float[source]

Calculates the robust mean of the input data by excluding outliers beyond a specified number of standard deviations from the median.

Parameters:
  • data (np.ndarray) – Input data array.

  • nsig (int | float, optional) – Number of standard deviations to use for outlier rejection. Defaults to 1.

  • axis (int, optional) – Axis along which to compute the robust mean. Defaults to 0.

Returns:

Robust mean of the input data.

Return type:

float

ACID_code_v2.utils.flux_to_od(flux=None, errors=None, linelist=None)[source]

Converts flux, errors, and linelist to optical depth.

Parameters:
  • flux (np.ndarray) – Input flux array.

  • errors (np.ndarray, optional) – Input errors array. Defaults to None.

  • linelist (np.ndarray, optional) – Input linelist array. Defaults to None.

Returns:

A tuple containing the flux in optical depth, errors in optical depth, and linelist in optical depth. The tuple length depends on which inputs were provided.

Return type:

tuple

ACID_code_v2.utils.od_to_flux(od=None, errors=None, linelist=None)[source]

Converts optical depth to flux, errors, and linelist.

Parameters:
  • od (np.ndarray) – Input optical depth array.

  • errors (np.ndarray, optional) – Input errors array. Defaults to None.

  • linelist (np.ndarray, optional) – Input linelist array. Defaults to None.

Returns:

A tuple containing the flux, errors, and linelist. The tuple length depends on which inputs were provided.

Return type:

tuple

ACID_code_v2.utils.configure_mp_environ(os)[source]

Configures the multiprocessing environment variables for optimal performance.

Parameters:

os (module) – The os module to use for setting environment variables.

ACID_code_v2.utils.next_pow_2(n)[source]

Calculates the next power of 2 greater than or equal to n.

Parameters:

n (int) – Input number. Must be real and non-negative.

Returns:

The next power of 2 greater than or equal to n.

Return type:

int

ACID_code_v2.utils.auto_window(taus: ndarray, c: float = 5.0)[source]

Automated windowing procedure following Sokal (1989) in emcee documentation. Returns the window index to use in taus[window].

ACID_code_v2.utils.autocorr_func_1d(x, norm=True)[source]

Autocorrelation estimate using FFT from the emcee tutorial.

ACID_code_v2.utils.autocorr_gw2010(y, c=5.0)[source]
ACID_code_v2.utils.autocorr_new(y, c=5.0)[source]