astrosaber package#
Subpackages#
Submodules#
astrosaber.hisa module#
- class astrosaber.hisa.HisaExtraction(fitsfile: str, path_to_noise_map: Path | None = None, path_to_data: Path = '.', smoothing: str | None = 'Y', phase: str | None = 'two', lam1: float | None = None, p1: float | None = 0.9, lam2: float | None = None, p2: float | None = 0.9, niters: int | None = 20, iterations_for_convergence: int | None = 3, noise: float | None = None, add_residual: bool | None = True, sig: float | None = 1.0, velo_range: float | None = 15.0, cunit3: str = 'm/s', check_signal_sigma: float | None = 6.0, output_flags: bool | None = True, baby_yoda: bool | None = False, p_limit: float | None = 0.02, ncpus: int | None = None, suffix: str | None = '')[source]#
Bases:
objectA class used to execute the self-absorption extraction.
- fitsfile#
Name of the fitsfile.
- Type:
str
- path_to_noise_map#
Path to the noise map. If no noise map is given, a single value must be provided instead using the
HisaExtraction.noiseattribute.- Type:
Path
- path_to_data#
Path to the fitsfile.
- Type:
Path
- smoothing#
Whether to execute the asymmetric least squares smoothing routine. Default is True.
- Type:
bool, optional
- phase#
Mode of saber smoothing. Either ‘one’ or ‘two’ (default) phase smoothing.
- Type:
str, optional
- lam1#
Lambda_1 smoothing parameter of the first major cycle iteration if phase is set to ‘two’. Else smoothing parameter of all major cycle iterations.
- Type:
float
- p1#
Asymmetry weight of the first iteration of the major cycle smoothing. Default is 0.90.
- Type:
float, optional
- lam2#
Lambda_2 smoothing parameter. Has to be specified if phase is set to ‘two’.
- Type:
float
- p2#
Asymmetry weight of the remaining iterations of the major cycle smoothing. Has to be specified if phase is set to ‘two’. Default is 0.90.
- Type:
float, optional
- niters#
Maximum number of iterations of the smoothing. Default is 20.
- Type:
int, optional
- iterations_for_convergence#
Number of iterations of the major cycle for the baseline to be considered converged. Default is 3.
- Type:
int, optional
- noise#
Noise level of the data. Has to be specified if no path to noise map is given.
- Type:
float
- add_residual#
Whether to add the residual (=difference between first and last major cycle iteration) to the baseline. Default is True.
- Type:
bool, optional
- sig#
Defines how many sigma of the noise is used as a convergence criterion. If the change in baseline between major cycle iterations is smaller than
sig*noiseforiterations_for_convergence, then the baseline is considered converged. Default is 1.0.- Type:
float, optional
- velo_range#
Velocity range [in km/s] of the spectra that has to contain significant signal for it to be considered in the baseline extraction. Default is 15.0.
- Type:
float, optional
- cunit3#
Type of velocity unit specified in the fits file header keyword ‘CUNIT3’. Default is ‘m/s’.
- Type:
str, optional
- check_signal_sigma#
Defines the significance of the signal that has to be present in the spectra for at least the range defined by
HisaExtraction.velo_range. Default is 6.0.- Type:
float, optional
- output_flags#
Whether to save a mask containing the flags. Default is True.
- Type:
bool, optional
- baby_yoda#
??? Default is False.
- Type:
bool, optional
- p_limit#
The p-limit of the Markov chain to estimate signal ranges in the spectra. Default is 0.02.
- Type:
float, optional
- ncpus#
Number of CPUs to use. Defaults to 50% of the available CPUs.
- Type:
int
- suffix#
Optional suffix to add to the output filenames.
- Type:
str, optional
- prepare_data()[source]#
Prepares the extraction by reading in data and put them in a raveled list of spectra.
- saber()[source]#
Runs the self-absorption extraction and saves the data. It takes the instance attributes and reads in the noise to kick off the extraction routine.
Methods
Prints a message when preparation starts.
Runs the one-phase extraction for a single spectrum i.
Prepares the extraction by reading in data and put them in a raveled list of spectra.
saber()Runs the self-absorption extraction and saves the data.
Saves all the extracted data into FITS files.
Runs the two-phase extraction for a single spectrum i.
- one_step_extraction_single(i: int) Tuple[int, ndarray, ndarray, int, int][source]#
Runs the one-phase extraction for a single spectrum i.
- Parameters:
i (int) – Index of input spectrum.
- Returns:
index_1d (int) – Index of the spectrum.
bg (numpy.ndarray) – Background spectrum w/o self-absorption.
hisa (numpy.ndarray) – Inverted self-absorption spectrum (i.e. expressed as equivalent emission).
iterations (int) – Number of iterations until algorithm converged.
flag_map (int) – Flag whether background did/did not converge or whether spectrum does/does not contain signal. If flag is 1, the were no issues in the fit. If 0, fit did not converge or did not contain signal.
- prepare_data()[source]#
Prepares the extraction by reading in data and put them in a raveled list of spectra.
- saber()[source]#
Runs the self-absorption extraction and saves the data. It takes the instance attributes and reads in the noise to kick off the extraction routine.
- two_step_extraction_single(i: int) Tuple[int, ndarray, ndarray, int, int][source]#
Runs the two-phase extraction for a single spectrum i.
- Parameters:
i (int) – Index of input spectrum.
- Returns:
index_1d (int) – Index of the spectrum.
bg (numpy.ndarray) – Background spectrum w/o self-absorption.
hisa (numpy.ndarray) – Inverted self-absorption spectrum (i.e. expressed as equivalent emission).
iterations (int) – Number of iterations until algorithm converged.
flag_map (int) – Flag whether background did/did not converge or whether spectrum does/does not contain signal. If flag is 1, the were no issues in the fit. If 0, fit did not converge or did not contain signal.
astrosaber.parallel_processing module#
- astrosaber.parallel_processing.func(use_ncpus: int | None = None, function: ~typing.Callable[[...], ~typing.Tuple] | None = None, bar: ~typing.Callable[[~typing.Iterable], None] = <class 'tqdm.std.tqdm'>) Tuple[source]#
Function to execute in a parallel process.
- use_ncpusint
Number of CPUs to use.
- functionfunc
A python function to apply to the elements of array.
- barfunc
Progress bar to use.
- Returns:
[function(array[0]), function(array[1]), …]
- astrosaber.parallel_processing.func_wo_bar(use_ncpus: int | None = None, function: Callable[[...], Tuple] | None = None) Tuple[source]#
Function to execute in a parallel process.
- use_ncpusint
Number of CPUs to use.
- functionfunc
A python function to apply to the elements of array.
- Returns:
[function(array[0]), function(array[1]), …]
- astrosaber.parallel_processing.init(mp_info: List)[source]#
Initializes global params for parallel process.
- astrosaber.parallel_processing.lambda_extraction_i(i: int) Tuple[ndarray, ndarray, ndarray, float, float][source]#
Generate the test data for a single spectrum.
- astrosaber.parallel_processing.one_step_i(i: int) Tuple[int, ndarray, ndarray, int, int][source]#
Compute the baseline etc. for a single spectrum using the one-phase extraction.
- astrosaber.parallel_processing.parallel_process(array: ~numpy.ndarray, function: ~typing.Callable[[int], ~typing.Tuple], n_jobs: int = 4, use_kwargs: bool = False, front_num: int = 3, bar: ~typing.Callable[[~typing.Iterable], None] = <class 'tqdm.std.tqdm'>) List[source]#
A parallel version of the map function with a progress bar. Credit: http://danshiebler.com/2016-09-14-parallel-progress-bar/
- arraynumpy.ndarray
An array to iterate over.
- functionfunc
A python function to apply to the elements of array.
- n_jobsint
The number of cores to use.
- use_kwargsbool
Whether to consider the elements of array as dictionaries of keyword arguments to function. Default is False.
- front_numint
The number of iterations to run serially before kicking off the parallel job. Useful for catching bugs. Default is 3.
- Returns:
[function(array[0]), function(array[1]), …]
- astrosaber.parallel_processing.parallel_process_wo_bar(array: ndarray, function: Callable[[int], Tuple], n_jobs: int = 4, use_kwargs: bool = False, front_num: int = 3) List[source]#
A parallel version of the map function with a progress bar. Credit: http://danshiebler.com/2016-09-14-parallel-progress-bar/
- arraynumpy.ndarray
An array to iterate over.
- functionfunc
A python function to apply to the elements of array.
- n_jobsint
The number of cores to use.
- use_kwargsbool
Whether to consider the elements of array as dictionaries of keyword arguments to function. Default is False.
- front_numint
The number of iterations to run serially before kicking off the parallel job. Useful for catching bugs. Default is 3.
- Returns:
[function(array[0]), function(array[1]), …]
astrosaber.plotting module#
plotting functions
- astrosaber.plotting.add_figure_properties(ax, header=None, fontsize=10, velocity_range=None, vel_unit=Unit('km / s'), set_xlabel=True, set_ylabel=True)[source]#
- astrosaber.plotting.plot_pickle_spectra(pickle_file, outfile='spectra.pdf', ranges=None, path_to_plots='.', n_spectra=9, rowsize=4.0, rowbreak=10, dpi=72, velocity_range=[-110, 163], vel_unit=Unit('km / s'), seed=111)[source]#
pickle_file: pickled file to plot spectra from
- astrosaber.plotting.plot_spectra(fitsfiles, outfile='spectra.pdf', coordinates=None, radius=None, path_to_plots='.', n_spectra=9, rowsize=4.0, rowbreak=10, dpi=72, velocity_range=[-110, 163], cunit3='m/s', vel_unit=Unit('km / s'), seed=111)[source]#
fitsfiles: list of fitsfiles to plot spectra from coordinates: array of central coordinates [[Glon, Glat]] to plot spectra from radius: radius of area to be averaged for each spectrum [arcseconds]
astrosaber.prepare_training module#
- class astrosaber.prepare_training.saberPrepare(fitsfile: str, training_set_size: int | float = 100, path_to_noise_map: Path | None = None, path_to_data: Path = '.', mean_amp_snr: int | float = 7.0, std_amp_snr: int | float = 1.0, mean_linewidth: int | float | None = None, std_linewidth: int | float | None = None, mean_ncomponent: int | float = 2.0, std_ncomponent: int | float = 0.5, fix_velocities: List | None = None, fix_velocities_sigma: float | None = None, cunit3='m/s', smooth_testdata: bool = True, lam1: float | None = None, p1: float | None = None, lam2: float | None = None, p2: float | None = None, niters: int | None = 20, iterations_for_convergence: int | None = 3, noise: float | None = None, add_residual: bool | None = False, sig: float | None = 1.0, velo_range: float | None = 15.0, check_signal_sigma: float | None = 6.0, p_limit: float | None = 0.01, ncpus: int | None = 1, suffix: str | None = '', filename_out: str | None = None, path_to_file: Path | None = '.', seed: int | None = 111)[source]#
Bases:
objectA class used to obtain and prepare training data for the optimization.
- fitsfile#
Name of the fitsfile.
- Type:
str
- training_set_size#
Number of spectra to draw from the data. Default is 100.
- Type:
int, optional
- path_to_noise_map#
Path to the noise map. If no noise map is given, a single value must be provided instead using the
saberPrepare.noiseattribute.- Type:
Path
- path_to_data#
Path to the fitsfile.
- Type:
Path
- mean_amp_snr#
Mean of amplitude distribution. This sets the mean value of the Gaussian distribution of self-absorption amplitude values from which the training data are generated. Default is 7.0.
- Type:
int | float, optional
- std_amp_snr#
Standard deviation of amplitude distribution. This sets the standard deviation of the Gaussian distribution of self-absorption amplitude values from which the training data are generated. Default is 1.0.
- Type:
int | float, optional
- mean_linewidth#
Mean of linewidth distribution in units of km/s [FWHM]. This sets the mean value of the Gaussian distribution of self-absorption linewidths from which the training data are generated. Default is None.
- Type:
int | float, optional
- std_linewidth#
Standard deviation of linewidth distribution in units of km/s [FWHM]. This sets the standard deviation of the Gaussian distribution of self-absorption linewidths from which the training data are generated. Default is None.
- Type:
int | float, optional
- mean_ncomponent#
Mean number of self-absorption components. This sets the mean value of the Gaussian distribution from which the number of self-absorption features added per spectrum is determined. Default is 2.0.
- Type:
int | float, optional
- std_ncomponent#
Standard deviation of self-absorption components. This sets the standard deviation of the Gaussian distribution from which the number of self-absorption features added per spectrum is determined. Default is 0.5.
- Type:
int | float, optional
- fix_velocities#
List of fixed velocities where self-absorption features should be added (in units of the third axis in the fits file). If the velocities of absorption features are known, this information can be provided here to aid the optimization.
- Type:
list, optional
- fix_velocities_sigma#
Standard deviation of the fixed velocities. If a list of fixed velocities is given, some ‘wiggle room’ defined by this attribute can be added. This is the standard deviation of a Gaussian distribution around the fixed velocities in units of the third axis of the fits file. The default is one spectral channel.
- Type:
float, optional
- cunit3#
Type of velocity unit specified in the fits file header keyword ‘CUNIT3’. Default is ‘m/s’.
- Type:
str, optional
- smooth_testdata#
Option to apply prior smoothing to data when generating test data. If True, prior smoothing will be applied using the
saberPrepare.lam1andsaberPrepare.lam2attributes. If False, original data will be used as test data. Default is True.- Type:
bool, optional
- lam1#
Lambda_1 smoothing parameter to generate test data. Default is 2.0.
- Type:
float, optional
- p1#
Asymmetry weight of the first iteration of the major cycle smoothing to generate test data. Default is 0.90.
- Type:
float, optional
- lam2#
Lambda_2 smoothing parameter to generate test data. Default is 2.0.
- Type:
float, optional
- p2#
Asymmetry weight of the remaining iterations of the major cycle smoothing to generate test data. Default is 0.90.
- Type:
float, optional
- niters#
Maximum number of iterations of the smoothing. Only used to generate test data. Default is 20.
- Type:
int, optional
- iterations_for_convergence#
Number of iterations of the major cycle for the baseline to be considered converged. Only used to generate test data. Default is 3.
- Type:
int, optional
- noise#
Noise level of the data. Has to be specified if no path to noise map is given.
- Type:
float
- add_residual#
Whether to add the residual (=difference between first and last major cycle iteration) to the baseline. Only used to generate test data. Default is True.
- Type:
bool, optional
- sig#
Defines how many sigma of the noise is used as a convergence criterion. If the change in baseline between major cycle iterations is smaller than
sig*noiseforiterations_for_convergence, then the baseline is considered converged. Only used to generate test data. Default is 1.0.- Type:
float, optional
- velo_range#
Velocity range [in km/s] of the spectra that has to contain significant signal for it to be considered in the baseline extraction. Default is 15.0.
- Type:
float, optional
- check_signal_sigma#
Defines the significance of the signal that has to be present in the spectra for at least the range defined by
saberPrepare.velo_range. Default is 6.0.- Type:
float, optional
- p_limit#
The p-limit of the Markov chain to estimate signal ranges in the spectra. Default is 0.01.
- Type:
float, optional
- ncpus#
Number of CPUs to use. Defaults to 1.
- Type:
int, optional
- suffix#
Optional suffix to add to the output filenames.
- Type:
str, optional
- filename_out#
Output filename of the pickled file that contains the training and test data. The default is the fits filename base with the number of training spectra.
- Type:
str, optional
- path_to_file#
Optional path to where the pickled training data should be stored. The training data are stored by default in the subfolder astrosaber_training of the working directory.
- Type:
str, optional
- seed#
Seed to initialize the random generator.
- Type:
int, optional
- prepare_data()[source]#
Prepares the training data by reading in data and setting up lists for Gaussian parameter distributions.
- two_step_extraction_prepare()[source]#
Runs the two-phase smoothing with default parameters to generate test data and self-absorption parameters.
Methods
Prints a message when preparation starts.
Prepares the training data by reading in data and setting up lists for Gaussian parameter distributions.
Creates the test and training data and saves them into a pickled file.
Saves all the test and training data into a pickled file.
Runs the two-phase smoothing with default parameters to generate test data and self-absorption parameters.
- prepare_data()[source]#
Prepares the training data by reading in data and setting up lists for Gaussian parameter distributions.
- two_step_extraction_prepare(i: int) Tuple[ndarray, ndarray, ndarray, float, float][source]#
Runs the two-phase smoothing with default parameters to generate test data and self-absorption parameters.
- Returns:
mock_emission (numpy.ndarray) – ‘Pure’ emission spectrum w/o self-absorption.
mask_ranges (numpy.ndarray) – Array of range indices that contain signal.
mask (numpy.ndarray) – Array of signal mask.
mu_amps_HISA (float) – Mean amplitude value of self-absorption features.
sigma_amps_HISA (float) – Standard deviation of self-absorption features.
astrosaber.training module#
- class astrosaber.training.saberTraining(pickle_file: str, path_to_data: Path | None = '.', iterations: int | None = 100, phase: str | None = 'two', lam1_initial: float | None = None, p1: float | None = 0.9, lam2_initial: float | None = None, p2: float | None = 0.9, weight_1: float | None = None, weight_2: float | None = None, lam1_bounds: List | None = None, lam2_bounds: List | None = None, MAD: float | None = None, window_size: int | None = None, eps_l1: float | None = None, eps_l2: float | None = None, learning_rate_l1: float | None = None, learning_rate_l2: float | None = None, mom: float | None = None, get_trace: bool = False, niters: int | None = 20, iterations_for_convergence: int | None = 3, add_residual: bool = True, sig: float | None = 1.0, velo_range: float | None = 15.0, cunit3: str = 'm/s', check_signal_sigma: float | None = 6.0, p_limit: float | None = 0.01, ncpus: int | None = None, suffix: str | None = '', filename_out: str | None = None, seed: int | None = 111)[source]#
Bases:
objectA class used to optimize smoothing parameters based on training data.
- picklefile#
Name of the pickled file to use as an input.
- Type:
str
- path_to_data#
Path to the pickled file. Default is the working directory.
- Type:
Path, optional
- iterations#
Maximum number of iterations for gradient descent algorithm. Default is 100.
- Type:
int, optional
- phase#
Mode of saber smoothing. Either ‘one’ or ‘two’ (default) phase smoothing. Default is ‘two’.
- Type:
str, optional
- lam1_initial#
Initial value of the Lambda_1 smoothing parameter that is to be optimized.
- Type:
float
- p1#
Asymmetry weight of the first iteration (or all iterations if phase=’one’) of the major cycle smoothing. Default is 0.90.
- Type:
float, optional
- lam1_initial#
Initial value of the Lambda_2 smoothing parameter that is to be optimized. Has to be specified if phase is set to ‘two’.
- Type:
float
- p2#
Asymmetry weight of the remaining iterations of the major cycle smoothing. Has to be specified if phase is set to ‘two’. Default is 0.90.
- Type:
float, optional
- weight_1#
(DEPRECATED) Additional penalty that can be imposed on the Lambda_1 smoothing weight. This is deprecated and will be ignored.
- Type:
float, optional
- weight_2#
(DEPRECATED) Additional penalty that can be imposed on the Lambda_2 smoothing weight. This is deprecated and will be ignored.
- Type:
float, optional
- lam1_bounds#
List of two constraints on Lambda_1 smoothing parameter to limit the parameter space. Default is [0.1, 100.].
- Type:
List, optional
- lam2_bounds#
List of two constraints on Lambda_2 smoothing parameter to limit the parameter space. Default is [0.1, 100.].
- Type:
List, optional
- MAD#
Median absolute difference that is used together with
saberTraining.window_sizeas a convergence threshold for optimization. Default is 0.03.- Type:
float, optional
- window_size#
Trailing window size to determine convergence. Default is 10 (iterations).
- Type:
float, optional
- eps_l1#
Epsilon value to compute parameter space derivative in Lambda_1 direction. Default is 0.1.
- Type:
float, optional
- eps_l2#
Epsilon value to compute parameter space derivative in Lambda_2 direction. Default is 0.1.
- Type:
float, optional
- learning_rate_l1#
Step size to go in direction of Lambda_1 derivative at each iteration. Default is 0.5.
- Type:
float, optional
- learning_rate_l2#
Step size to go in direction of Lambda_2 derivative at each iteration. Default is 0.5.
- Type:
float, optional
- mom#
Momentum that influences the following step in the gradient descent run. Default is 0.3.
- Type:
float, optional
- get_trace#
If get_trace is set to True, the tracks of (lam1,lam2) will be returned and saved, instead of the final optimized smoothing parameters. Default is False.
- Type:
bool, optional
- niters#
Maximum number of iterations of the smoothing. Only used to generate test data. Default is 20.
- Type:
int, optional
- iterations_for_convergence#
Number of iterations of the major cycle for the baseline to be considered converged. Only used to generate test data. Default is 3.
- Type:
int, optional
- add_residual#
Whether to add the residual (=difference between first and last major cycle iteration) to the baseline. Only used to generate test data. Default is True.
- Type:
bool, optional
- sig#
Defines how many sigma of the noise is used as a convergence criterion. If the change in baseline between major cycle iterations is smaller than
sig*noiseforiterations_for_convergence, then the baseline is considered converged. Only used to generate test data. Default is 1.0.- Type:
float, optional
- velo_range#
Velocity range [km/s] of the spectra that has to contain significant signal for it to be considered in the baseline extraction. Default is 15.0.
- Type:
float, optional
- cunit3#
Type of velocity unit specified in the fits file header keyword ‘CUNIT3’. Default is ‘m/s’.
- Type:
str, optional
- check_signal_sigma#
Defines the significance of the signal that has to be present in the spectra for at least the range defined by
saberTraining.velo_range. Default is 6.0.- Type:
float, optional
- p_limit#
The p-limit of the Markov chain to estimate signal ranges in the spectra. Default is 0.01.
- Type:
float, optional
- ncpus#
Number of CPUs to use. Defaults to 50% of the available CPUs.
- Type:
int, optional
- suffix#
Optional suffix to add to the output filenames.
- Type:
str, optional
- filename_out#
Output filename of the pickled file that contains the training and test data. The default is the fits filename base with the number of training spectra.
- Type:
str, optional
- seed#
Seed to initialize the random generator. Default is 111.
- Type:
int, optional
- objective_function_lambda_set()[source]#
Kicks off the parallel process and computes the cost function based on the current Lambda parameters.
- single_cost(i)[source]#
Computes the cost, and optionally the reduced chi square and the median absolute deviation (MAD) of a single spectrum fit.
- single_cost_endofloop(i)[source]#
Computes the cost, and optionally the reduced chi square and the median absolute deviation (MAD) of a single spectrum fit using fixed lambda values.
- train_lambda_set()[source]#
Performs the gradient descent using an objective function (which is in this case the cost function evaluated for asymmetric smoothing with lambda_1, lambda_2 parameters).
- save_data()[source]#
If get_trace is set to False, this will save the optimized Lambda smoothing parameters in a .txt file. If get_trace is set to True, this will save the tracks of Lambda positions in the parameter space in a .txt file.
- update_pickle_file()[source]#
Updates the input pickle file with final background fit and reduced chi square value for each training spectrum and saves the data.
Methods
Prints a message when preparation starts.
gradient_descent_lambda_set(iterations)Bookkeeping object.
objective_function_lambda_set(lam1, p1, lam2, p2)Kicks off the parallel process and computes the cost function based on the current Lambda parameters.
Prepares the optimization by reading in data and setting parameters.
If get_trace is set to False, this will save the optimized Lambda smoothing parameters in a .txt file.
Saves the updated pickle file.
single_cost(i[, get_all])Computes the cost, and optionally the reduced chi square and the median absolute deviation (MAD) of a single spectrum fit.
single_cost_endofloop(i[, lam1_final, ...])Computes the cost, and optionally the reduced chi square and the median absolute deviation (MAD) of a single spectrum fit using fixed lambda values.
train()Optimizes the Lambda smoothing parameter(s).
train_lambda_set(objective_function[, ...])Performs the gradient descent using an objective function (which is in this case the cost function evaluated for asymmetric smoothing with lambda_1, lambda_2 parameters).
training()Prepares the data using
prepare_data()and calls the methodstrain(),save_data(); andupdate_pickle_file()andplot_pickle_spectra()if get_trace is set to False.update_pickle_file(training_data, lam1, lam2)Updates the input pickle file with final background fit and reduced chi square value for each training spectrum and saves the data.
- objective_function_lambda_set(lam1: float, p1: float, lam2: float, p2: float, get_all: bool = True, ncpus: int | None = None) Tuple[float, float, float][source]#
Kicks off the parallel process and computes the cost function based on the current Lambda parameters.
- Parameters:
lam1 (float) – Lambda_1 smoothing parameter.
p1 (float) – Asymmetry weight parameter.
lam2 (float) – Lambda_2 smoothing parameter.
p2 (float) – Asymmetry weight parameter
get_all (bool, optional) – If get_all is set to True, it will return the median results of the cost function, the reduced chi square, and the median absolute deviation (MAD). If set to False, it will return just the median result of the costs.
ncpus (int, optional) – Number of CPUs to use. Defaults to 50% of the available CPUs.
- Returns:
cost (float) – Median of costs.
rchi2 (float) – Median of reduced chi square values. Only returned if get_all=True.
MAD (float) – Median of MAD values. Only returned if get_all=True.
- save_data()[source]#
If get_trace is set to False, this will save the optimized Lambda smoothing parameters in a .txt file. If get_trace is set to True, this will save the tracks of Lambda positions in the parameter space in a .txt file.
- single_cost(i: int, get_all: bool | None = True) Tuple[float, float, float][source]#
Computes the cost, and optionally the reduced chi square and the median absolute deviation (MAD) of a single spectrum fit.
- Parameters:
i (int) – Index of spectrum.
get_all (bool, optional) – If set to True (default), returns cost, reduced chi square, and MAD. If False, returns only cost.
- Returns:
cost (float) – Cost. In this case, it is the reduced chi square.
rchi2 (float) – Reduced chi square value. Only returned if get_all=True.
MAD (float) – MAD value. Only returned if get_all=True.
- single_cost_endofloop(i: int, lam1_final: float | None = None, lam2_final: float | None = None, get_all: bool | None = True) Tuple[float, float, float][source]#
Computes the cost, and optionally the reduced chi square and the median absolute deviation (MAD) of a single spectrum fit using fixed lambda values.
- Parameters:
i (int) – Index of spectrum.
lam1_final (float) – Final lambda_1 smoothing parameter.
lam2_final (float) – Final lambda_2 smoothing parameter.
get_all (bool, optional) – If set to True (default), returns cost, reduced chi square, and MAD. If False, returns only cost.
- Returns:
cost (float) – Cost. In this case, it is the reduced chi square.
rchi2 (float) – Reduced chi square value. Only returned if get_all=True.
MAD (float) – MAD value. Only returned if get_all=True.
- train()[source]#
Optimizes the Lambda smoothing parameter(s).
- Returns:
popt_lam – Optimized Lambda parameters. Returns an array of lambda positions from the gradient descent run if get_trace is set to True.
- Return type:
List
- train_lambda_set(objective_function: Callable[[], Tuple[float, float, float]], training_data: ndarray | None = None, test_data: ndarray | None = None, noise: ndarray | None = None, lam1_initial: float | None = None, p1: float | None = None, lam2_initial: float | None = None, p2: float | None = None, lam1_bounds: List | None = None, lam2_bounds: List | None = None, iterations: int | None = 100, MAD: float | None = None, eps_l1: float | None = None, eps_l2: float | None = None, learning_rate_l1: float | None = None, learning_rate_l2: float | None = None, mom: float | None = None, window_size: int | None = None, iterations_for_convergence_training: int | None = 10, get_trace: bool | None = False, ncpus: int | None = None)[source]#
Performs the gradient descent using an objective function (which is in this case the cost function evaluated for asymmetric smoothing with lambda_1, lambda_2 parameters).
- Parameters:
objective_function (func) – Objective function whose parameters are to be optimized.
training_data (numpy.ndarray) – Array containing training spectra.
test_data (numpy.ndarray) – Array containing reference test spectra to evaluate the goodness of fit after each iteration.
noise (numpy.ndarray) – Array of noise values. Array of length N, with N being the number of training spectra.
lam1_initial (float) – Initial lambda_1 parameter at which gradient descent takes off.
p1 (float) – Asymmetry weight of first major cycle iteration. Default is None.
lam2_initial (float) – Initial lambda_2 parameter at which gradient descent takes off.
p2 (float) – Asymmetry weight of following major cycle iterations. Default is None.
lam1_bounds (List) – Imposed bounds of the lambda_1 parameter. If the gradient descent converges toward a value that is outside the range given by lam1_bounds, the next iteration of the gradient descent will be pushed in the other direction by an amount that is 0.1 times the current lam1 location. Default is [0.1, 100.]
lam2_bounds (List) – Imposed bounds of the lambda_2 parameter. If the gradient descent converges toward a value that is outside the range given by lam2_bounds, the next iteration of the gradient descent will be pushed in the other direction by an amount that is 0.1 times the current lam2 location. Default is [0.1, 100.]
iterations (int, optional) – Maximum number of iterations to reach a stable convergence. If the gradient descent did not converge until then, the lambda values at this point will be saved. Default is 100.
MAD (float, optional) – Mean absolute deviation used as a convergence threshold. Defaults to 0.03.
eps_l1 (float, optional) – Finite offset for computing derivatives in gradient in direction of lambda_1,lambda_2. Defaults to 0.1
eps_l2 (float, optional) – Finite offset for computing derivatives in gradient in direction of lambda_1,lambda_2. Defaults to 0.1
learning_rate_l1 (float, optional) – Step size to go during optimization in direction of gradient of objective_function(lam1,lam2). Defaults to 0.5.
learning_rate_l2 (float, optional) – Step size to go during optimization in direction of gradient of objective_function(lam1,lam2). Defaults to 0.5.
mom (float, optional) – Momentum value. Defaults to 0.3.
window_size (int, optional) – Trailing window size to determine convergence. Default is 10.
iterations_for_convergence_training (int, optional) – Number of continuous iterations within threshold tolerence required to achieve convergence. Default=10.
get_trace (bool, optional) – Option to save the lambda traces instead of final values. Default is False.
ncpus (int, optional) – Number of CPUs to use. Defaults to 0.5 of the available CPUs.
- Return type:
Returns the lam1, lam2 traces if get_trace is True. Else it returns the lam1, lam2 value after the gradient descent terminates.
- training()[source]#
Prepares the data using
prepare_data()and calls the methodstrain(),save_data(); andupdate_pickle_file()andplot_pickle_spectra()if get_trace is set to False.