Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for source_detection's test_outputs #665

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions romancal/source_detection/tests/test_source_detection_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,50 @@ def test_source_detection_scalar_threshold(setup_inputs):
def test_outputs(tmp_path, setup_inputs):
"""Make sure `save_catalogs` and `output_cat_filetype` work correctly."""

model = setup_inputs()

# add a single source to image so a non-empty catalog is produced
add_random_gauss(model.data, [50], [50])

# run step and direct it to save catalog. default format should be asdf
sd = SourceDetectionStep()
sd.save_catalogs = True
sd.process(model)
# make sure file exists
expected_output_path = os.path.join(tmp_path, "filename_tweakreg_catalog.asdf")
assert os.path.isfile(expected_output_path)

# run again, specifying that catalog should be saved in ecsv format
sd = SourceDetectionStep()
sd.save_catalogs = True
sd.output_cat_filetype = "ecsv"
expected_output_path = os.path.join(tmp_path, "filename_tweakreg_catalog.ecsv")
assert os.path.isfile(expected_output_path)
try:
# The contextlib.chdir() context manager was added in Python 3.11
# so once we drop support for Python 3.10 we can remove this try/except
from contextlib import chdir

context = chdir(tmp_path)
except ImportError:
# This reproduces enough of the behavior of contextlib.chdir()
# for Python < 3.11 (this part can be removed at a later date)
from contextlib import contextmanager
from pathlib import Path

@contextmanager
def ctx():
cwd = Path.cwd()
os.chdir(tmp_path)
try:
yield
finally:
os.chdir(cwd)

context = ctx()

with context:
model = setup_inputs()

# add a single source to image so a non-empty catalog is produced
add_random_gauss(model.data, [50], [50])

# run step and direct it to save catalog. default format should be asdf
sd = SourceDetectionStep()
sd.save_catalogs = True
sd.process(model)
# make sure file exists
expected_output_path = os.path.join(tmp_path, "filename_tweakreg_catalog.asdf")
assert os.path.isfile(expected_output_path)

# run again, specifying that catalog should be saved in ecsv format
sd = SourceDetectionStep()
sd.save_catalogs = True
sd.output_cat_filetype = "ecsv"
sd.process(model)
expected_output_path = os.path.join(tmp_path, "filename_tweakreg_catalog.ecsv")
assert os.path.isfile(expected_output_path)


# @pytest.mark.skipif(
Expand Down