picca_continuum
Continuum fitting module.
- class PiccaContinuumFitter(args)[source]
Bases:
objectPicca continuum fitter class.
Fits spectra without coadding. Pipeline inverse variance preferably should be smoothed before fitting. Mean continuum and var_lss are smoothed using inverse weights and cubic spline to help numerical stability.
When fitting for var_lss, number of wavelength bins in the observed frame for variance fitting
nwbinsare calculated by demanding 120 A steps between bins as closely as possible byVarLSSFitter.Contruct an instance, then call
iterate()with local spectra.- Parameters:
args (argparse.Namespace) – Namespace. Wavelength values are taken to be the edges (not centers). See respective parsers for default values.
- meancont_interp
Fast cubic spline object for the mean continuum.
- Type:
- comm
MPI comm object to reduce, broadcast etc.
- Type:
MPI.COMM_WORLD
- meanflux_interp
Interpolator for mean flux. If fiducial is not set, this equals to 1.
- Type:
- flux_stacker
Stacks flux. Set up with 8 A wavelength bin size.
- Type:
- varlss_fitter
None if fiducials are set for var_lss.
- Type:
VarLSSFitter or None
- varlss_interp
Cubic spline for var_lss if fitting. Linear if from file.
- Type:
- eta_interp
Interpolator for eta. Returns one if fiducial var_lss is set.
- Type:
- model
Continuum model to use
- Type:
Derived(BaseContinuumModel)
- _get_fiducial_interp(fname, col2read)[source]
Return an interpolator for mean flux or var_lss.
FITS file must have a STATS extention, which must have LAMBDA, MEANFLUX and VAR_LSS columns. This is the same format as rawio output from picca, except VAR column in picca is the variance of flux not deltas. We break away from that convention by explicitly requiring variance on deltas in a new column. LAMBDA must be linearly and equally spaced. This function sets up
col2readas FastLinear1DInterp object.- Parameters:
- Return type:
- Raises:
QsonicException – If LAMBDA is not equally spaced or
col2readis not in the file.
- _project_normalize_meancont(new_meancont)[source]
Project out higher order Legendre polynomials from the new mean continuum since these are degenerate with the free fitting parameters. Returns a normalized mean continuum. Integrals are calculated using
CubicSplinewithln lambda_RFas x array.
- update_mean_cont(spectra_list)[source]
Update the global mean continuum and stacked flux.
Uses
forestivar_smin inverse variance, but must be set beforehand. Raw mean continuum estimates are smoothed with a weightedscipy.interpolate.UnivariateSpline. The mean continuum is removed from higher Legendre polynomials and normalized by the mean. This function updatesmeancont_interp.fp.
- update_var_lss_eta(spectra_list)[source]
Fit and update var_lss and eta if enabled. See
VarLSSFitterfor fitting details.
- _normalize_flux(spectra_list)[source]
Multiplies continuum estimates with stacked values in order to normalize flux in the observed grid to be 1 if
--normalize-stacked-fluxis passed.
- _eta_calibate_ivar(spectra_list)[source]
Divides forest ivar values by eta estimates if
--eta-calib-ivaris passed.
- iterate(spectra_list)[source]
Main function to fit continua and iterate.
Consists of three major steps: initializing, fitting, updating global variables. The initialization sets
cont_paramsvariable of every Spectrum object. Continuum polynomial order is carried by settingcont_params[x]. At each iteration:Global variables (mean continuum, var_lss) are saved to
attributes.fitsfile. This ensures the order of what is used in each iteration.All spectra are fit.
Mean continuum is updated by stacking, smoothing and removing degenarate modes. Check for convergence if update is small.
If fitting for var_lss, fit and update by calculating variance statistics.
- class VarLSSFitter(w1obs, w2obs, nwbins=None, var1=0.0001, var2=20.0, nvarbins=100, nsubsamples=None, use_cov=False, comm=None)[source]
Bases:
objectVariance fitter for the large-scale fluctuations.
Input wavelengths and variances are the bin edges, so centers will be shifted. Valid bins require at least 500 pixels from 50 quasars. Assumes no spectra has wave < w1obs or wave > w2obs.
Note
This class is designed to be used in a linear fashion. You create it, add statistics to it and finally fit. After
fit()is called,fit()andadd()cannot be called again. You mayreset()and start over.Usage:
... varfitter = VarLSSFitter( wave1, wave2, nwbins, var1, var2, nvarbins, nsubsamples=100, comm=comm) # Change static minimum numbers for valid statistics VarLSSFitter.min_num_pix = min_num_pix VarLSSFitter.min_num_qso = min_num_qso for delta in deltas_list: varfitter.add(delta.wave, delta.delta, delta.ivar) fit_results = np.ones((nwbins, 2)) fit_results[:, 0] = 0.1 fit_results, std_results = varfitter.fit(fit_results) varfitter.save("variance-file.fits") # You CANNOT call ``fit`` again!
- Parameters:
w1obs (float) – Lower observed wavelength edge.
w2obs (float) – Upper observed wavelength edge.
nwbins (int, default: None) – Number of wavelength bins. If none, automatically calculated to yield 120 A wavelength spacing.
var1 (float, default: 1e-4) – Lower variance edge.
var2 (float, default: 20) – Upper variance edge.
nvarbins (int, default: 100) – Number of variance bins.
nsubsamples (int, default: None) – Number of subsamples for the Jackknife covariance. Auto determined by number of bins (
nvarbins^2) if None.use_cov (bool, default: False) – Use the Jackknife covariance when fitting.
comm (MPI.COMM_WORLD or None, default: None) – MPI comm object to allreduce if enabled.
- minlength
Minimum size of the combined bin count array. It includes underflow and overflow bins for both wavelength and variance bins.
- Type:
- subsampler
Subsampler object that stores mean_delta in axis=0, var_delta in axis=1 , var2_delta in axis=2, mean bin variance in axis=3.
- Type:
- comm
MPI comm object to allreduce if enabled.
- Type:
MPI.COMM_WORLD or None, default: None
- static variance_function(var_pipe, var_lss, eta=1)[source]
Variance model to be fit.
\[\sigma^2_\mathrm{obs} = \eta \sigma^2_\mathrm{pipe} + \sigma^2_\mathrm{LSS}\]
- construct_interp(y0=1.0)[source]
Return a FastCubic1DInterp object that interpolates in observed wavelength bins.
- Parameters:
y0 (float, default: 1.0) – Initial constant value for the interpolator.
- Return type:
- add(wave, delta, ivar)[source]
Add statistics of a single spectrum. Updates delta and num arrays.
Assumes no spectra has
wave < w1obsorwave > w2obs.
- calculate_subsampler_stats()[source]
Calculates mean, variance and error on the variance.
It also calculates the delete-one Jackknife variance over
nsubsamples. The variance on var_delta is updated with the variance on the mean estimates, then it is regularized by calculated var2_delta (Gaussian estimates), where if Jackknife variance is smaller than the Gaussian estimate, it is replaced by the Gaussian estimate.Covariance is calculated if
use_covis set to True in init. Covariance of mean_delta^2 is propagated using the formula inqsonic.mathtools.block_covariance_of_square().
- fit(initial_guess, smooth=True)[source]
Syncronize all MPI processes and fit for
var_lssandeta.Second axis always contains
etavalues. Example:var_lss = initial_guess[:, 0] eta = initial_guess[:, 1]
This implemented using
scipy.optimize.curve_fit()withe_var_deltaorcov_var_deltaas absolute errors. Domain is bounded to(0, 2). These fits are then smoothed viascipy.interpolate.UnivariateSplineusing weights fromcurve_fit, while missing values or failed wavelength bins are extrapolated.- Parameters:
- Returns:
fit_results (
ndarray) – Smoothed fit results at observed wavelengths where missing values are extrapolated. 1D array containing LSS variance ifinitial_guessis 1D. 2D containing eta values on the second axis ifinitial_guessis 2D ndarray.std_results (
ndarray) – Error onvar_lssfrom sqrt ofcurve_fitoutput. Same behavior asfit_results.
- write(mpi_saver, min_snr=0, max_snr=100)[source]
Write variance statistics to FITS file in ‘VAR_STATS’ extension.
- property e_var_delta
ndarray: Error on variance delta using delete-one Jackknife. The error ofmean_deltais propagated, then regularized usingvar2_delta. See source code ofcalculate_subsampler_stats().
- property cov_var_delta
ndarray: 3D array of shape(nwbins, nvarbins, nvarbins)for the covariance on variance delta using delete-one Jackknife. The covariance ofmean_deltais propagated. Seeqsonic.mathtools.block_covariance_of_square()for details. None ifuse_covis False.
- class FluxStacker(w1obs, w2obs, dwobs, waverf, dwrf, comm=None)[source]
Bases:
objectThe class to stack flux values to obtain IGM mean flux and other problems.
This object can be called. Stacked flux is initialized to one. Reset before adding statistics.
- Parameters:
w1obs (float) – Lower observed wavelength edge.
w2obs (float) – Upper observed wavelength edge.
dwobs (float) – Wavelength spacing.
waverf (
ndarray) – Rest-frame wavelength bins.dwrf (float) – Wavelength spacing in the rest frame.
comm (MPI.COMM_WORLD or None, default: None) – MPI comm object to allreduce if enabled.
- _interp
Interpolator. Saves stacked_flux in fp and weights in ep.
- Type:
- _interp_rf
Rest-frame interpolator. Saves stacked_flux in fp and weights in ep.
- Type:
- comm
MPI comm object to allreduce if enabled.
- Type:
MPI.COMM_WORLD or None, default: None
- add(wave, wave_rf, weighted_flux, weighted_cont, weight)[source]
Add statistics of a single spectrum.
Updates
stacked_fluxandweights. Assumes no spectra haswave < w1obsorwave > w2obs.
- reset()[source]
Reset
stacked_fluxandweightsarrays to zero.
Arguments
- add_picca_continuum_parser(parser=None)[source]
Adds PiccaContinuumFitter related arguments to parser. These arguments are grouped under ‘Continuum fitting options’. All of them come with defaults, none are required.
- Parameters:
parser (argparse.ArgumentParser, default: None) –
- Returns:
parser
- Return type:
usage: foo [-h] [--num-iterations NUM_ITERATIONS]
[--continuum-model {picca,true,input}] [--true-continuum]
[--input-continuum-dir INPUT_CONTINUUM_DIR]
[--fiducial-meanflux FIDUCIAL_MEANFLUX]
[--fiducial-varlss FIDUCIAL_VARLSS] [--cont-order CONT_ORDER]
[--var-fit-eta] [--var-use-cov] [--normalize-stacked-flux]
[--eta-calib-ivar] [--rfdwave RFDWAVE]
[--minimizer {iminuit,l_bfgs_b}]
Continuum fitting options
- --num-iterations
Number of iterations for continuum fitting.
Default: 10
- --continuum-model
Possible choices: picca, true, input
Continuum model. picca fits the continuum in the forest region. true is the true continuum analysis for mock analysis. input in the input continuum analysis for all.
Default: “picca”
- --true-continuum
Alternative argument for true continuum analysis.
Default: False
- --input-continuum-dir
Input continuum directory.
- --fiducial-meanflux
Fiducial mean flux FITS file.
- --fiducial-varlss
Fiducial var_lss FITS file.
- --cont-order
Order of continuum fitting polynomial.
Default: 1
- --var-fit-eta
Fit for noise calibration (eta).
Default: False
- --var-use-cov
Use covariance in varlss-eta fitting.
Default: False
- --normalize-stacked-flux
Force stacked flux to be one at the end.Note this does not change STACKED_FLUX in attributes. It only updates CONT of each delta.
Default: False
- --eta-calib-ivar
Calibrate IVAR with eta estimates.
Default: False
- --rfdwave
Rest-frame wave steps. Complies with forest limits
Default: 0.8
- --minimizer
Possible choices: iminuit, l_bfgs_b
Minimizer to fit the continuum.
Default: “iminuit”