spec_analysis.spectra¶
The spectra
module provides object representations of spectra and is
responsible for processing spectra (e.g. extinction correction) and sampling
any spectral features.
Note
Where applicable, this module uses the Schlegel et al. 1998 dust map and Fitzpatrick et al. 1999 extinction law.
Usage Examples¶
Spectrum
objects are used to represent individual spectra and provide
functionality for correcting MW extinction, rest-framing, and binning the
spectra (in that specific order!). A handful of examples demonstrating this
functionality is provided below:
Working with a Single Spectrum¶
First we define a demo spectrum with dummy values for wavelength, flux, redshift, right ascension (RA) and declination (Dec). All values are defined in the observer frame.
1 2 3 4 5 6 7 8 9 10 11 12 | import numpy as np from spec_analysis import simulate, spectra # Define feature using rest and observer frame average wavelength wave = np.arange(4000, 5000) lambda_rest = np.mean(wave) lambda_observed = lambda_rest - 10 flux, eflux = simulate.gaussian(wave, mean=lambda_observed, stddev=100) # Prepare a demo spectrum object cls.spectrum = Spectrum(wave, flux, ra=0, dec=-1, z=0) |
To correct for MW extinction and bin the spectrum, use the prepare_spectrum
method. Several filters are available for binning the spectrum. Here we use a
median filter
with a window size of 10
:
1 2 3 4 5 | spectrum.prepare_spectrum(bin_size=10, method='median') rest_wave = spectrum.rest_wave # Rest-framed wavelengths rest_flux = spectrum.rest_flux # Extinction corrected flux bin_flux = spectrum.bin_flux # Filtered, extinction corrected flux |
Once the spectrum is prepared, you can measure it’s properties for a given feature. This requires knowing the start / end wavelength of the feature in the current spectrum, and the feature’s rest frame wavelength.
1 2 3 | feat_start = spectrum.rest_wave[10] feat_end = spectrum.rest_wave[-10] spectrum.sample_feature_properties(feat_start, feat_end, lambda_rest) |
Custom Callbacks¶
If you want to inject your own custom calculations to the sampling process,
this can be added by specifying the callback
argument.
1 2 3 4 5 6 7 8 9 10 11 | def my_callback(feature): '''This function will be called for every iteration / sample Args: feature (ObservedFeature): The sampled flux as a feature object ''' pass spectrum.sample_feature_properties( feat_start, feat_end, rest_frame, callback=my_callback) |
Iterating over a Data Release¶
The SpectraIterator
class is used to iterate over spectra from data releases
provided by the sndata
package. Various arguments can be specified to
account for variations in data formatting between different surveys. As an
example, we consider the SDSS survey, which is accessed as follows:
1 2 3 4 5 6 7 8 9 10 | from sndata.sdss import Sako18Spec from spec_analysis.spectra import SpectraIterator # Make sure data is downloaded to your local machine data_release = Sako18Spec() data_release.download_module_data() # Just so we can see the SDSS data model demo_table = next(data_release.iter_data()) print(demo_table) |
The data tables from SDSS include multiple spectra, including host galaxy
spectra and multiple observations of the supernova. We use a pre-processing
function to remove the galaxy spectra, and distinguish between different
spectra using their observation time (the time
column in the data tables)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # Function called to process data tables before plotting / analysis def pre_process(table): """Pre-process spectral data Args: table: An astropy table with unprocessed data Returns: An astropy table with processed data """ # Remove galaxy spectra from data tables # (and any other spectral data you don't want to consider) return table[table['type'] != 'Gal'] data_iter = SpectraIterator(data_release, pre_process=pre_process, group_by='time') |
If we only wanted to consider data for a subset of targets, we can specify those targets using their object Id.
1 2 3 | obj_ids = ['722', '739', '744', '762', '774'] data_iter = SpectraIterator( data_release, obj_ids=obj_ids, pre_process=pre_process, group_by='time') |
Note
The pre_process
function is called before spectra are grouped
using the group_by
keyword. This allows you to add a custom group_by
value to the data tables if necessary.
The SpectraIterator
class expects data tables to have the ra
, dec
,
ad z
keys specified in their meta data. If those values are not available,
they can be added in the pre-processing function.
API Documentation¶
-
class
spec_analysis.spectra.
Spectrum
(wave, flux, ra, dec, z, **kwargs)[source]¶ Object representation of an observed spectrum
-
__init__
(wave, flux, ra, dec, z, **kwargs)[source]¶ Measures pEW and calc_area of spectral features
Parameters: - wave (ndarray) – Observer frame wavelength
- flux (ndarray) – Observer frame flux
- ra (float) – The target’s right ascension
- dec (float) – The target’s declination
- z (float) – The target’s redshift
- kwargs to set as instance attributes (Additional) –
-
prepare_spectrum
(rv=3.1, bin_size=5, bin_method='median')[source]¶ Correct for extinction, rest-frame, and bin the spectrum
Parameters: - bin_size (float) – Bin size in units of Angstroms
- bin_method (str) – Either ‘median’, ‘average’, ‘sum’, or ‘gauss’
- rv (float) – Rv value to use for extinction (Default: 3.1)
-
sample_feature_properties
(feat_start, feat_end, rest_frame, nstep=0, callback=None)[source]¶ Calculate the properties of a single feature in a spectrum
Velocity values are returned in km / s. Error values are determined both formally (summed in quadrature) and by re-sampling the feature boundaries
nstep
flux measurements in either direction.Parameters: - feat_start (float) – Starting wavelength of the feature
- feat_end (float) – Ending wavelength of the feature
- rest_frame (float) – Rest frame location of the specified feature
- nstep (int) – Number of samples taken in each direction
- callback (callable) – Call a function after every iteration. Function is passed the sampled feature.
Returns: - The line velocity
- The formal error in velocity
- The sampling error in velocity
- The equivalent width
- The formal error in equivalent width
- The sampling error in equivalent width
- The feature calc_area
- The formal error in calc_area
- The sampling error in calc_area
-
-
class
spec_analysis.spectra.
SpectraIterator
(data_release, obj_ids=None, pre_process=None, group_by='time')[source]¶ Iterator over individual spectra from an
sndata
data release-
__init__
(data_release, obj_ids=None, pre_process=None, group_by='time')[source]¶ An iterator over individual spectra in a data release
Instantiates an iterator over spectra in a
sndata
data release. Spectra are yielded individually asSpectrum
objects. Any meta data for a given spectrum is added as an attribute to the correspondingSpectrum
object. The time of the observation is also included as an attribute.Parameters: - data_release (SpectroscopicRelease) – An sndata style data release
- obj_ids (list) – Optionally only consider a subset of Id’s
- pre_process (Callable) – Function to prepare data before plotting
Yields: Spectrum
objects
-