From cfd40d40877653de8bbd40780bcbfc8726965c40 Mon Sep 17 00:00:00 2001 From: Drew Oldag <47493171+drewoldag@users.noreply.github.com> Date: Tue, 14 May 2024 20:33:14 -0700 Subject: [PATCH] Adding functions to convert between string dictionaries and keymap dicts. (#135) --- src/lephare/prepare.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/lephare/prepare.py b/src/lephare/prepare.py index 1ec1773d..15997b44 100644 --- a/src/lephare/prepare.py +++ b/src/lephare/prepare.py @@ -5,7 +5,15 @@ import lephare as lp -__all__ = ["prepare", "overwrite_config", "read_yaml_config", "write_yaml_config", "write_para_config"] +__all__ = [ + "prepare", + "overwrite_config", + "read_yaml_config", + "write_yaml_config", + "write_para_config", + "keymap_to_string_dict", + "string_dict_to_keymap", +] def prepare(config, star_config=None, gal_config=None, qso_config=None): @@ -29,6 +37,10 @@ def prepare(config, star_config=None, gal_config=None, qso_config=None): qso_config : dict of lephare.keyword or None Config values to override for QSO. """ + # check that the config is string to keyword map + for k in config: + assert isinstance(config[k], lp.keyword) + object_types = {"STAR": star_config, "GAL": gal_config, "QSO": qso_config} # Run the filter command # load filters from config @@ -123,3 +135,19 @@ def write_para_config(keymap, para_file_path): with open(para_file_path, "w") as file_handle: file_handle.write(para_contents) file_handle.close() + + +def keymap_to_string_dict(keymap): + """Convert a dictionary of keywords to a dictionary of strings""" + config_dict = {} + for k in keymap: + config_dict[keymap[k].name] = keymap[k].value + return config_dict + + +def string_dict_to_keymap(string_dict): + """Convert a dictionary of strings to a dictionary of keywords""" + keymap = {} + for k in string_dict: + keymap[k] = lp.keyword(k, str(string_dict[k])) + return keymap