Skip to content

Commit

Permalink
allow to test all formats of channels and if group_name is not in h5
Browse files Browse the repository at this point in the history
  • Loading branch information
zain-sohail committed Nov 6, 2023
1 parent 0e77b3c commit 8ed9b29
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions tests/data/loader/flash/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ dataframe:
format: per_train
group_name: "/uncategorised/FLASH.DIAG/TIMINGINFO/TIME1.BUNCH_FIRST_INDEX.1/"

delayStage:
format: per_pulse
group_name: "/zraw/FLASH.SYNC/LASER.LOCK.EXP/F1.PG.OSC/FMC0.MD22.1.ENCODER_POSITION.RD/dGroup"

# The prefixes of the stream names for different DAQ systems for parsing filenames
# (Not to be changed by user)
stream_name_prefixes:
Expand Down
62 changes: 60 additions & 2 deletions tests/loader/flash/test_flash_loader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from importlib.util import find_spec
from pathlib import Path
from typing import Literal

import pytest

Expand All @@ -16,7 +17,7 @@ def config_file():
return parse_config(config_path)


def test_get_channels_by_format(config_file):
def test_get_channels_by_format(config_file: dict):

fl = FlashLoader(config_file)
electron_channels = ["dldPosX", "dldPosY", "dldTimeSteps"]
Expand All @@ -28,23 +29,31 @@ def test_get_channels_by_format(config_file):
"cryoTemperature",
"sampleTemperature",
"dldTimeBinSize",
"delayStage",
]
train_channels = ["timeStamp"]

# Call get_channels_by_format method
format_electron = fl.get_channels_by_format(["per_electron"])
format_pulse = fl.get_channels_by_format(["per_pulse"])
format_train = fl.get_channels_by_format(["per_train"])
format_both = fl.get_channels_by_format(["per_pulse", "per_electron"])

assert set(electron_channels) == set(format_electron)
assert set(pulse_channels) == set(format_pulse)
assert set(train_channels) == set(format_train)
assert set(electron_channels + pulse_channels) == set(format_both)


@pytest.mark.parametrize(
"sub_dir",
["online-0/fl1user3/", "express-0/fl1user3/", "FL1USER3/"],
)
def test_initialize_paths(config_file, fs, sub_dir):
def test_initialize_paths(
config_file: dict,
fs,
sub_dir: Literal["online-0/fl1user3/", "express-0/fl1user3/", "FL1USER3/"],
):
config = config_file
del config["core"]["paths"]
config["core"]["beamtime_id"] = "12345678"
Expand All @@ -69,3 +78,52 @@ def test_initialize_paths(config_file, fs, sub_dir):
print(data_raw_dir)
assert expected_raw_path == data_raw_dir[0]
assert expected_processed_path == data_parquet_dir


def test_initialize_paths_filenotfound(config_file: dict):
# test the FileNotFoundError
config = config_file
del config["core"]["paths"]
config["core"]["beamtime_id"] = "11111111"
config["core"]["year"] = "2000"

# instance of class with correct config and call initialize_paths
fl = FlashLoader(config=config)
with pytest.raises(FileNotFoundError):
data_raw_dir, data_parquet_dir = fl.initialize_paths()


def test_invalid_channel_format(config_file: dict):
config = config_file
config["dataframe"]["channels"]["dldPosX"]["format"] = "foo"

fl = FlashLoader(config=config)

with pytest.raises(ValueError):
fl.read_dataframe()


def test_group_name_not_in_h5(config_file: dict):
config = config_file
config["dataframe"]["channels"]["dldPosX"]["group_name"] = "foo"
h5_path = "FLASH1_USER3_stream_2_run43878_file1_20230130T153807.1.h5"
fl = FlashLoader(config=config)

with pytest.raises(ValueError) as e:
fl.create_dataframe_per_file(config["core"]["paths"]["data_raw_dir"] + h5_path)
print(e)
assert str(e.value.args[0]) == "The group_name for channel dldPosX does not exist."


# def test_buffer_schema_mismatch(config_file: dict):
# fl = FlashLoader(config=config_file)

# fl.read_dataframe(runs=[43878])

# config = config_file
# config["dataframe"]["channels"]["dldPosX2"] = {
# "group_name": "/uncategorised/FLASH.EXP/HEXTOF.DAQ/DLD1/"}

# fl = FlashLoader(config=config)
# with pytest.raises(ValueError):
# fl.read_dataframe(runs=[43878])

0 comments on commit 8ed9b29

Please sign in to comment.