Skip to content

Commit

Permalink
Merge branch 'prep-for-lstbin-subpackage' of github.com:hera-team/her…
Browse files Browse the repository at this point in the history
…a_opm into prep-for-lstbin-subpackage
  • Loading branch information
steven-murray committed Mar 29, 2024
2 parents 39f6d42 + 645f5a4 commit aa28f13
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions hera_opm/mf_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pathlib import Path
import math


def get_jd(filename):
"""Get the JD from a data file name.
Expand Down Expand Up @@ -707,6 +708,7 @@ def build_makeflow_from_config(

return


def _get_timeout(config):
timeout = get_config_entry(config, "Options", "timeout", required=False)
if timeout is not None:
Expand All @@ -722,6 +724,7 @@ def _get_timeout(config):
)
return timeout


def build_analysis_makeflow_from_config(
obsids, config_file, mf_name=None, work_dir=None
):
Expand Down Expand Up @@ -1369,6 +1372,7 @@ def get_lstbin_datafiles(config, parent_dir):
for df in datafiles
]


def _legacy_make_lstbin_config_file(config, outdir: Path, datafiles):
try:
from hera_cal.lst_stack import make_lst_bin_config_file
Expand All @@ -1377,9 +1381,7 @@ def _legacy_make_lstbin_config_file(config, outdir: Path, datafiles):

# Get dlst. Updated version supports leaving dlst unspecified or set as null.
# To support older versions which required string 'None', set that to None here.
dlst = get_config_entry(
config, "LSTBIN_OPTS", "dlst", default=None, required=False
)
dlst = get_config_entry(config, "LSTBIN_OPTS", "dlst", default=None, required=False)
if isinstance(dlst, str) and dlst.lower() in ("none", "null", ""):
warnings.warn(
"dlst should not be set to (string) 'None', but rather left unspecified in your TOML.",
Expand Down Expand Up @@ -1429,11 +1431,12 @@ def _legacy_make_lstbin_config_file(config, outdir: Path, datafiles):
)
print(f"Created lstbin config file at {lstbin_config_file}.")

return len(file_config['matched_files'])
return len(file_config["matched_files"])


def make_lstbin_config_file(config, outdir: str) -> int:
# This must be a TOML file that specifies how to construct the LSTbin file-config
lstconfig = config['FILE_CFG']
lstconfig = config["FILE_CFG"]

Check warning on line 1439 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1439

Added line #L1439 was not covered by tests

from hera_cal.lst_stack.config import LSTBinConfiguration

Check warning on line 1441 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1441

Added line #L1441 was not covered by tests

Expand All @@ -1456,9 +1459,6 @@ def make_lstbin_config_file(config, outdir: str) -> int:
return lstbin_config_file, len(lst_file_config.matched_files)

Check warning on line 1459 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1459

Added line #L1459 was not covered by tests





def build_lstbin_makeflow_from_config(
config_file, mf_name=None, work_dir=None, **kwargs
):
Expand Down Expand Up @@ -1564,7 +1564,7 @@ def build_lstbin_makeflow_from_config(
_datafiles = [df for df in _datafiles if len(df) > 0]

nfiles = _legacy_make_lstbin_config_file(config, outdir, _datafiles)

if not parallelize:
nfiles = 1

Check warning on line 1569 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1569

Added line #L1569 was not covered by tests

Expand Down Expand Up @@ -1640,10 +1640,11 @@ def build_lstbin_makeflow_from_config(

return


def build_lstbin_notebook_makeflow_from_config(
config_file: str | Path,
mf_name: str | None=None,
work_dir: str | Path | None=None,
config_file: str | Path,
mf_name: str | None = None,
work_dir: str | Path | None = None,
) -> None:
"""Construct a notebook-based LST-binning makeflow file from input data and a config_file.
Expand All @@ -1657,7 +1658,7 @@ def build_lstbin_notebook_makeflow_from_config(
The name of makeflow file. Defaults to "<config_file_basename>.mf" if not
specified.
work_dir : str or Path, optional
The directory in which to write the makeflow file and wrapper files.
The directory in which to write the makeflow file and wrapper files.
If not specified, the parent directory of the config file will be used.
"""
config_file = Path(config_file)

Check warning on line 1664 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1664

Added line #L1664 was not covered by tests
Expand All @@ -1666,7 +1667,7 @@ def build_lstbin_notebook_makeflow_from_config(

if mf_name is None:
mf_name = config_file.with_suffix(".mf").name

Check warning on line 1669 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1668-L1669

Added lines #L1668 - L1669 were not covered by tests

work_dir = Path(work_dir or config_file.parent).absolute()

Check warning on line 1671 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1671

Added line #L1671 was not covered by tests

makeflowfile = work_dir / mf_name

Check warning on line 1673 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1673

Added line #L1673 was not covered by tests
Expand All @@ -1679,9 +1680,9 @@ def build_lstbin_notebook_makeflow_from_config(

shutil.copy2(config_file, outdir / "lstbin-config.toml")

Check warning on line 1681 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1681

Added line #L1681 was not covered by tests

# Also write a YAML version of just the parameters, to be used to run
# Also write a YAML version of just the parameters, to be used to run
# the notebook
cfg_opts = toml.load(config_file)['LSTAVG_OPTS']
cfg_opts = toml.load(config_file)["LSTAVG_OPTS"]
lstavg_config = outdir / "lstavg-config.toml"
with open(lstavg_config, "w") as fl:
toml.dump(cfg_opts, fl)

Check warning on line 1688 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1685-L1688

Added lines #L1685 - L1688 were not covered by tests
Expand All @@ -1695,20 +1696,22 @@ def build_lstbin_notebook_makeflow_from_config(

# set output_file_select to None
config["LSTBIN_OPTS"]["output_file_select"] = str("None")
config['LSTBIN_OPTS']['lstavg_toml_file'] = str(lstavg_config.absolute())
config['LSTBIN_OPTS']['kernel'] = conda_env
config["LSTBIN_OPTS"]["lstavg_toml_file"] = str(lstavg_config.absolute())
config["LSTBIN_OPTS"]["kernel"] = conda_env

Check warning on line 1700 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1698-L1700

Added lines #L1698 - L1700 were not covered by tests

if 'make_plots' not in config['LSTBIN_OPTS']:
if 'plot_every' in config["LSTBIN_OPTS"]:
plot_every = int(get_config_entry(config, "LSTBIN_OPTS", "plot_every", required=False))
if "make_plots" not in config["LSTBIN_OPTS"]:
if "plot_every" in config["LSTBIN_OPTS"]:
plot_every = int(

Check warning on line 1704 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1702-L1704

Added lines #L1702 - L1704 were not covered by tests
get_config_entry(config, "LSTBIN_OPTS", "plot_every", required=False)
)
else:
plot_every = 1

Check warning on line 1708 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1708

Added line #L1708 was not covered by tests
else:
plot_every = int(bool(config['LSTBIN_OPTS']['make_plots']))
plot_every = int(bool(config["LSTBIN_OPTS"]["make_plots"]))

Check warning on line 1710 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1710

Added line #L1710 was not covered by tests

# determine whether or not to parallelize
parallelize = get_config_entry(config, "LSTBIN_OPTS", "parallelize", required=True)

Check warning on line 1713 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1713

Added line #L1713 was not covered by tests

actions = get_config_entry(config, "WorkFlow", "actions", required=True)
if len(actions) > 1:
raise ValueError("This function only supports a single action in the workflow.")
Expand All @@ -1726,17 +1729,15 @@ def build_lstbin_notebook_makeflow_from_config(
base_mem = get_config_entry(config, "Options", "base_mem", required=True)
base_cpu = get_config_entry(config, "Options", "base_cpu", required=False)
mail_user = get_config_entry(config, "Options", "mail_user", required=False)
default_queue = get_config_entry(
config, "Options", "default_queue", required=False
)
default_queue = get_config_entry(config, "Options", "default_queue", required=False)
if default_queue is None:
default_queue = "hera"
batch_options = process_batch_options(

Check warning on line 1735 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1729-L1735

Added lines #L1729 - L1735 were not covered by tests
base_mem, base_cpu, mail_user, default_queue, batch_system
)

lstbin_config_file, nfiles = make_lstbin_config_file(config, outdir)
config['LSTBIN_OPTS']['lstconf'] = str(lstbin_config_file.absolute())
config["LSTBIN_OPTS"]["lstconf"] = str(lstbin_config_file.absolute())

Check warning on line 1740 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1739-L1740

Added lines #L1739 - L1740 were not covered by tests

if not parallelize:
nfiles = 1

Check warning on line 1743 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1742-L1743

Added lines #L1742 - L1743 were not covered by tests
Expand Down Expand Up @@ -1768,18 +1769,21 @@ def build_lstbin_notebook_makeflow_from_config(
fl.write(

Check warning on line 1769 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1768-L1769

Added lines #L1768 - L1769 were not covered by tests
f"""# makeflow file generated from config file {config_file.name}
# created at {dt}
export BATCH_OPTIONS = {batch_options}
""")

export BATCH_OPTIONS = {batch_options}
"""
)

