Skip to content

Commit

Permalink
add minimal unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
TjarkMiener committed Feb 10, 2025
1 parent 4b854bd commit 7f7d0a3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
42 changes: 42 additions & 0 deletions dl1_data_handler/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
common pytest fixtures for tests in dl1-data-handler.
Credits to ctapipe for the original code.
"""

import pytest

from ctapipe.core import run_tool
from ctapipe.utils import get_dataset_path
from ctapipe.utils.filelock import FileLock

@pytest.fixture(scope="session")
def prod5_gamma_simtel_path():
return get_dataset_path("gamma_prod5.simtel.zst")

@pytest.fixture(scope="session")
def dl1_tmp_path(tmp_path_factory):
"""Temporary directory for global dl1 test data"""
return tmp_path_factory.mktemp("dl1_")

@pytest.fixture(scope="session")
def dl1_gamma_file(dl1_tmp_path, prod5_gamma_simtel_path):
"""
DL1 file containing both images and parameters from a gamma simulation set.
"""
from ctapipe.tools.process import ProcessorTool

output = dl1_tmp_path / "gamma.dl1.h5"

# prevent running process multiple times in case of parallel tests
with FileLock(output.with_suffix(output.suffix + ".lock")):
if output.is_file():
return output

argv = [
f"--input={prod5_gamma_simtel_path}",
f"--output={output}",
"--write-images",
"--DataWriter.Contact.name=αℓℓ the äüöß",
]
assert run_tool(ProcessorTool(), argv=argv, cwd=dl1_tmp_path) == 0
return output
2 changes: 1 addition & 1 deletion dl1_data_handler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def __init__(
self.subarray = SubarrayDescription.from_hdf(self.first_file)
selected_tel_ids = None
if self.allowed_tels is not None:
selected_tel_ids = np.array(self.allowed_tels, dtype=np.int16)
selected_tel_ids = np.array(list(self.allowed_tels), dtype=np.int16)
else:
if self.allowed_tel_types is not None:
selected_tel_ids = np.ravel(
Expand Down
23 changes: 23 additions & 0 deletions dl1_data_handler/tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
from traitlets.config.loader import Config

from dl1_data_handler.reader import DLImageReader

def test_dl1_image_reading(dl1_tmp_path, dl1_gamma_file):
"""check reading from pixel-wise image data files"""
# Create a configuration suitable for the test
config = Config(
{
"DLImageReader": {
"allowed_tels": [4],
},
}
)
# Create an image reader and test basic properties
dl1_reader = DLImageReader(input_url_signal=[dl1_gamma_file], config=config)
assert dl1_reader._get_n_events() == 1
assert dl1_reader.tel_type == "LST_LST_LSTCam"
# Test the generation of a mono batch
mono_batch = dl1_reader.generate_mono_batch([0])
assert mono_batch["tel_id"] == 4
assert mono_batch["features"].shape == (1, 110, 110, 2)
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[tool:pytest]
pep8ignore =
[tool:pytest]

0 comments on commit 7f7d0a3

Please sign in to comment.