Skip to content

Commit

Permalink
Adding functions to convert between string dictionaries and keymap di…
Browse files Browse the repository at this point in the history
…cts. (#135)
  • Loading branch information
drewoldag authored May 15, 2024
1 parent 699d068 commit cfd40d4
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/lephare/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit cfd40d4

Please sign in to comment.