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

Write modified images from dl1ab in output DL1 files and fix add_config_metadata #1145

Merged
merged 24 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b9a3ca0
write modified images in dl1ab
vuillaut Jun 16, 2023
5901b0d
images will be saved
vuillaut Jun 16, 2023
7c5e7e5
images will be saved, really
vuillaut Jun 16, 2023
6d5a4ff
images will be saved, really
vuillaut Jun 16, 2023
7733fc3
images will be saved, really
vuillaut Jun 16, 2023
b54d133
images will be saved, really
vuillaut Jun 16, 2023
401ed76
write images unless specified not to
vuillaut Jul 21, 2023
8053972
fix bugs with no_image
vuillaut Jul 24, 2023
8984e8c
Merge branch 'main' of https://github.com/cta-observatory/cta-lstchai…
vuillaut Sep 4, 2023
03a968b
adding the image config in the metadata and use it to check of the im…
vuillaut Sep 4, 2023
f041555
dump config in dl1ab
vuillaut Sep 4, 2023
bdb3eab
rename function includes image modification
vuillaut Sep 4, 2023
d5cca47
formatting
vuillaut Sep 4, 2023
867872a
fix test
vuillaut Sep 4, 2023
54d20c4
fix config serialization and add unit test
vuillaut Sep 5, 2023
d8b354a
add standard config in test
vuillaut Sep 5, 2023
f478f0a
correct way of testing the failure of the function
vuillaut Sep 5, 2023
6b949fc
fix test dl1ab on modified images
vuillaut Sep 6, 2023
9b4888f
use importlib resources to find the tuned config file
vuillaut Sep 6, 2023
48ce6fe
Merge remote-tracking branch 'origin/run-summary-test' into write_ima…
vuillaut Sep 6, 2023
d9e75a7
remove unused import pathlib
vuillaut Sep 6, 2023
613a6d4
move includes_image_modification to io/config
vuillaut Sep 7, 2023
f7403ab
import includes_image_modfication from config
vuillaut Sep 14, 2023
432d24a
Merge branch 'main' of https://github.com/cta-observatory/cta-lstchai…
vuillaut Sep 14, 2023
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
29 changes: 29 additions & 0 deletions lstchain/scripts/lstchain_dl1ab.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import argparse
import logging
from pathlib import Path
import json

import astropy.units as u
import numpy as np
Expand Down Expand Up @@ -92,6 +93,27 @@
)


def image_modifier_checker(config):
vuillaut marked this conversation as resolved.
Show resolved Hide resolved
"""
Check if the image modifier has been used in the given configuration.

Parameters
----------
config : `dict`
The configuration dictionary to check.

Returns
-------
`bool`
`True` if the image modifier has been used, `False` otherwise.
"""
imconfig = config.get('image_modifier', {})
increase_nsb = imconfig.get("increase_nsb", False)
increase_psf = imconfig.get("increase_psf", False)

return increase_nsb or increase_psf


def main():
args = parser.parse_args()

Expand Down Expand Up @@ -149,7 +171,7 @@
if increase_nsb or increase_psf:
log.info(f"image_modifier configuration: {imconfig}")
if not args.no_image:
log.info("Modified images are saved in the output file.")

Check warning on line 174 in lstchain/scripts/lstchain_dl1ab.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_dl1ab.py#L174

Added line #L174 was not covered by tests

if increase_nsb:
extra_noise_in_dim_pixels = imconfig["extra_noise_in_dim_pixels"]
Expand Down Expand Up @@ -243,6 +265,13 @@

with tables.open_file(args.input_file, mode='r') as infile:
image_table = read_table(infile, dl1_images_lstcam_key)
# if the image modifier has been used to produce these images, stop here
config_from_image_table = json.loads(image_table.meta['config'])
if image_modifier_checker(config_from_image_table) and image_modifier_checker(config):
log.error(f"The image modifier has already been used to produce the images in file {args.input_file}."

Check warning on line 271 in lstchain/scripts/lstchain_dl1ab.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_dl1ab.py#L271

Added line #L271 was not covered by tests
"Re-applying the image modifier is not a good practice, start again from unmodified images please.")
sys.exit(0)

Check warning on line 273 in lstchain/scripts/lstchain_dl1ab.py

View check run for this annotation

Codecov / codecov/patch

lstchain/scripts/lstchain_dl1ab.py#L273

Added line #L273 was not covered by tests
vuillaut marked this conversation as resolved.
Show resolved Hide resolved

images = image_table['image']
params = read_table(infile, dl1_params_lstcam_key)
dl1_params_input = params.colnames
Expand Down
23 changes: 23 additions & 0 deletions lstchain/scripts/tests/test_dl1ab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from lstchain.scripts.lstchain_dl1ab import image_modifier_checker


def test_image_modifier_checker_no_modifiers():
config = {}
assert not image_modifier_checker(config)


def test_image_modifier_checker_empty_modifier():
config = {"image_modifier": {}}
assert not image_modifier_checker(config)


def test_image_modifier_checker_with_modifiers():
config = {"image_modifier": {"increase_psf": True, "increase_nsb": False}}
assert image_modifier_checker(config)
config = {"image_modifier": {"increase_nsb": True, "increase_psf": False}}
assert image_modifier_checker(config)
config = {"image_modifier": {"increase_psf": True, "increase_nsb": True}}
assert image_modifier_checker(config)
config = {"image_modifier": {"increase_psf": False, "increase_nsb": False}}
assert not image_modifier_checker(config)

18 changes: 18 additions & 0 deletions lstchain/scripts/tests/test_lstchain_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,24 @@ def test_dl1ab_no_images(simulated_dl1_file, tmp_path):
assert (new_parameters['length'] != old_parameters['length']).any()


@pytest.mark.xfail(raises=ValueError, reason="One should not be able to re-modify already modified images")
def test_dl1ab_on_modified_images(simulated_dl1ab, tmp_path):
config_path = tmp_path / 'config.json'
output_file = tmp_path / 'dl1ab_on_modified_images.h5'
with config_path.open('w') as f:
config = get_standard_config()
config['image_modifier'] = {'increase_psf': True, 'increase_nsb': True}
json.dump(config, f)

run_program(
'lstchain_dl1ab',
'-f', simulated_dl1ab,
'-o', output_file,
'-c', config_path,
'--no-image',
)


@pytest.mark.private_data
def test_observed_dl1ab(tmp_path, observed_dl1_files):
output_dl1ab = tmp_path / "dl1ab.h5"
Expand Down
Loading