Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #515 from DiamondLightSource/487_log_no_diffraction
Browse files Browse the repository at this point in the history
Improve logging to ispyb when there is no diffraction
  • Loading branch information
DominicOram committed Feb 9, 2023
2 parents ef88106 + 45c739d commit 10f0765
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 57 deletions.
32 changes: 17 additions & 15 deletions fake_zocalo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
from pathlib import Path
from typing import Tuple

import ispyb.sqlalchemy
import pika
Expand All @@ -12,7 +13,7 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

NO_DIFFRACTION_ID = 1
NO_DIFFRACTION_PREFIX = "NO_DIFF"

DEV_ISPYB_CONFIG = "/dls_sw/dasc/mariadb/credentials/ispyb-dev.cfg"

Expand All @@ -22,20 +23,22 @@ def load_configuration_file(filename):
return conf


def get_dcgid(dcid: int, Session) -> int:
if dcid == NO_DIFFRACTION_ID:
return NO_DIFFRACTION_ID
def get_dcgid_and_prefix(dcid: int, Session) -> Tuple[int, str]:
try:
with Session() as session:
query = session.query(DataCollection).filter(
DataCollection.dataCollectionId == dcid
query = (
session.query(DataCollection)
.filter(DataCollection.dataCollectionId == dcid)
.first()
)
dcgid: int = query.first().dataCollectionGroupId
dcgid: int = query.dataCollectionGroupId
prefix: str = query.imagePrefix
except Exception as e:
print("Exception occured when reading from ISPyB database:\n")
print(e)
dcgid = 4
return dcgid
prefix = ""
return dcgid, prefix


def main():
Expand Down Expand Up @@ -81,8 +84,7 @@ def main():
"queue": "xrc.i03",
"exchange": "results",
"parameters": {
"dcid": str(NO_DIFFRACTION_ID),
"dcgid": str(NO_DIFFRACTION_ID),
"parameters": {"dcid": "2", "dcgid": "4"},
},
},
},
Expand All @@ -103,18 +105,18 @@ def on_request(ch: BlockingChannel, method, props, body):
print('Doing "processing"...')

dcid = message.get("parameters").get("ispyb_dcid")
print(f"getting dcgid for dcid {dcid} from ispyb:")
dcgid = get_dcgid(dcid, Session)
print(dcgid)
print(f"Getting info for dcid {dcid} from ispyb:")
dcgid, prefix = get_dcgid_and_prefix(dcid, Session)
print(f"Dcgid {dcgid} and prefix {prefix}")

time.sleep(3)
time.sleep(1)
print('Sending "results"...')
resultprops = BasicProperties(
delivery_mode=2,
headers={"workflows-recipe": True, "x-delivery-count": 1},
)

if message.get("parameters").get("ispyb_dcid") == NO_DIFFRACTION_ID:
if prefix == NO_DIFFRACTION_PREFIX:
result = no_diffraction_result
else:
result = single_crystal_result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def wait_for_results(self, fallback_xyz: Point3D) -> Point3D:
log_msg = (
f"Zocalo: No diffraction found, using fallback centre {fallback_xyz}"
)
self.ispyb.append_to_comment("Found no diffraction.")
xray_centre = fallback_xyz
LOGGER.warn(log_msg)

Expand Down
73 changes: 33 additions & 40 deletions src/artemis/external_interaction/system_tests/test_zocalo_system.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -28,56 +27,50 @@ def test_when_running_start_stop_then_get_expected_returned_results(zocalo_env):
assert zc.zocalo_interactor.wait_for_result(4) == Point3D(x=1.2, y=2.3, z=1.4)


@pytest.mark.s03
def test_zocalo_callback_calls_append_comment(zocalo_env):
params = FullParameters()
zc: FGSZocaloCallback = FGSCallbackCollection.from_params(params).zocalo_handler
zc.ispyb.append_to_comment = MagicMock()
dcids = [1, 2]
zc.ispyb.ispyb_ids = (dcids, 0, 4)
for dcid in dcids:
zc.zocalo_interactor.run_start(dcid)
for dcid in dcids:
zc.zocalo_interactor.run_end(dcid)
zc.wait_for_results(fallback_xyz=Point3D(0, 0, 0))
assert zc.ispyb.append_to_comment.call_count == 1
@pytest.fixture
def run_zocalo_with_dev_ispyb(dummy_params, dummy_ispyb_3d):
def inner(sample_name="", fallback=Point3D(0, 0, 0)):
dummy_params.detector_params.prefix = sample_name
zc: FGSZocaloCallback = FGSCallbackCollection.from_params(
dummy_params
).zocalo_handler
zc.ispyb.ispyb.ISPYB_CONFIG_PATH = dummy_ispyb_3d.ISPYB_CONFIG_PATH
zc.ispyb.ispyb_ids = zc.ispyb.ispyb.begin_deposition()
for dcid in zc.ispyb.ispyb_ids[0]:
zc.zocalo_interactor.run_start(dcid)
zc.stop({})
centre = zc.wait_for_results(fallback_xyz=fallback)
return zc, centre

