spectrum

generate_spectra_list_from_data(cat_by_survey, data)[source]
valid_spectra(spectra_list)[source]

Generator for continuum valid spectra.

class Spectrum(catrow, wave, flux, ivar, mask, reso, idx)[source]

Bases: object

An object to represent one spectrum.

Parameters:
  • catrow (ndarray) – Catalog row.

  • wave (dict(ndarray)) – Dictionary of arrays specifying the wavelength grid. Static variable!

  • flux (dict(ndarray)) – Dictionary of arrays specifying the flux.

  • ivar (dict(ndarray)) – Dictionary of arrays specifying the inverse variance.

  • mask (dict(ndarray)) – Dictionary of arrays specifying the bitmask. Not stored

  • reso (dict(ndarray)) – Dictionary of 2D arrays specifying the resolution matrix.

  • idx (int) – Index to access in flux, ivar, mask and reso that corresponds to the quasar in catrow.

rsnr

Average SNR above Lya. Calculated in set_forest_region().

Type:

float

mean_snr

Mean signal-to-noise ratio in the forest.

Type:

dict(float)

_f1, _f2

Forest indices. Set up using set_forest_region() method. Then use property functions to access forest wave, flux, ivar instead.

Type:

dict(int)

cont_params

Continuum parameters. Initial estimates are constructed.

Type:

dict

WAVE_LYA_A = 1215.67

Lya wavelength in A.

Type:

float

_wave = None

Common wavelength grid for all Spectra.

Type:

dict(ndarray)

_coadd_wave = None

Common coadded wavelength grid for all Spectra.

Type:

dict(ndarray)

_dwave = None

Wavelength spacing.

Type:

float

_blinding = None

Blinding. Must be set for certain data.

Type:

str or None

_fits_colnames = ['LAMBDA', 'DELTA', 'IVAR', 'WEIGHT', 'CONT']

Column names to save in delta files.

Type:

list(str)

static _set_wave(wave, check_consistency=False)[source]

Set the common wavelength grid.

Parameters:

check_consistency (bool) – Asserts each time key and values are the same if True.

static _set_coadd_wave()[source]
static set_blinding(maxlastnight, args)[source]

Set the blinding strategy.

‘LASTNIGHT’ column, args.mock_analysis, args.forest_w1 decide the blinding strategy. Mock, side band and SV analyses are not blinded.

Parameters:
  • maxlastnight (int) – Maximum LASTNIGHT column of the entire quasar catalog.

  • args (argparse.Namespace) – Should have mock_analysis (bool) and forest_w1 (floar).

static blinding_not_set()[source]

bool: True if blinding is not set.

classmethod from_dictionary(catrow, data, idx)[source]

Create a Spectrum from dictionary. See Spectrum for argument details.

If cont key is present in data, cont_params dictionary gains the following:

cont_params['true_data_w1'] (float): First wavelength
cont_params['true_data_dwave'] (float): Wavelength spacing
cont_params['true_data'] (ndarray): True continuum
Return type:

Spectrum

_set_rsnr()[source]

Calculates and sets SNR above Lya.

Calculates the mean SNR in the forest region and an initial guess for the continuum amplitude.

slice(arm, i1, i2)[source]
set_forest_region(w1, w2, lya1, lya2)[source]

Sets slices for the forest region. Also calculates the mean SNR in the forest and an initial guess for the continuum amplitude. This is currently turned off, but it can mask outliers in each arm separately based on moving median statistics (see qsonic.mathtools.get_median_outlier_mask()).

Parameters:
  • w1 (float) – Observed wavelength range

  • w2 (float) – Observed wavelength range

  • lya1 (float) – Rest-frame wavelength for the forest

  • lya2 (float) – Rest-frame wavelength for the forest

drop_arm(arm)[source]
drop_short_arms(lya1=0, lya2=0, skip_ratio=0)[source]

Arms that have less than skip_ratio pixels are removed from forest dictionary.

Parameters:
  • lya1 (float) – Rest-frame wavelength for the forest

  • lya2 (float) – Rest-frame wavelength for the forest

  • skip_ratio (float) – Remove arms if they have less than this ratio of pixels

