WDmodel.io module¶
I/O methods. All the submodules of the WDmodel package use this module for almost all I/O operations.
-
WDmodel.io.
_read_ascii
(filename, **kwargs)[source]¶ Read ASCII files
Read space separated ASCII file, with column names provided on first line (leading
#
optional).kwargs
are passed along tonumpy.genfromtxt()
. Forces any string column data to be encoded in ASCII, rather than Unicode.Parameters: - filename (str) – Filename of the ASCII file. Column names must be provided on the first line.
- kwargs (dict) – Extra options, passed directly to
numpy.genfromtxt()
Returns: out – Record array with the data. Field names correspond to column names in the file.
Return type: See also
-
WDmodel.io.
copy_params
(params)[source]¶ Returns a deep copy of a dictionary. Necessary to ensure that dictionaries that nest dictionaries are properly updated.
Parameters: params (dict or Object) – Any python object for which a deepcopy needs to be created. Typically a parameter dictionary such as that from WDmodel.io.read_params()
Returns: params – A deepcopy of the object Return type: Object Notes
Simple wrapper around
copy.deepcopy()
-
WDmodel.io.
get_filepath
(infile)[source]¶ Returns the full path to a file. If the path is relative, it is converted to absolute. If this file does not exist, it is treated as a file within the
WDmodel
package. If that file does not exist, an error is raised.Parameters: infile (str) – The name of the file to set the full path for Returns: pkgfile – The path to the file Return type: str Raises: IOError
– If theinfile
could not be found at location or inside theWDmodel
package.
-
WDmodel.io.
get_options
(args, comm)[source]¶ Get command line options for the
WDmodel
fitter packageParameters: - args (array-like) – list of the input command line arguments, typically from
sys.argv
- comm (None or
mpi4py.mpi.MPI
instance) – Used to communicate options to all child processes if running with mpi
Returns: - args (Namespace) – Parsed command line options
- pool (None or :py:class`emcee.utils.MPIPool`) – If running with MPI, the pool object is used to distribute the computations among the child process
Raises: ValueError
– If any input value is invalid- args (array-like) – list of the input command line arguments, typically from
-
WDmodel.io.
get_outfile
(outdir, specfile, ext, check=False, redo=False, resume=False)[source]¶ Formats the output directory, spectrum filename, and an extension into an output filename.
Parameters: - outdir (str) – The output directory name for the output file
- specfile (str) – The spectrum filename
- ext (str) – The output file’s extension
- check (bool, optional) – If
True
, check if the output file exists - redo (bool, optional) – If
False
and the output file already exists, an error is raised - resume (bool, optional) – If
False
and the output file already exists, an error is raised
Returns: outfile – The output filename
Return type: Raises: IOError
– Ifcheck
isTrue
,redo
andresume
areFalse
, andoutfile
exists.Notes
We set the output file based on the spectrum name, since we can have multiple spectra per object.
If
outdir
is configured byset_objname_outdir_for_specfile()
forspecfile
, it’ll include the object name.See also
-
WDmodel.io.
get_params_from_argparse
(args)[source]¶ Converts an
argparse.Namespace
into an ordered parameter dictionary.Parameters: args ( argparse.Namespace
) – The parsed command-line options fromWDmodel.io.get_options()
Returns: params – The parameter dictionary Return type: collections.OrderedDict
Raises: RuntimeError
– If format ofargparse.Namespace
is invalid. or If parameter isfixed
butvalue
isNone
. or If parametervalue
is out ofbounds
.Notes
- Assumes that the argument parser options were names
<param>_value
: Value of the parameter (float orNone
)<param>_fix
: Bool specifying if the parameter<param>_bounds
: tuple with lower limit and upper limit
where <param> is one of
WDmodel.io._PARAMETER_NAMES
-
WDmodel.io.
get_phot_for_obj
(objname, filename)[source]¶ Gets the measured photometry for an object from a photometry lookup table.
Parameters: Returns: phot – The photometry of
objname
withdtype=[('pb', 'str'), ('mag', '<f8'), ('mag_err', '<f8')]
Return type: Raises: RuntimeError
– If there are no matches in the photometry lookup file or if there are multiple matches for an object in the photometry lookup fileValueError
– If the photometry or the photometry uncertainty values are not finite or if the photometry uncertainties are less<= 0
Notes
The lookup file must be readable by
read_phot()
The column name with the object name
objname
expected to beobj
If column names for magnitudes are named <passband>, the column names for errors in magnitudes in passband must be ‘d’+<passband_name>.
-
WDmodel.io.
get_pkgfile
(infile)[source]¶ Returns the full path to a file inside the
WDmodel
packageParameters: infile (str) – The name of the file to set the full package filename for Returns: pkgfile – The path to the file within the package. Return type: str Raises: IOError
– If thepkgfile
could not be found inside theWDmodel
package.Notes
This allows the package to be installed anywhere, and the code to still determine the location to a file included with the package, such as the model grid file.
-
WDmodel.io.
get_spectrum_resolution
(specfile, spectable, fwhm=None, lamshift=None)[source]¶ Gets the measured FWHM from a spectrum lookup table.
Parameters: - specfile (str) – The spectrum filename
- spectable (str) – The spectrum FWHM lookup table filename
- fwhm (None or float, optional) – If specified, this overrides the resolution provided in the lookup
table. If
None
lookups the resultion fromspectable
. - lamshift (None or float, optional) – If specified, this overrides the wavelength shift provided in the lookup
table. If
None
lookups the wavelength shift fromspectable
.
Returns: - fwhm (float) – The FWHM of the spectrum file. This is typically used as an initial
guess to the
WDmodel.fit
fitter routines. - lamshift (float) – The wavelength shift to apply additively to the spectrum. This is not a fit parameter, and is treated as an input
Raises: RuntimeWarning
– If thespectable
cannot be read, or thespecfile
name indicates that this is a test, or if there are no or multiple matches forspecfile
in thespectable
Notes
If the
specfile
is not found, it returns a default resolution of5
Angstroms, appropriate for the instruments used in our program.Note that there there’s some hackish internal name fixing since T. Matheson’s table spectrum names didn’t match the spectrum filenames.
-
WDmodel.io.
make_outdirs
(dirname, redo=False, resume=False)[source]¶ Makes output directories
Parameters: Returns: None – If the output directory
dirname
is successfully createdReturn type: Raises: Notes
If the options are parsed by
get_options()
then only one ofredo
orresume
can be set, as the options are mutually exclusive. Ifredo
is set, the fit is redone from scratch, whileresume
restarts the MCMC sampling from the last saved chain position.
-
WDmodel.io.
read_fit_inputs
(input_file)[source]¶ Read the fit input HDF5 file produced by
write_fit_inputs()
and returnnumpy.recarray
instances with the data.Parameters: input_file (str) – The HDF5 fit inputs filename
Returns: spec (
numpy.recarray
) – The spectrum withdtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8')]
cont_model (
numpy.recarray
) – The continuuum model. Has the same structure asspec
.linedata (
numpy.recarray
) – The observations of the spectrum corresponding to the hydrogen Balmer lines. Hasdtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8'), ('line_mask', 'i4')]
continuumdata (
numpy.recarray
) – Data used to generate the continuum model. Has the same structure asspec
.phot (None or
numpy.recarray
) –None
or the photometry withdtype=[('pb', 'str'), ('mag', '<f8'), ('mag_err', '<f8')]
fit_config (dict) –
- Dictionary with various keys needed to configure the fitter
rvmodel
:{'ccm89','od94','f99', 'custom'}
- Parametrization of the reddening law.covtype
:{'Matern32', 'SHO', 'Exp', 'White'}
- kernel type used to parametrize the covariancecoveps
: float - Matern32 kernel precisionphot_dispersion
: float - Excess dispersion to add in quadrature with photometric uncertaintiesscale_factor
: float - Flux scale factor
Raises: IOError
– If all the fit inputs could not be restored from the HDF5input_file
RuntimeWarning
– If theinput_file
includes aphot
group, but the data cannot be loaded.
See also
-
WDmodel.io.
read_full_model
(input_file)[source]¶ Read the full SED model from an output file.
Parameters: input_file (str) – Input HDF5 SED model filename
Returns: spec – Record array with the model SED. Has
dtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8')]
Return type: Raises: KeyError
– If any ofwave
,flux
orflux_err
is not found in the fileValueError
– If any value is not finite or ifflux
orflux_err
have any values<= 0
-
WDmodel.io.
read_mcmc
(input_file)[source]¶ Read the saved HDF5 Markov chain file and return samples, sample log probabilities and chain parameters
Parameters: input_file (str) – The HDF5 Markov chain filename Returns: - samples (array-like) – The model parameter sample chain
- samples_lnprob (array-like) – The log posterior corresponding to each of the
samples
- chain_params (dict) –
- The chain parameter dictionary
param_names
: list - list of model parameter namessamptype
:{'ensemble','pt','gibbs'}
- the sampler to usentemps
: int - the number of chain temperaturesnwalkers
: int - the number of Goodman & Ware walkersnprod
: int - the number of production steps of the chainndim
: int - the number of model parameters in the chainthin
: int - the chain thinning if anyeveryn
: int - the sparse of spectrum sampling step sizeascale
: float - the proposal scale for the sampler
Raises: IOError
– If a key in thefit_config
output is missing
-
WDmodel.io.
read_model_grid
(grid_file=None, grid_name=None)[source]¶ Read the Tlusty/Hubeny grid file
Parameters: Returns: - grid_file (str) – Filename of the HDF5 grid file
- grid_name (str) – Name of the group within the HDF5 grid file with the grid arrays
- wave (array-like) – The wavelength array of the grid with shape
(nwave,)
- ggrid (array-like) – The surface gravity array of the grid with shape
(ngrav,)
- tgrid (array-like) – The temperature array of the grid with shape
(ntemp,)
- flux (array-like) – The DA white dwarf model atmosphere flux array of the grid.
Has shape
(nwave, ngrav, ntemp)
Notes
There are no easy command line options to change this deliberately because changing the grid file essentially changes the entire model, and should not be done lightly, without careful comparison of the grids to quantify differences.
See also
-
WDmodel.io.
read_params
(param_file=None)[source]¶ Read a JSON file that configures the default guesses and bounds for the parameters, as well as if they should be fixed.
Parameters: param_file (str, optional) – The name of the input parameter file. If not the default file provided with the package, WDmodel_param_defaults.json
, is read.Returns: params – The dictionary with the parameter values
,bounds
,scale
and iffixed
. See notes for more detailed information on dictionary format andWDmodel_param_defaults.json
for an example file forparam_file
.Return type: dict Notes
params is a dict the parameter names, as defined with
WDmodel.io._PARAMETER_NAMES
as keys- Each key must have a dictionary with keys:
value
: valuefixed
: a bool specifying if the parameter is fixed (True
) or allowed to vary (False
)scale
: a scale parameter used to set the step size in this dimensionbounds
: An upper and lower limit on parameter values
The default bounds are set by the grids available for the DA White Dwarf atmospheres, and by reasonable plausible ranges for the other parameters. Don’t muck with them unless you really have good reason to.
This routine does not do any checking of types, values or bounds. This is done by
WDmodel.io.get_params_from_argparse()
before the fit. If you setup the fit using an external code, you should check these values.
-
WDmodel.io.
read_pbmap
(filename, **kwargs)¶ Read passband obsmode mapping table - wraps
_read_ascii()
-
WDmodel.io.
read_phot
(filename, **kwargs)¶ Read photometry - wraps
_read_ascii()
-
WDmodel.io.
read_reddening
(filename, **kwargs)¶ Read J. Holberg’s custom reddening function - wraps
_read_ascii()
-
WDmodel.io.
read_spec
(filename, **kwargs)[source]¶ Read a spectrum
Wraps
_read_ascii()
, adding testing of the input arrays to check if the elements are finite, and if the errors and flux are strictly positive.Parameters: - filename (str) – Filename of the ASCII file. Must have columns
wave
,flux
,flux_err
- kwargs (dict) – Extra options, passed directly to
numpy.genfromtxt()
Returns: spec – Record array with the spectrum data. Has
dtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8')]
Return type: Raises: ValueError
– If any value is not finite or ifflux
orflux_err
have any values<= 0
See also
- filename (str) – Filename of the ASCII file. Must have columns
-
WDmodel.io.
read_spectable
(filename, **kwargs)¶ Read spectrum FWHM table - wraps
_read_ascii()
-
WDmodel.io.
set_objname_outdir_for_specfile
(specfile, outdir=None, outroot=None, redo=False, resume=False, nocreate=False)[source]¶ Sets the short human readable object name and output directory
Parameters: - specfile (str) – The spectrum filename
- outdir (None or str, optional) – The output directory name to create. If
None
this is set based onspecfile
- outroot (None or str, optional) – The output root directory under which to store the fits. If
None
the default is'out'
- redo (bool, optional) – If
False
the directory will not be created if it already exists, and an error is raised - resume (bool, optional) – If
False
the directory will not be created if it already exists, and an error is raised - nocreate (bool, optional) – If
True
then creation of output directories is not even attempted
Returns: - objname (str) – The human readable object name based on the spectrum
- dirname (str) – The output directory name created if successful
See also
-
WDmodel.io.
write_fit_inputs
(spec, phot, cont_model, linedata, continuumdata, rvmodel, covtype, coveps, phot_dispersion, scale_factor, outfile)[source]¶ Save all the inputs to the fitter to a file
This file is enough to resume the fit with the same input, redoing the output, or restoring from a failure.
Parameters: - spec (
numpy.recarray
) – The spectrum withdtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8')]
- phot (None or
numpy.recarray
) –None
or the photometry withdtype=[('pb', 'str'), ('mag', '<f8'), ('mag_err', '<f8')]
- cont_model (
numpy.recarray
) – The continuuum model. Must have the same structure asspec
. Produced byWDmodel.fit.pre_process_spectrum()
. Used byWDmodel.viz
- linedata (
numpy.recarray
) – The observations of the spectrum corresponding to the hydrogen Balmer lines. Must havedtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8'), ('line_mask', 'i4')]
Produced byWDmodel.fit.pre_process_spectrum()
Used byWDmodel.viz
- continuumdata (
numpy.recarray
) – Data used to generate the continuum model. Must have the same structure asspec
. Produced byWDmodel.fit.pre_process_spectrum()
- rvmodel (
{'ccm89','od94','f99', 'custom'}
) – Parametrization of the reddening law. Used to initializeWDmodel.WDmodel.WDmodel()
instance. - covtype (
{'Matern32', 'SHO', 'Exp', 'White'}
) – stationary kernel type used to parametrize the covariance inWDmodel.covariance.WDmodel_CovModel
- coveps (float) – If
covtype
is'Matern32'
acelerite.terms.Matern32Term
is used to approximate a Matern32 kernel with precision coveps. - phot_dispersion (float, optional) – Excess photometric dispersion to add in quadrature with the
photometric uncertainties
phot.mag_err
inWDmodel.likelihood.WDmodel_Likelihood
. - scale_factor (float) – Factor by which the flux must be scaled. Critical to getting the right uncertainties.
- outfile (str) – Output HDF5 filename
Notes
- The outputs are stored in a HDF5 file with groups
spec
- storing the spectrum andscale_factor
cont_model
- stores the continuum modellinedata
- stores the hydrogen Balmer line datacontinuumdata
- stores the data used to generatecont_model
fit_config
- storescovtype
,coveps
andrvmodel
as attributesphot
- only created ifphot
is notNone
, storesphot
,phot_dispersion
- spec (
-
WDmodel.io.
write_full_model
(full_model, outfile)[source]¶ Write the full SED model to an output file.
Parameters: - full_model (
numpy.recarray
) – The SED model withdtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8')]
- outfile (str) – Output HDF5 SED model filename
Notes
- The output is written into a group
model
with datasets wave
: array-like - the SED model wavelengthflux
: array-like - the SED model fluxflux_err
: array-like - the SED model flux uncertainty
- full_model (
-
WDmodel.io.
write_params
(params, outfile)[source]¶ Dumps the parameter dictionary params to a JSON file
Parameters: - params (dict) – A parameter dict such as that produced by
WDmodel.io.read_params()
- outfile (str) – Output filename to save the parameter dict as a JSON file.
Notes
params is a dict the parameter names, as defined with
WDmodel.io._PARAMETER_NAMES
as keys- Each key must have a dictionary with keys:
value
: valuefixed
: a bool specifying if the parameter is fixed (True
) or allowed to vary (False
)scale
: a scale parameter used to set the step size in this dimensionbounds
: An upper and lower limit on parameter values
Any extra keys are simply written as-is JSON doesn’t preserve ordering necessarily. This is imposed by
WDmodel.io.read_params()
See also
- params (dict) – A parameter dict such as that produced by
-
WDmodel.io.
write_phot_model
(phot, model_mags, outfile)[source]¶ Write the photometry, model photometry and residuals to an output file.
Parameters: - phot (None or
numpy.recarray
) –None
or the photometry withdtype=[('pb', 'str'), ('mag', '<f8'), ('mag_err', '<f8')]
- model_mags (None or
numpy.recarray
) – The model magnitudes. Hasdtype=[('pb', 'str'), ('mag', '<f8')]
- outfile (str) – Output space-separated text filename
Notes
The data is saved to a space-separated ASCII text file with 6 decimal places of precision.
- The order of the columns is
pb
: array-like - the observation’s passbandmag
: array-like - the observed magnitudemag_err
: array-like - the observed magnitude uncertaintymodel_mag
: array-like - the model magnituderes_mag
: array-like - the magnitude residual
- phot (None or
-
WDmodel.io.
write_spectrum_model
(spec, model_spec, outfile)[source]¶ Write the spectrum and the model spectrum and residuals to an output file.
Parameters: - spec (
numpy.recarray
) – The spectrum withdtype=[('wave', '<f8'), ('flux', '<f8'), ('flux_err', '<f8')]
- model_spec (
numpy.recarray
) – The model spectrum. Hasdtype=[('wave', '<f8'), ('flux', '<f8'), ('norm_flux', '<f8'), ('flux_err', '<f8')]
- outfile (str) – Output space-separated text filename
Notes
The data is saved to a space-separated ASCII text file with 8 decimal places of precision.
- The order of the columns is
wave
: array-like - the spectrum wavelengthflux
: array-like - the observed fluxflux_err
: array-like - the observed flux uncertaintynorm_flux
: array-like - the model flux without the Gaussian process covariance modelmodel_flux
: array-like - the model fluxmodel_flux_err
: array-like - the model flux uncertaintyres_flux
: array-like - the flux residual
- spec (