squirrel package#

Submodules#

squirrel.data module#

This module contains the class to store data products in 3D datacube or 2D detector image.

class squirrel.data.Datacube(wavelengths, flux, wavelength_unit, fwhm, z_lens, z_source, center_pixel_x, center_pixel_y, coordinate_transform_matrix, flux_unit='arbitrary', noise=None, covariance=None)[source]#

Bases: Spectra

A class to store in 3D IFU datacubes.

property center_pixel_x#

Return the x coordinate of the center pixel.

property center_pixel_y#

Return the y coordinate of the center pixel.

get_1d_spectra(x=None, y=None, mask=None)[source]#

Return the spectra at a given pixel, or summed within a given spatial mask (over spaxels). If nothing is provided, all the spaxels in the datacube will be summed together.

Parameters:
  • x (int) – x coordinate of the pixel

  • y (int) – y coordinate of the pixel

  • mask (numpy.ndarray) – mask to sum the spectra

Returns:

spectrum at the given pixel

Return type:

Spectra

property x_coordinates#

Return the x coordinates of the data.

property y_coordinates#

Return the y coordinates of the data.

class squirrel.data.PowerBinnedSpectra(wavelengths, flux, wavelength_unit, fwhm, z_lens, z_source, x_coordinates, y_coordinates, num_bins, x_pixel_index_of_bins, y_pixel_index_of_bins, flux_unit='arbitrary', noise=None, covariance=None, bin_center_x=None, bin_center_y=None, area=None, snr=None)[source]#

Bases: VoronoiBinnedSpectra

A class to store binned spectra using power binning.

This class is functionally identical to VoronoiBinnedSpectra. This class extends the Spectra class to handle data that has been binned using power-law binning. It includes additional attributes to store the coordinates of the original datacube’s spatial pixels, the bin numbers, and the coordinates of the bin centers, among other properties.

class squirrel.data.RadiallyBinnedSpectra(wavelengths, flux, wavelength_unit, fwhm, z_lens, z_source, bin_radii, flux_unit='arbitrary', noise=None, covariance=None)[source]#

Bases: Spectra

A class to store radially binned spectra.

This class extends the Spectra class to handle data that has been binned radially. It includes an additional attribute to store the radial edges of the bins.

property bin_radii#

Return the radii of the bins.

class squirrel.data.Spectra(wavelengths, flux, wavelength_unit, fwhm, z_lens=0.0, z_source=0.0, flux_unit='arbitrary', noise=None, covariance=None)[source]#

Bases: object

A class to store spectroscopic data.

clip(wavelength_min, wavelength_max)[source]#

Clip the data to the given wavelength range.

Parameters:
  • wavelength_min (float) – minimum wavelength to clip

  • wavelength_max (float) – maximum wavelength to clip

concat(other)[source]#

Concatenate two spectra together.

Parameters:

other (Spectra) – spectra to concatenate

Returns:

concatenated spectra

Return type:

Spectra

copy()[source]#

Return a deep copy of the spectra object.

property covariance#

Return the covariance of the data.

deredshift(redshift=None, target_frame=None)[source]#

Deredshift the spectra.

Parameters:
  • redshift (float) – redshift to deredshift the data to

  • target_frame (str) – frame to deredshift the data to, β€œlens” or β€œsource”

property flux#

Return the flux of the data.

property flux_unit#

Return the unit of the flux.

property fwhm#

Return the full width at half maximum of the data.

property noise#

Return the noise of the data.

reset()[source]#

Reset the data to the original state.

property spectra_modifications#

Return the state of the spectra.

Possible states are β€˜original’, β€˜rebinned’, or β€˜log_rebinned’.

property velocity_scale#

Return the velocity scale of the data.

property wavelength_unit#

Return the unit of the wavelengths.

property wavelengths#

Return the wavelengths of the data.

property wavelengths_frame#

Return the frame of the wavelengths.

Possible frames are β€˜observed’, β€˜lens’, β€˜source’, or β€˜z={redshift}’.

property z_lens#

