Skip to content

Commit

Permalink
add docstrings to internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Morgan committed Jan 25, 2021
1 parent 530c57f commit e906f49
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 5 deletions.
55 changes: 55 additions & 0 deletions deeplenstronomy/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,43 @@ def set_lenstronomy_maps(self):

@staticmethod
def config_dict_format(*args):
"""
From a list of parameters, construct the path through the config dictionary
"""
return "['" + "']['".join(args) + "']"

def config_lookup(self, lookup_str, full=False):
"""
From a key path, get the value in the dictionary
Args:
lookup_str (str): path of keys through a nested dictionary
full (bool, optional, default=False): `True for lookup in the `full_dict`, `False` for lookup in the `config_dict`
Returns:
The value in the dictionary at the location of the keypath
"""
if not full:
return eval("self.config" + lookup_str)
else:
return eval("self.full" + lookup_str)

### Check functions
def check_top_level_existence(self):
"""
Check for the DATASET, SURVEY, IMAGE, COSMOLOGY, SPECIES, and GEOMETRY sections
in the config file
"""
errs = []
for name in ['DATASET', 'SURVEY', 'IMAGE', 'COSMOLOGY', 'SPECIES', 'GEOMETRY']:
if name not in self.full.keys():
errs.append("Missing {0} section from config file".format(name))
return errs

def check_random_seed(self):
"""
Check whether the passed value for the random seed is valid
"""
errs = []
try:
seed = int(self.config["DATASET"]["PARAMETERS"]["SEED"])
Expand All @@ -205,6 +225,14 @@ def check_random_seed(self):
return errs

def check_low_level_existence(self):
"""
Check that the DATASET.NAME, DATASET.PARAMETERS.SIZE, COSMOLOGY.PARAMETERS.H0,
COSMOLOGY.PARAMETERS.Om0, IMAGE.PARAMETERS.exposure_time, IMAGE.PARAMETERS.numPix,
IMAGE.PARAMETERS.pixel_scale, IMAGE.PARAMETERS.psf_type, IMAGE.PARAMETERS.read_noise,
IMAGE.PARAMETERS.ccd_gain, SURVEY.PARAMETERS.BANDS, SURVEY.PARAMETERS.seeing,
SURVEY.PARAMETERS.magnitude_zero_point, SURVEY.PARAMETERS.sky_brightness, and
SURVEY.PARAMETERS.num_exposures are all present in the config file
"""
errs = []
param_names = {"DATASET.NAME",
"DATASET.PARAMETERS.SIZE",
Expand All @@ -230,6 +258,13 @@ def check_low_level_existence(self):
return errs

def check_not_allowed_to_be_drawn_from_a_distribution(self):
"""
Check that parameters that must be fixed in the simulation (DATASET.NAME,
DATASET.PARAMETERS.SIZE, DATASET.PARAMETERS.OUTDIR, IMAGE.PARAMETERS.numPix,
COSMOLOGY.PARAMETERS.H0, COSMOLOGY.PARAMETERS.Tcmb, COSMOLOGY.PARAMETERS.Neff,
COSMOLOGY.PARAMETERS.m_nu, and COSMOLOGY.PARAMETERS.Ob0) are not being
drawn from a distribution with the DISTRIBUTION keyword
"""
errs = []
param_names = {"DATASET.NAME",
"DATASET.PARAMETERS.SIZE",
Expand All @@ -253,6 +288,10 @@ def check_not_allowed_to_be_drawn_from_a_distribution(self):
return errs

def check_for_auxiliary_files(self):
"""
Check that any auxiliary files specified with the INPUT keyword are
able to be found
"""
errs = []
input_paths = [x for x in self.full_keypaths if x.find("INPUT") != -1]
input_files = [self.config_lookup(self.config_dict_format(*param.split('.')), full=True) for param in input_paths]
Expand All @@ -262,6 +301,10 @@ def check_for_auxiliary_files(self):
return errs

def check_for_valid_distribution_entry(self):
"""
Check that use of the DISTRIBUTION keyword in the configuration file (1) points
to a valid distribution and (2) has an entry for each parameter
"""
errs = []
distribution_paths = [x for x in self.full_keypaths if x.endswith("DISTRIBUTION")]
distribution_dicts = [self.config_lookup(self.config_dict_format(*param.split('.'))) for param in distribution_paths]
Expand Down Expand Up @@ -302,6 +345,9 @@ def check_for_valid_distribution_entry(self):
return errs

def check_input_distributions(self):
"""
Check that a USERDIST file can be read in and has the proper format
"""
errs = []
if "DISTRIBUTIONS" in self.config.keys():
# there must be at least 1 USERDIST_ key
Expand Down Expand Up @@ -349,6 +395,9 @@ def check_input_distributions(self):
return errs

def check_image_backgrounds(self):
"""
Check that images used for backgrounds can be read in and organized successfully
"""
errs = []
if "BACKGROUNDS" in self.config.keys():
# value must be a dict
Expand Down Expand Up @@ -657,6 +706,9 @@ def _valid_index(self, k, path):
return detections, errs

def check_valid_species(self):
"""
Check that all GALAXY, POINTSOURCE, and NOISE objects are formatted correctly
"""
errs, names = [], []

# There must be at least one species
Expand Down Expand Up @@ -703,6 +755,9 @@ def check_valid_species(self):
return errs

def check_valid_geometry(self):
"""
Check that all configurations in the geometry section are formatted correctly
"""
errs = []

# There must be at least one configuration
Expand Down
Loading

0 comments on commit e906f49

Please sign in to comment.