Skip to content

Commit

Permalink
Finalise LOQ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasarus committed Oct 10, 2024
1 parent 8f74e66 commit 9431635
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 54 deletions.
9 changes: 5 additions & 4 deletions rundetection/rules/loq_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def _is_sample_transmission_file(sans_file: SansFileData, sample_title: str) ->


def _is_sample_direct_file(sans_file: SansFileData) -> bool:
return "direct" in sans_file.title.lower() or "empty" in sans_file.title.lower() and sans_file.type == "TRANS"
return ("direct" in sans_file.title.lower() or "empty" in sans_file.title.lower()) and sans_file.type == "TRANS"


def _is_can_scatter_file(sans_file: SansFileData, can_title: str) -> bool:
Expand Down Expand Up @@ -150,17 +150,18 @@ def __init__(self, value: bool):

def verify(self, job_request: JobRequest) -> None:
# Expecting 3 values
if len(job_request.experiment_title.split("_")) < 3: # noqa: PLR2004
if len(job_request.experiment_title.split("_")) != 3: # noqa: PLR2004
job_request.will_reduce = False
logger.error(
f"Less than 3 sections to the experiment_title, probably missing Can Scatter title: "
f"Less or more than 3 sections to the experiment_title, probably missing Can Scatter title: "
f"{job_request.experiment_title}"
)
return
sample_title, can_title, ___ = job_request.experiment_title.split("_")
sans_files = create_list_of_files(job_request)
if sans_files == []:
logger.error("")
job_request.will_reduce = False
logger.error("No files found for this cycle excluding this run.")
return
sans_files = strip_excess_files(sans_files, scatter_run_number=job_request.run_number)

Expand Down
278 changes: 228 additions & 50 deletions test/rules/test_loq_rules.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import tempfile
from pathlib import Path
from unittest import mock

import pytest

from rundetection.job_requests import JobRequest
from rundetection.rules.loq_rules import (
LoqFindFiles,
LoqUserFile,
SansFileData,
_extract_cycle_from_file_path,
_extract_run_number_from_filename,
_find_can_scatter_file,
_find_can_trans_file,
_find_direct_file,
_find_trans_file,
_is_can_scatter_file,
_is_can_transmission_file,
_is_sample_direct_file,
_is_sample_transmission_file,
find_path_for_run_number,
grab_cycle_instrument_index,
strip_excess_files,
)

SANS_FILES = [
SansFileData(title="{direct/empty beam}", type="TRANS", run_number="-1"),
SansFileData(title="{Banana}", type="SANS/TRANS", run_number="0"),
SansFileData(title="{Banana}", type="TRANS", run_number="1"),
SansFileData(title="{Apple}", type="SANS/TRANS", run_number="2"),
SansFileData(title="{Apple}", type="TRANS", run_number="3"),
SansFileData(title="{direct beam}", type="TRANS", run_number="4")
]


