Esempio n. 1
0
    def test_mask_neighbors_3d(allnbrs=True):

        #randexp = random_exponential(shape=(2,2,30,80), a0=1)
        randexp = random_exponential(shape=(2, 30, 80), a0=1)

        fig = gr.figure(figsize=(16, 7), title='Random > 2-d mask')
        axim1 = gr.add_axes(fig, axwin=(0.05, 0.05, 0.40, 0.91))
        axcb1 = gr.add_axes(fig, axwin=(0.452, 0.05, 0.01, 0.91))

        axim2 = gr.add_axes(fig, axwin=(0.55, 0.05, 0.40, 0.91))
        axcb2 = gr.add_axes(fig, axwin=(0.952, 0.05, 0.01, 0.91))

        mask = np.select((randexp > 6, ), (0, ), default=1)
        mask_nbrs = mask_neighbors(mask, allnbrs)

        img1 = reshape_to_2d(mask)
        img2 = reshape_to_2d(mask_nbrs)

        imsh1, cbar1 = gr.imshow_cbar(fig,
                                      axim1,
                                      axcb1,
                                      img1,
                                      amin=0,
                                      amax=10,
                                      orientation='vertical',
                                      cmap='jet')
        imsh2, cbar2 = gr.imshow_cbar(fig,
                                      axim2,
                                      axcb2,
                                      img2,
                                      amin=0,
                                      amax=10,
                                      orientation='vertical',
                                      cmap='jet')
        gr.show(mode=None)
Esempio n. 2
0
def table_nxm_jungfrau_from_ndarr(nda):
    """returns table of epix10ka panels shaped as (nxn)
       generated from epix10ka array shaped as (N, 352, 384) in data.
    """
    segsize = 512 * 1024
    a = np.array(nda)  # make a copy

    if a.size == segsize:
        a.shape = (512, 1024)
        return a

    elif a.size == 2 * segsize:
        logger.warning('jungfrau1m panels are stacked as [1,0]')
        sh = a.shape = (2, 512, 1024)
        return np.vstack([a[q, :] for q in (1, 0)])

    elif a.size == 8 * segsize:
        logger.warning(
            'jungfrau4m panels are stacked as [(7,3), (6,2), (5,1), (4,0)]')
        sh = a.shape = (8, 512, 1024)
        return np.hstack([np.vstack([a[q,:] for q in (7,6,5,4)]),\
                          np.vstack([a[q,:] for q in (3,2,1,0)])])
    else:
        from psana.pyalgos.generic.NDArrUtils import reshape_to_2d
        return reshape_to_2d(a)
Esempio n. 3
0
def table_nxm_cspad2x1_from_ndarr(nda):
    """returns table of cspad2x1 panels shaped as (nxm)
       generated from cspad array shaped as (N,185,388) in data.
    """
    segsize = 185 * 388
    a = np.array(nda)  # make a copy

    if a.size == segsize:
        a.shape = (185, 388)
        return a

    elif a.size == 2 * segsize:
        if a.shape[-1] == 2:
            from psana.pscalib.geometry.GeometryObject import data2x2ToTwo2x1  # ,two2x1ToData2x2
            a = data2x2ToTwo2x1(a)
        a.shape = (2 * 185, 388)
        return a

    elif a.size == 8 * segsize:
        sh = a.shape = (2, 4 * 185, 388)
        return np.hstack([a[q, :] for q in range(sh[0])])

    elif a.size == 32 * segsize:
        sh = a.shape = (4, 8 * 185, 388)
        return np.hstack([a[q, :] for q in range(sh[0])])

    else:
        from psana.pyalgos.generic.NDArrUtils import reshape_to_2d
        return reshape_to_2d(a)
Esempio n. 4
0
    def test_08():
        import psana.pyalgos.generic.Graphics as gg
        from psana.pyalgos.generic.NDArrGenerators import random_standard
        from psana.pyalgos.generic.NDArrUtils import reshape_to_2d

        print('%s\n%s\n' %
              (80 * '_', 'Test method locxymax(nda, order, mode):'))
        #data = random_standard(shape=(32,185,388), mu=0, sigma=10)
        data = random_standard(shape=(2, 185, 388), mu=0, sigma=10)
        t0_sec = time()
        mask = locxymax(data, order=1, mode='clip')
        print('Consumed t = %10.6f sec' % (time() - t0_sec))

        if True:
            img = data if len(data.shape) == 2 else reshape_to_2d(data)
            msk = mask if len(mask.shape) == 2 else reshape_to_2d(mask)

            ave, rms = img.mean(), img.std()
            amin, amax = ave - 2 * rms, ave + 2 * rms
            gg.plotImageLarge(img, amp_range=(amin, amax), title='random')
            gg.plotImageLarge(msk, amp_range=(0, 1), title='mask loc max')
            gg.show()
