Skip to content

Commit 9d375c2

Browse files
Match grid squares on sample rather than tag if sample known (#827)
There is a problem with registering grid squares from an atlas, as the dcg tag is being used as the matching but this can change if atlas converts to SPA. Instead, when registering squares from an atlas file, provide the sample and match this to a dcg. And send dcg request before that to make sure one will exist.
1 parent 6d93c78 commit 9d375c2

5 files changed

Lines changed: 78 additions & 26 deletions

File tree

src/murfey/client/contexts/atlas.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ def post_transfer_epu(
167167
except KeyError:
168168
logger.info("Unable to read grid square locations from Atlas.dm")
169169
return
170+
for p in transferred_file.parts:
171+
if p.startswith("Sample"):
172+
sample = int(p.replace("Sample", ""))
173+
break
174+
else:
175+
logger.warning(f"Sample could not be identified for {transferred_file}")
176+
return
177+
178+
# Make sure a dcg is requested before doing grid squares
179+
capture_post(
180+
base_url=str(environment.url.geturl()),
181+
router_name="workflow.router",
182+
function_name="register_dc_group",
183+
token=self._token,
184+
instrument_name=environment.instrument_name,
185+
visit_name=environment.visit,
186+
session_id=environment.murfey_session,
187+
data={
188+
"experiment_type_id": 44, # Atlas
189+
"tag": str(transferred_file.parent),
190+
"sample": sample,
191+
},
192+
)
193+
# Register all grid squares on this atlas
170194
for gs, pos_data in gs_pix_positions.items():
171195
if pos_data:
172196
capture_post(
@@ -179,6 +203,7 @@ def post_transfer_epu(
179203
gsid=int(gs),
180204
data={
181205
"tag": str(transferred_file.parent),
206+
"sample": sample,
182207
"x_location": pos_data[0],
183208
"y_location": pos_data[1],
184209
"x_stage_position": pos_data[2],
@@ -188,16 +213,8 @@ def post_transfer_epu(
188213
"angle": pos_data[6],
189214
},
190215
)
216+
# Register atlas in smartem
191217
if gs_pix_positions:
192-
for p in transferred_file.parts:
193-
if p.startswith("Sample"):
194-
sample = int(p.replace("Sample", ""))
195-
break
196-
else:
197-
logger.warning(
198-
f"Sample could not be identified for {transferred_file}"
199-
)
200-
return
201218
capture_post(
202219
base_url=str(environment.url.geturl()),
203220
router_name="session_control.spa_router",

src/murfey/util/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class Base(BaseModel):
272272
class GridSquareParameters(BaseModel):
273273
tag: str
274274
image: str = ""
275+
sample: int | None = None
275276

276277
# Actual coordinates for image centre in real space
277278
x_location: Optional[float] = None

src/murfey/workflows/spa/flush_spa_preprocess.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,22 @@ def register_grid_square(
6565
if grid_square_params.width is not None:
6666
grid_square_params.width_scaled = int(grid_square_params.width / 7.8)
6767

68+
if grid_square_params.sample is not None:
69+
dcg = murfey_db.exec(
70+
select(DataCollectionGroup)
71+
.where(DataCollectionGroup.session_id == session_id)
72+
.where(DataCollectionGroup.sample == grid_square_params.sample)
73+
).one()
74+
else:
75+
dcg = murfey_db.exec(
76+
select(DataCollectionGroup)
77+
.where(DataCollectionGroup.session_id == session_id)
78+
.where(DataCollectionGroup.tag == grid_square_params.tag)
79+
).one()
6880
grid_square_query = murfey_db.exec(
6981
select(GridSquare)
7082
.where(GridSquare.name == gsid)
71-
.where(GridSquare.tag == grid_square_params.tag)
83+
.where(GridSquare.tag == dcg.tag)
7284
.where(GridSquare.session_id == session_id)
7385
).all()
7486
if grid_square_query:
@@ -101,11 +113,6 @@ def register_grid_square(
101113
else:
102114
# No existing grid square in the murfey database
103115
if _transport_object:
104-
dcg = murfey_db.exec(
105-
select(DataCollectionGroup)
106-
.where(DataCollectionGroup.session_id == session_id)
107-
.where(DataCollectionGroup.tag == grid_square_params.tag)
108-
).one()
109116
gs_ispyb_response = _transport_object.do_insert_grid_square(
110117
dcg.atlas_id, gsid, grid_square_params
111118
)
@@ -149,12 +156,7 @@ def register_grid_square(
149156
instrument_name=murfey_session.instrument_name
150157
)[murfey_session.instrument_name]
151158
if machine_config.smartem_api_url:
152-
dcg = murfey_db.exec(
153-
select(DataCollectionGroup)
154-
.where(DataCollectionGroup.session_id == session_id)
155-
.where(DataCollectionGroup.tag == grid_square_params.tag)
156-
).one_or_none()
157-
if dcg and dcg.smartem_grid_uuid:
159+
if dcg.smartem_grid_uuid:
158160
secured_grid_square_image_path_full_res: Path | None = None
159161
if grid_square_params.image:
160162
secured_grid_square_image_path_full_res = secure_path(

tests/client/contexts/test_atlas.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,21 @@ def test_atlas_context_dm(mock_capture_post, tmp_path):
139139
context = AtlasContext("tomo", tmp_path, {}, "token")
140140
context.post_transfer(atlas_dm, environment=env)
141141

142-
assert mock_capture_post.call_count == 5
142+
assert mock_capture_post.call_count == 6
143+
mock_capture_post.assert_any_call(
144+
base_url="http://localhost:8000",
145+
router_name="workflow.router",
146+
function_name="register_dc_group",
147+
token="token",
148+
instrument_name="m01",
149+
visit_name="cm12345-6",
150+
session_id=1,
151+
data={
152+
"experiment_type_id": 44, # Atlas
153+
"tag": str(atlas_dm.parent),
154+
"sample": 2,
155+
},
156+
)
143157
mock_capture_post.assert_any_call(
144158
base_url="http://localhost:8000",
145159
router_name="session_control.spa_router",
@@ -150,6 +164,7 @@ def test_atlas_context_dm(mock_capture_post, tmp_path):
150164
gsid=101,
151165
data={
152166
"tag": str(atlas_dm.parent),
167+
"sample": 2,
153168
"x_location": 1200,
154169
"y_location": 1500,
155170
"x_stage_position": 2e9,

tests/workflows/spa/test_flush_spa_preprocess.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ def test_register_grid_square_update_add_locations(
1414
):
1515
"""Test the updating of an existing grid square"""
1616
# Create a grid square to update
17+
dcg = DataCollectionGroup(
18+
id=1,
19+
session_id=ExampleVisit.murfey_session_id,
20+
tag="session_tag",
21+
atlas_id=90,
22+
sample=2,
23+
)
24+
murfey_db_session.add(dcg)
1725
grid_square = GridSquare(
1826
id=1,
1927
name=101,
@@ -58,6 +66,14 @@ def test_register_grid_square_update_add_nothing(
5866
):
5967
"""Test the updating of an existing grid square, but with nothing to update with"""
6068
# Create a grid square to update
69+
dcg = DataCollectionGroup(
70+
id=1,
71+
session_id=ExampleVisit.murfey_session_id,
72+
tag="session_tag",
73+
atlas_id=90,
74+
sample=2,
75+
)
76+
murfey_db_session.add(dcg)
6177
grid_square = GridSquare(
6278
id=1,
6379
name=101,
@@ -71,8 +87,8 @@ def test_register_grid_square_update_add_nothing(
7187
murfey_db_session.add(grid_square)
7288
murfey_db_session.commit()
7389

74-
# Parameters to update with
75-
new_parameters = GridSquareParameters(tag="session_tag")
90+
# Parameters to update with - use a new tag but same sample
91+
new_parameters = GridSquareParameters(tag="atlas_tag", sample=2)
7692

7793
# Run the registration
7894
flush_spa_preprocess.register_grid_square(
@@ -95,13 +111,14 @@ def test_register_grid_square_insert_with_ispyb(
95111
mock_transport, murfey_db_session: Session, tmp_path
96112
):
97113
# Create a data collection group for lookups
98-
grid_square = DataCollectionGroup(
114+
dcg = DataCollectionGroup(
99115
id=1,
100116
session_id=ExampleVisit.murfey_session_id,
101117
tag="session_tag",
102118
atlas_id=90,
119+
sample=2,
103120
)
104-
murfey_db_session.add(grid_square)
121+
murfey_db_session.add(dcg)
105122
murfey_db_session.commit()
106123

107124
# Set the ispyb return

0 commit comments

Comments
 (0)