44
55from murfey .util .db import (
66 AutoProcProgram ,
7+ ClassificationFeedbackParameters ,
78 DataCollection ,
89 DataCollectionGroup ,
910 ParticleSizes ,
1516from tests .conftest import ExampleVisit , get_or_create_db_entry
1617
1718
18- @mock .patch ("murfey.workflows.tomo.picking._transport_object" )
19- @mock .patch ("murfey.workflows.tomo.picking._ids_tomo_classification" )
20- def test_picked_tomogram_not_run_class2d (
21- mock_ids , mock_transport , murfey_db_session : Session , tmp_path
22- ):
23- """Run the picker feedback with less particles than needed for classification"""
24- mock_ids .return_value = [2 , 1 ]
25-
26- # Insert table dependencies
19+ def set_up_picking_db (murfey_db_session ):
20+ # Insert common elements needed in all picking tests
2721 dcg_entry : DataCollectionGroup = get_or_create_db_entry (
2822 murfey_db_session ,
2923 DataCollectionGroup ,
@@ -42,7 +36,7 @@ def test_picked_tomogram_not_run_class2d(
4236 "dcg_id" : dcg_entry .id ,
4337 },
4438 )
45- get_or_create_db_entry (
39+ processing_job_entry : ProcessingJob = get_or_create_db_entry (
4640 murfey_db_session ,
4741 ProcessingJob ,
4842 lookup_kwargs = {
@@ -51,6 +45,41 @@ def test_picked_tomogram_not_run_class2d(
5145 "dc_id" : dc_entry .id ,
5246 },
5347 )
48+ get_or_create_db_entry (
49+ murfey_db_session ,
50+ AutoProcProgram ,
51+ lookup_kwargs = {
52+ "id" : 0 ,
53+ "pj_id" : processing_job_entry .id ,
54+ },
55+ )
56+ get_or_create_db_entry (
57+ murfey_db_session ,
58+ ClassificationFeedbackParameters ,
59+ lookup_kwargs = {
60+ "pj_id" : processing_job_entry .id ,
61+ "estimate_particle_diameter" : True ,
62+ "hold_class2d" : False ,
63+ "hold_class3d" : False ,
64+ "class_selection_score" : 0 ,
65+ "star_combination_job" : 0 ,
66+ "initial_model" : "" ,
67+ "next_job" : 0 ,
68+ },
69+ )
70+ return dcg_entry .id , dc_entry .id , processing_job_entry .id
71+
72+
73+ @mock .patch ("murfey.workflows.tomo.picking._transport_object" )
74+ @mock .patch ("murfey.workflows.tomo.picking._ids_tomo_classification" )
75+ def test_picked_tomogram_not_run_class2d (
76+ mock_ids , mock_transport , murfey_db_session : Session , tmp_path
77+ ):
78+ """Run the picker feedback with less particles than needed for classification"""
79+ mock_ids .return_value = [2 , 1 ]
80+
81+ # Insert table dependencies
82+ set_up_picking_db (murfey_db_session )
5483
5584 message = {
5685 "program_id" : 0 ,
@@ -61,7 +90,6 @@ def test_picked_tomogram_not_run_class2d(
6190 "register" : "picked_tomogram" ,
6291 "tomogram" : f"{ tmp_path } /Tomograms/job006/tomograms/sample.mrc" ,
6392 }
64-
6593 picking ._register_picked_tomogram_use_diameter (message , murfey_db_session )
6694
6795 mock_ids .assert_called_once_with (0 , "em-tomo-class2d" , murfey_db_session )
@@ -86,53 +114,19 @@ def test_picked_tomogram_not_run_class2d(
86114
87115@mock .patch ("murfey.workflows.tomo.picking._transport_object" )
88116@mock .patch ("murfey.workflows.tomo.picking._ids_tomo_classification" )
89- def test_picked_tomogram_run_class2d (
117+ def test_picked_tomogram_run_class2d_with_diameter (
90118 mock_ids , mock_transport , murfey_db_session : Session , tmp_path
91119):
92- """Run the picker feedback with less particles than needed for classification """
120+ """Run the picker feedback with a pre-determined particle diameter """
93121 mock_transport .feedback_queue = "murfey_feedback"
94122
95123 # Insert table dependencies
96- dcg_entry : DataCollectionGroup = get_or_create_db_entry (
97- murfey_db_session ,
98- DataCollectionGroup ,
99- lookup_kwargs = {
100- "id" : 0 ,
101- "session_id" : ExampleVisit .murfey_session_id ,
102- "tag" : "test_dcg" ,
103- },
104- )
105- dc_entry : DataCollection = get_or_create_db_entry (
106- murfey_db_session ,
107- DataCollection ,
108- lookup_kwargs = {
109- "id" : 0 ,
110- "tag" : "test_dc" ,
111- "dcg_id" : dcg_entry .id ,
112- },
113- )
114- processing_job_entry : ProcessingJob = get_or_create_db_entry (
115- murfey_db_session ,
116- ProcessingJob ,
117- lookup_kwargs = {
118- "id" : 1 ,
119- "recipe" : "test_recipe" ,
120- "dc_id" : dc_entry .id ,
121- },
122- )
123- get_or_create_db_entry (
124- murfey_db_session ,
125- AutoProcProgram ,
126- lookup_kwargs = {
127- "id" : 0 ,
128- "pj_id" : processing_job_entry .id ,
129- },
130- )
124+ dcg_id , dc_id , pj_id = set_up_picking_db (murfey_db_session )
131125 get_or_create_db_entry (
132126 murfey_db_session ,
133127 TomographyProcessingParameters ,
134128 lookup_kwargs = {
135- "dcg_id" : dcg_entry . id ,
129+ "dcg_id" : dcg_id ,
136130 "pixel_size" : 1.34 ,
137131 "dose_per_frame" : 1 ,
138132 "frame_count" : 5 ,
@@ -147,12 +141,12 @@ def test_picked_tomogram_run_class2d(
147141 ParticleSizes ,
148142 lookup_kwargs = {
149143 "id" : particle ,
150- "pj_id" : processing_job_entry . id ,
144+ "pj_id" : pj_id ,
151145 "particle_size" : 100 ,
152146 },
153147 )
154148
155- mock_ids .return_value = [dcg_entry . id , 1 ]
149+ mock_ids .return_value = [dcg_id , 1 ]
156150
157151 message = {
158152 "session_id" : 1 ,
@@ -164,17 +158,6 @@ def test_picked_tomogram_run_class2d(
164158 "register" : "picked_tomogram" ,
165159 "tomogram" : f"{ tmp_path } /Tomograms/job006/tomograms/sample.mrc" ,
166160 }
167-
168- # Create a data collection group for lookups
169- grid_square = DataCollectionGroup (
170- id = 1 ,
171- session_id = ExampleVisit .murfey_session_id ,
172- tag = "session_tag" ,
173- atlas_id = 90 ,
174- )
175- murfey_db_session .add (grid_square )
176- murfey_db_session .commit ()
177-
178161 picking ._register_picked_tomogram_use_diameter (message , murfey_db_session )
179162
180163 mock_ids .assert_called_once_with (0 , "em-tomo-class2d" , murfey_db_session )
@@ -204,6 +187,121 @@ def test_picked_tomogram_run_class2d(
204187 "picker_id" : None ,
205188 "class2d_grp_uuid" : 6 ,
206189 "class_uuids" : {str (i ): i for i in range (1 , 6 )},
190+ "next_job" : 9 ,
191+ "feedback_queue" : "murfey_feedback" ,
192+ },
193+ "recipes" : ["em-tomo-class2d" ],
194+ },
195+ new_connection = True ,
196+ )
197+
198+
199+ @mock .patch ("murfey.workflows.tomo.picking._transport_object" )
200+ @mock .patch ("murfey.workflows.tomo.picking._ids_tomo_classification" )
201+ def test_picked_tomogram_run_class2d_estimate_diameter (
202+ mock_ids , mock_transport , murfey_db_session : Session , tmp_path
203+ ):
204+ """Run the picker feedback for Class2D, including diameter estimation"""
205+ mock_transport .feedback_queue = "murfey_feedback"
206+
207+ # Insert table dependencies
208+ dcg_id , dc_id , pj_id = set_up_picking_db (murfey_db_session )
209+ get_or_create_db_entry (
210+ murfey_db_session ,
211+ TomographyProcessingParameters ,
212+ lookup_kwargs = {
213+ "dcg_id" : dcg_id ,
214+ "pixel_size" : 1.34 ,
215+ "dose_per_frame" : 1 ,
216+ "frame_count" : 5 ,
217+ "tilt_axis" : 0 ,
218+ "voltage" : 300 ,
219+ "particle_diameter" : None ,
220+ },
221+ )
222+ for particle in range (10001 ):
223+ get_or_create_db_entry (
224+ murfey_db_session ,
225+ ParticleSizes ,
226+ lookup_kwargs = {
227+ "id" : particle ,
228+ "pj_id" : pj_id ,
229+ "particle_size" : 100 ,
230+ },
231+ )
232+ # Insert one existing tomogram which should get flushed out
233+ get_or_create_db_entry (
234+ murfey_db_session ,
235+ TomogramPicks ,
236+ lookup_kwargs = {
237+ "pj_id" : pj_id ,
238+ "tomogram" : f"{ tmp_path } /Tomograms/job006/tomograms/tomogram1.mrc" ,
239+ "cbox_3d" : f"{ tmp_path } /AutoPick/job007/CBOX_3d/tomogram1_picks.cbox" ,
240+ "particle_count" : 10 ,
241+ "tomogram_pixel_size" : 5.3 ,
242+ },
243+ )
244+
245+ mock_ids .return_value = [dcg_id , 1 ]
246+
247+ message = {
248+ "session_id" : 1 ,
249+ "program_id" : 0 ,
250+ "cbox_3d" : f"{ tmp_path } /AutoPick/job007/CBOX_3d/sample.cbox" ,
251+ "particle_count" : 2 ,
252+ "particle_diameters" : [10.1 , 20.2 ],
253+ "pixel_size" : 5.3 ,
254+ "register" : "picked_tomogram" ,
255+ "tomogram" : f"{ tmp_path } /Tomograms/job006/tomograms/sample.mrc" ,
256+ }
257+ picking ._register_picked_tomogram_use_diameter (message , murfey_db_session )
258+
259+ mock_ids .assert_called_once_with (0 , "em-tomo-class2d" , murfey_db_session )
260+
261+ # Two mock calls - one flushed tomogram and one new
262+ assert mock_transport .send .call_count == 2
263+ mock_transport .send .assert_any_call (
264+ "processing_recipe" ,
265+ {
266+ "parameters" : {
267+ "tomogram" : f"{ tmp_path } /Tomograms/job006/tomograms/tomogram1.mrc" ,
268+ "cbox_3d" : f"{ tmp_path } /AutoPick/job007/CBOX_3d/tomogram1_picks.cbox" ,
269+ "pixel_size" : 5.3 ,
270+ "particle_diameter" : 100.0 ,
271+ "kv" : 300 ,
272+ "node_creator_queue" : "node_creator" ,
273+ "session_id" : message ["session_id" ],
274+ "autoproc_program_id" : 0 ,
275+ "batch_size" : 10000 ,
276+ "nr_classes" : 5 ,
277+ "picker_id" : None ,
278+ "class2d_grp_uuid" : 6 ,
279+ "class_uuids" : {str (i ): i for i in range (1 , 6 )},
280+ "next_job" : 9 ,
281+ "feedback_queue" : "murfey_feedback" ,
282+ },
283+ "recipes" : ["em-tomo-class2d" ],
284+ },
285+ new_connection = True ,
286+ )
287+ mock_transport .send .assert_any_call (
288+ "processing_recipe" ,
289+ {
290+ "parameters" : {
291+ "tomogram" : message ["tomogram" ],
292+ "cbox_3d" : message ["cbox_3d" ],
293+ "pixel_size" : message ["pixel_size" ],
294+ "particle_diameter" : 100.0 ,
295+ "kv" : 300 ,
296+ "node_creator_queue" : "node_creator" ,
297+ "session_id" : message ["session_id" ],
298+ "autoproc_program_id" : 0 ,
299+ "batch_size" : 10000 ,
300+ "nr_classes" : 5 ,
301+ "picker_id" : None ,
302+ "class2d_grp_uuid" : 12 ,
303+ "class_uuids" : {str (i ): i for i in range (7 , 12 )},
304+ "next_job" : 11 ,
207305 "feedback_queue" : "murfey_feedback" ,
208306 },
209307 "recipes" : ["em-tomo-class2d" ],
0 commit comments