"""Methods for reading and writing input, intermediate, and output files."""
import os
import numpy as np
[docs]
def get_posterior_filename(lc_name, fits_dir='.', sampler=None):
"""Get the file name for equal weight posterior samples from a lightcurve fit.
Parameters
----------
lc_name : str
Lightcurve name.
fits_dir : str, optional
Output directory path. Defaults to FITS_DIR.
sampler : str, optional
Variety of sampler. Can be included in the sample file name.
Returns
-------
str
File name for numpy array file containing the posterior samples.
"""
if sampler is not None:
if sampler == 'snII':
posterior_filename = os.path.join(fits_dir, f"{lc_name}.npz")
else:
posterior_filename = os.path.join(fits_dir, f"{lc_name}_eqwt_{sampler}.npz")
else:
posterior_filename = os.path.join(fits_dir, f"{lc_name}_eqwt.npz")
return posterior_filename
[docs]
def get_posterior_samples(lc_name, fits_dir=None, sampler=None):
"""Get all EQUAL WEIGHT posterior samples from a lightcurve fit.
Parameters
----------
lc_name : str
Lightcurve name.
fits_dir : str, optional
Output directory path. Defaults to FITS_DIR.
sampler : str, optional
Variety of sampler. Can be included in the sample file name.
Returns
-------
np.ndarray
Numpy array containing the posterior samples.
"""
posterior_filename = get_posterior_filename(lc_name, fits_dir, sampler)
loaded_arr = np.load(posterior_filename, allow_pickle=True)
samples = None
kwargs = {}
for k in loaded_arr.files:
extracted = loaded_arr[k]
if extracted.shape == ():
extracted = extracted[()]
if k == 'arr_0' or k == 'samples' or k == 'fits':
samples = extracted
else:
kwargs[k] = extracted
return samples, kwargs
[docs]
def get_multiple_posterior_samples(lc_names, fits_dir, sampler=None):
"""Reads all EQUAL WEIGHT posterior samples for a set of lightcurve fits.
Parameters
----------
lc_names : str
Lightcurve names.
fits_dir : str, optional
Output directory path. Defaults to FITS_DIR.
sampler : str, optional
Variety of sampler. Can be included in the sample file name.
Returns
-------
dict of np.ndarray
Dictionary mapping the posterior samples to the light curves specified.
"""
posterior_samples = {}
for lc_name in np.unique(lc_names):
posterior_samples[lc_name] = get_posterior_samples(lc_name, fits_dir, sampler)
return posterior_samples
[docs]
def has_posterior_samples(lc_name, fits_dir=None, sampler=None):
"""Determine if we already have some posterior sample data for the lightcurve.
Parameters
----------
lc_name : str
Lightcurve name.
fits_dir : str, optional
Output directory path. Defaults to FITS_DIR.
sampler : str, optional
Variety of sampler. Can be included in the sample file name.
Returns
-------
boolean
Does a file already exist for the lightcurve fit
"""
posterior_filename = get_posterior_filename(lc_name, fits_dir, sampler)
return os.path.isfile(posterior_filename)