Return the lens redshift.

property z_source#

Return the source redshift.

class squirrel.data.VoronoiBinnedSpectra(wavelengths, flux, wavelength_unit, fwhm, z_lens, z_source, x_coordinates, y_coordinates, num_bins, x_pixel_index_of_bins, y_pixel_index_of_bins, flux_unit='arbitrary', noise=None, covariance=None, bin_center_x=None, bin_center_y=None, area=None, snr=None)[source]#

Bases: Spectra

A class to store binned spectra using Voronoi binning.

This class extends the Spectra class to handle data that has been binned using Voronoi binning. It includes additional attributes to store the coordinates of the original datacube’s spatial pixels, the bin numbers, and the coordinates of the bin centers, among other properties.

property area#

Return the area of the bins.

property bin_center_x#

Return the x coordinates of the bin centers.

property bin_center_y#

Return the y coordinates of the bin centers.

property bin_numbers#

Return the bin number of the data.

get_single_spectra(bin_index)[source]#

Return the spectra of a single bin.

Parameters:

bin_index (int) – bin number

Returns:

spectra of the bin

Return type:

numpy.ndarray

get_spaxel_map_with_bin_number()[source]#

Return Voronoi bin mapping. -1 is masked pixel. Unmasked pixel start counting from 0.

Returns:

2D array with bin mapping

Return type:

numpy.ndarray

property snr#

Return the signal-to-noise ratio of the bins.

property x_coordinates#

Return the x coordinates of the data.

property x_pixels_of_bins#

Return the x pixel values corresponding to the bin numbers.

property y_coordinates#

Return the y coordinates of the data.

property y_pixels_of_bins#

Return the y pixel values corresponding to the bin numbers.

squirrel.diagnostics module#

This module contains class and functions for diagnostics.

class squirrel.diagnostics.Diagnostics[source]#

Bases: object

This class contains functions to diagnose the performance of the pipeline.

classmethod check_bias_vs_snr(spectra_data, template, spectra_mask_for_snr=None, target_snrs=array([10, 20, 30, 40, 50]), input_velocity_dispersions=[250], template_weight=1.0, polynomial_degree=0, multiplicative_polynomial_degree=0, polynomial_weights=[1.0], multiplicative_component=1.0, add_component=0.0, num_sample=50, z_factor=1.0, v_systematic=0.0, plot=True)[source]#

Check the bias in the velocity dispersion measurement as a function of SNR.

Parameters:
  • spectra_data (squirrel.data.Spectra) – data object

  • template (squirrel.template.Template) – template object

  • spectra_mask_for_snr (numpy.ndarray) – mask of the spectra to compute SNR from

  • target_snrs (numpy.ndarray) – target SNRs

  • input_velocity_dispersions (numpy.ndarray) – input velocity dispersions

  • template_weight (float) – weight of the template

  • polynomial_degree (int) – degree of the polynomial

  • multiplicative_polynomial_degree (int) – degree of the multiplicative polynomial for fitting

  • polynomial_weights (numpy.ndarray) – weights for the additive polynomial to be added to the mock spectra

  • multiplicative_component (np.ndarray or float) – multiplicative component to be multiplied to the mock spectra

  • add_component (float) – additive component to be added to the mock spectra

  • num_sample (int) – number of Monte Carlo noise realizations

  • z_factor (float) – multiplicative factor for wavelength (e.g., 1 + z), if the SNR is computed at a different frame

  • v_systematic (float) – systematic velocity, km/s, the vsyst parameter in pPXF

  • plot (bool) – plot one example simulation for each input velocity dispersion and SNR

Returns:

recovered values

Return type:

tuple

static get_specific_signal_and_noise(spectra, mask, z_factor=1.0)[source]#

Get the mean signal per (wavelength unit)^(1/2) and noise of the spectra.

Parameters:
  • spectra (squirrel.data.Spectra) – spectra object

  • mask (numpy.ndarray) – mask for spectra

  • z_factor (float) – multiplicative factor for wavelength (e.g., 1 + z), if the SNR is computed at a different frame

