Skip to content

Commit

Permalink
Merge pull request #136 from tjgalvin/dumpfield
Browse files Browse the repository at this point in the history
Dumping field options to yaml file
  • Loading branch information
tjgalvin authored Jul 5, 2024
2 parents c3e4923 + 3ef2227 commit facdba5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
39 changes: 39 additions & 0 deletions flint/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from pathlib import Path
from typing import Collection, List, NamedTuple, Optional, Union

import yaml

from flint.logging import logger


class BandpassOptions(NamedTuple):
"""Container that reoresents the flint related options that
Expand Down Expand Up @@ -114,6 +118,41 @@ class FieldOptions(NamedTuple):
"""Rename MSs throught rounds of imaging and self-cal instead of creating copies. This will delete data-columns throughout. """


def dump_field_options_to_yaml(
output_path: Path, field_options: FieldOptions, overwrite: bool = False
) -> Path:
"""Dump the supplied instance of `FieldOptions` to a yaml file
for record keeping.
The parent directory of the `output_path` will be created if it
does not already exist.
Args:
output_path (Path): Path of the output file.
field_options (FieldOptions): The `FieldOptions` class to write.
overwrite (bool, optional): Overwrite the file if it exists. Defaults to False.
Raises:
FileExistsError: Raise if `output_path` already exists and `overwrite` is `False`
Returns:
Path: Output path written to.
"""

logger.info(f"Writing field_options to {output_path}")

if not overwrite and output_path.exists():
raise FileExistsError(f"{output_path=} exists. ")

# Create the directory just in case
output_path.parent.mkdir(parents=True, exist_ok=True)

with open(output_path, "w") as out_file:
yaml.dump(data=field_options, stream=out_file, sort_keys=False)

return output_path


# TODO: Perhaps move these to flint.naming, and can be built up
# based on rules, e.g. imager used, source finder etc.
DEFAULT_TAR_RE_PATTERNS = (
Expand Down
10 changes: 9 additions & 1 deletion flint/prefect/flows/continuum_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
from flint.ms import find_mss
from flint.naming import (
CASDANameComponents,
add_timestamp_to_path,
extract_components_from_name,
get_sbid_from_path,
)
from flint.options import FieldOptions
from flint.options import FieldOptions, dump_field_options_to_yaml
from flint.prefect.clusters import get_dask_runner
from flint.prefect.common.imaging import (
_create_convol_linmos_images,
Expand Down Expand Up @@ -153,6 +154,13 @@ def process_science_fields(
science_path=science_path, split_path=split_path, check_exists=True
)

dump_field_options_to_yaml(
output_path=add_timestamp_to_path(
input_path=output_split_science_path / "field_options.yaml"
),
field_optios=field_options,
)

archive_wait_for: List[Any] = []

strategy = _load_and_copy_strategy(
Expand Down
27 changes: 26 additions & 1 deletion tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@

from pathlib import Path

from flint.options import FieldOptions
import pytest

from flint.options import FieldOptions, dump_field_options_to_yaml
from flint.prefect.flows.continuum_pipeline import get_parser


def test_dump_field_options_to_yaml(tmpdir):
"""See if the field options file can be dumped to an output directory"""
tmpdir = Path(tmpdir)

field_options = FieldOptions(
flagger_container=Path("a"), calibrate_container=Path("b")
)

assert not (tmpdir / "Jack").exists()

path_1 = tmpdir / "field_options.yaml"
path_2 = tmpdir / "Jack" / "Sparrow" / "field_options.yaml"

for path in (path_1, path_2):
output_path = dump_field_options_to_yaml(
output_path=path, field_options=field_options
)
assert output_path.exists()

with pytest.raises(FileExistsError):
dump_field_options_to_yaml(output_path=path_2, field_options=field_options)


def test_config_field_options(tmpdir):
output_file = f"{tmpdir}/example.config"
contents = """--holofile /scratch3/projects/spiceracs/RACS_Low2_Holography/akpb.iquv.square_6x6.63.887MHz.SB39549.cube.fits
Expand Down

0 comments on commit facdba5

Please sign in to comment.