def issue_2020_12_19(): """First version of det.raw.calib""" from psana.pyalgos.generic.NDArrUtils import print_ndarr, info_ndarr from psana import DataSource ds = DataSource(exp='ueddaq02',run=28) for run in ds.runs(): print('====== run.runnum: ', run.runnum) det = run.Detector('epixquad') print('det._det_name : ', det._det_name) # epixquad print('det._dettype : ', det._dettype) # epix print('det.raw._det_name : ', det.raw._det_name) # epixquad print('det.raw._dettype : ', det.raw._dettype) # epix print('det.raw._uniqueid : ', det.raw._uniqueid) # epix_3926196238-017.... print('_sorted_segment_ids: ', det.raw._sorted_segment_ids) # [0, 1, 2, 3] for stepnum,step in enumerate(run.steps()): print('%s\n==== Step %1d ====' % (50*'_',stepnum)) for evnum,evt in enumerate(step.events()): if evnum>2 and evnum%500!=0: continue print('%s\nStep %1d Event %04d' % (50*'_',stepnum, evnum)) calib = det.raw.calib(evt) ########################### print(info_ndarr(det.raw._pedestals(), 'peds ')) print(info_ndarr(det.raw.raw(evt), 'raw ')) print(info_ndarr(calib, 'calib '))
def test_calib_constants_directly(expname, runnum, detnameid): logger.info('in test_calib_constants_directly') from psana.pscalib.calib.MDBWebUtils import calib_constants #'pixel_rms', 'pixel_status', 'pedestals', 'pixel_gain', 'geometry' pedestals, _ = calib_constants(detnameid, exp=expname, ctype='pedestals', run=runnum) gain, _ = calib_constants(detnameid, exp=expname, ctype='pixel_gain', run=runnum) rms, _ = calib_constants(detnameid, exp=expname, ctype='pixel_rms', run=runnum) status, _ = calib_constants(detnameid, exp=expname, ctype='pixel_status', run=runnum) logger.info(info_ndarr(pedestals, 'pedestals')) logger.info(info_ndarr(gain, 'gain ')) logger.info(info_ndarr(rms, 'rms ')) logger.info(info_ndarr(status, 'status '))
def view_data(self, data=None, fname=None): from psana.pyalgos.generic.NDArrUtils import info_ndarr, np cp.last_selected_data = data cp.last_selected_fname.setValue(fname) if data is None and fname is None: logger.debug('data and fname are None - do not switch to viewer') elif data is None: tabname = 'Text' if 'geometry' in fname or 'common_mode' in fname else 'Image' self.set_tab(tabname) elif isinstance(data, np.ndarray): logger.info( info_ndarr(data, 'switch to Image Viewer to view data:')) self.set_tab(tabname='Image') elif isinstance(data, str): logger.info(info_ndarr(data, 'switch to Text Viewer to view data:')) self.set_tab(tabname='Text') elif isinstance(data, dict): from psana.pscalib.calib.MDBConvertUtils import info_dict logger.info( 'data is dict switch to Text Viewer to view data as info_dict') cp.last_selected_data = info_dict( data, offset=' ', s='') #str(cp.last_selected_data) self.set_tab(tabname='Text') else: logger.debug( 'data of the selected document is not numpy array - do not switch to Image Viewer' ) cp.last_selected_data = None
def cached_pixel_coord_indexes(self, evt, **kwa): """ """ logger.debug('AreaDetector.cached_pixel_coord_indexes') resp = self.pixel_coord_indexes(**kwa) if resp is None: return None # PRESERVE PIXEL INDEXES FOR USED SEGMENTS ONLY segs = self.segments(evt) if segs is None: return None logger.debug(info_ndarr(segs, 'preserve pixel indices for segments ')) rows, cols = self.pix_rc = [reshape_to_3d(a)[segs, :, :] for a in resp] #self.pix_rc = [dict_from_arr3d(reshape_to_3d(v)) for v in resp] s = 'evaluate_pixel_coord_indexes:' for i, a in enumerate(self.pix_rc): s += info_ndarr(a, '\n %s ' % ('rows', 'cols')[i], last=3) logger.info(s) mapmode = kwa.get('mapmode', 2) if mapmode < 4: self.img_entries, self.dmulti_pix_to_img_idx, self.dmulti_imgidx_numentries=\ statistics_of_pixel_arrays(rows, cols) if mapmode == 4: rsp = self.pixel_coords(**kwa) if rsp is None: return None x, y, z = self.pix_xyz = [ reshape_to_3d(a)[segs, :, :] for a in rsp ] self.interpol_pars = init_interpolation_parameters( rows, cols, x, y) if mapmode < 4 and kwa.get('fillholes', True): self.img_pix_ascend_ind, self.img_holes, self.hole_rows, self.hole_cols, self.hole_inds1d =\ statistics_of_holes(rows, cols, **kwa) # TBD parameters for image interpolation if False: t0_sec = time() self.imgind_to_seg_row_col = image_of_pixel_seg_row_col( img_pix_ascend_ind, arr_shape) logger.debug( 'statistics_of_holes.imgind_to_seg_row_col time (sec) = %.6f' % (time() - t0_sec)) # 47ms logger.debug( info_ndarr(self.imgind_to_seg_row_col, ' imgind_to_seg_row_col ')) if False: s = ' imgind_to_seg_row_col ' # (n,352,384) first = (352 + 5) * 384 + 380 for i in range(first, first + 10): s += '\n s:%02d r:%03d c:%03d' % tuple( imgind_to_seg_row_col[i]) logger.debug(s)
def statistics_of_holes(rows, cols, **kwa): """generates and returns a few useful arrays img_pix_ascend_ind - image-shaped [int32] contains ravel index in pixel (data) array img_holes - image-shaped [bool] hole_rows, hole_cols - arrays of hole rows and cols in image """ assert isinstance(rows, np.ndarray) assert isinstance(cols, np.ndarray) assert rows.size == cols.size arr_shape = rows.shape img_shape = nrows, ncols = image_shape(rows, cols) img_size = nrows * ncols # make img_pix_ascend_ind mapping [r,c] to index in pixelarray t0_sec = time() img_pix_ascend_ind = image_of_pixel_array_ascending_index( rows, cols, img_shape, np.int32) logger.debug( 'statistics_of_holes.image_of_pixel_array_ascending_index time (sec) = %.6f' % (time() - t0_sec)) # 8ms logger.debug(info_ndarr(img_pix_ascend_ind, ' img_pix_ascend_ind:')) busy_img_bins = img_pix_ascend_ind > -1 n_img_pixbins = np.sum(busy_img_bins) #np.count_nonzero(busy_img_bins) fr_noon_empty = float(n_img_pixbins) / busy_img_bins.size logger.debug('statistics_of_holes busy image bins/total: %d/%d = %.3f' % (n_img_pixbins, busy_img_bins.size, fr_noon_empty)) t0_sec = time() img_holes = image_of_holes(busy_img_bins) logger.debug('statistics_of_holes hole finding time (sec) = %.6f' % (time() - t0_sec)) logger.debug('statistics_of_holes number of holes = %d' % np.sum(img_holes)) hole_inds1d = hole_inds_ravel( img_holes ) #np.where(img_holes.ravel())[0] # [0] because np.where returns tuple logger.debug('statistics_of_holes hole indexes ravel = %s' % str(hole_inds1d)) logger.debug(info_ndarr(hole_inds1d, 'hole_inds1d:')) hole_rows, hole_cols = hole_rows_cols(img_holes) logger.debug( info_ndarr(hole_rows, 'XXXC statistics_of_holes hole hrows:', last=10)) logger.debug( info_ndarr(hole_cols, 'XXXC statistics_of_holes hole hcols:', last=10)) return img_pix_ascend_ind, img_holes, hole_rows, hole_cols, hole_inds1d
def set_metadata_values(self): """Sets metadata values associated with self.data_nda """ logger.debug('in set_metadata_values') model = self.model nda = self.data_nda colk, colv = 0, 1 for row in range(model.rowCount()): key = model.item(row, colk).text() if key == 'data_size': model.item(row, colv).setText(str(nda.size)) elif key == 'data_dtype': model.item(row, colv).setText(str(nda.dtype)) elif key == 'data_ndim': model.item(row, colv).setText(str(nda.ndim)) elif key == 'data_shape': model.item(row, colv).setText(str(nda.shape)) elif key == 'host': model.item(row, colv).setText(gu.get_hostname()) elif key == 'uid': model.item(row, colv).setText(gu.get_login()) elif key == 'cwd': model.item(row, colv).setText(gu.get_cwd()) logger.info('Model document content:\n %s\n%s' % (self.info_model_dicdoc(), info_ndarr(self.data_nda, 'data n-d array ')))
def test_xtcav_lasing_on(args=Arguments()): from psana import DataSource from psana.xtcav.LasingOnCharacterization import LasingOnCharacterization, setDetectors from psana.pyalgos.generic.NDArrUtils import info_ndarr, np ds = DataSource(files=args.fname) run = next(ds.runs()) dets = setDetectors(run) lon = LasingOnCharacterization(args, run, dets) nimgs = 0 for nev, evt in enumerate(run.events()): img = dets._camraw(evt) print('Event %03d raw data: %s' % (nev, info_ndarr(img))) if img is None: continue if not lon.processEvent(evt): continue t, power, agr, pulse = lon.resultsProcessImage() print('%sAgreement:%7.3f%% Max power: %g GW Pulse Delay: %.3f ' % (10 * ' ', agr * 100, np.amax(power), pulse[0])) nimgs += 1 if nimgs >= args.events: break
def add_doc(self): """Adds document from editor to DB """ logger.debug('In add_doc') wdoce = cp.cmwdbdoceditor if wdoce is None or wdoce.data_nda is None : logger.warning('Document is not selected. Select collection in DB then document in List mode.') return dicdoc = wdoce.get_model_dicdoc() nda = wdoce.get_data_nda() msg = '\n '.join(['%12s : %s' % (k,v) for k,v in dicdoc.items()]) from psana.pyalgos.generic.NDArrUtils import info_ndarr logger.debug('add_doc \n%s \n%s' % (msg, info_ndarr(nda, 'data n-d array '))) dbnexp = dbu.db_prefixed_name(dicdoc.get('experiment', 'exp_def')) dbndet = dbu.db_prefixed_name(dicdoc.get('detector', 'det_def')) colname = dicdoc.get('detector', None) d = {dbnexp : True, dbndet : True} resp = change_check_box_dict_in_popup_menu(d, msg='Add constants\nand metadata to DB', parent=self.but_add) logger.debug('add_doc resp: %s' % resp) if resp==1 : if d[dbnexp] : _,_=dbu.insert_document_and_data(dbnexp, colname, dicdoc, nda) if d[dbndet] : _,_=dbu.insert_document_and_data(dbndet, colname, dicdoc, nda) wdocs = cp.cmwdbdocswidg if wdocs is None : return cp.cmwdbdocs.show_documents(wdocs.dbname, wdocs.colname, force_update=True) else : logger.warning('Uploading of calibration constants in DB is cancelled')
def procEvents(args): fname = getattr( args, 'fname', '/reg/g/psdm/detector/data2_test/xtc/data-amox23616-r0137-e000100-xtcav-v2.xtc2' ) max_shots = getattr(args, 'max_shots', 200) mode = getattr(args, 'mode', 'smd') ds = DataSource(files=fname) run = next(ds.runs()) dets = setDetectors( run) # NEEDS IN camera, ebeam, gasdetecto, eventid, xtcavpars lon = LasingOnCharacterization(args, run, dets) nimgs = 0 for nev, evt in enumerate(run.events()): img = dets._camraw(evt) logger.info('Event %03d' % nev) logger.debug(info_ndarr(img, 'camera raw:')) if img is None: continue if not lon.processEvent(evt): continue t, power, agr, pulse = lon.resultsProcessImage() print('%sAgreement:%7.3f%% Max power: %g GW Pulse Delay: %.3f ' % (12 * ' ', agr * 100, np.amax(power), pulse[0])) nimgs += 1 if nimgs >= max_shots: break
def _loadDarkReference(self): """ Loads the dark reference from file or DB. """ self._darkreference = None if self.dark_reference_path: self._darkreference = DarkBackgroundReference.load( self.dark_reference_path) logger.info('Using file ' + self.dark_reference_path.split('/')[-1] + ' for dark reference') if self._darkreference is None: #dark_data, dark_meta = self._camera.calibconst.get('xtcav_pedestals') dark_data, dark_meta = xtup.get_calibconst(self._camera, 'xtcav_pedestals', cons.DETNAME, self.run.expt, self.run.runnum) self._darkreference = xtu.xtcav_calib_object_from_dict(dark_data) logger.debug('==== dark_meta:\n%s' % str(dark_meta)) logger.debug('==== dir(_darkreference):\n%s' % str(dir(self._darkreference))) logger.debug('==== _darkreference.ROI:\n%s' % str(self._darkreference.ROI)) logger.debug( info_ndarr(self._darkreference.image, '==== darkreference.image:')) logger.info('Using dark reference from DB')
def test_mask(args): ds, run, det = ds_run_det(args) #mask = det.raw._mask_from_status() #raw = det.raw.raw() #mask = det.raw._mask_calib() #mask_edges = det.raw._mask_edges(mask, edge_rows=20, edge_cols=10) #mask = det.raw._mask_edges(edge_rows=20, edge_cols=10, center_rows=4, center_cols=2) #mask = det.raw._mask(calib=True, status=True, edges=True,\ # edge_rows=20, edge_cols=10, center_rows=4, center_cols=2) mask = det.raw._mask_comb(mbits=0o7,\ edge_rows=20, edge_cols=10, center_rows=4, center_cols=2) print(info_ndarr(mask, 'mask ')) if args.dograph: from psana.detector.UtilsGraphics import gr, fleximage evnum, evt = None, None for evnum, evt in enumerate(run.events()): if evt is None: print('Event %d is None' % evnum) continue print('Found non-empty event %d' % evnum) break if evt is None: exit('ALL events are None') #arr = det.raw.raw(evt) arr = mask + 1 img = det.raw.image(evt, nda=arr, pix_scale_size_um=args.pscsize, mapmode=args.mapmode) flimg = fleximage(img, arr=None, h_in=8, amin=-1, amax=2) #, cmap='jet') gr.show() if args.ofname is not None: gr.save_fig(flimg.fig, fname=args.ofname, verb=True)
def loop_run_step_evt(args): """Data access example for confluence run, step, event loops """ typeinfo = args.typeinfo.lower() do_loopruns = 'r' in typeinfo do_loopevts = 'e' in typeinfo do_loopsteps = 's' in typeinfo from psana.pyalgos.generic.NDArrUtils import info_ndarr #from psana import DataSource #ds = DataSource(exp=args.expt, run=args.run, dir=f'/cds/data/psdm/{args.expt[:3]}/{args.expt}/xtc', max_events=1000) ds = DataSource(**datasource_arguments(args)) if do_loopruns: for irun, run in enumerate(ds.runs()): print('\n==== %02d run: %d exp: %s detnames: %s' % (irun, run.runnum, run.expt, ','.join(run.detnames))) if not do_loopsteps: continue print('%s detector object' % args.detname) det = run.Detector(args.detname) is_epix10ka = 'epix' in det.raw._uniqueid try: step_docstring = run.Detector('step_docstring') except: step_docstring = None print('step_docstring detector object is %s' % ('missing' if step_docstring is None else 'created')) dcfg = ue.config_object_epix10ka(det) if is_epix10ka else None for istep, step in enumerate(run.steps()): print('\nStep %02d' % istep, end='') metadic = None if step_docstring is None else json.loads( step_docstring(step)) print(' metadata: %s' % str(metadic)) if not do_loopevts: continue ievt, evt, segs = None, None, None for ievt, evt in enumerate(step.events()): #if ievt>args.evtmax: exit('exit by number of events limit %d' % args.evtmax) if not selected_record(ievt): continue if segs is None: segs = det.raw._segment_numbers(evt) print(' Event %05d %s' % (ievt, info_ndarr(segs, 'segments '))) #print('gain mode statistics:' + ue.info_pixel_gain_mode_statistics(gmaps)) if dcfg is not None: s = ' gain mode fractions for: FH FM FL'\ ' AHL-H AML-M AHL-L AML-L\n%s' % (29*' ') print( ue.info_pixel_gain_mode_fractions( dcfg, data=det.raw.raw(evt), msg=s)) print(info_det_evt(det, evt, ievt), end='\r') print(info_det_evt(det, evt, ievt), end='\n')
def test_raw(args): ds, run, det = ds_run_det(args) for stepnum, step in enumerate(run.steps()): print('%s\nStep %1d' % (50 * '_', stepnum)) for evnum, evt in enumerate(step.events()): if evnum > args.events: exit('exit by number of events limit %d' % args.events) if not selected_record(evnum): continue print('%s\nEvent %04d' % (50 * '_', evnum)) segs = det.raw._segment_numbers(evt) raw = det.raw.raw(evt) logger.info(info_ndarr(segs, 'segs ')) logger.info(info_ndarr(raw, 'raw ')) print(50 * '-')
def test_pedestas_difference_between_dark_runs(run1, run2): from psana.pyalgos.generic.NDArrUtils import info_ndarr from psana.pscalib.calib.MDBWebUtils import calib_constants_all_types #d = calib_constants_all_types("epixquad", exp="ueddaq02", run=108) dcc1 = calib_constants_all_types("epix_000001", exp="ueddaq02", run=93) dcc1 = calib_constants_all_types("epix10ka_000001", exp="ueddaq02", run=run1) dcc2 = calib_constants_all_types("epix10ka_000001", exp="ueddaq02", run=run2) print(50*'-') print('d.keys():',dcc1.keys()) print(50*'-') peds1 = dcc1['pedestals'][0] meta1 = dcc1['pedestals'][1] print('meta1', meta1) print(info_ndarr(peds1, 'peds1: ')) print(50*'-') peds2 = dcc2['pedestals'][0] meta2 = dcc2['pedestals'][1] print('meta2', meta2) print(info_ndarr(peds2, 'peds2: ')) peds_diff = peds2 - peds1 print(info_ndarr(peds_diff, 'pedestal difference between runs (%d - %d): ' % (run2, run1))) for ig in range(5): arr = peds_diff[ig,:] amplimits = arr_median_limits(arr, nneg=50, npos=50) fig, axhi, hi = gr.hist1d(arr, bins=None, amp_range=amplimits, weights=None, color=None, show_stat=True, log=False, figsize=(6,5), axwin=(0.15, 0.12, 0.78, 0.80), title=None, xlabel=None, ylabel=None, titwin=None) #gr.move_fig(fig, x0=ig*100, y0=10) title = 'peds-diff-runs-%04d-%04d-igain-%d' % (run2, run1, ig) gr.set_win_title(fig, titwin=title) gr.add_title_labels_to_axes(axhi, title=title)#, xlabel=None, ylabel=None, fslab=14, fstit=20, color='k') gr.show()#mode='non-hold') gr.save_fig(fig, fname=title+'.png', verb=True) fig.clf()
def change_value(self, item, key, path): logger.debug('change_value for key: %s' % (key)) if key == self.data_fname: item.setText(str(path)) self.data_nda = self.load_nda_from_file(path) logger.info(info_ndarr(self.data_nda, 'From file %s loaded array' % path)) self.set_metadata_values() item.setBackground(QBrush(Qt.cyan)) else: txt = gu.load_textfile(path) logger.info('From file %s fill field: %s' % (path,txt)) item.setText(txt)
def det_calib_constants(det, ctype): #ctype = 'pixel_rms', 'pixel_status', 'pedestals', 'pixel_gain', 'geometry' calib_const = det.calibconst if hasattr(det, 'calibconst') else None if calib_const is not None: logger.info('det.calibconst.keys(): ' + str(calib_const.keys())) cdata, cmeta = calib_const[ctype] logger.info('%s meta: %s' % (ctype, str(cmeta))) logger.info(info_ndarr(cdata, '%s data' % ctype)) return cdata, cmeta else: logger.debug('det.calibconst is None') return None, None
def info_gain_mode_arrays(gmaps, first=0, last=5): gr0, gr1, gr2, gr3, gr4, gr5, gr6 = gmaps return 'gain range arrays:\n %s\n %s\n %s\n %s\n %s\n %s\n %s'%(\ info_ndarr(gr0, 'gr0', first, last),\ info_ndarr(gr1, 'gr1', first, last),\ info_ndarr(gr2, 'gr2', first, last),\ info_ndarr(gr3, 'gr3', first, last),\ info_ndarr(gr4, 'gr4', first, last),\ info_ndarr(gr5, 'gr5', first, last),\ info_ndarr(gr6, 'gr6', first, last))
def show_document(self, dbname, colname, doc): """Implementation of the abstract method in CMWDBDocsBase """ #CMWDBDocsBase.show_documents(self, dbname, colname, docs) msg = 'Show document for db: %s col: %s'%(dbname, colname) logger.debug(msg) if doc.get('id_data', None) is not None: doc[self.data_fname] = '' #for doc in docs: print(doc) self.fill_table_model(doc) self.data_nda = dbu.get_data_for_doc(dbname, doc) logger.debug(info_ndarr(self.data_nda, 'array from DB linked to the document'))
def on_color_table_changed(self): w = cp.ivspectrum if w is None: logger.debug('on_color_table_changed - do nothing here') return ctab = w.wcbar.color_table() logger.debug( info_ndarr(ctab, 'on_color_table_changed: new color table')) w = cp.ivimageaxes if w is not None: wi = w.wimg wi.set_coltab(coltab=ctab) rs = wi.scene().sceneRect() # preserve current scene rect wi.set_pixmap_from_arr(wi.arr, set_def=False) wi.set_rect_scene(rs, set_def=False)
def on_item_selected(self, selected, deselected): logger.debug('on_item_selected') wtree = cp.h5vmain.wtree self.enable_buts() itemsel = wtree.model.itemFromIndex(selected) if itemsel is None: return if isinstance(itemsel.data(), wtree.h5py.Dataset): logger.debug('data.value:\n%s' % str(itemsel.data()[()])) data = self.data = itemsel.data()[()] dname = self.dname = wtree.full_path(itemsel) logger.info(info_ndarr(data, 'data:')) logger.info('full name: %s' % dname) is_good_to_save = isinstance(data, np.ndarray) and data.size > 1 self.enable_buts(is_good_to_save)
def image_from_ndarray(nda): if nda is None: logger.warning('nda is None - return None for image') return None if not isinstance(nda, np.ndarray): logger.warning( 'nda is not np.ndarray, type(nda): %s - return None for image' % type(nda)) return None img = psu.table_nxn_epix10ka_from_ndarr(nda) if (nda.size % (352*384) == 0) else\ psu.table_nxm_jungfrau_from_ndarr(nda) if (nda.size % (512*1024) == 0) else\ psu.table_nxm_cspad2x1_from_ndarr(nda) if (nda.size % (185*388) == 0) else\ reshape_to_2d(nda) logger.debug(info_ndarr(img, 'img')) return img
def test_insert_one(tname): """Insert one calibration data in data base. """ data = None if tname == '1': data = get_test_txt() logger.debug('txt:', data) elif tname == '2': data = get_test_nda() logger.debug(info_ndarr(data, 'nda')) elif tname == '3': data = get_test_dic() logger.debug('dict:', data) #insert_calib_data(data, host=cc.HOST, port=cc.PORT, experiment='cxi12345', detector='camera-0-cxids1-0',\ # run='10', ctype='pedestals', time_sec=str(int(time())), verbose=True) insert_constants(data, 'cxi12345', 'camera-0-cxids1-0', 'pedestals', '10', '1600000000', verbose=True,\ time_stamp='2018-01-01T00:00:00-0800', )
def info_xtcav_object(o, space=' '): '''returns str with info about xtcav "namespace" object ''' atrs = [k for k in dir(o) if k[0] != '_'] s = '\n%s Attrs: %s' % (space, str(atrs)) for name in atrs: v = getattr(o, name, None) tv = type(v) sv = info_ndarr(v) if isinstance(v, np.ndarray) else\ str(v) if isinstance(v, (float, int, bool, np.int64, np.float64)) else\ str(tv) #str(v(0)) if name in ('index', 'count') else\ #str(v()) if isinstance(v, types.BuiltinFunctionType) else\ s += '\n%s attr:%32s value: %s' % (space, name, sv) #if tv in (np.ndarray, float, int, bool) : continue if name in ('physical_units', 'roi', 'shot_to_shot', 'roi'): s += info_xtcav_object(v, space=space + ' ') return s
def on_but_save(self): logger.debug('on_but_save') prefix = self.dname data = self.data logger.info(info_ndarr(data, 'data:')) logger.info('data name prefix: %s' % prefix) from psana.graphqt.QWUtils import change_check_box_dict_in_popup_menu control = {'txt': True, 'npy': True} resp = change_check_box_dict_in_popup_menu(control, 'Select and confirm',\ msg='save selected data in file\n%s\nfor types:'%prefix, parent=None) #self.but_save) if resp == 1: logger.info('save hdf5 data in file(s) with prefix: %s' % prefix) fmt = '%d' if 'int' in str(data.dtype) else '%.3f' save_data_in_file(data, prefix, control, fmt) cp.last_selected_fname.setValue('%s.npy' % prefix) else: logger.info('command "Save" is cancelled')
def save_doc(self): """Saves document metadata and data in files """ logger.debug('In save_doc') wdoce = cp.cmwdbdoceditor if wdoce is None or wdoce.data_nda is None: logger.warning( 'Document editor is not available. Select collection in DB then document in List mode.' ) return dicdoc = wdoce.get_model_dicdoc(discard_id_ts=False) nda = wdoce.get_data_nda() msg = '\n '.join( ['%12s : %s' % (k, dicdoc[k]) for k in sorted(dicdoc.keys())]) logger.info('Save array and document metadata in files\n%s \n%s' % (msg, info_ndarr(nda, 'data n-d array '))) prefix = self.out_fname_prefix(dicdoc) d = {'data': True, 'meta': True} resp = change_check_box_dict_in_popup_menu(d, 'Select and confirm',\ msg='Save current document in file\n%s\nfor types:'%prefix, parent=self.but_add) if resp == 1: logger.info( 'Save document data and metadata in files with prefix: %s' % prefix) for k in d.keys(): if d[k]: fname = '%s.%s' % (prefix, k) logger.debug('save_doc %s in file %s' % (k, fname)) if k == 'data': save_txt(fname, nda, cmts=(), fmt='%.1f') if k == 'meta': save_textfile(msg, fname, mode='w') else: logger.warning('Command "Save" is cancelled')
def _mask_edges(self, edge_rows=1, edge_cols=1, center_rows=0, center_cols=0, dtype=DTYPE_MASK, **kwa): """ Parameters ---------- edge_rows: int number of edge rows to mask on both side of the panel edge_cols: int number of edge columns to mask on both side of the panel center_rows: int number of edge rows to mask for all ASICs center_cols: int number of edge columns to mask for all ASICs dtype: numpy dtype of the output array **kwa: is not used Returns ------- mask: np.ndarray, ndim=3, shaped as full detector data, mask of the panel and asic edges """ logger.debug('epix10ka_base._mask_edges') mask1 = seg.pixel_mask_array(edge_rows, edge_cols, center_rows, center_cols, dtype) nsegs = self._number_of_segments_total() if nsegs is None: logger.debug('_number_of_segments_total is None') return None logger.info('_mask_edges for %d-segment epix10ka'%nsegs) mask = np.stack([mask1 for i in range(nsegs)]) logger.info(info_ndarr(mask, '_mask_edges ')) return mask
def on_changed_fname_nda(self, fname): logger.debug('on_changed_fname_nda: %s' % fname) wia = cp.ivimageaxes if wia is not None: nda = psu.load_ndarray_from_file(fname) cp.last_selected_fname.setValue(fname) logger.debug(info_ndarr(nda, 'nda')) img = image_from_ndarray(nda) self.wctl_spec.set_amin_amax_def(nda.min(), nda.max()) mode, nbins, amin, amax, frmin, frmax = self.spectrum_parameters() wia.wimg.set_pixmap_from_arr(img, set_def=True, amin=amin, amax=amax, frmin=frmin, frmax=frmax) h, w = img.shape aspect = float(w) / h if aspect > 1.5 or aspect < 0.7: ww = max(h, w) rs = QRectF(-0.5 * (ww - min(h, w)), 0, ww, ww) wia.wimg.set_rect_scene(rs, set_def=False)
def on_but_view(self): logger.debug(info_ndarr(self.data, 'on_but_view data:')) if cp.cmwmaintabs is not None: cp.cmwmaintabs.view_data(data=self.data)
def __init__(self, args): #self.args = args #fname = getattr(args, 'fname', '/reg/g/psdm/detector/data2_test/xtc/data-amox23616-r0104-e000400-xtcav-v2.xtc2') experiment = getattr(args, 'experiment', 'amox23616') run_number = getattr(args, 'run_number', 104) max_shots = getattr(args, 'max_shots', 400) validity_range = getattr(args, 'validity_range', None) save_to_file = getattr(args, 'save_to_file', True) calibration_path = getattr(args, 'calibration_path', '') start_image = getattr(args, 'start_image', 0) self.image = None self.ROI = None self.n = 0 self.parameters = DarkBackgroundParameters( experiment=experiment, max_shots=max_shots, run_number=run_number, validity_range=validity_range, calibration_path=calibration_path) """ After setting all the parameters, this method has to be called to generate the dark reference and save it in the proper location. """ logger.info('dark background reference') #logger.info('\t Data file: %s' % fname) logger.info('\t Experiment: %s' % self.parameters.experiment) logger.info('\t Run: %s' % self.parameters.run_number) logger.info('\t Valid shots to process: %d' % self.parameters.max_shots) logger.info('\t Detector name: %s' % cons.DETNAME) #Loading the dataset from the "dark" run, this way of working should be compatible with both xtc and hdf5 files #ds=DataSource(files=fname) ds = DataSource(exp=self.parameters.experiment, run=self.parameters.run_number) run = next(ds.runs()) logger.info('\t RunInfo expt: %s runnum: %d\n' % (run.expt, run.runnum)) #Camera and type for the xtcav images camera = run.Detector(cons.DETNAME) #ebeam = run.Detector(cons.EBEAM) #eventid = run.Detector(cons.EVENTID) #gasdetector = run.Detector(cons.GAS_DETECTOR) xtcavroipars = xtup.get_roi_parameters(run) #Stores for environment variables #configStore=dataSource.env().configStore() #epicsStore=dataSource.env().epicsStore() print('\n', 100 * '_', '\n') #camraw = xtup.get_attribute(camera, 'raw') #valsxtp = xtup.get_attribute(xtcavpars, 'valsxtp') #valsebm = xtup.get_attribute(ebeam, 'valsebm') #valseid = xtup.get_attribute(eventid, 'valseid') #valsgd = xtup.get_attribute(gasdetector, 'valsgd') #if camraw is None : # valsebm, eventid, valsgd) : # sys.error('FATAL ERROR IN THE DETECTOR INTERFACE: MISSING ATTRIBUTE MUST BE IMPLEMENTED') accumulator_xtcav = None n = 0 #Counter for the total number of xtcav images processed for nev, evt in enumerate(run.events()): #print('Event %03d'%nev, end='') img = camera.raw.value(evt) if img is None: continue if self.ROI is None: if not self._getImageROI(nev, evt, xtcavroipars): continue accumulator_xtcav = np.zeros((self.ROI.yN, self.ROI.xN), dtype=np.float64) accumulator_xtcav += img n += 1 if n % 5 == 0: sys.stdout.write('\r%.1f %% done, %d / %d' % (float(n) / self.parameters.max_shots * 100, n, self.parameters.max_shots)) sys.stdout.flush() if n >= self.parameters.max_shots: break #At the end of the program the total accumulator is saved sys.stdout.write('\nMaximum number of images processed\n') self.image = accumulator_xtcav / n if not self.parameters.validity_range or not type( self.parameters.validity_range) == tuple: self.parameters = self.parameters._replace( validity_range=(self.parameters.run_number, 9999)) #'end')) elif len(self.parameters.validity_range) == 1: self.parameters = self.parameters._replace( validity_range=(self.parameters.validity_range[0], 9999)) #'end')) logger.info(info_ndarr(self.image, 'averaged raw:')) logger.info('self.parameters: %s' % str(self.parameters)) if save_to_file: #cp = CalibrationPaths(dataSource.env(), self.parameters.calibration_path) #fname = cp.newCalFileName(cons.DB_FILE_NAME, self.parameters.validity_range[0], self.parameters.validity_range[1]) fname = 'cons-%s-%04d-xtcav-pedestals.data' % (run.expt, run.runnum ) #, cons.DETNAME) self.save(fname)
def procEvents(args): #fname = getattr(args, 'fname', '/reg/g/psdm/detector/data2_test/xtc/data-amox23616-r0137-e000100-xtcav-v2.xtc2') #fname_loff = getattr(args, 'fname_loff', '/reg/g/psdm/detector/data2_test/xtc/data-amox23616-r0131-e000200-xtcav-v2.xtc2') nevents = getattr(args, 'nevents', 100) mode = getattr(args, 'mode', 'smd') exp = getattr(args, 'experiment', None) grmode = getattr(args, 'grmode', 1) pause = getattr(args, 'pause', 1) ds = DataSource(exp=args.experiment, run=args.run_number) run = next(ds.runs()) lon = LasingOnCharacterization(args, run, setDetectors(run)) #camraw = lon._camraw #valsebm = lon._valsebm #valsgd = lon._valsgd #valseid = lon._valseid #valsxtp = lon._valsxtp if grmode == 1: fig, axes, titles = figaxtitles() ax11, ax12, ax21, ax22, ax31, ax32, axcb11, axcb12 = axes plt.ion() # do not hold control on plt.show() fig.canvas.mpl_connect('key_press_event', press) nimgs = 0 for nev, evt in enumerate(run.events()): raw = lon._camera.raw.value(evt) logger.info('Event %03d' % nev) logger.debug(info_ndarr(raw, 'camera raw:')) if raw is None: continue if not lon.processEvent(evt): continue nimgs += 1 if nimgs >= nevents: break time, power, agreement, pulse = lon.resultsProcessImage() #time, power = lon.xRayPower(method="COM") #agreement = lon.reconstructionAgreement() print('%sAgreement:%7.3f%% Max power: %g GW Pulse Delay: %.3f ' % (12 * ' ', agreement * 100, np.amax(power), pulse[0])) #TODO: f_11_ENRC is not avaliable directly #gd = valsgd(evt) #f_11_ENRC = 'N/A' if lon._gasdetector is None else valsgd.f_11_ENRC(evt) #print('Agreement:', agreement, 'Gasdet.f_11_ENRC:', f_11_ENRC) if agreement < 0.5: continue results = lon._pulse_characterization #raw_off = getLasingOffShot(lon, fname_loff) profiles = lon._lasingoffreference.averaged_profiles #print('XXX profiles.eCOMslice', profiles.eCOMslice) if grmode == 0: fig, axes, titles = figaxtitles() ax11, ax12, ax21, ax22, ax31, ax32, axcb11, axcb12 = axes for ax in axes: ax.cla() for ax, title in zip(axes[:6], titles): ax.set_title(title, color='k', fontsize=12) #img_loff = profiles.eCOMslice[0]; ax12.set_title('Lasing Off: eCOMslice', color='k', fontsize=12) #img_loff = profiles.eRMSslice[0]; ax12.set_title('Lasing Off: eRMSslice', color='k', fontsize=12) img_loff = profiles.eCurrent[0] ax12.set_title('Lasing Off: eCurrent', color='k', fontsize=12) imsh11 = ax11.imshow(raw, interpolation='nearest', aspect='auto', origin='upper', extent=None, cmap='inferno') cbar11 = fig.colorbar(imsh11, cax=axcb11, orientation='vertical') imsh12 = ax12.imshow(img_loff, interpolation='nearest', aspect='auto', origin='upper', extent=None, cmap='inferno') cbar12 = fig.colorbar(imsh12, cax=axcb12, orientation='vertical') ax21.plot(time[0], results.lasingECurrent[0], label='lasing') ax21.plot(time[0], results.nolasingECurrent[0], label='nolasing') ax22.plot(time[0], results.lasingECOM[0], label='lasing') ax22.plot(time[0], results.nolasingECOM[0], label='nolasing') ax31.plot(time[0], results.lasingERMS[0], label='lasing') ax31.plot(time[0], results.nolasingERMS[0], label='nolasing') ax32.plot(time[0], power[0]) # LCLS1 #plt.subplot(3,2,1) #plt.title('Lasing On') #plt.imshow(raw) #xtcav_lasingoff = getLasingOffShot(lon, fname_loff) #plt.subplot(3,2,2) #plt.title('Lasing Off') #plt.imshow(xtcav_lasingoff) #plt.subplot(3,2,3) #plt.title('Current') #plt.plot(time[0],results.lasingECurrent[0],label='lasing') #plt.plot(time[0],results.nolasingECurrent[0],label='nolasing') ##plt.legend() #plt.subplot(3,2,4) #plt.title('E (Delta)') #plt.plot(time[0],results.lasingECOM[0],label='lasing') #plt.plot(time[0],results.nolasingECOM[0],label='nolasing') ##plt.legend() #plt.subplot(3,2,5) #plt.title('E (Sigma)') #plt.plot(time[0],results.lasingERMS[0],label='lasing') #plt.plot(time[0],results.nolasingERMS[0],label='nolasing') ##plt.legend() #plt.subplot(3,2,6) #plt.title('Power') #plt.plot(time[0],power[0]) fig.canvas.set_window_title('Event %3d good %3d' % (nev, nimgs)) #fig.canvas.draw() #plt.show(block=False) plt.draw() if grmode == 0: print( '\nConntinue - close graphics window (click on [x] on window frame)' ) plt.show() elif grmode == 1: #print('\nPAUSE %s: ' % CONTROL.PAUSE) print( '\nControl keys: e-exit, h/p/d-hold/pause/delay, c/g-continue/go' ) plt.pause( pause ) # hack to make it work... othervise show() does not work... while CONTROL.PAUSE: plt.pause(1) #ch = input_single_char('Next event? [y/n]') #if ch == 'y': pass #else : sys.exit('Exit by request') plt.ioff( ) # hold contraol at show(); plt.ion() is set on any keyboard key press event plt.show()