Skip to content

Commit 76b0497

Browse files
committed
Some tests of the search map function
1 parent 82ee1b1 commit 76b0497

File tree

2 files changed

+210
-2
lines changed

2 files changed

+210
-2
lines changed

src/murfey/workflows/tomo/tomo_metadata.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def register_search_map_in_database(
2929
.where(DataCollectionGroup.tag == search_map_params.tag)
3030
).one()
3131
try:
32+
# See if there is already a search map with this name and update if so
3233
search_map = murfey_db.exec(
3334
select(SearchMap)
3435
.where(SearchMap.name == search_map_name)
@@ -105,6 +106,7 @@ def register_search_map_in_database(
105106
else:
106107
# mock up response so that below still works
107108
sm_ispyb_response = {"success": False, "return_value": None}
109+
# Register new search map
108110
search_map = SearchMap(
109111
id=(
110112
sm_ispyb_response["return_value"]
@@ -161,6 +163,7 @@ def register_search_map_in_database(
161163
dcg.atlas_pixel_size,
162164
]
163165
):
166+
# Work out the shifted positions if all required information is present
164167
reference_shift_matrix = np.array(
165168
[
166169
[
@@ -195,12 +198,14 @@ def register_search_map_in_database(
195198
),
196199
)
197200

201+
# Flip positions based on camera type
198202
camera = getattr(Camera, machine_config.camera)
199203
if camera == Camera.K3_FLIPY:
200204
corrected_vector = np.matmul(np.array([[1, 0], [0, -1]]), corrected_vector)
201-
elif camera == Camera.K3_FLIPX or Camera.FALCON:
202-
corrected_vector = -1 * corrected_vector
205+
elif camera == Camera.K3_FLIPX:
206+
corrected_vector = np.matmul(np.array([[-1, 0], [0, 1]]), corrected_vector)
203207

208+
# Convert from metres to pixels
204209
search_map_params.height_on_atlas = int(
205210
search_map.height * search_map.pixel_size / dcg.atlas_pixel_size
206211
)
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
from unittest import mock
2+
3+
from sqlmodel import Session, select
4+
5+
from murfey.util.db import DataCollectionGroup, SearchMap
6+
from murfey.util.models import SearchMapParameters
7+
from murfey.workflows.tomo import tomo_metadata
8+
from tests.conftest import ExampleVisit
9+
10+
11+
@mock.patch("murfey.workflows.tomo.tomo_metadata._transport_object")
12+
def test_register_search_map_update_with_dimensions(
13+
mock_transport, murfey_db_session: Session
14+
):
15+
"""Test the updating of an existing grid square"""
16+
# Create a grid square to update
17+
search_map = SearchMap(
18+
id=1,
19+
name="SearchMap_1",
20+
session_id=ExampleVisit.murfey_session_id,
21+
tag="session_tag",
22+
x_stage_position=0.1,
23+
y_stage_position=0.2,
24+
)
25+
murfey_db_session.add(search_map)
26+
murfey_db_session.commit()
27+
28+
# Make sure DCG is present with a pixel size
29+
dcg = DataCollectionGroup(
30+
id=1,
31+
session_id=ExampleVisit.murfey_session_id,
32+
tag="session_tag",
33+
atlas_id=90,
34+
atlas_pixel_size=1e-5,
35+
)
36+
murfey_db_session.add(dcg)
37+
murfey_db_session.commit()
38+
39+
# Parameters to update with
40+
new_parameters = SearchMapParameters(
41+
tag="session_tag",
42+
width=2000,
43+
height=4000,
44+
)
45+
46+
# Run the registration
47+
tomo_metadata.register_search_map_in_database(
48+
ExampleVisit.murfey_session_id, "SearchMap_1", new_parameters, murfey_db_session
49+
)
50+
51+
# Check this would have updated ispyb
52+
mock_transport.do_update_search_map.assert_called_with(1, new_parameters)
53+
54+
# Confirm the database was updated
55+
sm_final_parameters = murfey_db_session.exec(select(SearchMap)).one()
56+
assert sm_final_parameters.width == new_parameters.width
57+
assert sm_final_parameters.height == new_parameters.height
58+
assert sm_final_parameters.x_stage_position == 0.1
59+
assert sm_final_parameters.y_stage_position == 0.2
60+
assert sm_final_parameters.x_location is None
61+
62+
63+
@mock.patch("murfey.workflows.tomo.tomo_metadata._transport_object")
64+
def test_register_search_map_update_with_all_parameters(
65+
mock_transport, murfey_db_session: Session
66+
):
67+
"""Test the updating of an existing grid square"""
68+
# Create a grid square to update
69+
search_map = SearchMap(
70+
id=1,
71+
name="SearchMap_1",
72+
session_id=ExampleVisit.murfey_session_id,
73+
tag="session_tag",
74+
x_stage_position=0.1,
75+
y_stage_position=0.2,
76+
width=2000,
77+
height=4000,
78+
)
79+
murfey_db_session.add(search_map)
80+
murfey_db_session.commit()
81+
82+
# Make sure DCG is present with a pixel size
83+
dcg = DataCollectionGroup(
84+
id=1,
85+
session_id=ExampleVisit.murfey_session_id,
86+
tag="session_tag",
87+
atlas_id=90,
88+
atlas_pixel_size=1e-5,
89+
)
90+
murfey_db_session.add(dcg)
91+
murfey_db_session.commit()
92+
93+
# Parameters to update with
94+
new_parameters = SearchMapParameters(
95+
tag="session_tag",
96+
x_stage_position=0.3,
97+
y_stage_position=0.4,
98+
pixel_size=1e-7,
99+
image="path/to/image",
100+
binning=1,
101+
reference_matrix_m11=1.01,
102+
reference_matrix_m12=0.01,
103+
reference_matrix_m21=0.02,
104+
reference_matrix_m22=1.02,
105+
stage_correction_m11=0.99,
106+
stage_correction_m12=-0.01,
107+
stage_correction_m21=-0.02,
108+
stage_correction_m22=0.98,
109+
image_shift_correction_m11=1.03,
110+
image_shift_correction_m12=0.03,
111+
image_shift_correction_m21=-0.03,
112+
image_shift_correction_m22=0.97,
113+
)
114+
115+
# Run the registration
116+
tomo_metadata.register_search_map_in_database(
117+
ExampleVisit.murfey_session_id, "SearchMap_1", new_parameters, murfey_db_session
118+
)
119+
120+
# Confirm the database was updated
121+
sm_final_parameters = murfey_db_session.exec(select(SearchMap)).one()
122+
assert sm_final_parameters.width == new_parameters.width
123+
assert sm_final_parameters.height == new_parameters.height
124+
assert sm_final_parameters.x_stage_position == 0.3
125+
assert sm_final_parameters.y_stage_position == 0.4
126+
assert sm_final_parameters.pixel_size == 0.1
127+
assert sm_final_parameters.image == "path/to/image"
128+
assert sm_final_parameters.binning == 1
129+
assert sm_final_parameters.reference_matrix_m11 == 1.01
130+
assert sm_final_parameters.reference_matrix_m12 == 0.01
131+
assert sm_final_parameters.reference_matrix_m21 == 0.02
132+
assert sm_final_parameters.reference_matrix_m22 == 1.02
133+
assert sm_final_parameters.stage_correction_m11 == 0.99
134+
assert sm_final_parameters.stage_correction_m12 == -0.01
135+
assert sm_final_parameters.stage_correction_m21 == -0.02
136+
assert sm_final_parameters.stage_correction_m22 == 0.98
137+
assert sm_final_parameters.image_shift_correction_m11 == 1.03
138+
assert sm_final_parameters.image_shift_correction_m12 == 0.03
139+
assert sm_final_parameters.image_shift_correction_m21 == -0.03
140+
assert sm_final_parameters.image_shift_correction_m22 == 0.97
141+
142+
# These two should have been updated, but what that update should be is messy
143+
assert sm_final_parameters.x_location is not None
144+
assert sm_final_parameters.y_location is not None
145+
146+
# Check this would have updated ispyb
147+
mock_transport.do_update_search_map.assert_called_with(1, new_parameters)
148+
new_parameters.x_location = sm_final_parameters.x_location
149+
new_parameters.y_location = sm_final_parameters.y_location
150+
new_parameters.height_on_atlas = 40
151+
new_parameters.width_on_atlas = 20
152+
mock_transport.do_update_search_map.assert_called_with(1, new_parameters)
153+
154+
155+
@mock.patch("murfey.workflows.tomo.tomo_metadata._transport_object")
156+
def test_register_search_map_insert_with_ispyb(
157+
mock_transport, murfey_db_session: Session, tmp_path
158+
):
159+
# Create a data collection group for lookups
160+
dcg = DataCollectionGroup(
161+
id=1,
162+
session_id=ExampleVisit.murfey_session_id,
163+
tag="session_tag",
164+
atlas_id=90,
165+
atlas_pixel_size=1e-5,
166+
)
167+
murfey_db_session.add(dcg)
168+
murfey_db_session.commit()
169+
170+
# Set the ispyb return
171+
mock_transport.do_insert_search_map.return_value = {
172+
"return_value": 1,
173+
"success": True,
174+
}
175+
176+
# Parameters to update with
177+
new_parameters = SearchMapParameters(
178+
tag="session_tag",
179+
x_stage_position=1.3,
180+
y_stage_position=1.4,
181+
pixel_size=1.02,
182+
)
183+
184+
# Run the registration
185+
tomo_metadata.register_search_map_in_database(
186+
ExampleVisit.murfey_session_id, "SearchMap_1", new_parameters, murfey_db_session
187+
)
188+
189+
# Check this would have updated ispyb
190+
mock_transport.do_insert_search_map.assert_called_with(
191+
90, "SearchMap_1", new_parameters
192+
)
193+
194+
# Confirm the database entry was made
195+
sm_final_parameters = murfey_db_session.exec(select(SearchMap)).one()
196+
assert sm_final_parameters.id == 1
197+
assert sm_final_parameters.name == "SearchMap_1"
198+
assert sm_final_parameters.session_id == ExampleVisit.murfey_session_id
199+
assert sm_final_parameters.tag == "session_tag"
200+
assert sm_final_parameters.x_stage_position == 1.3
201+
assert sm_final_parameters.y_stage_position == 1.4
202+
assert sm_final_parameters.pixel_size == 1.02
203+
assert sm_final_parameters.x_location is None

0 commit comments

Comments
 (0)