def read_data_from_binary_file(ifname   = 'data.bin',\
                               npixels  = 32*185*388,\
                               dtype    = np.int16,\
                               verbos   = True) :
    """Test read/unpack binary file.
       Binary file does not have shape, so image size in pixels and data type should be provided.
    """

    print 'Read file %s' % ifname

    BUF_SIZE_BYTE = npixels * 2

    f = open(ifname, 'rb')
    buf = f.read()
    f.close()
    nmax = len(buf) / BUF_SIZE_BYTE
    print 'len(buf)', len(buf), 'nmax', nmax

    for nevt in range(nmax):
        nda = np.frombuffer(buf,
                            dtype=dtype,
                            count=npixels,
                            offset=nevt * BUF_SIZE_BYTE)
        print_ndarr(nda, name='%4d nda' % (nevt), first=0, last=10)

#------------------------------
#------------------------------

    def print_raw(args, opts, defs):
        print 'Command:', ' '.join(sys.argv)
        print '  opts: ', opts
        print '  defs: ', defs
        print '  args: ', args
Beispiel #2
0
def test_pfv3r3(tname):

    import numpy as np
    from pyimgalgos.GlobalUtils import print_ndarr
    print 'test_pfv3r3: %s' % {
        '1': '2-d np.array',
        '2': '3-d np.array',
        '3': 'list of 2-d np.array'
    }[tname]

    sh = None
    data = None
    mask = None

    sh = (1000, 1000) if tname == '1' else (32, 185, 388)

    mu, sigma = 200, 25
    data = np.array(mu + sigma * np.random.standard_normal(sh),
                    dtype=np.double)
    mask = np.ones(sh, dtype=np.uint16)

    if tname == '3':
        data = [data[i, :, :] for i in range(sh[0])]
        mask = [mask[i, :, :] for i in range(sh[0])]

    #print 'data object', str(data)
    #print 'mask object', str(mask)
    #print 'data[0].shape', data[0].shape
    print_ndarr(data, 'input data')
    print_ndarr(mask, 'input mask')

    t0_sec = time()
    peaks = peaks_adaptive(data, mask, rank=5, r0=7.0, dr=2.0, nsigm=3,\
                           npix_min=1, npix_max=None, amax_thr=0, atot_thr=0, son_min=8)

    print 'peaks_adaptive: img.shape=%s consumed time = %.6f(sec)' % (
        str(sh), time() - t0_sec)

    for p in peaks:
        #print dir(p)
        print '  seg:%4d, row:%4d, col:%4d, npix:%4d, son:%4.1f' % (
            p.seg, p.row, p.col, p.npix, p.son)
Beispiel #3
0
def test03():
    import numpy as np
    from pyimgalgos.GlobalUtils import print_ndarr
    print 'test numpy.array'

    af8 = np.ones((5, 5), dtype=np.float64)
    print_ndarr(af8, 'input array ')
    #psalgos.test_nda_f8(af8)
    psalgos.test_nda_v1(af8)
    print_ndarr(af8, 'output array')

    ai2 = np.ones((6, 3), dtype=np.int16)
    print_ndarr(ai2, 'input array ')
    #psalgos.test_nda_i2(ai2)
    psalgos.test_nda_v1(ai2)
    print_ndarr(ai2, 'output array')
Beispiel #4
0
def make_data_binary_file(dsname   = 'exp=xpptut15:run=54',\
                          dname    = 'CxiDs2.0:Cspad.0',\
                          nevs     = 1000000,\
                          nskip    = 0,\
                          do_calib = False,\
                          ofname   = 'data.bin',\
                          verbos   = True) :

    ds = psana.DataSource(dsname)
    det = psana.Detector(dname)
    #run = ds.runs().next()

    nev_min = nskip
    nev_max = nskip + nevs

    f = open(ofname, 'wb')

    myrun = next(ds.runs())
    for nevt, evt in enumerate(myrun.events()):
        if nevt % 100 == 0: print 'Event %d' % nevt
        if nevt < nev_min: continue
        if nevt >= nev_max: break
        data = det.calib(evt) if do_calib else\
               det.raw(evt)
        if data is None: continue
        if do_calib: data = data.astype(np.int16)

        if verbos:
            ent = entropy(data)
            msg = '%4d: data entropy=%.3f' % (nevt, ent)
            print_ndarr(data, name=msg, first=0, last=10)

        #data.tofile(f)
        f.write(data.tobytes())

    f.close()
    print '%d datasets saved file %s' % (nevt, ofname)