@pytest.mark.parametrize(
("filename", "result"),
Expand All @@ -31,84 +54,239 @@ def test_extract_cycle_from_file_path(filename, result):


@pytest.mark.parametrize(
("file_title", "sample_title", "result"),
("sans_file", "sample_title", "result"),
[
("{Banana}_SANS/TRANS", "Banana", False),
("{Banana}_{}_SANS/TRANS", "Banana", False),
("{Banana}_TRANS", "Banana", True),
("{Banana}_SANS", "Banana", False),
("{Banana}_TRANS", "Apple", False),
(SansFileData(title="{Banana}", type="SANS/TRANS", run_number="0"), "Banana", False),
(SansFileData(title="{Banana}", type="TRANS", run_number="0"), "Banana", True),
(SansFileData(title="{Banana}", type="SANS", run_number="0"), "Banana", False),
(SansFileData(title="{Banana}", type="TRANS", run_number="0"), "Apple", False),
],
)
def test_is_sample_transmission_file(file_title, sample_title, result):
assert _is_sample_transmission_file(file_title, sample_title) == result
def test_is_sample_transmission_file(sans_file, sample_title, result):
assert _is_sample_transmission_file(sans_file, sample_title) == result


@pytest.mark.parametrize(
("file_title", "result"),
("sans_file", "result"),
[
("{Banana}_SANS/TRANS", False),
("{Banana direct}_{}_TRANS", False),
("{Banana direct}_TRANS", True),
("{Banana}_SANS", False),
("{Banana direct}_SANS/TRANS", False),
(SansFileData(title="{Banana}", type="TRANS", run_number="0"), False),
(SansFileData(title="{Banana direct}", type="SANS/TRANS", run_number="0"), False),
(SansFileData(title="{Banana direct}", type="TRANS", run_number="0"), True),
(SansFileData(title="{Banana empty}", type="TRANS", run_number="0"), True),
(SansFileData(title="{Banana direct}", type="SANS", run_number="0"), False),
],
)
def test_is_sample_direct_file(file_title, result):
assert _is_sample_direct_file(file_title) == result
def test_is_sample_direct_file(sans_file, result):
assert _is_sample_direct_file(sans_file) == result


@pytest.mark.parametrize(
("file_title", "can_title", "result"),
("sans_file", "can_title", "result"),
[
("{Banana}_SANS/TRANS", "{Banana}", True),
("{Banana}_SANS/TRANS", "{Apple}", False),
("{Banana}_{}_SANS/TRANS", "{Banana}", False),
("{Banana}_TRANS", "{Banana}", False),
("{Banana}_{}_TRANS", "{Banana}", False),
(SansFileData(title="{Banana}", type="SANS/TRANS", run_number="0"), "{Banana}", True),
(SansFileData(title="{Banana}", type="SANS/TRANS", run_number="0"), "{Apple}", False),
(SansFileData(title="{Banana}", type="TRANS", run_number="0"), "{Banana}", False),
(SansFileData(title="{Banana}_{}", type="TRANS", run_number="0"), "{Banana}", False),
],
)
def test_is_can_scatter_file(file_title, can_title, result):
assert _is_can_scatter_file(file_title, can_title) == result


def test_is_can_scatter_file_raises():
with pytest.raises(ValueError, match="The can title contains an _ character rundetection cannot handle that."):
_is_can_scatter_file("{Banana}_SANS", "CAN_SCATTER_TITLE")
def test_is_can_scatter_file(sans_file, can_title, result):
assert _is_can_scatter_file(sans_file, can_title) == result


# @pytest.mark.parametrize("")
def test_is_can_transmission_file():
pass
@pytest.mark.parametrize(
("sans_file", "can_title", "result"),
[
(SansFileData(title="{Banana}", type="SANS/TRANS", run_number="0"), "{Banana}", False),
(SansFileData(title="{Banana}", type="TRANS", run_number="0"), "{Apple}", False),
(SansFileData(title="{Banana}", type="TRANS", run_number="0"), "{Banana}", True),
],
)
def test_is_can_transmission_file(sans_file, can_title, result):
assert _is_can_transmission_file(sans_file, can_title) == result


def test_find_trans_file_success():
pass
@pytest.mark.parametrize(
("sans_files", "sample_title", "expected"),
[
(SANS_FILES, "{Apple}", SANS_FILES[4]),
(SANS_FILES, "{Banana}", SANS_FILES[2])
],
)
def test_find_trans_file_success(sans_files, sample_title, expected):
assert _find_trans_file(sans_files, sample_title) == expected


def test_find_trans_file_fail():
pass


def test_create_list_of_files():
pass
assert _find_trans_file(SANS_FILES, "{Lemmon}") is None


def test_find_direct_file():
pass
assert _find_direct_file(SANS_FILES) == SANS_FILES[-1]


def test_find_can_scatter_file():
pass
assert _find_can_scatter_file(SANS_FILES, "{Apple}") == SANS_FILES[3]


def test_can_trans_files():
pass


def test_loq_find_files_verify():
pass


def test_loq_user_file():
pass
assert _find_can_trans_file(SANS_FILES, "{Apple}") == SANS_FILES[4]


def test_path_for_run_number_with_some_zeros():
tempdir = tempfile.mkdtemp()
path = f"{tempdir}/LOQ0012345.nxs"
with Path(path).open("a"):
assert find_path_for_run_number(tempdir, 12345) == Path(path)


def test_path_for_run_number_with_no_zeros():
tempdir = tempfile.mkdtemp()
path = f"{tempdir}/LOQ12345.nxs"
with Path(path).open("a"):
assert find_path_for_run_number(tempdir, 12345) == Path(path)


def test_path_for_run_number_too_many_zeros():
tempdir = tempfile.mkdtemp()
with Path(f"{tempdir}/LOQ00000000000012345.nxs").open("a"):
assert find_path_for_run_number(tempdir, 12345) is None


def test_path_for_run_number_doesnt_exist():
tempdir = tempfile.mkdtemp()
assert find_path_for_run_number(tempdir, 12345) is None


def test_grab_cycle_instrument_index():
with mock.patch("rundetection.rules.loq_rules.requests") as requests:
cycle_index_text = grab_cycle_instrument_index("cycle_24_2")
assert cycle_index_text == requests.get.return_value.text
requests.get.assert_called_once_with("http://data.isis.rl.ac.uk/journals/ndxloq/journal_24_2.xml", timeout=5)


def test_strip_excess_files():
files = [
SansFileData(title="", type="", run_number="0"),
SansFileData(title="", type="", run_number="1"),
SansFileData(title="", type="", run_number="2")
]
new_list = strip_excess_files(files, 1)
assert new_list == [
SansFileData(title="", type="", run_number="0")
]


def test_loq_find_files_verify_title_too_long():
job_request = JobRequest(run_number=0, instrument="", experiment_title="too_long_problems_here", experiment_number="",
filepath=Path(), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
LoqFindFiles(value=True).verify(job_request)
assert job_request.will_reduce is False


def test_loq_find_files_verify_title_too_short():
job_request = JobRequest(run_number=0, instrument="", experiment_title="tooshortproblemshere", experiment_number="",
filepath=Path(), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
LoqFindFiles(value=True).verify(job_request)
assert job_request.will_reduce is False


def test_loq_find_files_verify_no_files_left():
job_request = JobRequest(run_number=0, instrument="", experiment_title="{}_{}_sans/trans", experiment_number="",
filepath=Path(), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
with mock.patch("rundetection.rules.loq_rules.create_list_of_files", return_value=[]):
loq_find_files = LoqFindFiles(value=True)
loq_find_files.verify(job_request)
assert job_request.will_reduce is False


def test_loq_find_files_verify_some_files_found_but_none_valid():
job_request = JobRequest(run_number=0, instrument="", experiment_title="{}_{}_sans/trans", experiment_number="",
filepath=Path("/path/cycle_24_2/LOQ.nxs"), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
with (mock.patch("rundetection.rules.loq_rules.create_list_of_files", return_value=[SansFileData("", "", "")]),
mock.patch("rundetection.rules.loq_rules.strip_excess_files",
return_value=[SansFileData("", "", ""), SansFileData("", "", ""), SansFileData("", "", "")])):
loq_find_files = LoqFindFiles(value=True)
loq_find_files.verify(job_request)
assert job_request.will_reduce is True
assert job_request.additional_values["run_number"] == 0


def test_loq_find_files_trans_file_found():
job_request = JobRequest(run_number=5, instrument="", experiment_title="{scatter}_{background}_sans/trans", experiment_number="",
filepath=Path("/path/cycle_24_2/LOQ.nxs"), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
with (mock.patch("rundetection.rules.loq_rules.create_list_of_files", return_value=[SansFileData("", "", "")]),
mock.patch("rundetection.rules.loq_rules.strip_excess_files",
return_value=[SansFileData(title="{scatter}", type="TRANS", run_number="1"),
SansFileData(title="{background}", type="TRANS", run_number="2"),
SansFileData(title="{direct}", type="SANS/TRANS", run_number="3")])):
loq_find_files = LoqFindFiles(value=True)
loq_find_files.verify(job_request)
assert job_request.will_reduce is True
assert job_request.additional_values["run_number"] == 5 # noqa: PLR2004
assert job_request.additional_values["scatter_transmission"] == "1"


def test_loq_find_files_can_transmission_file_found():
job_request = JobRequest(run_number=5, instrument="", experiment_title="{scatter}_{background}_sans/trans", experiment_number="",
filepath=Path("/path/cycle_24_2/LOQ.nxs"), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
with (mock.patch("rundetection.rules.loq_rules.create_list_of_files", return_value=[SansFileData("", "", "")]),
mock.patch("rundetection.rules.loq_rules.strip_excess_files",
return_value=[SansFileData(title="{scatter}", type="TRANS", run_number="1"),
SansFileData(title="{background}", type="SANS/TRANS", run_number="2"),
SansFileData(title="{background}", type="TRANS", run_number="3"),
SansFileData(title="{direct}", type="SANS/TRANS", run_number="4")])):
loq_find_files = LoqFindFiles(value=True)
loq_find_files.verify(job_request)
assert job_request.will_reduce is True
assert job_request.additional_values["run_number"] == 5 # noqa: PLR2004
assert job_request.additional_values["can_transmission"] == "3"


def test_loq_find_files_direct_file_found():
job_request = JobRequest(run_number=5, instrument="", experiment_title="{scatter}_{background}_sans/trans", experiment_number="",
filepath=Path("/path/cycle_24_2/LOQ.nxs"), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
with (mock.patch("rundetection.rules.loq_rules.create_list_of_files", return_value=[SansFileData("", "", "")]),
mock.patch("rundetection.rules.loq_rules.strip_excess_files",
return_value=[SansFileData(title="{scatter}", type="TRANS", run_number="1"),
SansFileData(title="{background}", type="SANS/TRANS", run_number="2"),
SansFileData(title="{background}", type="TRANS", run_number="3"),
SansFileData(title="{direct}", type="TRANS", run_number="4")])):
loq_find_files = LoqFindFiles(value=True)
loq_find_files.verify(job_request)
assert job_request.will_reduce is True
assert job_request.additional_values["run_number"] == 5 # noqa: PLR2004
assert job_request.additional_values["scatter_direct"] == "4"
assert job_request.additional_values["can_direct"] == "4"


def test_loq_find_files_can_scatter_file_found():
job_request = JobRequest(run_number=5, instrument="", experiment_title="{scatter}_{background}_sans/trans", experiment_number="",
filepath=Path("/path/cycle_24_2/LOQ.nxs"), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
with (mock.patch("rundetection.rules.loq_rules.create_list_of_files", return_value=[SansFileData("", "", "")]),
mock.patch("rundetection.rules.loq_rules.strip_excess_files",
return_value=[SansFileData(title="{scatter}", type="TRANS", run_number="1"),
SansFileData(title="{background}", type="SANS/TRANS", run_number="2"),
SansFileData(title="{background}", type="TRANS", run_number="3"),
SansFileData(title="{direct}", type="SANS/TRANS", run_number="4")])):
loq_find_files = LoqFindFiles(value=True)
loq_find_files.verify(job_request)
assert job_request.will_reduce is True
assert job_request.additional_values["run_number"] == 5 # noqa: PLR2004
assert job_request.additional_values["can_scatter"] == "2"


def test_loq_user_file_():
job_request = JobRequest(run_number=0, instrument="", experiment_title="", experiment_number="",
filepath=Path(), run_start="", run_end="", raw_frames=0, good_frames=0, users="",
will_reduce=True, additional_values={}, additional_requests=[])
LoqUserFile(value="loq_user_file").verify(job_request)
assert job_request.additional_values["user_file"] == "/extras/loq/loq_user_file"
assert len(job_request.additional_values) == 1

0 comments on commit 9431635

Please sign in to comment.