Esempio n. 5
0
def plot_det_image(det, evt, nda, tit=''):
    """Plots averaged image
    """
    import psana.pyalgos.generic.Graphics as gr
    from psana.pyalgos.generic.NDArrUtils import reshape_to_2d
    #img = det.image(evt, nda)
    img = np.array(nda)
    if img is None:
        sys.exit('Image is not available. PROCESSING IS TERMINATED')
    img = reshape_to_2d(img)

    ave, rms = nda.mean(), nda.std()
    gr.plotImageLarge(img, amp_range=(ave - 1 * rms, ave + 3 * rms), title=tit)
    gr.show()
Esempio n. 6
0
def table_nxn_epix10ka_from_ndarr(nda):
    """returns table of epix10ka panels shaped as (nxn)
       generated from epix10ka array shaped as (N, 352, 384) in data.
    """
    gapv = 20
    segsize = 352 * 384
    a = np.array(nda)  # make a copy

    if a.size == segsize:
        a.shape = (352, 384)
        return a

    elif a.size == 4 * segsize:
        logger.warning('quad panels are stacked as [(3,2),(1,0)]')
        sh = a.shape = (4, 352, 384)
        return np.vstack([np.hstack([a[3], a[2]]), np.hstack([a[1], a[0]])])
        #sh = a.shape = (2,2*352,384)
        #return np.hstack([a[q,:] for q in range(sh[0])])

    elif a.size == 16 * segsize:
        sh = a.shape = (4, 4 * 352, 384)
        return np.hstack([a[q, :] for q in range(sh[0])])

    elif a.size == 7 * 4 * segsize:
        logger.warning('quad panels are stacked as [(3,2),(1,0)]')
        agap = np.zeros((gapv, 2 * 384))
        sh = a.shape = (7, 4, 352, 384)
        return np.vstack([
            np.vstack([
                np.hstack([a[g, 3], a[g, 2]]),
                np.hstack([a[g, 1], a[g, 0]]), agap
            ]) for g in range(7)
        ])
        #sh = a.shape = (7,2,2*352,384)
        #return np.vstack([np.vstack([np.hstack([a[g,q,:] for q in range(2)]), agap]) for g in range(7)])

    elif a.size == 7 * 16 * segsize:
        agap = np.zeros((gapv, 4 * 384))
        sh = a.shape = (7, 4, 4 * 352, 384)
        return np.vstack([
            np.vstack([np.hstack([a[g, q, :] for q in range(4)]), agap])
            for g in range(7)
        ])

    else:
        from psana.pyalgos.generic.NDArrUtils import reshape_to_2d
        return reshape_to_2d(a)
Esempio n. 7
0
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
Esempio n. 8
0
    def test_mask_edges_3d(mrows=1, mcols=1):

        fig = gr.figure(figsize=(8, 7), title='Mask edges 2-d')
        axim1 = gr.add_axes(fig, axwin=(0.05, 0.05, 0.87, 0.91))
        axcb1 = gr.add_axes(fig, axwin=(0.922, 0.05, 0.01, 0.91))

        #mask = np.ones((2,2,20,30))
        mask = np.ones((2, 20, 30))
        mask_out = mask_edges(mask, mrows, mcols)

        img1 = reshape_to_2d(mask_out)
        imsh1, cbar1 = gr.imshow_cbar(fig,
                                      axim1,
                                      axcb1,
                                      img1,
                                      amin=0,
                                      amax=10,
                                      orientation='vertical',
                                      cmap='jet')
        gr.show(mode=None)
Esempio n. 9
0
 def image(self, evt, nda=None, **kwa) -> Array2d:
     logger.debug('opal_base.image')
     arr = self.calib(evt, **kwa) if nda is None else nda
     return arr if arr.ndim == 2 else reshape_to_2d(arr)
