-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpicking.py
More file actions
387 lines (360 loc) · 16.3 KB
/
picking.py
File metadata and controls
387 lines (360 loc) · 16.3 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
from logging import getLogger
from typing import List
import numpy as np
from sqlalchemy import func
from sqlmodel import Session, select
import murfey.server.prometheus as prom
from murfey.server import _transport_object
from murfey.server.feedback import (
_app_id,
_pj_id,
)
from murfey.util.config import get_machine_config
from murfey.util.db import (
AutoProcProgram,
ClassificationFeedbackParameters,
CtfParameters,
DataCollection,
Movie,
NotificationParameter,
NotificationValue,
ParticleSizes,
ProcessingJob,
Session as MurfeySession,
SPARelionParameters,
)
from murfey.util.processing_params import default_spa_parameters
logger = getLogger("murfey.workflows.spa.picking")
def _register_picked_particles_use_diameter(message: dict, _db: Session):
"""Received picked particles from the autopick service"""
# Add this message to the table of seen messages
params_to_forward = message.get("extraction_parameters")
assert isinstance(params_to_forward, dict)
pj_id = _pj_id(message["program_id"], _db)
ctf_params = CtfParameters(
pj_id=pj_id,
micrographs_file=params_to_forward["micrographs_file"],
extract_file=params_to_forward["extract_file"],
coord_list_file=params_to_forward["coord_list_file"],
ctf_image=params_to_forward["ctf_values"]["CtfImage"],
ctf_max_resolution=params_to_forward["ctf_values"]["CtfMaxResolution"],
ctf_figure_of_merit=params_to_forward["ctf_values"]["CtfFigureOfMerit"],
defocus_u=params_to_forward["ctf_values"]["DefocusU"],
defocus_v=params_to_forward["ctf_values"]["DefocusV"],
defocus_angle=params_to_forward["ctf_values"]["DefocusAngle"],
)
_db.add(ctf_params)
_db.commit()
_db.close()
picking_db_len = _db.exec(
select(func.count(ParticleSizes.id)).where(ParticleSizes.pj_id == pj_id)
).one()
if picking_db_len > default_spa_parameters.nr_picks_before_diameter:
# If there are enough particles to get a diameter
instrument_name = (
_db.exec(
select(MurfeySession).where(MurfeySession.id == message["session_id"])
)
.one()
.instrument_name
)
machine_config = get_machine_config(instrument_name=instrument_name)[
instrument_name
]
relion_params = _db.exec(
select(SPARelionParameters).where(SPARelionParameters.pj_id == pj_id)
).one()
relion_options = dict(relion_params)
particle_diameter = relion_params.particle_diameter
if not particle_diameter:
# If the diameter has not been calculated then find it
picking_db = _db.exec(
select(ParticleSizes.particle_size).where(ParticleSizes.pj_id == pj_id)
).all()
particle_diameter = float(np.quantile(list(picking_db), 0.75))
relion_params.particle_diameter = particle_diameter
_db.add(relion_params)
_db.commit()
ctf_db = _db.exec(
select(CtfParameters).where(CtfParameters.pj_id == pj_id)
).all()
for saved_message in ctf_db:
# Send on all saved messages to extraction
_db.expunge(saved_message)
zocalo_message: dict = {
"parameters": {
"micrographs_file": saved_message.micrographs_file,
"coord_list_file": saved_message.coord_list_file,
"output_file": saved_message.extract_file,
"pixel_size": (
relion_options["angpix"]
* relion_options["motion_corr_binning"]
),
"ctf_image": saved_message.ctf_image,
"ctf_max_resolution": saved_message.ctf_max_resolution,
"ctf_figure_of_merit": saved_message.ctf_figure_of_merit,
"defocus_u": saved_message.defocus_u,
"defocus_v": saved_message.defocus_v,
"defocus_angle": saved_message.defocus_angle,
"particle_diameter": particle_diameter,
"downscale": relion_options["downscale"],
"kv": relion_options["voltage"],
"node_creator_queue": machine_config.node_creator_queue,
"session_id": message["session_id"],
"autoproc_program_id": _app_id(
_pj_id(message["program_id"], _db, recipe="em-spa-extract"),
_db,
),
"batch_size": default_spa_parameters.batch_size_2d,
},
"recipes": ["em-spa-extract"],
}
if _transport_object:
zocalo_message["parameters"]["feedback_queue"] = (
_transport_object.feedback_queue
)
_transport_object.send(
"processing_recipe", zocalo_message, new_connection=True
)
else:
# If the diameter is known then just send the new message
particle_diameter = relion_params.particle_diameter
zocalo_message = {
"parameters": {
"micrographs_file": params_to_forward["micrographs_file"],
"coord_list_file": params_to_forward["coord_list_file"],
"output_file": params_to_forward["extract_file"],
"pixel_size": (
relion_options["angpix"] * relion_options["motion_corr_binning"]
),
"ctf_image": params_to_forward["ctf_values"]["CtfImage"],
"ctf_max_resolution": params_to_forward["ctf_values"][
"CtfMaxResolution"
],
"ctf_figure_of_merit": params_to_forward["ctf_values"][
"CtfFigureOfMerit"
],
"defocus_u": params_to_forward["ctf_values"]["DefocusU"],
"defocus_v": params_to_forward["ctf_values"]["DefocusV"],
"defocus_angle": params_to_forward["ctf_values"]["DefocusAngle"],
"particle_diameter": particle_diameter,
"downscale": relion_options["downscale"],
"kv": relion_options["voltage"],
"node_creator_queue": machine_config.node_creator_queue,
"session_id": message["session_id"],
"autoproc_program_id": _app_id(
_pj_id(message["program_id"], _db, recipe="em-spa-extract"), _db
),
"batch_size": default_spa_parameters.batch_size_2d,
},
"recipes": ["em-spa-extract"],
}
if _transport_object:
zocalo_message["parameters"]["feedback_queue"] = (
_transport_object.feedback_queue
)
_transport_object.send(
"processing_recipe", zocalo_message, new_connection=True
)
else:
# If not enough particles then save the new sizes
particle_list = message.get("particle_diameters")
assert isinstance(particle_list, list)
for particle in particle_list:
new_particle = ParticleSizes(pj_id=pj_id, particle_size=particle)
_db.add(new_particle)
_db.commit()
_db.close()
def _register_picked_particles_use_boxsize(message: dict, _db: Session):
"""Received picked particles from the autopick service"""
# Add this message to the table of seen messages
params_to_forward = message.get("extraction_parameters")
assert isinstance(params_to_forward, dict)
instrument_name = (
_db.exec(select(MurfeySession).where(MurfeySession.id == message["session_id"]))
.one()
.instrument_name
)
machine_config = get_machine_config(instrument_name=instrument_name)[
instrument_name
]
pj_id = _pj_id(message["program_id"], _db)
ctf_params = CtfParameters(
pj_id=pj_id,
micrographs_file=params_to_forward["micrographs_file"],
coord_list_file=params_to_forward["coord_list_file"],
extract_file=params_to_forward["extract_file"],
ctf_image=params_to_forward["ctf_values"]["CtfImage"],
ctf_max_resolution=params_to_forward["ctf_values"]["CtfMaxResolution"],
ctf_figure_of_merit=params_to_forward["ctf_values"]["CtfFigureOfMerit"],
defocus_u=params_to_forward["ctf_values"]["DefocusU"],
defocus_v=params_to_forward["ctf_values"]["DefocusV"],
defocus_angle=params_to_forward["ctf_values"]["DefocusAngle"],
)
_db.add(ctf_params)
_db.commit()
_db.close()
# Set particle diameter as zero and send box sizes
relion_params = _db.exec(
select(SPARelionParameters).where(SPARelionParameters.pj_id == pj_id)
).one()
# Send the message to extraction with the box sizes
zocalo_message: dict = {
"parameters": {
"micrographs_file": params_to_forward["micrographs_file"],
"coord_list_file": params_to_forward["coord_list_file"],
"output_file": params_to_forward["extract_file"],
"pixel_size": relion_params.angpix * relion_params.motion_corr_binning,
"ctf_image": params_to_forward["ctf_values"]["CtfImage"],
"ctf_max_resolution": params_to_forward["ctf_values"]["CtfMaxResolution"],
"ctf_figure_of_merit": params_to_forward["ctf_values"]["CtfFigureOfMerit"],
"defocus_u": params_to_forward["ctf_values"]["DefocusU"],
"defocus_v": params_to_forward["ctf_values"]["DefocusV"],
"defocus_angle": params_to_forward["ctf_values"]["DefocusAngle"],
"particle_diameter": relion_params.particle_diameter,
"boxsize": relion_params.boxsize,
"small_boxsize": relion_params.small_boxsize,
"downscale": relion_params.downscale,
"kv": relion_params.voltage,
"node_creator_queue": machine_config.node_creator_queue,
"session_id": message["session_id"],
"autoproc_program_id": _app_id(
_pj_id(message["program_id"], _db, recipe="em-spa-extract"), _db
),
"batch_size": default_spa_parameters.batch_size_2d,
},
"recipes": ["em-spa-extract"],
}
if _transport_object:
zocalo_message["parameters"]["feedback_queue"] = (
_transport_object.feedback_queue
)
_transport_object.send("processing_recipe", zocalo_message, new_connection=True)
_db.close()
def _request_email(
failed_params: List[str], dcg_id: int, session_id: int, murfey_db: Session
) -> None:
session = murfey_db.exec(
select(MurfeySession).where(MurfeySession.id == session_id)
).one()
config = get_machine_config(instrument_name=session.instrument_name)[
session.instrument_name
]
if _transport_object:
_transport_object.send(
config.notifications_queue,
{
"groupId": dcg_id,
"message": f"The following parameters consistently exceeded the user set bounds: {failed_params}",
},
new_connection=True,
)
logger.debug(
f"Sent notification to {config.notifications_queue!r} for "
f"visit {session.visit!r}, data collection group ID {dcg_id} about the following abnormal parameters: \n"
f"{', '.join([f'{p}' for p in failed_params])}"
)
return None
def _check_notifications(message: dict, murfey_db: Session) -> None:
data_collection_hierarchy = murfey_db.exec(
select(DataCollection, ProcessingJob, AutoProcProgram)
.where(ProcessingJob.dc_id == DataCollection.id)
.where(AutoProcProgram.pj_id == ProcessingJob.id)
.where(AutoProcProgram.id == message["program_id"])
).all()
dcgid = data_collection_hierarchy[0][0].dcg_id
notification_parameters = murfey_db.exec(
select(NotificationParameter).where(NotificationParameter.dcg_id == dcgid)
).all()
failures = []
for param in notification_parameters:
if message.get(param.name) is not None:
# Load instances of current parameter from database
param_values = murfey_db.exec(
select(NotificationValue).where(
NotificationValue.notification_parameter_id == param.id
)
).all()
param_values.sort(key=lambda x: x.index)
# Drop oldest value if number of entries exceeds threshold
param_value_to_drop = None
if len(param_values) >= 25:
param_value_to_drop = param_values[0]
param_values = param_values[1:]
# Add newest value to end of list
param_values.append(
NotificationValue(
notification_parameter_id=param.id,
index=param_values[-1].index + 1 if len(param_values) else 0,
within_bounds=param.min_value
<= message[param.name]
<= param.max_value,
)
)
# Trigger message if this param has consistently exceeded the set threshold
if (
len(param_values) >= 25
and sum(p.within_bounds for p in param_values) / len(param_values)
< 0.25
):
# If notifications disabled, enable them now
trigger = False
if not param.notification_active:
# Use a variable to trigger the notification for the first
# time within the first 500 messages received
if param_values[-1].index < 500:
logger.debug(
f"First abnormal instance of parameter {param.name!r} detected"
)
trigger = True
param.notification_active = True
if param.num_instances_since_triggered >= 500 or trigger:
if not trigger:
logger.debug(
f"Parameter {param.name!r} has exceeded normal operating thresholds"
)
failures.append(param.name)
param.num_instances_since_triggered = 0
else:
# Only reset to False if there are more than 500 instances
# to stop multiple triggers within the first 500
if param.notification_active and param_values[-1].index > 500:
param.notification_active = False
# Delete oldest value
if param_value_to_drop is not None:
murfey_db.delete(param_value_to_drop)
# Add newest value and increment record of instances
murfey_db.add(param_values[-1])
param.num_instances_since_triggered += 1
murfey_db.add_all(notification_parameters)
murfey_db.commit()
murfey_db.close()
if failures:
logger.debug(
"Requested email notification for the following abnormal parameters: \n"
f"{', '.join([f'{p}' for p in failures])}"
)
_request_email(failures, dcgid, message["session_id"], murfey_db)
return None
def particles_picked(message: dict, murfey_db: Session) -> dict[str, bool]:
movie = murfey_db.exec(
select(Movie).where(Movie.murfey_id == message["motion_correction_id"])
).one()
movie.preprocessed = True
murfey_db.add(movie)
murfey_db.commit()
feedback_params = murfey_db.exec(
select(ClassificationFeedbackParameters).where(
ClassificationFeedbackParameters.pj_id
== _pj_id(message["program_id"], murfey_db)
)
).one()
if feedback_params.estimate_particle_diameter:
_register_picked_particles_use_diameter(message, murfey_db)
else:
_register_picked_particles_use_boxsize(message, murfey_db)
prom.preprocessed_movies.labels(
processing_job=_pj_id(message["program_id"], murfey_db)
).inc()
_check_notifications(message, murfey_db)
return {"success": True}