Returns:

mean signal per (wavelength unit)^(1/2), and noise

Return type:

float, float

classmethod get_specific_snr(spectra, mask, z_factor=1.0)[source]#

Get the mean SNR of the spectra.

Parameters:
  • spectra (squirrel.data.Spectra) – spectra object

  • mask (numpy.ndarray) – mask for spectra

  • z_factor (float) – multiplicative factor for wavelength (e.g., 1 + z), if the SNR is computed at a different frame

Returns:

mean SNR per (wavelength unit)^(1/2)

Return type:

float

static get_stats(values, uncertainties, sigma=3)[source]#

Compute the mean and standard deviation of the array after sigma- clipping.

Parameters:
  • values – values

  • uncertainties (numpy.ndarray) – uncertainties

  • sigma (float) – sigma for clipping

Returns:

mean, uncertainty of the mean, and scatter

Return type:

tuple

classmethod make_convolved_spectra(template_flux, velocity_dispersion, velocity_scale, velocity_scale_ratio, data_wavelength, velocity=0.0, data_weight=1, polynomial_degree=0, polynomial_weights=[1.0], multiplicative_polynomial=1.0, v_systematic=0.0)[source]#

Make a convolved spectra.

This function generates a mock spectrum by convolving a template spectrum with a given velocity dispersion. It also applies a polynomial to the convolved spectrum to simulate various observational effects.

Parameters:
  • template_flux (numpy.ndarray) – template flux. Wavelengths are not needed as v_systematic and velocity_scale_ratio will be used to obtain that.

  • velocity_dispersion (float) – velocity dispersion, in km/s

  • velocity_scale (float) – velocity scale, in km/s

  • velocity_scale_ratio (int) – velocity scale ratio

  • data_wavelength (numpy.ndarray) – data wavelength

  • velocity (float) – velocity, km/s

  • data_weight (float) – multiplicative factor for the template flux, effective when polynomial_degree > 0 to set the relative amplitude of data and polynomial

  • polynomial_degree (int) – degree of the polynomial

  • polynomial_weights (numpy.ndarray) – weights of the polynomial

  • v_systematic (float) – systematic velocity, km/s, the vsyst parameter in pPXF

Returns:

convolved spectra

Return type:

numpy.ndarray

classmethod plot_bias_vs_snr(recovered_values, input_velocity_dispersions, fig_width=10, bias_threshold=0.02, show_scatter=True, show_mean_uncertainty=False, errorbar_kwargs_scatter={}, errorbar_kwargs_mean={}, **kwargs)[source]#

Plot the bias in the velocity dispersion measurement as a function of SNR.

This function generates plots to visualize the bias in the velocity dispersion and velocity measurements as a function of Signal-to-Noise Ratio (SNR). It creates subplots for each input velocity dispersion and plots the recovered values along with their uncertainties and scatters.

Parameters:
  • recovered_values (tuple) – recovered values from check_bias_vs_snr

  • input_velocity_dispersions (numpy.ndarray) – input velocity dispersions

  • fig_width (float) – width of the figure

  • bias_threshold (float) – bias threshold line for plotting

  • show_scatter (bool) – show scatter of the points

  • show_mean_uncertainty (bool) – show mean uncertainty of the points

  • errorbar_kwargs_scatter (dict) – keyword arguments for errorbar for scatter

  • errorbar_kwargs_mean (dict) – keyword arguments for errorbar for mean uncertainty

Returns:

figure and axes

Return type:

tuple

static plot_bias_vs_snr_single(axes, input_values, recovered_snrs, recovered_values, recovered_value_uncertainties, recovered_value_scatters, show_scatter=True, show_mean_uncertainty=False, bias_threshold=0.02, x_label='', y_label='', errorbar_kwargs_mean={}, errorbar_kwargs_scatter={}, **kwargs)[source]#

Plot the bias in velocity dispersion measurement as a function of SNR.

This function visualizes the bias in a single kinematic value as a function of Signal-to-Noise Ratio (SNR). It plots the recovered values along with their uncertainties and scatters.

