GroupFitter¶
- class grizli.fitting.GroupFitter[source]¶
Bases:
object
Base class for
StackFitter
andMultiBeam
spectrum fitting objectsMethods Summary
check_tfit_coeffs
(tfit, templates[, ...])Compare emission line fluxes fit at each grism/PA to the combined value.
compute_D4000
(z[, fit_background, fit_type, ...])Compute D4000 with step templates
compute_scale_array
(pscale, wave)Return the scale array given the coefficients
fit_line_width
([bl, nl, z0, max_nfev, tol, ...])Fit for emisson line width
get_SDSS_photometry
([bands, templ, radius, ...])Try too get SDSS photometry from
astroquery
get_flat_background
(bg_params[, apply_mask])Generate background array the same size as the flattened total science array.
get_flat_model
(spectrum_1d[, id, ...])Generate model array based on the model 1D spectrum in
spectrum_1d
initialize_masked_arrays
([seg_ids])Initialize flat masked arrays for faster likelihood calculation
objfun_scale
(pscale, AxT, data, self, retval)Objective function for fitting for a scale term between photometry and spectra
oned_figure
([bin, wave, show_beams, minor, ...])Make a figure showing the 1D spectra
optimal_extract
([data, bin, wave, ivar, ...])Binned optimal extractions by grism with algorithm from Horne 1984
scale_to_photometry
([tfit, tol, order, ...])Compute scale factor between spectra and photometry
set_photometry
([flam, eflam, filters, ...])Set photometry attributes
template_at_z
([z, templates, fwhm, ...])Get the best-fit template at a specified redshift
Unset photometry-related attributes
xfit_at_z
([z, templates, fitter, ...])Fit the 2D spectra with a set of templates at a specified redshift.
xfit_redshift
([prior, templates, fwhm, ...])Two-step procedure for fitting redshifts
xfit_star
([tstar, spline_correction, ...])Fit stellar templates
xmake_fit_plot
(fit, tfit[, show_beams, bin, ...])Make a diagnostic plot of the redshift fit
Methods Documentation
- check_tfit_coeffs(tfit, templates, refit_others=True, fit_background=True, fitter='nnls', bounded_kwargs={'method': 'bvls', 'tol': 1e-08, 'verbose': 0})[source]¶
Compare emission line fluxes fit at each grism/PA to the combined value. If
refit_others=True
, then compare the line fluxes to a fit from a new object generated excluding that grism/PA.- Parameters
- tfitdict
Output of
template_at_z
for the line template.- templatesdict
Dictionary of
SpectrumTemplate
objects.- refit_othersbool
Refit the other grism/PA combinations.
- fit_backgroundbool
Include background in the fit.
- fitterstr
Fitting method passed to
template_at_z
. Default isnnls
.- bounded_kwargsdict
Bounded least-squares fitting parameters.
- Returns
- max_linestr
Line species with the maximum deviation
- max_line_difffloat
The maximum deviation for
max_line
(sigmas).- comparedict
The full comparison dictionary
- compute_D4000(z, fit_background=True, fit_type='D4000', fitter='lstsq')[source]¶
Compute D4000 with step templates
- Parameters
- zfloat
Redshift where to evaluate D4000
- fit_backgroundbool
Include background in step template fit
- fit_type‘D4000’, ‘Dn4000’
- Definition to use:
D4000 = f_nu(3750-3950) / f_nu(4050-4250) Dn4000 = f_nu(3850-3950) / f_nu(4000-4100)
- fitterstr
Least-squares method passed to
template_at_z
.
- Returns
- w_d4000, t_d4000
ndarray
, dict Step wavelengths and template dictionary
- tfitdict
Fit dictionary returned by
template_at_z
.- d4000, d4000_sigmafloat
D4000 estimate and uncertainty from simple error propagation and step template fit covariance.
- w_d4000, t_d4000
- static compute_scale_array(pscale, wave)[source]¶
Return the scale array given the coefficients
- Parameters
- pscalearray-like
Coefficients of the linear model normalized by factors of 10 per order, i.e,
pscale = [10]
is a constant unit scaling. Note that parameter is what is expected bynumpy.polynomial.Polynomial
.- wavearray-like
Wavelength grid in Angstroms. Scaling is normalized to
(wave - 1e4)/1000
.
- Returns
- wscalearray-like
Scale factor
>>> pscale = [10] >>> N = len(pscale) >>> rescale = 10**(np.arange(N)+1) >>> wscale = np.polynomial.Polynomial(pscale/rescale)((wave-1.e4)/1000.)
- fit_line_width(bl=2.5, nl=1.1, z0=1.9367, max_nfev=100, tol=0.001, verbose=False)[source]¶
Fit for emisson line width
- Parameters
- blfloat
Initial guess for broad line width in 1000 km/s.
- nlfloat
Initial guess for narrow line width in 1000 km/s.
- z0float
Initial guess for redshift.
- max_nfevint
Maximum number of function evaluations.
- tolfloat
Tolerance for the fit.
- verbosebool
Passed to
least_squares
.- Returns:
- reslist
List of results: [bl, nl, z, nfev, max_nfev_reached].
- blfloat
Best-fit broad line width in 1000 km/s.
- nlfloat
Best-fit narrow line width in 1000 km/s.
- zfloat
Best-fit redshift.
- nfevint
Number of function evaluations.
- max_nfev_reachedbool
True if the maximum number of function evaluations was reached.
- get_SDSS_photometry(bands='ugriz', templ=None, radius=2, SDSS_CATALOG='V/147/sdss12', get_panstarrs=False)[source]¶
Try too get SDSS photometry from
astroquery
(developmental)
- Parameters
- bandsstr
SDSS bands to query.
- templdict
Templates to use for fitting.
- radiusfloat
Search radius in arcsec.
- SDSS_CATALOGstr
Catalog to query. Default is
V/147/sdss12
.- get_panstarrsbool
Get PanSTARRS photometry.
- Returns
- photdict
Dictionary with keys
flam
,eflam
,filters
,tempfilt
, whereflam
andeflam
are arrays of flux densities and uncertainties in f-lambda units,filters
are theFilterDefinition
objects, andtempfilt
is aTemplateGrid
object.
- get_flat_background(bg_params, apply_mask=True)[source]¶
Generate background array the same size as the flattened total science array.
- Parameters
- bg_paramsarray with shape (self.N) or (self.N, M)
Background parameters for each beam, where the
M
axis is polynomial cofficients in the order expected byPolynomial2D
. If the array is 1D, then provide a simple pedestal background.- apply_maskbool
Return the background pixels applying the
BeamCutout
fit_mask
attribute.
- Returns
- bg_modelArray with dimensions
(self.fit_mask.sum(),)
Flattened, masked background array.
- bg_modelArray with dimensions
- get_flat_model(spectrum_1d, id=None, apply_mask=True, is_cgs=True)[source]¶
Generate model array based on the model 1D spectrum in
spectrum_1d
- Parameters
- spectrum_1dtuple, -1
Tuple of 1D arrays (wavelength, flux). If
-1
, then use the in_placemodel
attributes of each beam.- idint
Value that identifies pixels in the segmentation thumbnail with the desired object to model
- apply_maskbool
Return the model pixels applying the
BeamCutout
fit_mask
attribute- is_cgsbool
spectrum_1d
flux array has CGS f-lambda flux density units.
- Returns
- modelArray with dimensions
(self.fit_mask.sum(),)
Flattened, masked model array.
- modelArray with dimensions
- initialize_masked_arrays(seg_ids=None)[source]¶
Initialize flat masked arrays for faster likelihood calculation
- Parameters
- seg_idslist
List of segmentation IDs to use for the optimal profile.
- static objfun_scale(pscale, AxT, data, self, retval)[source]¶
Objective function for fitting for a scale term between photometry and spectra
- Parameters
- pscalearray-like
Scale factor for each template in
AxT
and the photometry.- AxT
ndarray
Template array,
AxT.T
is the transpose of the template array where each column is a template.- data
ndarray
Photometry data.
- self
GroupFitter
Object with the templates and photometry.
- retvalstr
Return type, one of [‘resid’, ‘coeffs’].
- Returns
- oned_figure(bin=1, wave=None, show_beams=True, minor=0.1, tfit=None, show_rest=False, axc=None, figsize=[6, 4], fill=False, units='flam', min_sens_show=0.1, ylim_percentile=2, scale_on_stacked=False, show_individual_templates=False, apply_beam_mask=True, loglam_1d=True, trace_limits=None, show_contam=False, add_label=True, beam_models=None, median_filter_kwargs=None)[source]¶
Make a figure showing the 1D spectra
- Parameters
- binfloat
Binning factor relative to nominal resolution (per pix) of each grism
- waveNone, array
Fixed wavelength array for the sampled spectra
- show_beamsbool
Show all individual beams
- minorfloat
Minor axis tick interval (microns)
- tfitdict
Fit information from
template_at_z
. If provided, then will include the best-fit models in the figure- show_restbool
Show rest-frame wavelengths
- axc
AxesSubplot
If provided, then draw into existing axis without making a new figure
- figsize(float, float)
Figure size (inches)
- fillbool
plot filled spectra
- unitsstr
Y-axis units
‘flam’ = Scaled f-lambda cgs
‘nJy’ = nanoJansky
‘mJy’ = milliJansky
‘eps’ = native detector units of electrons per second
‘meps’ = “milli”-electrons per second
‘spline[N]’ = Divide out a spline continuum
‘resid’ = Residuals w.r.t. model in
tfit
- min_sens_showfloat
Minimum sensitivity to show in the plot.
- ylim_percentilefloat
Percentile of the data to use for the y-axis limits.
- scale_on_stackedbool
Set the y-axis limits based on the stacked spectrum.
- show_individual_templatesbool
Show each individual template with its scaling along with the best-fit combination
- apply_beam_maskbool
Apply the beam mask to the 1D spectra.
- loglam_1dbool
Plot as log wavelength
- trace_limits(float, float)
If provided, extract spectra relative to the (tilted) spectral trace
- show_contambool
Include curves for contamination model
- add_labelbool
Add a label to the plot.
- beam_modelslist
List of 2D arrays with the model for each beam. If not None, then use these models instead of computing them from the beam objects.
- median_filter_kwargsdict
Parameters passed to
safe_nanmedian_filter
to filter the 2D models before extracting the 1D spectrum.
- Returns
- fig
Figure
Figure object
- fig
- optimal_extract(data=None, bin=1, wave=None, ivar=None, trace_limits=None, loglam=True, **kwargs)[source]¶
Binned optimal extractions by grism with algorithm from Horne 1984
The spatial profile for each beam is the 2D model spectrum generated using its attached direct image thumbnail. The Horne (1984) algorithm is essentially a least-squares fit of the spatial model to the observed 2D spectrum, weighted by the uncertainties.
Along with the optimal extraction, this method also implements an option to extract an effective “aperture” within a specified region above and below the spectral trace.
While the traces may not be directly aligned with the
x
axis of the 2D spectra, both the optimal and trace extractions extract alongy
pixels at a givenx
.- Parameters
- data
ndarray
, None Data array with same dimensions as
self.scif_mask
(flattened & masked) 2D spectra of all beams. IfNone
, then useself.scif_mask
.- binbool
Binning factor relative to the grism-dependent resolution values, specified in
GRISM_LIMITS
.- wave
ndarray
, None Wavelength bin edges. If
None
, then compute from parameters inGRISM_LIMITS
.- ivar
ndarray
, None Inverse variance array with same dimensions as
self.scif_mask
(flattened & masked) 2D spectra of all beams. IfNone
, then useself.weighted_sigma2_mask
.- trace_limits[float, float] or None
If specified, perform a simple sum in cross-dispersion axis between
trace_limits
relative to the central pixel of the trace rather than the optimally-weighted sum. Similarly, the output variances are the sum of the input variances in the trace interval.Note that the trace interval is evaluated with
< >
, as opposed to<= >=
, as the center of the trace is a float rather than an integer pixel index.- loglambool
If True and
wave
not specified (see above), then output wavelength grid is log-spaced.
- data
- Returns
- tabdict
Dictionary of
Table
spectra for each available grism.
- scale_to_photometry(tfit=None, tol=0.0001, order=0, init=None, fit_background=True, Rspline=50, use_fit=True, **kwargs)[source]¶
Compute scale factor between spectra and photometry
- Parameters
- tfitdict
Template fit info at a specific redshift from
template_at_z
. If not specified, then makes and fits spline templates- tolfloat
Fit tolerance passed to the minimizer
- orderint
Order of the polynomial scaling to fit
- initNone
Initial parameters
- fit_backgroundbool
Include additive background
- Rsplinefloat
Spectral resolution
R
of spline templates- use_splinebool
Use spline templates
- Returns
- resobject
Result from
scipy.optimize.least_squares
. The coefficients of the linear scaling are inres.x
.
- set_photometry(flam=[], eflam=[], filters=[], ext_corr=1, force=False, tempfilt=None, min_err=0.02, TEF=None, pz=None, source='unknown', **kwargs)[source]¶
Set photometry attributes
- Parameters
- flam, eflamarray-like
Flux densities and uncertainties in f-lambda cgs units
- filterslist
List of
FilterDefinition
objects- ext_corrfloat or array-like
MW extinction correction
- lcarray-like
Precomputed filter central wavelengths. Will automatically be computed from
filters
if not specified- forcebool
Don’t try to set if already specified (
Nphot
> 0)- tempfilt
eazy.photoz.TemplateGrid
Precomputed grid of templates integrated through the
filters
bandpasses- min_errfloat
minimum or systematic error to add in quadrature to
eflam
- TEF
eazy.templates.TemplateError
Template error function
- pzNone, (array, array)
Precomputed (z, pz) pdf from, e.g.,
eazy
(not used)- sourcestr
String to indicate the provenance of the photometry
- Returns
- photom_flamarray_like
Flux densities from
flam
- photom_eflamarray-like
Uncertainties including
min_err
- photom_filterslist
filters
- Nphotint
Number of photometry bandpasses
- The returned parameters above are not returned but are rather
- attributes that are set. This function also updates the
sivarf
,weightf
,fit_mask
attributes to include the- spectra + photometry
- template_at_z(z=0, templates=None, fwhm=1400, get_uncertainties=2, draws=0, **kwargs)[source]¶
Get the best-fit template at a specified redshift
- Parameters
- zfloat
Redshift
- templatesdict
Dictionary of
SpectrumTemplate
objects- fwhmfloat
FWHM of line templates if
templates
generated in-place- get_uncertaintiesint
Get coefficient uncertainties from covariance matrix. See
xfit_at_z
.- drawsint
Number of random draws from covariance matrix
- kwargsdict
Any additional keywords are passed to
xfit_at_z
- Returns
- tfitdict
Dictionary of fit results, used in various other places like
oned_figure
, etc.Keyword
Description
cfit
Dict of template normalizations and uncertainties
cont1d
SpectrumTemplate
of best-fit continuumline1d
SpectrumTemplate
of best-fit continuum + emission linecoeffs
Array of fit coefficients
chi2
(float) chi-squared of the fit
z
(float) The input redshift
templates
Copy of the input
templates
dictionaryline1d_err
If
draws > 0
, this will be template draws with the same dimension asline1d
- xfit_at_z(z=0, templates=[], fitter='nnls', fit_background=True, get_uncertainties=False, get_design_matrix=False, pscale=None, COEFF_SCALE=1e-19, get_components=False, huber_delta=4, get_residuals=False, include_photometry=True, use_cached_templates=False, bounded_kwargs={'method': 'bvls', 'tol': 1e-08, 'verbose': 0}, apply_sensitivity=True, median_filter_kwargs=None)[source]¶
Fit the 2D spectra with a set of templates at a specified redshift.
- Parameters
- zfloat
Redshift.
- templateslist
List of templates to fit.
- fitterstr
Minimization algorithm to compute template coefficients.
Available options are:
‘nnls’, Non-negative least squares (
scipy.optimize.nnls
)‘lstsq’, Standard least squares (
numpy.linalg.lstsq
)‘bounded’, Bounded least squares (
scipy.optimize.lsq_linear
)
For the last option, the line flux limits are set by the limits in the global
grizli.fitting.LINE_BOUNDS
list andbounded_kwargs
are passed tolsq_linear
.- fit_backgroundbool
Fit additive pedestal background offset.
- get_uncertaintiesbool, int
Compute coefficient uncertainties from the covariance matrix. If specified as an int > 1, then the covariance matrix is computed only for templates with non-zero coefficients
- get_design_matrixbool
Return design matrix and data, rather than nominal outputs.
- pscalefloat, array-like
Scale parameters passed to
compute_scale_array
. If None, then try to use fromself.pscale
.- COEFF_SCALEfloat
Scale factor for the template coefficients. The coefficients are stored as
coeffs * COEFF_SCALE
to improve numerical precision.- get_componentsbool
Return the individual components of the fit.
- huber_deltafloat
Use the Huber loss function (
scipy.special.huber
) rather than direct chi-squared. Ifhuber_delta < 0
, then fall back to chi-squared.- get_residualsbool
Return the residuals of the fit.
- include_photometrybool
Include photometry in the fit.
- use_cached_templatesbool
Use cached template arrays for speedup.
- bounded_kwargsdict
Keyword arguments passed to
lsq_linear
.- apply_sensitivitybool
Apply the sensitivity function to the templates.
- median_filter_kwargsdict
Keyword arguments passed to
safe_nanmedian_filter
.
- Returns
- AxTnumpy.ndarray
Transposed design matrix, weighted by 1/sigma. (If
get_design_matrix == True
).- datanumpy.ndarray
Masked data array, including background pedestal. (If
get_design_matrix == True
)- modelnumpy.ndarray
Full model array, including the contributions from all templates. (If
get_components == True
)- backgroundnumpy.ndarray
Background array computed from the fit. (If
get_components == True
)- chi2float
Chi-squared of the fit, computed using the Huber loss function if
huber_delta
> 0.- coeffsnumpy.ndarray
Template coefficients from the fit.
- coeffs_errnumpy.ndarray
Uncertainties of the template coefficients.
- covariancenumpy.ndarray
Full covariance matrix of the fit parameters.
- xfit_redshift(prior=None, templates={}, fwhm=1200, line_complexes=True, fsps_templates=False, zr=[0.65, 1.6], dz=[0.005, 0.0004], zoom=True, verbose=True, fit_background=True, fitter='nnls', bounded_kwargs={'method': 'bvls', 'tol': 1e-08, 'verbose': 0}, delta_chi2_threshold=0.004, poly_order=3, use_cached_templates=True, get_uncertainties=True, Rspline=30, huber_delta=4, get_student_logpdf=False)[source]¶
Two-step procedure for fitting redshifts
polynomial, spline template fits
redshift grids
…
- Parameters
- priorNone, (array, array)
Redshift prior (z, pz). Will be interpolated to the redshift fit grid
- templatesdict
Dictionary the
SpectrumTemplate
objects to use for the fits- fwhm, line_complexes, fsps_templatesfloat, bool, bool
Parameters passed to
load_templates
iftemplates
is empty.- zr(float, float)
Redshift limits of the logarithmic (1+z) redshift grid
- dz(float, float)
Step size of the grid. The second value will be used to “zoom in” on the peaks found in the coarse grid step from the first value.
- zoombool
Do the second pass with the
dz[1]
step size- verbosebool
Some verbosity control
- fit_backgroundbool
Include contribution of additive background
- fitter, bounded_kwargsstr, dict
Least-squares optimization method. See
xfit_at_z
- delta_chi2_thresholdfloat
Not used
- poly_orderint
Order of polynomials for the “uninformative” polynomial fit. The parameters of the polynomial and full template fits are computed to evaluate the extent to which the galaxy / stellar templates improve the fit
- use_cached_templatesbool
Try to used cached versions of dispersed template models for templates that don’t depend on redshift (polynomials, splines)
- get_uncertaintiesbool
Get template fit coefficient uncertainties from the fit covariance matrix
- Rsplinefloat
Spectral resolution,
R
, of spline templates for another “uninformative” fit.- huber_deltafloat
Parameter for Huber loss function (see
xfit_at_z
)- get_student_logpdfbool
Get logpdf for likelihood assuming Student-t distribution rather than standard normal assumption
- Returns
- fit
Table
Table with fit information on the redshift grid and metadata on some fit characteristics.
- Table metadata
Meta
Description
N
Number of spectrum extensions / beams
polyord
Order of the polynomial fit
chi2poly
\(\chi^2\) of the polynomial fit
chi2spl
\(\chi^2\) of the spline fit
Rspline
Spectral resolution of the spline templates
kspl
Effective number of parameters of spline fit
huberdel
huber_delta
splf[i]
Flux of spline fit at fixed wavelengths
sple[i]
Unc. of spline fit at fixed wavelengths
NTEMP
Number of
templates
DoF
Degrees of freedom of the fit (total number of unmasked pixels in all 2D beams)
ktempl
N parameters of the template fit
chimin
Minimum \(\chi^2\) of the template fit
chimax
Maximum \(\chi^2\) of the template fit
fitter
Least squares method
as_epsf
Fit was done as
EffectivePSF
bic_poly
Bayesian Information Criterion (BIC) of the polynomial fit.
BIC = log(DoF)*k + min(chi2) + C
bic_spl
BIC of the spline fit
bic_temp
BIC of the template (redshift) fit
st_df
Student-
t
df of spline fitst_loc
Student-
t
loc of spline fitst_scl
Student-
t
scale of spline fitZ02
Integrated
cdf(<z) = 0.025
Z16
Integrated
cdf(<z) = 0.16
Z50
Integrated
cdf(<z) = 0.50
Z84
Integrated
cdf(<z) = 0.84
Z97
Integrated
cdf(<z) = 0.975
ZWIDTH1
Z84 - Z16
ZWIDTH2
Z97 - Z02
z_map
Redshift at
Max(PDF)
zrmin
Start of the redshift grid
zr
zrmax
End of the redshift grid
zr
z_risk
Redshift at minimum
risk
min_risk
Minimum
risk
gam_loss
gamma
parameter ofrisk
Column
Description
zgrid
Redshift grid
NZ
chi2
\(\chi^2(z)\)
NZ
student_logpdf
log PDF of student-t likelihood
NZ
coeffs
Template coefficients
(NZ,NTEMP)
covar
Template covariance
(NZ,NTEMP,NTEMP)
pdf
Full likelihood including optional
prior
risk
“Risk” parameter from Tanaka et al. (arXiv/1704.05988)
- fit
- xfit_star(tstar=None, spline_correction=True, fitter='nnls', fit_background=True, spline_args={'Rspline': 5}, oned_args={})[source]¶
Fit stellar templates
- Parameters
- tstardict
Dictionary of stellar
SpectrumTemplate
objects- spline_correctionbool
Include spline scaling correction for template mismatch
- fitterstr
Least-squares method passed to
template_at_z
.- fit_backgroundbool
Fit for additive background component
- spline_argsdict
Parameters passed to
split_spline_template
for generating the spline correction arrays- oned_argsdict
Keywords passed to
oned_figure
- Returns
- fig
Figure
Figure object
- linestr
Line of text describing the best fit
- tfitdict
Fit information from
template_at_z
- fig
- xmake_fit_plot(fit, tfit, show_beams=True, bin=1, minor=0.1, scale_on_stacked_1d=True, loglam_1d=True, zspec=None)[source]¶
Make a diagnostic plot of the redshift fit
- Parameters
- fit
Table
Redshift fit results from
xfit_redshift
- tfitdict
Template fit at best redshift from
template_at_z
- show_beamsbool
Show 1D spectra of all individual “beams”
- binfloat
Binning factor relative to nominal wavelength resolution (1 pix) of each grism
- minorfloat
Minor axis ticks, microns
- scale_on_stacked_1dbool
Set y limits based on stacked spectrum
- loglam_1dbool
Show log wavelengths
- zspecfloat, None
Spectroscopic redshift that will be indicated on the figure
- fit
- Returns
- fig
Figure
Figure object
- fig