src.superphot_plus.utils
Module Contents
Functions
|
Get extinction list from MWEBV value and wavelengths. |
|
Get g- and r-band extinctions in magnitudes for a single |
|
Calculates the accuracy of the random forest after predicting all |
|
Calculates the F1 score for the classifier. If |
|
Converts magnitudes to flux. |
|
Given "cube" of fit parameters, returns the flux measurements for |
|
Check if parameters are valid given certain model constraints. |
|
Convert output param dict from numpyro sampler to match that |
|
Calculate the log-likelihood of a single lightcurve. |
|
Calculate the mean-square error for a lightcurve. |
|
Gets the negative chi-squared of posterior fits from the model |
|
Creates a PyTorch dataset object from numpy arrays. |
|
Calculate the prediction accuracy. |
|
Sets the time it takes for each epoch to train. |
|
Saves probabilities to a separate file for ROC curve generation. |
|
Calculates the accuracy and f1 score metrics for the |
|
Extracts the wrong model classifications and copies them to a separate folder. |
|
Calculates the validation loss and accuracy for the hyperparameter set. |
|
Calculates the training and validation accuracies and losses |
- get_band_extinctions_from_mwebv(mwebv, wvs)[source]
Get extinction list from MWEBV value and wavelengths.
- get_band_extinctions(ra, dec, wvs)[source]
Get g- and r-band extinctions in magnitudes for a single supernova lightcurve based on right ascension (RA) and declination (DEC).
- Parameters:
ra (float) – The right ascension of the object of interest, in degrees.
dec (float) – The declination of the object of interest, in degrees.
wvs (list or np.ndarray) – Array of wavelengths, in angstroms.
- Returns:
ext_dict – A dictionary mapping bands to extinction magnitudes for the given coordinates.
- Return type:
Dict
- calc_accuracy(pred_classes, test_labels)[source]
Calculates the accuracy of the random forest after predicting all classes.
- Parameters:
pred_classes (np.ndarray of int) – Classes predicted by MLP.
test_labels (np.ndarray of int) – True spectroscopic classes.
- Returns:
Accuracy – The percentage of the predictions that are correct.
- Return type:
float
- Raises:
ValueError – If the pred_classes or test_labels arrays are empty or if they are of mismatched sizes.
- f1_score(pred_classes, true_classes, class_average=False)[source]
Calculates the F1 score for the classifier. If class_average=True, then the macro-F1 is used. Else, uses the weighted-F1 score.
- Parameters:
pred_classes (np.ndarray of int) – Classes predicted by MLP.
true_classes (np.ndarray of int) – True spectroscopic classes.
class_average (bool, optional) – Determines whether F1 score is weighted equally for each class, or by number of samples per class. Defaults to False.
- Returns:
The calculated F1 score.
- Return type:
float
- convert_mags_to_flux(mag, merr, zero_point)[source]
Converts magnitudes to flux.
- Parameters:
mag (array-like) – The magnitudes.
merr (array-like) – The error in magnitudes.
zero_point (float) – The zero point in the magnitude system.
- Returns:
fluxes, flux_unc – The calculated fluxes and their uncertainties.
- Return type:
tuple of numpy array
- flux_model(cube, t_data, b_data, max_flux, ordered_bands, ref_band)[source]
Given “cube” of fit parameters, returns the flux measurements for a given set of time and band data.
- Parameters:
cube (array-like) – The cube of fit parameters.
t_data (array-like) – The time data.
b_data (array-like) – The band data.
ordered_bands (array-like) – The band names in the cube’s parameter order.
ref_band (str) – The base/reference band which all other values are multiples of.
- Returns:
f_model – The flux model for the given set of time and band data.
- Return type:
numpy array
- params_valid(beta, gamma, tau_rise, tau_fall)[source]
Check if parameters are valid given certain model constraints.
- Parameters:
beta (float) – Parameter beta.
gamma (float) – Parameter gamma.
tau_rise (float) – Parameter tau_rise.
tau_fall (float) – Parameter tau_fall.
- Returns:
True if parameters are valid, False otherwise.
- Return type:
bool
- get_numpyro_cube(params, max_flux, ref_band, ordered_bands)[source]
Convert output param dict from numpyro sampler to match that of dynesty.
- Parameters:
params (dict) – Parameter dictionary
max_flux (float) – Max flux of light curve
aux_bands (array-like, optional) – The names of auxiliary bands, in order. If None or excluded, attempts to infer them from the dictionary.
- Returns:
cube (np.ndarray) – Array of all equal-weight parameter draws
aux_bands (np.ndarray) – Auxiliary bands, including those inferred if input arg was None.
- calculate_log_likelihood(cube, lightcurve, unique_bands, ref_band)[source]
Calculate the log-likelihood of a single lightcurve. Copied from dynesty_sampler.py
- Parameters:
cube (np.ndarray) – Array of parameters. Must be length 7 * B where B is the number of unique bands.
lightcurve (Lightcurve) – The lightcurve object to evaluate.
unique_bands (list) – A list of bands to use.
ref_band (str) – The reference band.
- Returns:
logL – Log-likelihood value.
- Return type:
float
- calculate_mse(cube, lightcurve, unique_bands, ref_band)[source]
Calculate the mean-square error for a lightcurve.
- Parameters:
cube (np.ndarray) – Array of parameters. Must be length 7 * B where B is the number of unique bands.
lightcurve (Lightcurve) – The lightcurve object to evaluate.
unique_bands (list) – A list of bands to use.
ref_band (str) – The reference band.
- Returns:
mse – The mean square error of the predictions
- Return type:
float
- calculate_chi_squareds(cubes, t, f, ferr, b, max_flux, ordered_bands=None, ref_band='r')[source]
Gets the negative chi-squared of posterior fits from the model parameters and original data files. :param names: The names of the objects. :type names: list of str :param fit_dir: The directory where the fit files are located. :type fit_dir: str :param data_dirs: The directories where the data files are located. :type data_dirs: list of str :param ordered_bands: Bands in order they appear in cubes. Defaults to ZTF band order. :type ordered_bands: list of str :param ref_band: Base/reference band. Defaults to ‘r’. :type ref_band: str
- Returns:
log_likelihoods – The log likelihoods for each object.
- Return type:
np.ndarray
- create_dataset(features, labels)[source]
Creates a PyTorch dataset object from numpy arrays.
- Parameters:
features (np.ndarray) – The features array.
labels (np.ndarray) – The labels array.
idxs (np.ndarray, optional) – The indices array. Defaults to None.
- Returns:
The created dataset.
- Return type:
torch.utils.data.TensorDataset
- calculate_accuracy(y_pred, y)[source]
Calculate the prediction accuracy.
- Parameters:
y_pred (torch.Tensor) – The predicted tensor.
y (torch.Tensor) – The true tensor.
- Returns:
The calculated accuracy.
- Return type:
torch.Tensor
- epoch_time(start_time, end_time)[source]
Sets the time it takes for each epoch to train.
- Parameters:
start_time (float) – The start time.
end_time (float) – The end time.
- Returns:
A tuple containing the elapsed minutes and elapsed seconds.
- Return type:
tuple
- save_test_probabilities(obj_names, pred_probabilities, output_path, true_labels=None, target_label=None)[source]
Saves probabilities to a separate file for ROC curve generation.
- Parameters:
output_filename (str) – The file name to save to.
pred_probabilities (array-like) – The prediction probabilities.
true_label (str or int) – The true label.
output_dir (str) – Where to store the generated file.
- write_metrics_to_file(config, true_classes, pred_classes, prob_above_07)[source]
Calculates the accuracy and f1 score metrics for the test set and outputs them to a log file.
- Parameters:
config (ModelConfig) – The configuration of the model used for evaluation.
true_classes (np.ndarray) – The ground truth for the test ZTF objects.
pred_classes (np.ndarray) – The predicted classes for the test ZTF objects.
prob_above_07 (np.ndarray) – Indicates which predictions had a 70% confidence.
log_file (str) – The file where the metrics information will be written.
- extract_wrong_classifications(true_classes, pred_classes, ztf_test_names, fit_folder, wc_folder)[source]
Extracts the wrong model classifications and copies them to a separate folder.
- Parameters:
true_classes (np.ndarray) – The ground truth for the classified samples.
predicted_classes (np.ndarray) – The predictions for the classified samples.
ztf_test_names (np.ndarray) – The ZTF object names for the classified samples.
- get_session_metrics(metrics)[source]
Calculates the validation loss and accuracy for the hyperparameter set. The best model is considered the one with the lowest validation loss.
- Parameters:
metrics (tuple) – Tuple containing the training accuracies and losses, and the validation accuracies and losses, for each epoch and fold.
- Returns:
The mean validation loss and accuracy for the hyperparameter set.
- Return type:
tuple
- log_metrics_to_tensorboard(metrics, config, trial_id, base_dir='runs')[source]
Calculates the training and validation accuracies and losses for each epoch (by averaging each fold) and logs these metrics to Tensorboard using a SummaryWriter. It also stores the run configuration for further reference.
- Parameters:
metrics (tuple) – Tuple containing the training accuracies and losses, and the validation accuracies and losses, for each epoch and fold.
config (ModelConfig) – The model’s training configuration.
trial_id (str) – The experiment identifier.
base_dir (str) – The directory where all tensorboard metrics should be stored. Defaults to “runs”.
- Returns:
The training losses and accuracies, followed by the validation losses and accuracies, for each epoch.
- Return type:
tuple