forked from NSLS2/lsdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ispybLib.py
409 lines (364 loc) · 18.3 KB
/
ispybLib.py
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
import ispyb.factory
from ispyb.exception import ISPyBNoResultException
import os
from datetime import datetime
from ispyb.xmltools import mx_data_reduction_to_ispyb, xml_file_to_dict
import daq_utils
from epics import PV
import db_lib
import time
import mysql.connector
import logging
logger = logging.getLogger(__name__)
#12/19 - I'm leaving all commented lines alone on this. Karl Levik, DLS, is an immense help with this.
conf_file = os.environ["CONFIGDIR"] + "ispybConfig.cfg"
visit = 'mx99999-1'
# Get a list of request dicts
#request_dicts = lsdb2.getColRequestsByTimeInterval('2018-02-14T00:00:00','2018-02-15T00:00:00')
# Connect to ISPyB, get the relevant data area objects
conn = ispyb.open(conf_file)
core = ispyb.factory.create_data_area(ispyb.factory.DataAreaType.CORE, conn)
mxacquisition = ispyb.factory.create_data_area(ispyb.factory.DataAreaType.MXACQUISITION, conn)
mxprocessing = ispyb.factory.create_data_area(ispyb.factory.DataAreaType.MXPROCESSING, conn)
mxscreening = ispyb.factory.create_data_area(ispyb.factory.DataAreaType.MXSCREENING, conn)
cnx = mysql.connector.connect(user='ispyb_api', password=os.environ['ISPYB_PASSWORD'],host='ispyb-db-dev.cs.nsls2.local',database='ispyb')
cursor = cnx.cursor()
beamline = os.environ["BEAMLINE_ID"]
detSeqNumPVNAME = db_lib.getBeamlineConfigParam(beamline,"detSeqNumPVNAME") #careful - this pvname is stored in DB and in detControl.
detSeqNumPV = PV(detSeqNumPVNAME)
# Find the id for a particular
def queryOneFromDB(q):
cursor.execute(q)
try:
return list(cursor.fetchone())[0]
except TypeError:
return 0
def personIdFromLogin(loginName):
q = ("select personId from Person where login = \""+ loginName + "\"")
return (queryOneFromDB(q))
def personIdFromProposal(propNum):
q = ("select personId from Proposal where proposalNumber = " + str(propNum))
return (queryOneFromDB(q))
def proposalIdFromProposal(propNum):
q = ("select proposalId from Proposal where proposalNumber = " + str(propNum))
return (queryOneFromDB(q))
def maxVisitNumfromProposal(propNum):
propID = proposalIdFromProposal(propNum)
q = ("select max(visit_number) from BLSession where proposalId = " + str(propID))
return (queryOneFromDB(q))
def createPerson(firstName,lastName,loginName):
params = core.get_person_params()
params['given_name'] = firstName
params['family_name'] = lastName
params['login'] = loginName
pid = core.upsert_person(list(params.values()))
cnx.commit()
def createProposal(propNum,PI_login="boaty"):
pid = personIdFromLogin(PI_login)
if (pid == 0):
createPerson("Not","Sure",PI_login)
pid = personIdFromLogin(PI_login)
params = core.get_proposal_params()
params['proposal_code'] = 'mx'
params['proposal_number'] = int(propNum)
params['proposal_type'] = 'mx'
params['person_id'] = pid
params['title'] = 'SynchWeb Dev Proposal'
proposal_id = core.upsert_proposal(list(params.values()))
cnx.commit() #not sure why I needed to do this. Maybe mistake in stored proc?
def createVisitName(propNum): # this is for the GUI to know what a datapath would be in row_clicked
logger.info("creating visit Name for propnum " + str(propNum))
propID = proposalIdFromProposal(propNum)
if (propID == 0): #proposal doesn't exist, just create and assign to boaty
createProposal(propNum)
maxVis = maxVisitNumfromProposal(propNum)
if (maxVis == None): #1st visit
newVisitNum = 1
else:
newVisitNum = 1 + maxVis
logger.info('new visit number: %s' % newVisitNum)
visitName = "mx"+str(propNum)+"-"+str(newVisitNum)
return visitName, newVisitNum
def createVisit(propNum):
visitName, newVisitNum = createVisitName(propNum)
personID = personIdFromProposal(propNum)
params = core.get_session_for_proposal_code_number_params()
params['proposal_code'] = 'mx'
params['proposal_number'] = propNum
params['visit_number'] = newVisitNum
params['beamline_name'] = daq_utils.beamline.upper()
params['startdate'] = datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
params['enddate'] = datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
params['comments'] = 'For software testing'
sid = core.upsert_session_for_proposal_code_number(list(params.values()))
cnx.commit()
assert sid is not None
assert sid > 0
# Test upsert_person:
# params = core.get_person_params()
# params['given_name'] = 'Baldur'
# params['family_name'] = 'Odinsson'
# params['login'] = 'bo%s' % str(time.time()) # login must be unique
# pid = core.upsert_person(list(params.values()))
# assert pid is not None
# assert pid > 0
# params = core.get_person_params()
# params['id'] = pid
# params['email_address'] = '[email protected]'
# pid2 = core.upsert_person(list(params.values()))
# assert pid2 is not None
# assert pid2 == pid
# Test upsert_session_has_person:
params = core.get_session_has_person_params()
params['session_id'] = sid
params['person_id'] = personID
params['role'] = 'Co-Investigator'
params['remote'] = True
core.upsert_session_has_person(list(params.values()))
cnx.commit()
try:
personsOnProposalList = core.retrieve_persons_for_proposal("mx",propNum)
logger.debug(f'list of all persons: {personsOnProposalList}')
except:
logger.error(f'exception when retrieving persons for proposal {propNum}')
return visitName
for i in range (0,len(personsOnProposalList)):
personLogin = personsOnProposalList[i]["login"]
personID = personIdFromLogin(personLogin)
params = core.get_session_has_person_params()
params['session_id'] = sid
params['person_id'] = personID
params['role'] = 'Co-Investigator'
params['remote'] = True
core.upsert_session_has_person(list(params.values()))
cnx.commit()
# Test upsert_proposal_has_person:
# params = core.get_proposal_has_person_params()
# params['proposal_id'] = 141666
# params['person_id'] = pid
# params['role'] = 'Principal Investigator'
# phpid = core.upsert_proposal_has_person(list(params.values()))
# assert phpid is not None
# assert phpid > 0
return visitName
def addPersonToProposal(personLogin,propNum):
personID = personIdFromLogin(personLogin)
if (personID == 0):
createPerson("Not","Sure",personLogin)
personID = personIdFromLogin(personLogin)
propID = proposalIdFromProposal(propNum)
if (propID == 0):
createProposal(propNum,personLogin)
propID = proposalIdFromProposal(propNum)
params = core.get_proposal_has_person_params()
params['proposal_id'] = propID
params['role'] = 'Co-Investigator'
params['personid'] = personID
phpid = core.upsert_proposal_has_person(list(params.values()))
cnx.commit()
def insertPlotResult(dc_id,imageNumber,spotTotal,goodBraggCandidates,method2Res,totalIntegratedSignal):
params = mxprocessing.get_quality_indicators_params()
params['datacollectionid'] = dc_id
params['imageNumber'] = imageNumber
params['spotTotal'] = spotTotal
params['goodBraggCandidates'] = goodBraggCandidates
params['method2Res'] = method2Res
params['totalIntegratedSignal'] = totalIntegratedSignal
id = mxprocessing.upsert_quality_indicators(list(params.values()))
cnx.commit()
def insertResult(result,resultType,request,visitName,dc_id=None,xmlFileName=None): #xmlfilename for fastDP
#keep in mind that request type can be standard and result type be fastDP - multiple results per req.
try:
sessionid = core.retrieve_visit_id(visitName)
except ISPyBNoResultException as e:
logger.error("insert result - caught ISPyBNoResultException: '%s'. make sure visit name is in the format mx999999-1234. NOT HAVING MX IN FRONT IS A SIGN OF PROBLEMS - try newVisit() in that case." % e)
propNum = visitName.split('-')[0]
sessionid = createVisit(propNum)
request_type = request['request_type']
if request_type in('standard', 'vector') :
sample = request['sample'] # this needs to be created and linked to a DC group
if (resultType == 'fastDP'):
mx_data_reduction_dict = xml_file_to_dict(xmlFileName)
(app_id, ap_id, scaling_id, integration_id) = mx_data_reduction_to_ispyb(mx_data_reduction_dict, dc_id, mxprocessing)
mxprocessing.upsert_program_ex(program_id=app_id,status=1)
elif resultType == 'mxExpParams':
result_obj = result['result_obj']
request_obj = result_obj['requestObj']
directory = request_obj["directory"]
filePrefix = request_obj['file_prefix']
basePath = request_obj["basePath"]
visitName = daq_utils.getVisitName()
jpegDirectory = visitName + "/jpegs/" + directory[directory.find(visitName)+len(visitName):len(directory)]
fullJpegDirectory = basePath + "/" + jpegDirectory
jpegImagePrefix = fullJpegDirectory+"/"+filePrefix
daq_utils.take_crystal_picture(filename=jpegImagePrefix)
jpegImageFilename = jpegImagePrefix+".jpg"
jpegImageThumbFilename = jpegImagePrefix+"t.jpg"
node = db_lib.getBeamlineConfigParam(beamline,"adxvNode")
comm_s = f"ssh -q {node} \"{os.environ['MXPROCESSINGSCRIPTSDIR']}resize.sh {jpegImageFilename} {jpegImageThumbFilename} 40% \"&"
logger.info('resizing image: %s' % comm_s)
os.system(comm_s)
seqNum = int(detSeqNumPV.get())
node = db_lib.getBeamlineConfigParam(beamline,"adxvNode")
request_id = result['request']
comm_s = f"ssh -q {node} \"{os.environ['MXPROCESSINGSCRIPTSDIR']}eiger2cbf.sh {request_id} 1 1 0 {seqNum}\""
logger.info(f'diffraction thumbnail conversion to cbf: {comm_s}')
os.system(comm_s)
comm_s = f"ssh -q {node} \"{os.environ['MXPROCESSINGSCRIPTSDIR']}cbf2jpeg.sh {request_id}\""
logger.info(f'diffraction thumbnail conversion to jpeg: {comm_s}')
os.system(comm_s)
# Create a new data collection group entry:
params = mxacquisition.get_data_collection_group_params()
params['parentid'] = sessionid
if request_type == 'standard':
params['experimenttype'] = 'OSC'
elif request_type == 'vector':
params['experimenttype'] = 'Helical'
return createDataCollection(directory, filePrefix, jpegImageFilename, params, request_obj, sessionid)
## For strategies (EDNA or otherwise)
# params = mxscreening.get_screening_params()
# params['dcgid'] = dcg_id
# ...
# s_id = mxscreening.insert_screening(list(params.values()))
# params = mxscreening.get_screening_input_params()
# params['screening_id'] = s_id
# ...
# s_in_id = mxscreening.insert_screening_input(list(params.values()))
# params = mxscreening.get_screening_output_params()
# params['screening_id'] = s_id
# ...
# s_out_id = mxscreening.insert_screening_output(list(params.values()))
# params = mxscreening.get_screening_output_lattice_params()
# params['screening_output_id'] = s_out_id
# ...
# mxscreening.insert_screening_output_lattice(list(params.values()))
# params = mxscreening.get_screening_strategy_params()
# params['screening_output_id'] = s_out_id
# ...
# s_s_id = mxscreening.insert_screening_strategy(list(params.values()))
# params = mxscreening.get_screening_strategy_wedge_params()
# params['screening_strategy_id'] = s_s_id
# ...
# s_s_wedge_id = mxscreening.insert_screening_strategy_wedge(list(params.values()))
# params = mxscreening.get_screening_strategy_sub_wedge_params()
# params['screening_strategy_wedge_id'] = s_s_wedge_id
# ...
# mxscreening.insert_screening_strategy_sub_wedge(list(params.values()))
## For raster scans a.k.a. grid scans:
# params = mxacquisition.get_dc_position_params()
# params['id'] = dc_id
# params['posx'] =
# params['posy'] =
# params['posz'] =
# mxacquisition.update_dc_position(list(params.values()))
## For per-image analysis results (raster scans or otherwise)
# for image in images:
# params = mxprocessing.get_quality_indicators_params()
# imq.imagenumber as nim, imq.method2res as res, imq.spottotal as s, imq.totalintegratedsignal, imq.goodbraggcandidates as b
# params['imagenumber'] =
# params['datacollectionid'] =
# params['method2res'] =
# params['spottotal'] =
# params['totalintegratedsignal'] =
# params['goodbraggcandidates'] =
# mxprocessing.upsert_quality_indicators(list(params.values()))
## For fast_dp and similar MX data reduction pipeline results:
# ...
# (app_id, ap_id, scaling_id, integration_id) = mx_data_reduction_to_ispyb(mx_data_reduction_dict, dc_id, mxprocessing)
## For raster scans a.k.a. grid scans:
# params = mxacquisition.get_dcg_grid_params()
# params['parentid'] = dcg_id
# params['dx_mm'] =
# params['dy_mm'] =
# params['steps_x'] =
# params['steps_y'] =
# params['pixelspermicronx'] =
# params['pixelspermicrony'] =
# params['snapshot_offsetxpixel'] =
# params['snapshot_offsetypixel'] =
# params['orientation'] =
# params['snaked'] =
# mxacquisition.upsert_dcg_grid(list(params.values()))
# Beamsize:
# params['beamsize_at_samplex'] = ?
# params['beamsize_at_sampley'] = ?
# Other things:
# params['xbeam'] = ?
# params['ybeam'] = ?
# params['phistart'] = ?
# params['kapppastart'] = ?
# params['omegastart'] = ?
# hard-coding hack to make SynchWeb understand whether it's a full data collection or a screening
def createDataCollection(directory, filePrefix, jpegImageFilename, params, request_obj, sessionid):
params['starttime'] = datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
params['endtime'] = datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
dcg_id = mxacquisition.insert_data_collection_group(list(params.values()))
logger.info("dcg_id: %i" % dcg_id)
params = mxacquisition.get_data_collection_params()
params['parentid'] = dcg_id
params['visitid'] = sessionid
params['imgdir'] = directory
params['imgprefix'] = filePrefix
params['imgsuffix'] = 'cbf' # assume CBF ...?
params['wavelength'] = request_obj['wavelength']
params['starttime'] = datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
params['run_status'] = 'DataCollection Successful' # assume success / not aborted
params['datacollection_number'] = request_obj['runNum']
if request_obj['img_width'] > 0:
params['n_images'] = int(round((request_obj['sweep_end'] - request_obj['sweep_start']) / request_obj['img_width']))
else:
params['n_images'] = 1 # stills mode
params['exp_time'] = request_obj['exposure_time']
params['start_image_number'] = request_obj['file_number_start']
params['axis_start'] = request_obj['sweep_start']
params['axis_end'] = request_obj['sweep_end']
params['axis_range'] = request_obj['img_width']
params['resolution'] = request_obj['resolution']
params['detector_distance'] = request_obj['detDist']
params['slitgap_horizontal'] = request_obj['slit_width']
params['slitgap_vertical'] = request_obj['slit_height']
params['transmission'] = request_obj['attenuation'] * 100.0
params['file_template'] = '%s_####.cbf' % (request_obj['file_prefix']) # assume cbf ...
params['overlap'] = 0.0
params['rotation_axis'] = 'Omega' # assume Omega unless we know otherwise
logger.info("jpegimfilename = " + jpegImageFilename)
try:
params['xbeam'] = request_obj['xbeam']
params['ybeam'] = request_obj['ybeam']
except KeyError:
logger.error('Value of xbeam or ybeam not present')
params['xtal_snapshot1'] = jpegImageFilename
params['xtal_snapshot2'] = '/dls/i03/data/2016/cm14451-2/jpegs/20160413/test_xtal/xtal1_1_1_90.0.png'
params['xtal_snapshot3'] = '/dls/i03/data/2016/cm14451-2/jpegs/20160413/test_xtal/xtal1_3_1_183.0.png'
params['xtal_snapshot4'] = '/dls/i03/data/2016/cm14451-2/jpegs/20160413/test_xtal/xtal1_3_1_93.0.png'
dc_id = mxacquisition.insert_data_collection(list(params.values()))
logger.info("dc_id: %i" % dc_id)
return dc_id
# if request_type == 'screening':
# params['overlap'] = 89.0
def insertRasterResult(request,visitName):
try:
sessionid = core.retrieve_visit_id(visitName)
except ISPyBNoResultException as e:
logger.error("insertRasterResult - caught ISPyBNoResultException: '%s'. make sure visit name is in the format mx999999-1234. NOT HAVING MX IN FRONT IS A SIGN OF PROBLEMS - try newVisit() in that case." % e)
return
sample = request['sample'] # this needs to be created and linked to a DC group
#result_obj = result['result_obj'] this doesn't appear to be used -DK
request_obj = request['request_obj']
directory = request_obj["directory"]
filePrefix = request_obj['file_prefix']
basePath = request_obj["basePath"]
visitName = daq_utils.getVisitName()
jpegDirectory = visitName + "/jpegs/" + directory[directory.find(visitName)+len(visitName):len(directory)]
fullJpegDirectory = basePath + "/" + jpegDirectory
jpegImagePrefix = fullJpegDirectory+"/"+filePrefix
jpegImageFilename = jpegImagePrefix+".jpg"
jpegImageThumbFilename = jpegImagePrefix+"t.jpg"
comm_s = "convert " + jpegImageFilename + " -resize 40% " + jpegImageThumbFilename + "&"
logger.info('raster thumbnail creation: %s' %comm_s)
os.system(comm_s)
# Create a new data collection group entry:
params = mxacquisition.get_data_collection_group_params()
params['parentid'] = sessionid
params['experimenttype'] = 'OSC'
return createDataCollection(directory, filePrefix, jpegImageFilename, params, request_obj, sessionid)