Esempio n. 10
0
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 = 10 + 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)

    mask = np.ones(shape, dtype=np.uint16)

    # 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)
    fig1, axim1, axcb1 = gr.fig_img_cbar_axes(gr.figure(
        figsize=fs)) if DO_PLOT_IMAGE else (None, None, None)
    fig2, axim2, axcb2 = gr.fig_img_cbar_axes(gr.figure(
        figsize=fs)) if DO_PLOT_PIXEL_STATUS else (None, None, None)
    fig3, axim3, axcb3 = gr.fig_img_cbar_axes(gr.figure(
        figsize=fs)) if DO_PLOT_CONNECED_PIXELS else (None, None, None)
    fig4, axim4, axcb4 = gr.fig_img_cbar_axes(gr.figure(
        figsize=fs)) if DO_PLOT_LOCAL_MAXIMUMS else (None, None, None)
    fig5, axim5, axcb5 = gr.fig_img_cbar_axes(gr.figure(
        figsize=fs)) if DO_PLOT_LOCAL_MINIMUMS else (None, None, None)
    imsh1 = None
    imsh2 = None
    imsh3 = None
    imsh4 = None
    imsh5 = None
    ##-----------------------------

    alg = peak_finder_algos(pbits=0)

    if PF == V1:
        alg.set_peak_selection_parameters(npix_min=0,
                                          npix_max=1e6,
                                          amax_thr=0,
                                          atot_thr=0,
                                          son_min=6)

    elif PF == V2:
        alg.set_peak_selection_parameters(npix_min=0,
                                          npix_max=1e6,
                                          amax_thr=0,
                                          atot_thr=0,
                                          son_min=6)

    elif PF == V3:
        alg.set_peak_selection_parameters(npix_min=0,
                                          npix_max=1e6,
                                          amax_thr=0,
                                          atot_thr=0,
                                          son_min=8)

    elif PF == V4:
        alg.set_peak_selection_parameters(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_v3r3_d2(img, mask, rank=5, r0=7, dr=2, nsigm=3)                 if PF == V3 else\
                alg.peak_finder_v4r3_d2(img, mask, thr_low=20, thr_high=40, rank=6, r0=7, dr=2) if PF == V4 else\
                None
        #alg.peak_finder_v3r3_d2(img, rank=5, r0=7, dr=2, nsigm=3)        if PF == V3 else\
        #alg.peak_finder_v4r3_d2(img, thr_low=20, thr_high=40, rank=6, r0=7, dr=2) if PF == V3 else\

        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)

        for p in peaks:
            #print('  algos:', p.parameters())
            print('  row:%4d, col:%4d, npix:%4d, son:%4.1f amp_tot:%4.1f' %
                  (p.row, p.col, p.npix, p.son, p.amp_tot))

        if DO_PLOT_PIXEL_STATUS:
            gr.plot_imgcb(fig2,
                          axim2,
                          axcb2,
                          imsh2,
                          map2,
                          amin=0,
                          amax=30,
                          title='Pixel status, ev: %04d' % ev1)
            gr.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)
            gr.plot_imgcb(fig3,
                          axim3,
                          axcb3,
                          imsh3,
                          map3,
                          amin=cmin,
                          amax=cmax,
                          title='Connected pixel groups, ev: %04d' % ev1)
            gr.move_fig(fig3, x0=100, y0=30)

        if DO_PLOT_LOCAL_MAXIMUMS:
            gr.plot_imgcb(fig4,
                          axim4,
                          axcb4,
                          imsh4,
                          map4,
                          amin=0,
                          amax=10,
                          title='Local maximums, ev: %04d' % ev1)
            gr.move_fig(fig4, x0=200, y0=30)

        if DO_PLOT_LOCAL_MINIMUMS:
            gr.plot_imgcb(fig5,
                          axim5,
                          axcb5,
                          imsh5,
                          map5,
                          amin=0,
                          amax=10,
                          title='Local minimums, ev: %04d' % ev1)
            gr.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
            gr.imshow_cbar(fig1, axim1, axcb1, img, amin=amin, amax=amax, extent=None,\
                           interpolation='nearest', aspect='auto', origin='upper',\
                           orientation='vertical', cmap='inferno')
            #gr.plot_imgcb(fig1, axim1, axcb1, imsh1, img, amin=amin, amax=amax, title='Image, ev: %04d' % ev1)
            fig1.canvas.set_window_title('Event: %04d random data' % ev1)
            gr.move_fig(fig1, x0=400, y0=30)

            #plot_peaks_on_img(peaks_gen, axim1, imRow, imCol, color='g', lw=5)
            plot_peaks_on_img(peaks, axim1, imRow, imCol, color='w', lw=1)

            fig1.canvas.draw()  # re-draw figure content

            #gr.plotHistogram(nda, amp_range=(-100,100), bins=200, title='Event %d' % i)
            gr.show(mode='do not hold')

    gr.show()