def getDetectorInformation(exp, runnum, det): """ Returns the detector, the peak finding algorithm, and the number of events for this run. Arguments: exp -- the experiment name runnum -- the run number for this experiment det -- the detector used for this experiment """ ds = psana.DataSource('exp=%s:run=%d:idx' % (exp, runnum)) d = psana.Detector(det) d.do_reshape_2d_to_3d(flag=True) alg = PyAlgos(mask=None, pbits=0) alg.set_peak_selection_pars(npix_min=2, npix_max=30, amax_thr=300, atot_thr=600, son_min=10) hdr = '\nSeg Row Col Npix Amptot' fmt = '%3d %4d %4d %4d %8.1f' run = ds.runs().next() times = run.times() env = ds.env() numEvents = len(times) mask = d.mask(runnum, calib=True, status=True, edges=True, central=True, unbond=True, unbondnbrs=True) return [d, alg, hdr, fmt, numEvents, mask, times, env, run]
def algorithm(self, **kwargs): """ Initialize the peakfinding algorithim with keyword arguments given by the user, then run the peakfinding algorithm on random sets of experiment runs Arguments: **kwargs -- a dictionary of arguments containing peakfinding parameters """ npxmin = kwargs["npix_min"] npxmax = kwargs["npix_max"] amaxthr = kwargs["amax_thr"] atotthr = kwargs["atot_thr"] sonmin = kwargs["son_min"] alg = PyAlgos(mask = None, pbits = 0) alg.set_peak_selection_pars(npix_min=npxmin, npix_max=npxmax, amax_thr=amaxthr, atot_thr=atotthr, son_min=sonmin) #(npix_min=2, npix_max=30, amax_thr=300, atot_thr=600, son_min=10) self.run(alg, **kwargs)
def test_pf(tname): ##----------------------------- PF = V4 # default if tname == '1': PF = V1 if tname == '2': PF = V2 if tname == '3': PF = V3 if tname == '4': PF = V4 SKIP = 0 EVTMAX = 5 + SKIP DO_PLOT_IMAGE = True DO_PLOT_PIXEL_STATUS = False #True if PF in (V2,V4) else False DO_PLOT_CONNECED_PIXELS = False #True if PF in (V2,V3,V4) else False DO_PLOT_LOCAL_MAXIMUMS = False #True if PF == V3 else False DO_PLOT_LOCAL_MINIMUMS = False #True if PF == V3 else False shape = (1000, 1000) # Pixel image indexes #arr3d = np.array((1,shape[0],shape[1])) INDS = np.indices((1, shape[0], shape[1]), dtype=np.int64) imRow, imCol = INDS[1, :], INDS[2, :] #iX = np.array(det.indexes_x(evt), dtype=np.int64) #- xoffset #iY = np.array(det.indexes_y(evt), dtype=np.int64) #- yoffset fs = (8, 7) # (11,10) ##----------------------------- fig5, axim5, axcb5, imsh5 = gg.fig_axim_axcb_imsh( figsize=fs) if DO_PLOT_LOCAL_MINIMUMS else (None, None, None, None) fig4, axim4, axcb4, imsh4 = gg.fig_axim_axcb_imsh( figsize=fs) if DO_PLOT_LOCAL_MAXIMUMS else (None, None, None, None) fig3, axim3, axcb3, imsh3 = gg.fig_axim_axcb_imsh( figsize=fs) if DO_PLOT_CONNECED_PIXELS else (None, None, None, None) fig2, axim2, axcb2, imsh2 = gg.fig_axim_axcb_imsh( figsize=fs) if DO_PLOT_PIXEL_STATUS else (None, None, None, None) fig1, axim1, axcb1, imsh1 = gg.fig_axim_axcb_imsh( figsize=fs) if DO_PLOT_IMAGE else (None, None, None, None) ##----------------------------- alg = PyAlgos(windows=None, mask=None, pbits=10) # 0177777) if PF == V1: alg.set_peak_selection_pars(npix_min=0, npix_max=1e6, amax_thr=0, atot_thr=0, son_min=6) elif PF == V2: alg.set_peak_selection_pars(npix_min=0, npix_max=1e6, amax_thr=0, atot_thr=0, son_min=6) elif PF == V3: alg.set_peak_selection_pars(npix_min=0, npix_max=1e6, amax_thr=0, atot_thr=0, son_min=8) elif PF == V4: alg.set_peak_selection_pars(npix_min=0, npix_max=1e6, amax_thr=0, atot_thr=0, son_min=6) alg.print_attributes() for ev in range(EVTMAX): ev1 = ev + 1 if ev < SKIP: continue #if ev>=EVTMAX : break print 50 * '_', '\nEvent %04d' % ev1 img, peaks_sim = image_with_random_peaks(shape) # --- for debugging #np.save('xxx-image', img) #np.save('xxx-peaks', np.array(peaks_sim)) #img = np.load('xxx-image-crash.npy') #peaks_sim = np.load('xxx-peaks-crash.npy') # --- peaks_gen = [(0, r, c, a, a * s, 9 * s * s) for r, c, a, s in peaks_sim] t0_sec = time() #peaks = alg.peak_finder_v1(img, thr_low=20, thr_high=40, radius=6, dr=2) if PF == V1 else\ # alg.peak_finder_v2r1(img, thr=30, r0=7, dr=2) if PF == V2 else\ # alg.peak_finder_v3r2(img, rank=5, r0=7, dr=2, nsigm=3) if PF == V3 else\ # alg.peak_finder_v4r2(img, thr_low=20, thr_high=40, rank=6, r0=7, dr=2) peaks = None if PF == V1 else\ None if PF == V2 else\ alg.peak_finder_v3r3(img, rank=5, r0=7, dr=2, nsigm=3) if PF == V3 else\ alg.peak_finder_v4r3(img, thr_low=20, thr_high=40, rank=6, r0=7, dr=2) print ' Time consumed by the peak_finder = %10.6f(sec)' % (time() - t0_sec) map2 = reshape_to_2d(alg.maps_of_pixel_status( )) if DO_PLOT_PIXEL_STATUS else None # np.zeros((10,10)) map3 = reshape_to_2d(alg.maps_of_connected_pixels( )) if DO_PLOT_CONNECED_PIXELS else None # np.zeros((10,10)) map4 = reshape_to_2d(alg.maps_of_local_maximums( )) if DO_PLOT_LOCAL_MAXIMUMS else None # np.zeros((10,10)) map5 = reshape_to_2d(alg.maps_of_local_minimums( )) if DO_PLOT_LOCAL_MINIMUMS else None # np.zeros((10,10)) print 'arrays are extracted' #print_arr(map2, 'map_of_pixel_status') #print_arr(map3, 'map_of_connected_pixels') #maps.shape = shape print 'Simulated peaks:' for i, (r0, c0, a0, sigma) in enumerate(peaks_sim): print ' %04d row=%6.1f col=%6.1f amp=%6.1f sigma=%6.3f' % ( i, r0, c0, a0, sigma) #plot_image(img) print 'Found peaks:' print hdr reg = 'IMG' for pk in peaks: seg,row,col,npix,amax,atot,rcent,ccent,rsigma,csigma,\ rmin,rmax,cmin,cmax,bkgd,rms,son = pk[0:17] rec = fmt % (ev, reg, seg, row, col, npix, amax, atot, rcent, ccent, rsigma, csigma,\ rmin, rmax, cmin, cmax, bkgd, rms, son) #,\ #imrow, imcol, xum, yum, rum, phi) print rec if DO_PLOT_PIXEL_STATUS: gg.plot_imgcb(fig2, axim2, axcb2, imsh2, map2, amin=0, amax=30, title='Pixel status, ev: %04d' % ev1) gg.move_fig(fig2, x0=0, y0=30) if DO_PLOT_CONNECED_PIXELS: cmin, cmax = (map3.min(), map3.max()) if map3 is not None else (None, None) print 'Connected pixel groups min/max:', cmin, cmax gg.plot_imgcb(fig3, axim3, axcb3, imsh3, map3, amin=cmin, amax=cmax, title='Connected pixel groups, ev: %04d' % ev1) gg.move_fig(fig3, x0=100, y0=30) if DO_PLOT_LOCAL_MAXIMUMS: gg.plot_imgcb(fig4, axim4, axcb4, imsh4, map4, amin=0, amax=10, title='Local maximums, ev: %04d' % ev1) gg.move_fig(fig4, x0=200, y0=30) if DO_PLOT_LOCAL_MINIMUMS: gg.plot_imgcb(fig5, axim5, axcb5, imsh5, map5, amin=0, amax=10, title='Local minimums, ev: %04d' % ev1) gg.move_fig(fig5, x0=300, y0=30) if DO_PLOT_IMAGE: #nda = maps_of_conpix_arc #nda = maps_of_conpix_equ #img = det.image(evt, nda)[xoffset:xoffset+xsize,yoffset:yoffset+ysize] #img = det.image(evt, mask_img*nda)[xoffset:xoffset+xsize,yoffset:yoffset+ysize] #img = det.image(evt, maps_of_conpix_equ)[xoffset:xoffset+xsize,yoffset:yoffset+ysize] ave, rms = img.mean(), img.std() amin, amax = ave - 1 * rms, ave + 8 * rms axim1.clear() if imsh1 is not None: del imsh1 imsh1 = None gg.plot_imgcb(fig1, axim1, axcb1, imsh1, img, amin=amin, amax=amax, title='Image, ev: %04d' % ev1) gg.move_fig(fig1, x0=400, y0=30) gg.plot_peaks_on_img(peaks_gen, axim1, imRow, imCol, color='g', lw=5) #, pbits=3) gg.plot_peaks_on_img(peaks, axim1, imRow, imCol, color='w') #, pbits=3) fig1.canvas.draw() # re-draw figure content #gg.plotHistogram(nda, amp_range=(-100,100), bins=200, title='Event %d' % i) gg.show(mode='do not hold') gg.show()
Rum = np.sqrt(Xum*Xum + Yum*Yum) Phi = np.arctan2(Yum,Xum) * 180 / np.pi imRow.shape = imCol.shape = \ Xum.shape = Yum.shape = \ Rum.shape = Phi.shape = det.shape() #------------------------------ fig1, axim1, axcb1, imsh1 = gg.fig_axim_axcb_imsh(figsize=(12,11)) #------------------------------ alg = None if TEST_BW_COMP : print "BACKWARD COMPATABILITY TEST" #alg = PyAlgos(windows=None, mask=mask, pbits=0) # for ImgAlgos.PyAlgos alg = PyAlgos(mask, pbits=0) if PF == V3 : alg.set_peak_selection_pars(npix_min=2, npix_max=1e6, amax_thr=0, atot_thr=0, son_min=10) elif PF == V4 : alg.set_peak_selection_pars(npix_min=2, npix_max=1e6, amax_thr=0, atot_thr=0, son_min=5) else : print "NEW PF TEST" t0_sec_evloop = time() nda = None peaks = None # loop over events in data set myrun = next(ds.runs()) for evnum, evt in enumerate(myrun.events()) :
def __init__(self): """ Initialize instance of imported PyAlgos algorithm. """ self.alg = PyAlgos(mask=None, pbits=0) self.setDefaultParams()
class adaptiveAlgorithm(abstractAlgorithm.abstractAlgorithm): def __init__(self): """ Initialize instance of imported PyAlgos algorithm. """ self.alg = PyAlgos(mask=None, pbits=0) self.setDefaultParams() def setParams(self): """ Use the algorithm's function, set_peak_selection_pars, to set parameters. """ self.alg.set_peak_selection_pars(npix_min=self.alg1_npix_min, npix_max=self.alg1_npix_max, amax_thr=self.alg1_amax_thr, atot_thr=self.alg1_atot_thr, son_min=self.alg1_son_min) def initParams(self, **kwargs): """ Save the values of the parameters from kwargs. Arguments: **kwargs -- dictionary of parameters, either default or user inputted """ self.alg1_npix_min = kwargs["npix_min"] self.alg1_npix_max = kwargs["npix_max"] self.alg1_amax_thr = kwargs["amax_thr"] self.alg1_atot_thr = kwargs["atot_thr"] self.alg1_son_min = kwargs["son_min"] self.alg1_rank = kwargs["rank"] self.alg1_r0 = kwargs["r0"] self.alg1_dr = kwargs["dr"] self.alg1_nsigm = kwargs["nsigm"] self.setParams() def algorithm(self, nda, mask, kw=None): """ Uses peak_finder_v3r3 (a.k.a. adaptive peak finder) to find peaks on an image. Arguments: nda -- detector image mask -- detector mask kw -- dictionary or None, if None default parameters are used, otherwise kw is used to initialize parameters """ if kw == None: self.initParams(**self.default_params) else: self.initParams(**kw) self.peaks = self.alg.peak_finder_v3r3(nda, rank=self.alg1_rank, r0=self.alg1_r0, dr=self.alg1_dr, nsigm=self.alg1_nsigm, mask=mask) return self.peaks def getDefaultParams(self): """ Return the default parameters in the form of a string, for Psocake to display """ return json.dumps(self.default_params) def setDefaultParams(self): #The default parameters for the adaptive peak finding algorithm #self.default_params_str = "{\"npix_min\": 2,\"npix_max\":30,\"amax_thr\":300, \"atot_thr\":600,\"son_min\":10, \"rank\":3, \"r0\":3, \"dr\":2, \"nsigm\":5 }" self.default_params = { "npix_min": 2, "npix_max": 30, "amax_thr": 300, "atot_thr": 600, "son_min": 10, "rank": 3, "r0": 3, "dr": 2, "nsigm": 5 }