remove_nonforest_pixels()[source]

Remove non-forest pixels from storage.

This sets flux, ivar and reso to empty dictionary, but wave is not modified, since it is a static variable. Good practive is to loop using, e.g., for arm, wave_arm in self.forestwave.items():.

get_real_size()[source]
Returns:

int

Return type:

Sum of number of pixels with forestivar > 0 for all arms.

get_effective_meansnr()[source]

Calculate a weighted average of mean_snr over arms. Only call if set_forest_region() has been called.

\[\langle\mathrm{SNR}\rangle = \sum_{i} \mathrm{SNR}_i^3 \bigg/ {\sum_{i} \mathrm{SNR}_i^2}\]
Returns:

float

Return type:

Effective mean SNR.

is_long(dforest_wave, skip_ratio)[source]

Determine if spectrum is long enough to be accepted.

The condition is get_real_size() > skip_ratio * npixels, where npixels \(=(1 + z_\mathrm{qso}) \times\) dforest_wave \(/ \mathrm{d}\lambda\) and \(\mathrm{d}\lambda\) is wavelength spacing in the observed frame in A.

Parameters:
  • dforest_wave (float) – Length of the forest in the rest-frame in A.

  • skip_ratio (float) – Minimum ratio that needs to be present and unmasked to keep the spectrum.

Return type:

bool

set_smooth_forestivar(smoothing_size=16.0)[source]

Set forestivar_sm to smoothed inverse variance. Before this call forestivar_sm points to forestivar. If smoothing_size <= 0, smoothing is undone such that ivar_sm points to ivar. Also, forestweight points to this.

smoothing_size is saved to a private _smoothing_scale variable for future use.

Parameters:

smoothing_size (float, default: 16) – Gaussian smoothing spread in A.

set_forest_weight(varlss_interp=<function _zero_function>, eta_interp=<function _one_function>)[source]

Sets forestweight for a given var_lss and eta correction. Always uses forestivar_sm, which is not actually smoothed if set_smooth_forestivar() is not called.

\[w = i / (\eta + i \sigma^2_\mathrm{LSS} C^2),\]

where i is IVAR and C is the continuum.

Parameters:
  • varlss_interp (Callable[[ndarray], ndarray], default: 0) – LSS variance interpolator.

  • eta_interp (Callable[[ndarray], ndarray], default: 1) – eta interpolator.

calc_continuum_chi2()[source]

Calculate the chi2 of the continuum fitting. This is just a sum of weight * (flux - cont)^2.

Returns:

chi2

Return type:

float

simple_coadd()[source]

Coadding without continuum and var_lss terms on the full spectrum. Weights, forests etc. will not be set. Replaces wave, flux, ivar and reso attributes with dictionaries that has a single arm brz as key to access the coadded data.

We first set the static coadded wavelength grid if it is not set. Then we add each arm using ivar (not smoothed). If reso is set, we coadd the resolution matrix using the same inverse variance weights. If these weights are zero for both arms, resolution matrix coadding reverts to equal weights. Final private assignments are done at the end to keep using arm by arm values of ivar.

coadd_arms_forest(varlss_interp=<function _zero_function>, eta_interp=<function _one_function>)[source]

Coadds different arms using forestweight. Interpolators are needed to reset forestweight.

Replaces forest variables and cont_params['cont'] with a dictionary that has a single arm brz as key to access coadded data.

Parameters:
  • varlss_interp (Callable[[ndarray], ndarray], default: 0) – LSS variance interpolator or function.

  • eta_interp (Callable[[ndarray], ndarray], default: 1) – eta interpolator or function.

mean_resolution(arm, weight=None)[source]

Returns the weighted mean Gaussian sigma of the spectrograph resolution in the forest.

Parameters:
  • arm (str) – Arm.

  • weight (None or ndarray, default: None) – Weights. If None, forestweight is used.

Returns:

mean_reso – Gaussian sigma. None if forestreso is not set.

Return type:

float or None

write(fts_file)[source]

Writes each arm to FITS file separately.

Writes ‘LAMBDA’, ‘DELTA’, ‘IVAR’, ‘WEIGHT’, ‘CONT’ columns and ‘RESOMAT’ column if resolution matrix is present to extension name targetid-arm. FITS file must be initialized before. Each arm has its own MEANSNR.

