hostsub_gp package

Subpackages

Submodules

hostsub_gp.gp module

class hostsub_gp.gp.GP(kernel_type: str, X: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray, y: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray | None = None, yerr: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray | None = None, params: dict | None = None, params_init: dict | None = None, params_limit: dict | None = None, optimization: bool = False, **kwargs)[source]

Bases: object

Gaussian Process. A wrapper around tinygp.GaussianProcess.

log_probability(y: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Array[source]

Log-probability of the Gaussian Process.

predict(X_test: Array, return_var: bool = False) Array | tuple[Array, Array][source]

Predict the mean and variance of the Gaussian Process at the input points.

hostsub_gp.host_image module

class hostsub_gp.host_image.PS1Image(ra: float, dec: float, filters: str = 'grizy', path: str = './ps1_cutout', size: int = 2400)[source]

Bases: ArchivalImage

Class to load images from the PS1 Image Cutout Service

download(overwrite: bool = False)[source]

Download images from the PS1 Image Cutout Service

class hostsub_gp.host_image.SDSSImage(ra: float, dec: float, filters: str = 'ugriz', path: str = './sdss_cutout')[source]

Bases: ArchivalImage

Class to load images from astroquery.sdss.SDSS

download(overwrite: bool = False) None[source]

Download images from astroquery.sdss.SDSS

hostsub_gp.host_model module

class hostsub_gp.host_model.HostProfile(filters: str | list[str], wv_eff: list[float], spat_slit: list[Array], counts_slit: list[Array], counts_err_slit: list[Array], spec_model: SpecModelP | None = None, slit_len: float | None = None, pixel_scale: float | None = None)[source]

Bases: object

classmethod from_archival(img_products: list[ImageProduct], spec_model: SpecModelP | None = None, slit_len: float | None = None, slit_wid: float | None = None, pixel_scale: float | None = None, dseeing: float | None = None, alpha: float | None = 0.2, verbose: bool = False)[source]

Load archival images from PS1 and SDSS and estimate the host galaxy spatial profile.

Parameters:
  • spec_model (any, optional) – SpecModel object.

  • center_ra (float, optional) – Right ascension of the object.

  • center_dec (float, optional) – Declination of the object.

  • slit_len (float, optional) – Slit length in arcsec.

  • slit_wid (float, optional) – Slit width in arcsec.

  • pixel_scale (float, optional) – Pixel scale in arcsec of the 2D spectrum (not the archival images)

  • position_angle (float, optional) – Position angle of the slit.

  • filters (str or list, optional) – Filters to load the images.

  • survey (str, optional) – Survey to use for loading images. Options are ‘PS1’ or ‘LS’.

  • dseeing (float, optional)

  • alpha (float, optional, default = 0.2)

static load_archival_images(spec_model: SpecModelP | None = None, center_ra: float | None = None, center_dec: float | None = None, slit_len: float | None = None, slit_wid: float = 1.0, position_angle: float | None = None, filters: str | list | None = None, survey: Literal['PS1', 'LS', 'any'] = 'PS1') list[ImageProduct][source]

Load, rotate, and resample archival images from PS1, LS, and SDSS

model_host_profile_prior(spat_resln: float = 1.0, params_init: dict | None = None, **kwargs) Callable[[Any], Array | tuple[Array, Array]][source]

Model the host galaxy spatial profile using Gaussian Process regression.

Parameters:

spat_resln (float, optional) – Spatial resolution (seeing) in arcsec.

Returns:

host_prior – A function that returns the mean and variance of the host profile.

Return type:

Callable[[Array], tuple[Array, Array]]

hostsub_gp.inputfiles module

class hostsub_gp.inputfiles.Digitize(_call: Callable)[source]

Bases: object

Convert the input value to the specified data type.

If the input contains multiple tuples, convert them to a list of tuples.

class hostsub_gp.inputfiles.HostSubInput(*args: Any, **kwargs: Any)[source]

Bases: InputFile

data_block = 'hostsub'
datablock_required = True
flavor = 'HostSub'
required_columns = ['filename', 'objid', 'frametype']
setup_required = False

hostsub_gp.interp module

class hostsub_gp.interp.Interp1D_Grid(points: Array, values: Array, method='linear')[source]

Bases: object

1D interpolation using a regular grid. A wrapper around jax.numpy.interp.

class hostsub_gp.interp.Interp2D_Grid(points: tuple[Array, Array], values: Array, method='linear')[source]

Bases: object

2D interpolation using a regular grid. A wrapper around jax.scipy.interpolate.RegularGridInterpolator.

class hostsub_gp.interp.Interp2D_Linear(scales: tuple | Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = (1, 1))[source]

Bases: Interp2D_base

2D bilinear interpolation on semi-uniform grid.

class hostsub_gp.interp.Interp2D_RBF(kernel: str = 'gaussian', epsilon: float = 1.0, scales: tuple | Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = (1, 1))[source]

Bases: Interp2D_base

2D interpolation using radial basis functions.

class hostsub_gp.interp.Interp2D_Scipy(method: str = 'linear', scales: tuple | Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = (1, 1))[source]

Bases: Interp2D_base

2D interpolation using scipy’s griddata.

predict(query_points: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Array[source]

Make predictions at new points with scipy’s griddata

Parameters:

query_points (ArrayLike) – Array of shape (n_queries, 2) containing points to interpolate

Returns:

Array of interpolated values at query_points

Return type:

Array

class hostsub_gp.interp.Interp2D_base(scales: tuple | Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = (1, 1))[source]

Bases: ABC

Abstract base class for 2D interpolation on semi-uniform grids.

fit(points: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray, values: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) None[source]

Store the training data and grid information.

Parameters:
  • points (ArrayLike) – Array of shape (nx, ny, 2) containing the 2D coordinates

  • values (ArrayLike) – Array of shape (nx, ny) containing the values at each point

predict(query_points: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Array[source]

Make predictions at new points with NaN handling

Parameters:

query_points (ArrayLike) – Array of shape (n_queries, 2) containing points to interpolate

Returns:

Array of interpolated values at query_points

Return type:

Array

hostsub_gp.spec_base module

class hostsub_gp.spec_base.SpecModelProtocol(*args, **kwargs)[source]

Bases: Protocol

Protocol for the SpecModel class

center_dec: float
center_ra: float
mask_offset: float
pixel_scale: float
position_angle: float
shape: tuple[int, int]
slit_wid: float
spat: Array
spat_edges: dict[str, tuple[float, float]]
spat_filter: dict[str, Array]
spat_resln: float
spec: Array
spec_resln: float

hostsub_gp.spec_model module

class hostsub_gp.spec_model.SpecModel(dat: Array, dat_err: Array, *, spat: Array, spec: Array, pixel_scale: float, position_angle: float, slit_wid: float, spat_resln: float, spec_resln: float, center_ra: float | None = None, center_dec: float | None = None, slit_len: float | None = None, slit_trim: tuple[int, int] = (1, 1), spec_range: tuple[float, float] | None = None, mask_wid: float = 2.0, mask_offset: float = 0.0, host_region: tuple[float, float] = (-5.0, 5.0), sky_region: tuple[float, float] = (-5.0, 5.0))[source]

Bases: object

A class for the host galaxy modeling on a rectified 2D spectrum.

spat

The spatial grids of the 2D spectrum.

Type:

ArrayLike

spec

The spectral grids of the 2D spectrum.

Type:

ArrayLike

shape

The shape of the 2D spectrum.

Type:

tuple[int, int]

pixel_scale

The instrumental pixel scale of the 2D spectrum on the spatial axes (arcsec per pixel).

Type:

float

center_ra

The right ascension of science object.

Type:

float

center_dec

The declination of science object.

Type:

float

slit_wid

The width of the slit (arcsec).

Type:

float

position_angle

The position angle of the slit (degree).

Type:

float

spat_resln

The spatial resolution (FWHM/seeing) of the 2D spectrum (arcsec).

Type:

float

spec_resln

The spectral resolution of the 2D spectrum (angstrom).

Type:

float

slit_len

The length of the slit (arcsec).

Type:

float

slit_trim

The trimming edges of the slit (pixels).

Type:

tuple[int, int]

spec_range

The spectral range of the 2D spectrum (angstrom).

Type:

tuple[float, float]

host_wid

The width of the host galaxy region used for the fitting (in arcsec).

Type:

float

mask_wid

The width of the mask to mask the source trace (in spat_resln/seeing).

Type:

float

sky_region

The (inner edges of) global sky region defined as the distance from the source (in arcsec).

Type:

tuple

batch_2d

The batch size for modeling the slowly varying host profiles.

Type:

tuple

f_obs

The 2D spectrum of the observed data.

Type:

SpecWrapper

f_sky

The 1D spectrum of the global sky background (mean of the sky region).

Type:

SpecWrapper

f_sky_sub

The 2D spectrum of the sky-subtracted data.

Type:

SpecWrapper

f_host

The 2D spectrum of the sky-subtracted host galaxy (within the host region).

Type:

SpecWrapper

f_host_1d

The 1D spectrum of the sky-subtracted host galaxy (within the host region).

Type:

SpecWrapper

f_host_batch_2d

The batched 2D spectrum (batch size = batch_2d) of the sky-subtracted host galaxy (within the host region).

Type:

SpecWrapper

f_batch_2d

The batched 2D spectrum (batch size = batch_2d) of the sky-subtracted data.

Type:

SpecWrapper

f_host_prior

GP model of the photometric data (within the host region).

Type:

SpecWrapper

f_batch_prior

GP model of the photometric data (batched 2D spectrum).

Type:

SpecWrapper

f_host_batch_prior

GP model of the photometric data (batched 2D spectrum within the host region).

Type:

SpecWrapper

dist_host_batch_2d

The distance relative to the prior of the batched 2D spectrum (within the host region).

Type:

SpecWrapper

dist_batch_2d

The distance relative to the prior of the batched 2D spectrum.

Type:

SpecWrapper

spat_filter

Spatial filters - mask: mask the source trace - host, host_left, host_right: mask the host galaxy region - sky, sky_left, sky_right: mask the sky region

Type:

dict[str, ArrayLike]

spat_edges

The edges of each spatial region - mask: mask the source trace - host: mask the host galaxy region - sky: mask the sky region

Type:

dict[str, tuple]

model_host_prior()

Build the prior of the host galaxy using Gaussian Process regression.

model_host()[source]

Model the host galaxy using Gaussian Process regression.

extract_sci()[source]

Extract the 1D spectrum after host galaxy subtraction.

build_host_prior(filters: str | list[str] = 'grizy', from_archival: bool = True, survey: Literal['PS1', 'LS'] = 'PS1', wv_eff: list[float] | None = None, spat_slit: list[Array] | None = None, counts_slit: list[Array] | None = None, counts_err_slit: list[Array] | None = None, dseeing: float | None = None, alpha: float = 0.2, verbose: bool = False, **kwargs) Callable[[Array], tuple[Array, Array]][source]

Build the prior of the host galaxy using Gaussian Process regression.

construct_spec_wrapper(f_obs: SpecWrapper, batch_2d: tuple[int, int] = (1, 64), host_emission_cfg: dict | None = None, sigma_clip: float = 5.0, show: bool = False, save: str | None = None)[source]

Construct the SpecWrapper objects corresponding to the 1D/2D spectrum in the global sky region, the host region, and in the batched 2D grids.

Parameters:
  • f_obs (SpecWrapper) – The 2D spectrum of the observed data.

  • batch_2d (tuple[int, int], optional) – The spatial and spectral batch size for modeling the slowly varying host profiles, by default (2, 64).

  • host_emission_cfg (dict, optional) – Parameters for identifying host emission lines, by default None.

  • sigma_clip (float, optional) – Sigma clipping threshold, by default 5.0.

  • show (bool, optional) – Whether to show the diagnostic plots, by default False.

  • save (str, optional) – The path to save the diagnostic plots, by default None (do not save).

extract_sci(extr_method='sum') Axes[source]

Extract the science spectrum after host galaxy subtraction (within the mask).

extract_sci_classic(extr_method='sum') None[source]

Extract the science spectrum after host galaxy subtraction (within the mask) using classic linear and B-spline background estimation methods.

get_host_prior(host_prior_gp: Callable[[Array], tuple[Array, Array]] | None = None) Callable[[Array], tuple[Array, Array]][source]

Normalize the GP solution of the prior

get_normalized_batch_spec(spat_batch_idx: list, spec_batch_idx: list, f_2d: SpecWrapper, f_1d_norm: SpecWrapper, nan_threshold: float = 0.1) SpecWrapper[source]

Get the batched 2D spectrum normalized by a 1D spectrum.

Parameters:
  • spat_batch_idx (ArrayLike) – Batch indices for the spatial axis.

  • spec_batch_idx (ArrayLike) – Batch indices for the spectral axis.

  • f_2d (SpecWrapper) – The 2D spectrum to be batched.

  • f_1d_norm (SpecWrapper) – The 1D spectrum to normalize the 2D spectrum.

  • nan_threshold (float, optional) – The threshold for the fraction of NaN values in the batch.

Returns:

The batched 2D spectrum.

Return type:

SpecWrapper

model_host(params_init: tuple[dict, dict], params_limit: tuple[dict, dict] | None = None, optimization: bool = False, optimization_kwargs: dict = {})[source]

Model the host galaxy using Gaussian Process regression.

Parameters:
  • params_init (dict) – Initial parameters for optimization.

  • optimization (bool, optional (default: False)) – Whether to optimize the model with the jaxopt.ScipyMinimize solver.

update_seeing(dseeing: float | None = None, alpha: float | None = 0.2, **kwargs) tuple[float, float][source]

Update the seeing of the host galaxy profile with the instrumental seeing.

hostsub_gp.spec_proc module

class hostsub_gp.spec_proc.SpecData(pixel_scale: float, center_ra: float, center_dec: float, slit_wid: float, position_angle: float, spat_resln: float, spec_resln: float, spat_rect: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray, spec_rect: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray, flux_rect: Array | None = None, flux_ivar_rect: Array | None = None, dist: Array | None = None, waveimg: Array | None = None, flux: Array | None = None, flux_global_sky: Array | None = None, flux_ivar: Array | None = None, sky_offset: float | None = None, survey: Literal['PS1', 'LS'] = 'PS1', to_caches: bool | None = False, cache_path: str | None = None)[source]

Bases: object

A class to load (from different spectrograph) and preprocess (rectification & alignment) the 2D spectrum.

classmethod coadd2d(spec_data_list: list[SpecData], **kwargs) tuple[SpecData, ndarray | None][source]

Coadd multiple SpecData objects.

Parameters:

spec_data_list (list[SpecData]) – A list of SpecData objects to be coadded.

Returns:

  • SpecData – The coadded SpecData object.

  • cr_mask (np.ndarray) – The mask of cosmic rays.

classmethod from_fits(fits_path: str | None = None, **kwargs)[source]

Load 2D spectra from cache files.

Parameters:

cache_path (str, optional (default: ".cache.json")) – The path to the cache file.

classmethod from_pypeit(sci_file: str, raw_dir: str | None = None, obj_id: str | None = None, std_file: str | None = None, ra: float | None = None, dec: float | None = None, spat_resln: float | None = None, spat_rect: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray | None = None, spec_rect: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray | None = None, **kwargs)[source]

Load 2D spectra from PypeIt output files.

Parameters:
  • sci_file (str) – The filename of the science object.

  • obj_id (str, optional (default: None)) – The object ID in the science frame.

  • std_file (str, optional (default: None)) – The filename of the standard star.

  • raw_dir (str, optional (default: None)) – The directory of the raw files (not needed for LRIS data).

  • ra (float, optional (default: None)) – The RA and DEC of the science object. If not provided, the RA and DEC in the header will be used.

  • dec (float, optional (default: None)) – The RA and DEC of the science object. If not provided, the RA and DEC in the header will be used.

  • spat_resln (float, optional (default: None)) – The spatial resolution (seeing) of the science frame.

  • spat_rect (ArrayLike, optional (default: None)) – The spatial coordinates of the rectified 2D spectrum.

  • spec_rect (ArrayLike, optional (default: None)) – The spectral coordinates of the rectified 2D spectrum.

to_SpecModel(**kwargs) SpecModel[source]

Convert the 2D spectra to a SpecModel object.

Parameters:

spec_range (tuple or list, optional (default: None)) – The range of the spectral pixels to include.

to_fits()[source]

Save the 2D spectra to cache files.

Parameters:

cache_path (str, optional (default: ".cache.json")) – The path to the cache file.

update_pypeit_skymodel(spec_model: SpecModel, spec2d_file: str)[source]

Update the sky model and the mask in the PypeIt spec2d file.

hostsub_gp.spec_wrapper module

class hostsub_gp.spec_wrapper.SpecWrapper(points: Array | tuple[Array, Array], values: Array | None = None, values_err: Array | None = None)[source]

Bases: object

A wrapper for the 1D and 2D spectra.

apply_spatial_filter(spat_filter: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) SpecWrapper[source]

Creates a new spectrum by applying a spatial mask to the current spectrum.

Parameters:

spat_filter (ArrayLike) – Filter to apply to the spatial axis.

Returns:

  • SpecWrapper

  • A new spectrum containing only the data points selected by the mask

convolve(kernel_wid: float | Array | ndarray | bool | number | bool | int | complex | TypedNdArray) SpecWrapper[source]

Convolve the spectrum with a kernel.

Parameters:

kernel_wid (float | ArrayLike) – The width (FWHM in pixel) of the Gaussian kernel

Returns:

The seeing-matched spectrum.

Return type:

SpecWrapper

fill_nan() SpecWrapper[source]

Fill the NaN values in the spectrum by interpolation.

marginalize(margin_type: str = 'mean', weights: str | Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray | None = None, sigma_clip: float = 5.0, nan_threshold: float = 0.1) SpecWrapper[source]

Marginalize the 2D spectrum along the spatial axis to obtain the 1D spectrum.

Parameters:
  • margin_type (str, optional) – Type of the marginalization: mean or sum. Default is mean.

  • weights (str, optional) – Weights for the marginalization: None, ivar, snr, or an array of the weights. Default is None. None: no weights ivar: inverse variance snr: signal-to-noise ratio squared

  • sigma_clip (float, optional) – Sigma clipping threshold for the marginalization. Default is 5.

  • nan_threshold (float, optional) – Threshold for the fraction of NaN values in a column. Columns with NaN fraction > nan_threshold will be masked. Default is 0.1.

Returns:

The marginalized 1D spectrum.

Return type:

SpecWrapper

sigma_clip(sigma: float | None = None, clip_cr: bool = False, batch_idx: list | tuple[list, list] | None = None) SpecWrapper[source]

Sigma clipping for the spectrum.

Parameters:
  • sigma (float, optional) – Sigma clipping threshold. Default is 5.

  • clip_cr (bool, optional) – Whether to clip cosmic rays only (i.e., positive outliers). Default is False.

  • batch_idx (list | tuple[list, list], optional) – Batch indices for sigma clipping. Default is None.

Returns:

The clipped spectrum.

Return type:

SpecWrapper

subtract(other: SpecWrapper) SpecWrapper[source]

Subtract another spectrum.

Parameters:

other (SpecWrapper) – The other spectrum to be subtracted.

Returns:

The subtracted spectrum.

Return type:

SpecWrapper

hostsub_gp.spec_wrapper.mad_std(data: Array, axis: int | None = None) Array[source]

Compute the Median Absolute Deviation (MAD) of the input data.