-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_workflow.py
More file actions
512 lines (466 loc) · 16.2 KB
/
Copy pathtest_workflow.py
File metadata and controls
512 lines (466 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from pathlib import Path
from unittest import mock
from unittest.mock import MagicMock
import numpy as np
import PIL.Image
import pytest
from pytest_mock import MockerFixture
from sqlmodel import Session, select
from murfey.server.api.workflow import (
DCGroupParameters,
MillingParameters,
make_gif,
register_dc_group,
)
from murfey.util.db import DataCollectionGroup, SearchMap
from tests.conftest import ExampleVisit
@mock.patch("murfey.server.api.workflow._transport_object")
def test_register_dc_group_new_dcg(mock_transport, murfey_db_session: Session):
"""Test the request for a completely new data collection group"""
mock_transport.feedback_queue = "mock_feedback_queue"
# Request new dcg registration
dcg_params = DCGroupParameters(
experiment_type_id=44,
tag="/path/to/Sample10/Atlas",
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
sample=10,
atlas_pixel_size=1e-5,
)
register_dc_group(
visit_name="cm12345-6",
session_id=ExampleVisit.murfey_session_id,
dcg_params=dcg_params,
db=murfey_db_session,
)
# Check request for registering dcg in ispyb and murfey
mock_transport.send.assert_called_once_with(
"mock_feedback_queue",
{
"register": "data_collection_group",
"start_time": mock.ANY,
"experiment_type_id": 44,
"tag": "/path/to/Sample10/Atlas",
"session_id": ExampleVisit.murfey_session_id,
"atlas": "/path/to/Sample10/Atlas/Atlas_1.jpg",
"sample": 10,
"atlas_pixel_size": 1e-5,
"microscope": "",
"proposal_code": ExampleVisit.proposal_code,
"proposal_number": str(ExampleVisit.proposal_number),
"visit_number": str(ExampleVisit.visit_number),
},
)
@mock.patch("murfey.server.api.workflow._transport_object")
def test_register_dc_group_atlas_to_processing(
mock_transport, murfey_db_session: Session
):
"""
Test the request to update an existing data collection group
from atlas type 44 to a processing type with a different tag
"""
mock_transport.feedback_queue = "mock_feedback_queue"
# Add a processing dcg to ensure this is not touched
proc_dcg = DataCollectionGroup(
id=1,
session_id=ExampleVisit.murfey_session_id,
tag="initial_processing_tag",
atlas_id=90,
atlas_pixel_size=1e-5,
sample=10,
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
)
murfey_db_session.add(proc_dcg)
# Make sure dcg is present for update
dcg = DataCollectionGroup(
id=2,
session_id=ExampleVisit.murfey_session_id,
tag="/path/to/Sample10/Atlas",
atlas_id=90,
atlas_pixel_size=1e-5,
sample=10,
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
)
murfey_db_session.add(dcg)
murfey_db_session.commit()
# Request new dcg registration with processing experiment type and tag
dcg_params = DCGroupParameters(
experiment_type_id=36,
tag="processing_tag",
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
sample=10,
atlas_pixel_size=1e-5,
)
register_dc_group(
visit_name="cm12345-6",
session_id=ExampleVisit.murfey_session_id,
dcg_params=dcg_params,
db=murfey_db_session,
)
# Check request to ispyb for updating the experiment type
mock_transport.send.assert_called_once_with(
"mock_feedback_queue",
{
"register": "experiment_type_update",
"experiment_type_id": 36,
"dcgid": 2,
},
)
# Check that the tag of the data collection group was updated
initial_dcg = murfey_db_session.exec(
select(DataCollectionGroup).where(DataCollectionGroup.id == 1)
).one()
assert initial_dcg.tag == "initial_processing_tag"
new_dcg = murfey_db_session.exec(
select(DataCollectionGroup).where(DataCollectionGroup.id == 2)
).one()
assert new_dcg.tag == "processing_tag"
@mock.patch("murfey.server.api.workflow._transport_object")
def test_register_dc_group_processing_to_atlas(
mock_transport, murfey_db_session: Session
):
"""
Test the request to update an existing data collection group
of processing type with a new atlas type 44, which should leave the tag unchanged
"""
mock_transport.feedback_queue = "mock_feedback_queue"
# Make sure dcg is present
dcg = DataCollectionGroup(
id=1,
session_id=ExampleVisit.murfey_session_id,
tag="processing_tag",
atlas_id=90,
atlas_pixel_size=1e-5,
sample=10,
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
)
murfey_db_session.add(dcg)
second_dcg = DataCollectionGroup(
id=2,
session_id=ExampleVisit.murfey_session_id,
tag="second_processing_tag",
atlas_id=90,
atlas_pixel_size=1e-5,
sample=10,
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
)
murfey_db_session.add(second_dcg)
murfey_db_session.commit()
# Request new dcg registration with atlas experiment type and tag
dcg_params = DCGroupParameters(
experiment_type_id=44,
tag="/path/to/Sample10/Atlas",
atlas="/path/to/Sample10/Atlas/Atlas_2.jpg",
sample=10,
atlas_pixel_size=1e-4,
)
register_dc_group(
visit_name="cm12345-6",
session_id=ExampleVisit.murfey_session_id,
dcg_params=dcg_params,
db=murfey_db_session,
)
# Check request to ispyb for updating the experiment type
assert mock_transport.send.call_count == 2
mock_transport.send.assert_any_call(
"mock_feedback_queue",
{
"register": "atlas_update",
"atlas_id": 90,
"atlas": "/path/to/Sample10/Atlas/Atlas_2.jpg",
"sample": 10,
"atlas_pixel_size": 1e-4,
"dcgid": 1,
"session_id": ExampleVisit.murfey_session_id,
"tag": "processing_tag",
},
)
mock_transport.send.assert_any_call(
"mock_feedback_queue",
{
"register": "atlas_update",
"atlas_id": 90,
"atlas": "/path/to/Sample10/Atlas/Atlas_2.jpg",
"sample": 10,
"atlas_pixel_size": 1e-4,
"dcgid": 2,
"session_id": ExampleVisit.murfey_session_id,
"tag": "second_processing_tag",
},
)
# Check the data collection group atlas was updated
new_dcg = murfey_db_session.exec(
select(DataCollectionGroup).where(DataCollectionGroup.id == 1)
).one()
second_new_dcg = murfey_db_session.exec(
select(DataCollectionGroup).where(DataCollectionGroup.id == 1)
).one()
assert new_dcg.atlas == "/path/to/Sample10/Atlas/Atlas_2.jpg"
assert new_dcg.atlas_pixel_size == 1e-4
assert second_new_dcg.atlas == "/path/to/Sample10/Atlas/Atlas_2.jpg"
assert second_new_dcg.atlas_pixel_size == 1e-4
# Check the tag of the data collection group was not updated
assert new_dcg.tag != "/path/to/Sample10/Atlas"
assert second_new_dcg.tag != "/path/to/Sample10/Atlas"
@mock.patch("murfey.server.api.workflow._transport_object")
def test_register_dc_group_new_dcg_old_atlas(
mock_transport, murfey_db_session: Session
):
"""
Test the request to register a new processing type data collection group
in the case where there is already one for that atlas
"""
mock_transport.feedback_queue = "mock_feedback_queue"
# Make sure dcg is present
dcg = DataCollectionGroup(
id=1,
session_id=ExampleVisit.murfey_session_id,
tag="processing_tag",
atlas_id=90,
atlas_pixel_size=1e-5,
sample=10,
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
)
murfey_db_session.add(dcg)
murfey_db_session.commit()
# Request new dcg registration with atlas experiment type and new processing tag
dcg_params = DCGroupParameters(
experiment_type_id=37,
tag="second_processing_tag",
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
sample=10,
atlas_pixel_size=1e-5,
)
register_dc_group(
visit_name="cm12345-6",
session_id=ExampleVisit.murfey_session_id,
dcg_params=dcg_params,
db=murfey_db_session,
)
# Check request for registering dcg in ispyb and murfey
mock_transport.send.assert_called_once_with(
"mock_feedback_queue",
{
"register": "data_collection_group",
"start_time": mock.ANY,
"experiment_type_id": 37,
"tag": "second_processing_tag",
"session_id": ExampleVisit.murfey_session_id,
"atlas": "/path/to/Sample10/Atlas/Atlas_1.jpg",
"sample": 10,
"atlas_pixel_size": 1e-5,
"microscope": "",
"proposal_code": ExampleVisit.proposal_code,
"proposal_number": str(ExampleVisit.proposal_number),
"visit_number": str(ExampleVisit.visit_number),
},
)
@mock.patch("murfey.server.api.workflow._transport_object")
def test_register_dc_group_new_atlas(mock_transport, murfey_db_session: Session):
"""
Test the request to update an existing data collection group
by adding an atlas, using the same tag
"""
mock_transport.feedback_queue = "mock_feedback_queue"
mock_transport.do_insert_atlas.return_value = {"return_value": 5}
# Make sure dcg is present without an atlas id
dcg = DataCollectionGroup(
id=1,
session_id=ExampleVisit.murfey_session_id,
tag="processing_tag",
)
murfey_db_session.add(dcg)
murfey_db_session.commit()
# Request new dcg registration with atlas and exisiting tag
dcg_params = DCGroupParameters(
experiment_type_id=36,
tag="processing_tag",
atlas="/path/to/Sample10/Atlas/Atlas_2.jpg",
sample=10,
atlas_pixel_size=1e-4,
)
register_dc_group(
visit_name="cm12345-6",
session_id=ExampleVisit.murfey_session_id,
dcg_params=dcg_params,
db=murfey_db_session,
)
# Check no sends are made by the transport object
mock_transport.send.assert_not_called()
# Check the call to insert the atlas into ispyb
atlas_args = mock_transport.do_insert_atlas.call_args_list
assert len(atlas_args) == 1
assert atlas_args[0][0][0].dataCollectionGroupId == 1
assert atlas_args[0][0][0].atlasImage == "/path/to/Sample10/Atlas/Atlas_2.jpg"
assert atlas_args[0][0][0].pixelSize == 1e-4
assert atlas_args[0][0][0].cassetteSlot == 10
# Check the data collection group atlas was updated
new_dcg = murfey_db_session.exec(
select(DataCollectionGroup).where(DataCollectionGroup.id == 1)
).one()
assert new_dcg.atlas == "/path/to/Sample10/Atlas/Atlas_2.jpg"
assert new_dcg.sample == 10
assert new_dcg.atlas_pixel_size == 1e-4
assert new_dcg.tag == "processing_tag"
assert new_dcg.atlas_id == 5
@mock.patch("murfey.server.api.workflow._transport_object")
@mock.patch("murfey.server.api.workflow.register_search_map_in_database")
def test_register_dc_group_new_atlas_with_searchmaps(
mock_register_search_map, mock_transport, murfey_db_session: Session
):
"""
Test the request to update an existing data collection group
by adding an atlas, using the same tag, and also update search maps
"""
mock_transport.feedback_queue = "mock_feedback_queue"
# Make sure dcg is present with an atlas id
dcg = DataCollectionGroup(
id=1,
session_id=ExampleVisit.murfey_session_id,
tag="processing_tag",
atlas_id=90,
atlas_pixel_size=1e-5,
sample=10,
atlas="/path/to/Sample10/Atlas/Atlas_1.jpg",
)
murfey_db_session.add(dcg)
murfey_db_session.commit()
# Add some search maps with the dcg tag and one with a different tag
sm1 = SearchMap(
id=1,
session_id=ExampleVisit.murfey_session_id,
tag="processing_tag",
name="searchmap1",
)
sm2 = SearchMap(
id=2,
session_id=ExampleVisit.murfey_session_id,
tag="processing_tag",
name="searchmap2",
)
sm3 = SearchMap(
id=3,
session_id=ExampleVisit.murfey_session_id,
tag="different_tag",
name="searchmap3",
)
murfey_db_session.add(sm1)
murfey_db_session.add(sm2)
murfey_db_session.add(sm3)
murfey_db_session.commit()
# Request new dcg registration with new atlas tag and sample
dcg_params = DCGroupParameters(
experiment_type_id=37,
tag="processing_tag",
atlas="/path/to/Sample12/Atlas/Atlas_2.jpg",
sample=12,
atlas_pixel_size=1e-4,
)
register_dc_group(
visit_name="cm12345-6",
session_id=ExampleVisit.murfey_session_id,
dcg_params=dcg_params,
db=murfey_db_session,
)
# Check request to ispyb for updating the experiment type
mock_transport.send.assert_called_once_with(
"mock_feedback_queue",
{
"register": "atlas_update",
"atlas_id": 90,
"atlas": "/path/to/Sample12/Atlas/Atlas_2.jpg",
"sample": 12,
"atlas_pixel_size": 1e-4,
"dcgid": 1,
"session_id": ExampleVisit.murfey_session_id,
"tag": "processing_tag",
},
)
# Check the data collection group atlas was updated
new_dcg = murfey_db_session.exec(
select(DataCollectionGroup).where(DataCollectionGroup.id == dcg.id)
).one()
assert new_dcg.atlas == "/path/to/Sample12/Atlas/Atlas_2.jpg"
assert new_dcg.sample == 12
assert new_dcg.atlas_pixel_size == 1e-4
assert new_dcg.tag == "processing_tag"
assert new_dcg.atlas_id == 90
# Check the search map update calls
assert mock_register_search_map.call_count == 2
mock_register_search_map.assert_any_call(
ExampleVisit.murfey_session_id,
"searchmap1",
mock.ANY,
murfey_db_session,
close_db=False,
)
mock_register_search_map.assert_any_call(
ExampleVisit.murfey_session_id,
"searchmap2",
mock.ANY,
murfey_db_session,
close_db=False,
)
@pytest.mark.asyncio
async def test_make_gif(
mocker: MockerFixture,
tmp_path: Path,
):
# Set up test variables
session_id = 10
instrument_name = "test_instrument"
rsync_basepath = tmp_path / "data"
visit_name = "cm12345-6"
year = 2020
visit_dir = rsync_basepath / str(year) / visit_name
lamella_num = 12
lamella_folder = "Lamella"
if lamella_num > 1:
lamella_folder += f" ({lamella_num})"
raw_directory = "autotem"
# Create a list of test image file paths
raw_images = [
visit_dir
/ "autotem"
/ visit_name
/ "Sites"
/ lamella_folder
/ "DCImages/DCM_asdfjkl/asdfjkl-Polishing-dc_rescan-image-.png"
] * 5
# Mock the output of PIL.Image.open to always return a NumPY array
mocker.patch(
"murfey.server.api.workflow.Image.open",
return_value=PIL.Image.fromarray(np.ones((512, 512), dtype=np.uint16)),
)
# Create the Pydantic model
params = MillingParameters(
lamella_number=lamella_num,
images=[str(f) for f in raw_images],
raw_directory=raw_directory,
)
# Mock the database query
mock_db = MagicMock()
mock_db.exec.return_value.one.return_value.instrument_name = instrument_name
# Mock the machine config and 'get_machine_config'
mock_machine_config = MagicMock()
mock_machine_config.rsync_basepath = rsync_basepath
mock_machine_config.mkdir_chmod = 0o775
mocker.patch(
"murfey.server.api.workflow.get_machine_config",
return_value={
instrument_name: mock_machine_config,
},
)
# Create the save directory directory
save_dir = visit_dir / "processed" / raw_directory
save_dir.mkdir(parents=True, exist_ok=True)
# Run the function and check that the expected outputs are there
result = await make_gif(
year=year,
visit_name=visit_name,
session_id=session_id,
gif_params=params,
db=mock_db,
)
image_path = save_dir / f"lamella_{lamella_num}_milling.gif"
assert image_path.exists()
assert result.get("output_gif") == str(image_path)