# loop over output files
for output_file_index in range(nfiles):

Check warning on line 1777 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1777

Added line #L1777 was not covered by tests
# if parallize, update output_file_select
if parallelize:
config["LSTBIN_OPTS"]["output_file_select"] = str(output_file_index)
if plot_every > 0:
config["LSTBIN_OPTS"]['make_plots'] = str(output_file_index % plot_every == 0)
config["LSTBIN_OPTS"]["make_plots"] = str(

Check warning on line 1782 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1779-L1782

Added lines #L1779 - L1782 were not covered by tests
output_file_index % plot_every == 0
)
else:
config["LSTBIN_OPTS"]['make_plots'] = "False"
config["LSTBIN_OPTS"]["make_plots"] = "False"

Check warning on line 1786 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1786

Added line #L1786 was not covered by tests

# make outfile list
outfile = Path(f"{output_file_index:04}.LSTBIN.out")

Check warning on line 1789 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1789

Added line #L1789 was not covered by tests
Expand All @@ -1799,9 +1803,11 @@ def build_lstbin_notebook_makeflow_from_config(
# make a small wrapper script that will run the actual command
# can't embed if; then statements in makeflow script
wrapper_script = work_dir / f"wrapper_{outfile.with_suffix('.sh').name}"

Check warning on line 1805 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1805

Added line #L1805 was not covered by tests

with open(wrapper_script, "w") as f2:
f2.write(wrapper_template.format(args=args, outfile=outfile, logfile=logfile))
f2.write(

Check warning on line 1808 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1807-L1808

Added lines #L1807 - L1808 were not covered by tests
wrapper_template.format(args=args, outfile=outfile, logfile=logfile)
)

# make file executable
os.chmod(wrapper_script, 0o755)

Check warning on line 1813 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1813

Added line #L1813 was not covered by tests
Expand All @@ -1811,14 +1817,13 @@ def build_lstbin_notebook_makeflow_from_config(
lines = f"{outfile}: {command}\n\t{wrapper_script} > {logfile} 2>&1\n"
fl.write(lines)

Check warning on line 1818 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1817-L1818

Added lines #L1817 - L1818 were not covered by tests



# Also write the conda_env export to the LSTbin dir
if conda_env is not None:
os.system(

Check warning on line 1822 in hera_opm/mf_tools.py

View check run for this annotation

Codecov / codecov/patch

hera_opm/mf_tools.py#L1821-L1822

Added lines #L1821 - L1822 were not covered by tests
f"conda env export -n {conda_env} --file {outdir}/environment.yaml"
)


def clean_wrapper_scripts(work_dir):
"""Clean up wrapper scripts from work directory.
Expand Down

0 comments on commit aa28f13

Please sign in to comment.