Parameters:
  • axes (numpy.ndarray or list) – array of ax objects to plot, must match the length of input_values

  • input_values (numpy.ndarray) – input values

  • recovered_snrs (numpy.ndarray) – recovered SNRs

  • recovered_values (numpy.ndarray) – recovered values

  • recovered_value_uncertainties (numpy.ndarray) – uncertainties of recovered values

  • recovered_value_scatters (numpy.ndarray) – scatters of recovered values

  • show_scatter (bool) – whether to show scatter of the points

  • show_mean_uncertainty (bool) – whether to show mean uncertainty of the points

  • bias_threshold (float) – bias threshold line for plotting

  • x_label (str) – label for the x-axis

  • y_label (str) – label for the y-axis

  • errorbar_kwargs_mean (dict) – keyword arguments for errorbar for mean uncertainty

  • errorbar_kwargs_scatter (dict) – keyword arguments for errorbar for scatter

Returns:

None

Return type:

None

squirrel.pipeline module#

This module contains the class to wrap the pPXF package for kinematic analysis.

class squirrel.pipeline.Pipeline[source]#

Bases: object

A class to wrap the pPXF package for kinematic analysis.

This class provides various static methods to perform kinematic analysis using the pPXF package. It includes methods for log rebinning, Voronoi binning, creating kinematic maps, and running pPXF fits.

static boost_noise(spectra, boost_factor, boosting_mask=None)[source]#

Boost the noise in the spectra.

This function increases the noise in the spectra by a specified boost factor. It can optionally apply the boosting to a specific mask.

Parameters:
  • spectra (Spectra or a child class) – The spectra to boost the noise in.

  • boost_factor (float) – The factor to boost the noise by.

  • boosting_mask (np.ndarray) – The mask to apply the boosting to.

Returns:

The spectra with the boosted noise.

Return type:

Spectra or a child class

static calculate_weights_from_bic(delta_bic, sigma_delta_bic)[source]#

Calculate the relative BIC weight after accounting for the uncertainty.

This function follows the methodology provided by Knabel & Mozumdar et al. (2025), arxiv.org/abs/2502.16034.

Parameters:
  • delta_bic (float) – The difference in BIC values between the model and the best model.

  • sigma_delta_bic (float) – The uncertainty in the delta_BIC value.

Returns:

The relative weight of the model.

Return type:

float

classmethod combine_measurements_from_templates(values, uncertainties, ppxf_fits_list, apply_bic_weighting=True, num_fixed_parameters=0, num_bootstrap_samples=1000, weight_threshold=0.01, do_bessel_correction=True, verbose=False)[source]#

Combine measurements using the relative BIC weights.

This function follows the methodology provided by Knabel & Mozumdar et al. (2025), arxiv.org/abs/2502.16034. It combines the values and uncertainties from multiple templates using relative BIC weights.

Parameters:
  • values (np.ndarray) – The values to combine, with shape [number of templates, number of bins or systems], or just [number of templates].

  • uncertainties (np.ndarray) – The uncertainties in the values.

  • ppxf_fits_list (np.ndarray) – The list of pPXF fits.

  • apply_bic_weighting (bool) – Whether to apply BIC weighting.

  • num_fixed_parameters (int) – The number of fixed parameters in the model.

  • num_bootstrap_samples (int) – The number of bootstrap samples to use.

  • weight_threshold (float) – The threshold for the relative BIC weights. Default is 1% (0.01).

  • do_bessel_correction (bool) – Whether to apply Bessel correction.

  • verbose (bool) – Whether to print the results.

Returns:

The combined values, combined systematic uncertainty, combined statistical uncertainty, and covariance matrix.

Return type:

tuple of np.ndarray

classmethod combine_weighted(values, uncertainties, weights, do_bessel_correction=True)[source]#

Combine the values using the weights.

The weighted combination with Bessel correction is described in Knabel & Mozumdar et al. (2025), arxiv.org/abs/2502.16034.