Beispiel #5
0
    xtcav_OTRS_DMP1_695_TCAL_X[nevt] = Detector('OTRS:DMP1:695:TCAL_X')(evt)
    xtcav_XTCAV_Amp_Des_calib_MV[nevt] = Detector('XTCAV_Amp_Des_calib_MV')(
        evt)
    xtcav_SIOC_SYS0_ML01_AO214[nevt] = Detector('SIOC:SYS0:ML01:AO214')(evt)
    xtcav_XTCAV_Phas_Des_calib_deg[nevt] = Detector(
        'XTCAV_Phas_Des_calib_deg')(evt)
    xtcav_SIOC_SYS0_ML01_AO215[nevt] = Detector('XTCAV_Beam_energy_dump_GeV')(
        evt)
    xtcav_XTCAV_Beam_energy_dump_GeV[nevt] = Detector('REFS:DMP1:400:EDES')(
        evt)
    xtcav_REFS_DMP1_400_EDES[nevt] = Detector('REFS:DMP1:400:EDES')(evt)
    xtcav_XTCAV_calib_disp_posToEnergy[nevt] = Detector(
        'XTCAV_calib_disp_posToEnergy')(evt)
    xtcav_SIOC_SYS0_ML01_AO216[nevt] = Detector('SIOC:SYS0:ML01:AO216')(evt)

    raw_ds[nevt] = raw

    ###smldata.event(raw=raw) # waveforms=wfs,times=times)

    if not (nevt < EVENTS): break

print 'End of event loop, ev: %3d' % nevt
print_ndarr(raw, 'raw')

###smldata.save()
h5out.close()
print usage()
print 'saved file %s' % OFNAME

#----------
def test01(tname='1', NUMBER_OF_EVENTS=5, DO_PRINT=False):


    print 'local extrema : %s' % ('minimums' if tname=='1'\
                             else 'maximums' if tname=='2'\
                             else 'maximums runk=1 cross'  if tname=='3'\
                             else 'two-threshold maximums')

    from time import time
    from pyimgalgos.GlobalUtils import print_ndarr
    import pyimgalgos.GlobalGraphics as gg

    #sh, fs = (200,200), (11,10)
    sh, fs = (1000, 1000), (11, 10)
    #sh, fs = (185,388), (11,5)
    fig1, axim1, axcb1, imsh1 = gg.fig_axim_axcb_imsh(figsize=fs)
    fig2, axim2, axcb2, imsh2 = gg.fig_axim_axcb_imsh(figsize=fs)

    print 'Image shape: %s' % str(sh)

    mu, sigma = 200, 25

    for evnum in range(NUMBER_OF_EVENTS):

        data = np.array(mu + sigma * np.random.standard_normal(sh),
                        dtype=np.float64)
        mask = np.ones(sh, dtype=np.uint16)
        extrema = np.zeros(sh, dtype=np.uint16)
        rank = 5

        thr_low = mu + 3 * sigma
        thr_high = mu + 4 * sigma

        nmax = 0

        if DO_PRINT: print_ndarr(data, 'input data')
        t0_sec = time()
        #----------
        if tname == '1':
            nmax = algos.local_minima_2d(data, mask, rank, extrema)
        elif tname == '2':
            nmax = algos.local_maxima_2d(data, mask, rank, extrema)
        elif tname == '3':
            nmax = algos.local_maxima_rank1_cross_2d(data, mask, extrema)
        else:
            nmax = algos.threshold_maxima_2d(data, mask, rank, thr_low,
                                             thr_high, extrema)
        #----------
        print 'Event: %4d,  consumed time = %10.6f(sec),  nmax = %d' % (
            evnum, time() - t0_sec, nmax)

        if DO_PRINT: print_ndarr(extrema, 'output extrema')

        img1 = data
        img2 = extrema

        axim1.clear()
        if imsh1 is not None: del imsh1
        imsh1 = None

        axim2.clear()
        if imsh2 is not None: del imsh2
        imsh2 = None

        ave, rms = img1.mean(), img1.std()
        amin, amax = ave - 1 * rms, ave + 5 * rms
        gg.plot_imgcb(fig1,
                      axim1,
                      axcb1,
                      imsh1,
                      img1,
                      amin=amin,
                      amax=amax,
                      title='Event: %d, Data' % evnum,
                      cmap='inferno')
        gg.move_fig(fig1, x0=400, y0=30)

        gg.plot_imgcb(fig2,
                      axim2,
                      axcb2,
                      imsh2,
                      img2,
                      amin=0,
                      amax=5,
                      title='Event: %d, Local extrema' % evnum,
                      cmap='inferno')
        gg.move_fig(fig2, x0=0, y0=30)

        gg.show(mode='DO_NOT_HOLD')
    gg.show()
#myrun = next(ds.runs())
#evt = next(myrun.events())
#runnum = evt.run()

#run = ds.runs().next()
#runnum = run.run()

#for key in evt.keys() : print key

##-----------------------------

det = AreaDetector(src, env, pbits=0)
print 85 * '_', '\nInstrument: %s  run number: %d' % (det.instrument(), runnum)

nda_peds = det.pedestals(runnum)
print_ndarr(nda_peds, 'nda_peds')

#nda_bkgd  = det.bkgd(runnum)
#smask = det.mask(runnum, calib=False, status=True, edges=True, central=True, unbond=True, unbondnbrs=True)
mask = det.mask(runnum, calib=False, status=True, edges=True).astype(np.uint16)

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

##-----------------------------
#mask_img = np.loadtxt('../rel-mengning/work/roi_mask_nda_equ_arc.txt')
#mask_arc.shape = mask_equ.shape = mask_img.shape = nda_peds.shape

#------------------------------

#xoffset, yoffset = 300, 300
#xsize,   ysize   = 1150, 1150