return inner


@pytest.mark.s03
def test_given_a_result_with_no_diffraction_when_zocalo_called_then_move_to_fallback(
zocalo_env,
run_zocalo_with_dev_ispyb, zocalo_env
):
params = FullParameters()
NO_DIFFFRACTION_ID = 1
zc: FGSZocaloCallback = FGSCallbackCollection.from_params(params).zocalo_handler
zc.ispyb.append_to_comment = MagicMock()
dcids = [NO_DIFFFRACTION_ID, NO_DIFFFRACTION_ID]
zc.ispyb.ispyb_ids = (dcids, 0, NO_DIFFFRACTION_ID)
for dcid in dcids:
zc.zocalo_interactor.run_start(dcid)
for dcid in dcids:
zc.zocalo_interactor.run_end(dcid)
fallback = Point3D(1, 2, 3)
centre = zc.wait_for_results(fallback_xyz=fallback)
zc, centre = run_zocalo_with_dev_ispyb("NO_DIFF", fallback)
assert centre == fallback


@pytest.mark.s03
def test_zocalo_adds_nonzero_comment_time(zocalo_env, dummy_ispyb_3d, fetch_comment):
params = FullParameters()
zc: FGSZocaloCallback = FGSCallbackCollection.from_params(params).zocalo_handler
zc.ispyb.ispyb = dummy_ispyb_3d
zc.ispyb.ispyb_ids = zc.ispyb.ispyb.begin_deposition()
ispyb_numbers = zc.ispyb.ispyb_ids
assert ispyb_numbers[0] is not None
dcids = ispyb_numbers[0]
for dcid in dcids:
zc.zocalo_interactor.run_start(dcid)
zc.stop({})
zc.wait_for_results(fallback_xyz=Point3D(0, 0, 0))
zc.ispyb.ispyb.end_deposition("success", "")
def test_given_a_result_with_no_diffraction_ispyb_comment_updated(
run_zocalo_with_dev_ispyb, zocalo_env, fetch_comment
):
zc, centre = run_zocalo_with_dev_ispyb("NO_DIFF")

comment = fetch_comment(zc.ispyb.ispyb_ids[0][0])
assert "Found no diffraction." in comment


@pytest.mark.s03
def test_zocalo_adds_nonzero_comment_time(
run_zocalo_with_dev_ispyb, zocalo_env, fetch_comment
):
zc, centre = run_zocalo_with_dev_ispyb()

comment = fetch_comment(ispyb_numbers[0][0])
comment = fetch_comment(zc.ispyb.ispyb_ids[0][0])
assert comment[-29:-6] == "Zocalo processing took "
assert float(comment[-6:-2]) > 0
assert float(comment[-6:-2]) < 90
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def test_nexus_file_validity_for_zocalo_with_three_linked_datasets(
check_validity_through_zocalo(dummy_nexus_writers_with_more_images)


@pytest.mark.skip("Needs fixing in Nexgen")
@pytest.mark.skip(reason="Needs fixing in Nexgen")
def test_GIVEN_some_datafiles_outside_of_VDS_range_THEN_they_are_not_in_nexus_file(
dummy_nexus_writers_with_more_images: tuple[NexusWriter, NexusWriter]
):
Expand Down
2 changes: 1 addition & 1 deletion src/artemis/system_tests/test_fgs_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def fgs_composite():
return fast_grid_scan_composite


@pytest.mark.skip("Broken due to eiger issues in s03")
@pytest.mark.skip(reason="Broken due to eiger issues in s03")
@pytest.mark.s03
@patch("artemis.fast_grid_scan_plan.wait_for_fgs_valid")
@patch("bluesky.plan_stubs.wait")
Expand Down

0 comments on commit 10f0765

Please sign in to comment.