Parameters:

fts_file (FITS file) – The file handler, not filename.

property z_qso

Quasar redshift.

Type:

float

property targetid

Unique TARGETID identifier.

Type:

int

property hpix

Healpix.

Type:

int

property ra

Right ascension.

Type:

float

property dec

Declination

Type:

float

property wave

Original wavelength grid in A.

Type:

dict(ndarray)

property dwave

Wavelength step size in A.

Type:

float

property forestwave

Forest wavelength field in A.

Type:

dict(ndarray)

property forestflux

Forest flux field.

Type:

dict(ndarray)

property forestivar

Forest inverse variance field.

Type:

dict(ndarray)

property forestivar_sm

Forest smoothed inverse variance field.

Initially equal to forestivar. Smoothed if set_smooth_forestivar() is called.

Type:

dict(ndarray)

property forestweight

Forest weight field. Initially equal to forestivar.

Type:

dict(ndarray)

property forestreso

Resolution matrix in the forest.

Type:

dict(ndarray)

class Delta(hdu)[source]

Bases: object

An object to read one delta from HDU.

Parameters:

hdu (fitsio.TableHDU) – Table containing delta data.

Raises:
  • RuntimeError – If hdu doesn’t have neither “LAMBDA” nor “LOGLAM” columns.

  • RuntimeError – If hdu doesn’t have neither “DELTA” nor “DELTA_BLIND” columns.

  • RuntimeError – If hdu doesn’t have none of “MOCKID”, “TARGETID” and “THING_ID” header keys.

wave

Wavelength array in A.

Type:

ndarray

delta

Deltas.

Type:

ndarray

ivar

Inverse variance.

Type:

ndarray

weight

Weights, which includes var_lss.

Type:

ndarray

cont

Continuum.

Type:

ndarray

reso

Resolution matrix. None if not present.

Type:

ndarray

header

Header.

Type:

FITS header

targetid

TARGETID, MOCKID or THING_ID from header.

Type:

int

mean_snr

MEANSNR from header.

Type:

float

_accepted_wave_columns = {'LAMBDA', 'LOGLAM'}

Supported column names for wavelength.

Type:

set

_accepted_delta_columns = {'DELTA', 'DELTA_BLIND'}

Supported column names for delta.

Type:

set

_accepted_targetid_keys = {'MOCKID', 'TARGETID', 'THING_ID'}

Supported header keys for unique ID.

Type:

set

_accepted_colums_map = {'delta': {'DELTA', 'DELTA_BLIND'}, 'wave': {'LAMBDA', 'LOGLAM'}}
static _check_hdu(colnames, attr)[source]
write(fts_file)[source]

Writes to FITS file. This function is aimed at saving coadded deltas.

Writes ‘LAMBDA’, ‘DELTA’, ‘IVAR’, ‘WEIGHT’, ‘CONT’ columns and ‘RESOMAT’ column if resolution matrix is present to extension name targetid. FITS file must be initialized before. Note that arm is lost in the extension name.

Parameters:

fts_file (FITS file) – The file handler, not filename.

_coadd_reso(other, nwaves, idxes)[source]
coadd(other, dwave=0.8)[source]
property ra

Right ascension in degrees.

Type:

float

property dec

Declination in degrees

Type:

float

Arguments

add_wave_region_parser(parser=None)[source]

Adds wavelength analysis related arguments to parser. These arguments are grouped under ‘Wavelength analysis region’. All of them come with defaults, none are required.

Parameters:

parser (argparse.ArgumentParser, default: None) –

Returns:

parser

Return type:

argparse.ArgumentParser

usage: foo [-h] [--wave1 WAVE1] [--wave2 WAVE2] [--forest-w1 FOREST_W1]
           [--forest-w2 FOREST_W2]

Wavelength analysis region

--wave1

First observed wavelength edge.

Default: 3600.0

--wave2

Last observed wavelength edge.

Default: 6000.0

--forest-w1

First forest wavelength edge.

Default: 1050.0

--forest-w2

Last forest wavelength edge.

Default: 1180.0