Parameters:
  • values (np.ndarray) – The values to combine.

  • uncertainties (np.ndarray) – The uncertainties in the values.

  • weights (np.ndarray) – The weights to use for the combination.

  • do_bessel_correction (bool) – Whether to apply Bessel correction.

Returns:

The combined values, combined systematic uncertainty, combined statistical uncertainty, and covariance matrix.

Return type:

tuple of np.ndarray

static create_kinematic_map_from_bins(bin_mapping, kinematic_values, nan_outside_bins=False)[source]#

Create a kinematic map from the binned spectra and the kinematic values.

This function generates a 2D kinematic map by assigning kinematic values to each pixel based on the bin mapping.

Parameters:
  • bin_mapping (np.ndarray) – A 2D array showing bin numbers for each pixel. Pixels not assigned to any bin should have a value of -1.

  • kinematic_values (list of float) – A list of kinematic values corresponding to each bin.

  • nan_outside_bins – Whether to set pixels outside of bins (with bin number -1) to NaN. If False (default), those pixels will be set to zero. Having NaN values can be useful for better visualization (e.g. with plt.imshow).

Returns:

A 2D kinematic map with the same shape as bin_mapping.

Return type:

np.ndarray

classmethod get_bic(ppxf_fit, num_fixed_parameters=0, weight_threshold=0.01)[source]#

Compute the Bayesian Information Criterion (BIC) for a given pPXF fit.

This method calculates the BIC for a pPXF fit object using the number of parameters (k), the number of data points (n), and the log-likelihood (log_L) extracted from the fit.

Parameters:
  • ppxf_fit (ppxf.ppxf) – ppxf fit object.

  • num_fixed_parameters (int) – Number of fixed parameters in fixed given to ppxf.

  • weight_threshold (float) – Threshold for the weights. Default is 1% (0.01).

Returns:

BIC value.

Return type:

float

classmethod get_bic_from_sample(ppxf_fits, num_fixed_parameters=0, weight_threshold=0.01)[source]#

Calculate the Bayesian Information Criterion (BIC) for a sample of pPXF fits.

This function follows the methodology provided by Knabel & Mozumdar et al. (2025), arxiv.org/abs/2502.16034. It computes the BIC for each pPXF fit in the sample and returns the total BIC.

Parameters:
  • ppxf_fits (list of ppxf.ppxf) – List of pPXF fit objects.

  • num_fixed_parameters (int) – Number of fixed parameters in fixed given to ppxf.

  • weight_threshold (float) – Threshold for the weights. Default is 1% (0.01).

Returns:

Total BIC for the sample.

Return type:

float

static get_emission_line_template(spectra, template_wavelengths, wavelength_factor=1.0, wavelength_range_extend_factor=1.05, **kwargs)[source]#

Get the emission line template.

This function generates an emission line template for the given spectra.

Parameters:
  • spectra (Spectra or a child class) – Log rebinned spectra.

  • template_wavelengths (np.ndarray) – Wavelengths of the template in Angstrom.

  • wavelength_factor (float) – Factor to multiply the wavelength range to get the templates for, used for de-redshifting, if necessary.

  • wavelength_range_extend_factor (float) – Factor to extend the wavelength range.

  • kwargs (dict) – Additional arguments for ppxf_util.emission_lines.

Returns:

Emission line template, line names, line wavelengths.

Return type:

Template class, list of str, np.ndarray

static get_power_binned_spectra(datacube, bin_mapping_output)[source]#

Perform the PowerBin binning.

Parameters:
  • datacube (DataCube class) – datacube to bin

  • bin_mapping_output (tuple) – outputs from get_power_binning_map()

Returns:

PowerBin binned spectra

Return type:

PowerBinBinnedSpectra class

static get_power_binning_map(datacube, signal_image_per_wavelength_unit, noise_image, target_snr, max_radius=None, min_snr_per_spaxel=1.0, capacity_spec=None, capacity_spec_args=None, capacity_spec_snr_relation=None, plot=False, quiet=True, **kwargs)[source]#

Get the PowerBin binning map.

Parameters:
  • datacube (DataCube class) – datacube to bin

  • signal_image_per_wavelength_unit (np.ndarray) – signal image per wavelength unit

  • noise_image (np.ndarray) – noise image

  • target_snr (float) – target S/N per wavelength unit for each bin

  • max_radius (float) – maximum radius for binning, in the unit of in datacube.x_coordinates

  • min_snr_per_spaxel (float) – minimum S/N per spaxel to include in the binning

  • capacity_spec – specification of bin capacity related to S/N, default will use util.powerbin_capacity_spec()

  • capacity_spec_args – input parameters for callable capacity_spec

  • capacity_spec_snr_relation – functional relationship between capacity definition and S/N, so that the output is given as S/N, S/N = capacity_spec_snr_relation(capacity)

  • plot (bool) – plot the results

  • quiet (bool) – suppress the output

  • kwargs (dict) – additional arguments for PowerBin()

Type:

callable with capacity_spec_args, or string β€˜additive’

Type:

tuple

Type:

callable

Returns:

PowerBin binning map outputs: bin number assigned to each pixel, x pixels, y pixels, bin center x, bin center y, capacity per bin, area of each bin

Return type:

tuple

classmethod get_relative_bic_weights_for_sample(ppxf_fits_list, num_fixed_parameters=0, num_bootstrap_samples=1000, weight_threshold=0.01)[source]#

Calculate the relative BIC weights for a given sample of pPXF fits.

This function follows the methodology provided by Knabel & Mozumdar et al. (2025), arxiv.org/abs/2502.16034. It computes the BIC for each pPXF fit in the sample, performs bootstrap sampling to estimate uncertainties, and calculates the relative BIC weights.

Parameters:
  • ppxf_fits_list (np.ndarray) – 2D array containing pPXF fits for the sample of galaxies or set of Voronoi bins with the dimension [number of models (or templates), number of systems (or bins) in sample].

  • num_fixed_parameters (int) – The number of fixed parameters in the model.

  • num_bootstrap_samples (int) – The number of bootstrap samples to use.

  • weight_threshold (float) – The threshold for the relative BIC weights. Default is 1% (0.01).

Returns:

Relative BIC weights for the sample.

Return type:

np.ndarray

static get_template_from_library(library_path, spectra, velocity_scale_ratio, wavelength_factor=1.0, wavelength_range_extend_factor=1.05, **kwargs)[source]#

Get the template object created for a stellar template library.

The library_path should point to a numpy.savez() file containing the following arrays for a given SPS models library, like FSPS, Miles, GALEXEV, BPASS. This file will be sent to ppxf.sps_util.sps_lib(). See the documentation of that function for the format of the file. The EMILES, FSPS, GALEXEV libraries are available at micappe/ppxf_data.

Parameters:
  • library_path (str) – Path to the library.

  • spectra (Spectra or a child class) – Log rebinned spectra, which the templates will be used for.

  • velocity_scale_ratio (float) – Velocity scale ratio for the template.

  • wavelength_factor (float) – Factor to multiply the wavelength range to get the templates for, used for de-redshifting, if necessary.

  • wavelength_range_extend_factor (float) – Factor to extend the wavelength range.

  • kwargs (dict) – Additional arguments for ppxf.sps_util.sps_lib() function.

Returns:

Template object.

Return type:

Template class

static get_terms_in_bic(ppxf_fit, num_fixed_parameters=0, weight_threshold=0.01)[source]#

Get the k, n, and log_L terms needed to compute the BIC.

This method extracts the number of parameters (k), the number of data points (n), and the log-likelihood (log_L) from a pPXF fit object, which are required to compute the Bayesian Information Criterion (BIC).

Parameters:
  • ppxf_fit (ppxf.ppxf) – ppxf fit object.

  • num_fixed_parameters (int) – Number of fixed parameters in fixed given to ppxf.

  • weight_threshold (float) – Threshold for the weights. Default is 1% (0.01).

Returns:

Number of parameters (k), number of data points (n), and log-likelihood (log_L).

Return type:

tuple of int, int, float

static get_voronoi_binned_spectra(datacube, bin_mapping_output)[source]#

Perform the Voronoi binning.

Parameters:
  • datacube (DataCube class) – datacube to bin

  • bin_mapping_output (tuple) – outputs from get_voronoi_binning_map()

Returns:

Voronoi binned spectra

Return type:

VoronoiBinnedSpectra class

static get_voronoi_binning_map(datacube, signal_image_per_wavelength_unit, noise_image, target_snr, max_radius=None, min_snr_per_spaxel=1.0, plot=False, quiet=True, **kwargs)[source]#

Get the Voronoi binning map.

Parameters:
  • datacube (DataCube class) – datacube to bin

  • signal_image_per_wavelength_unit (np.ndarray) – signal image per wavelength unit

  • noise_image (np.ndarray) – noise image

  • target_snr (float) – target S/N per wavelength unit for each bin

  • max_radius (float) – maximum radius for binning, in the unit of in datacube.x_coordinates

  • min_snr_per_spaxel (float) – minimum S/N per spaxel to include in the binning

  • plot (bool) – plot the results

  • quiet (bool) – suppress the output

  • kwargs (dict) – additional arguments for voronoi_2d_binning()

Returns:

Voronoi binning map outputs: number of bins, x pixels, y pixels, bin center x, bin center y, S/N per bin, area of each bin

Return type:

tuple

static join_templates(kinematic_template, kinematic_template_2=None, emission_line_template=None, emission_line_groups=None)[source]#

Join multiple templates into a single template object.

This function combines kinematic and emission line templates into a single template object. It also generates component indices to identify which component each template belongs to.

Parameters:
  • kinematic_template (Template class) – The primary kinematic template.

  • kinematic_template_2 (Template class, optional) – An optional secondary kinematic template.

  • emission_line_template (Template class, optional) – An optional emission line template.

  • emission_line_groups (list of int, optional) – Groups for the emission line templates.

Returns:

Combined template, component indices, and emission line indices.

Return type:

tuple of Template class, np.ndarray, np.ndarray

static log_rebin(spectra, velocity_scale=None, num_samples_for_covariance=None, take_covariance=True)[source]#

Rebin the data to log scale.

Parameters:
  • spectra (Spectra class) – data to rebin

  • velocity_scale (float) – velocity scale for the rebinning

  • num_samples_for_covariance (int) – number of samples for the covariance estimation

  • take_covariance (bool) – take the covariance into account

Returns:

rebinned spectra

Return type:

Spectra class

static make_template_from_array(fluxes, wavelengths, fwhm_template, spectra, velocity_scale_ratio, wavelength_factor=1.0, wavelength_range_extend_factor=1.05)[source]#

Create a template object from given fluxes and wavelengths.

This function generates a template object from provided fluxes and wavelengths. It performs convolution to match the spectral resolution and log rebinning.

Parameters:
  • fluxes (np.ndarray) – Fluxes of the templates, dimensions must be (n_wavelengths, n_templates).

  • wavelengths (np.ndarray) – Wavelengths of the templates in Angstrom.

  • spectra (Spectra or a child class) – Log rebinned spectra, which the templates will be used for.

  • velocity_scale_ratio (float) – Velocity scale ratio for the template.

  • wavelength_factor (float) – Factor to multiply the wavelength range to get the templates for, used for de-redshifting, if necessary.

  • wavelength_range_extend_factor (float) – Factor to extend the wavelength range.

Returns:

Template object.

Return type:

Template class

static run_ppxf(data, template, start, background_template=None, spectra_indices=None, quiet=True, plot=False, **kwargs_ppxf)[source]#

Perform the kinematic analysis using pPXF.

This method runs the Penalized Pixel-Fitting (pPXF) method on the provided data using the given template. It allows for the analysis of both single spectra and datacubes or binned spectra.

Parameters:
  • data (Data class) – Data to analyze.

  • template (Template class) – Template library to use for fitting.

  • start (list) – Initial guess for the velocity and dispersion for each kinematic component.

  • background_template (Data class, optional) – Background spectra to fit, if any.

  • spectra_indices (list of int or int, optional) – Indices of the spectra to fit, used for datacubes or binned spectra.

  • quiet (bool, optional) – Suppress the output.

  • plot (bool, optional) – Plot the fit results.

  • kwargs_ppxf (dict) – Additional options for ppxf, check documentation of ppxf.ppxf().

Returns:

pPXF fit object.

Return type:

ppxf class

classmethod run_ppxf_on_binned_spectra(binned_spectra, template, start, background_template=None, **kwargs_ppxf)[source]#

Perform the kinematic analysis using pPXF on binned spectra.

This method runs the Penalized Pixel-Fitting (pPXF) method on the provided binned spectra using the given template. It allows for the analysis of Voronoi binned spectra.

Parameters:
  • binned_spectra (VoronoiBinnedSpectra class) – Binned spectra to analyze.

  • template (Template class) – Template library to use for fitting.

  • start (list) – Initial guess for the velocity and dispersion for each kinematic component.

  • background_template (Template class, optional) – Background spectra to fit, if any.

  • kwargs_ppxf (dict) – Additional options for ppxf, check documentation of ppxf.ppxf().

Returns:

Velocity dispersions, velocity dispersion uncertainties, mean velocities, mean velocity uncertainties.

Return type:

tuple of np.ndarray

squirrel.template module#

This module contains the class to store stellar and other templates and process them.

class squirrel.template.Template(wavelengths, flux, wavelength_unit, fwhm, flux_unit='arbitrary')[source]#

Bases: Spectra

A class to store stellar and other templates and process them.

combine_weighted(weights)[source]#

Combine the templates into one single template using weighted sum.

Parameters:

weights (numpy.array) – weights for each template

Returns:

A new Template instance with combined flux

Return type:

squirrel.template.Template

discard_zero_weights(weights)[source]#

Discard the templates with zero weights.

Parameters:

weights (numpy.array) – weights for each template

Returns:

A new Template instance with non-zero weighted flux

Return type:

squirrel.template.Template

merge(other)[source]#

Merge the template with another template.

Parameters:

other (squirrel.template.Template) – template to merge with

Returns:

A new Template instance with merged flux

Return type:

squirrel.template.Template

squirrel.util module#

This module contains class and functions for general use.

squirrel.util.get_nearest_positive_definite_matrix(matrix)[source]#

Find the nearest positive-definite matrix to input.

A Python/Numpy port of John D’Errico’s nearestSPD MATLAB code [1], which credits [2].

[1] https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd

[2] N.J. Higham, β€œComputing a nearest symmetric positive semidefinite matrix” (1988): https://doi.org/10.1016/0024-3795(88)90223-6

Adapted from https://gist.github.com/fasiha/fdb5cec2054e6f1c6ae35476045a0bbd

Parameters:

matrix (numpy.ndarray) – matrix to find nearest positive-definite matrix to

Returns:

nearest positive-definite matrix

Return type:

numpy.ndarray

squirrel.util.is_positive_definite(matrix)[source]#

Returns true when input is positive-definite, via Cholesky.

This function attempts to perform a Cholesky decomposition of the input matrix. If the decomposition is successful, the matrix is positive-definite. If it fails, the matrix is not positive-definite.

Parameters:

matrix (numpy.ndarray) – matrix to check

Returns:

True if matrix is positive-definite

Return type:

bool

squirrel.util.powerbin_capacity_spec(index, signal, noise)[source]#

Calculates (S/N)^2 for a bin from its pixel indices for PowerBin method.

This function is the default specified for get_power_binning_map().

Parameters:
  • index (array) – The pixel indices for the bin.

  • signal (array) – The signal image for the datacube.

  • noise (array) – The signal image for the datacube.

Returns:

(S/N)^2 for the bin at its pixel indices.

Return type:

float

Module contents#

Top-level package for squirrel.