示例#1
0
    def _make_log_freq_map(self):
        """
        ::

            For the given ncoef (bands-per-octave) and nfft, calculate the center frequencies
            and bandwidths of linear and log-scaled frequency axes for a constant-Q transform.
        """
        fp = self.feature_params
        bpo = float(self.nbpo)  # Bands per octave
        self._fftN = float(self.nfft)
        hi_edge = float(self.hi)
        lo_edge = float(self.lo)
        f_ratio = 2.0**(1.0 / bpo)  # Constant-Q bandwidth
        self._cqtN = float(P.floor(P.log(hi_edge / lo_edge) / P.log(f_ratio)))
        self._dctN = self._cqtN
        self._outN = float(self.nfft / 2 + 1)
        if self._cqtN < 1: print("warning: cqtN not positive definite")
        mxnorm = P.empty(self._cqtN)  # Normalization coefficients
        fftfrqs = self._fftfrqs  #P.array([i * self.sample_rate / float(self._fftN) for i in P.arange(self._outN)])
        logfrqs = P.array([
            lo_edge * P.exp(P.log(2.0) * i / bpo) for i in P.arange(self._cqtN)
        ])
        logfbws = P.array([
            max(logfrqs[i] * (f_ratio - 1.0),
                self.sample_rate / float(self._fftN))
            for i in P.arange(self._cqtN)
        ])
        #self._fftfrqs = fftfrqs
        self._logfrqs = logfrqs
        self._logfbws = logfbws
        self._make_cqt()
示例#2
0
 def plot_log(self):
     x_axle = np.arange(1, len(self.seq_wordcount) + 1, 1)
     logcount = [p.log(y) for y in self.seq_wordcount]
     logx_axle = [p.log(x) for x in x_axle]
     p.figure()
     p.plot(logx_axle, logcount)
     p.title("logcount Vs logrange")
     p.savefig('./pic3.png')
def mu_law(a, mu=256, MAX=None):
    mu = mu - 1
    a = P.array(a)
    MAX = a.max() if MAX is None else MAX
    a = a / MAX
    y = (1 + P.sign(a) * P.log(1 + mu * abs(a)) / P.log(1 + mu)) / 2
    inds = P.around(y * mu).astype(P.uint8)
    return inds
def toevan(TC, catstr, ref_date, c=140):
    pes = []
    Ms = []
    q30acts = []
    q30ests = []
    for i in range(len(catstr)):

        q30totalact = TC[(TC["categoryid"] == catstr[i])
                         & (TC["ref_date"] == ref_date) &
                         (TC["c30"] != 0)].q30.sum()
        M = len(TC[(TC["categoryid"] == catstr[i])
                   & (TC["ref_date"] == ref_date) & (TC["c30"] != 0)])
        #        if M==0:continue

        if M < 18: continue

        a = betadist.fit(
            TC[(TC.categoryid == catstr[i]) & (TC["ref_date"] == ref_date) &
               (TC["c30"] != 0) & (TC["ratio"] < 1)]["ratio"],
            floc=0,
            fscale=1)
        EV = a[0] / (a[0] + a[1])
        con = EV * c * 0.001
        try:
            cce = sum(
                py.divide(
                    TC[(TC.categoryid == catstr[i])
                       & (TC["ref_date"] == ref_date) &
                       (TC["c30"] != 0)].sort_values(
                           by="c30", ascending=False).c30.tolist(),
                    con * py.log(range(2, M + 2))))
        except:
            cce = sum(
                TC[(TC.categoryid == catstr[i]) & (TC["ref_date"] == ref_date)
                   & (TC["c30"] != 0)].sort_values(
                       by="c30", ascending=False).c30.tolist() / con *
                py.log(range(2, M + 1)))

        pes.append(100 * (cce - q30totalact) / q30totalact)
        Ms.append(M)
        q30acts.append(q30totalact)
        q30ests.append(cce)

    return pd.DataFrame({
        "pe": pes,
        "M": Ms,
        "q30act": q30acts,
        "q30est": q30ests
    })
示例#5
0
文件: mobility.py 项目: ejetzer/hall
def fit_exp(Ts, Rs, Rerr, eguess):
    'Fit an exponential function to the data'
    Rs, Rerr = pylab.array(Rs), pylab.array(Rerr)
    model, ps = 'a * exp(b / x)', 'a,b'
    # Make intelligent guesses for the parameters
    a, b = eguess
    b = pylab.log(Rs[0]/Rs[1]) / ( Rs[-1] - Rs[0] )
    # Create a spinmob fitter
    fitter = spinmob.data.fitter(model, ps)
    fitter.set_data(Ts, Rs, Rerr)
    fitter.set(a=a, b=b) # Set guesses
    # Fit.
    fitter.fit()
    return fitter
示例#6
0
文件: rbf.py 项目: keithwoj/SCAMR
def phs(d,**parms):
    # phs, f(r) = r^m for m odd positive integer
    c = parms.get('centers')    
    m = parms.get('power')
    DM = dmatrix(d, centers = c)
    # Check to see if m is a positive integer
    if (m == int(m)) & (m > 0):
        if (m%2):
            #print("PHS odd m = {}".format(m))
            return DM**m            
        else:
            #print("PHS even m = {}".format(m))
            return DM**m*log(DM + 1*(DM==0))            
    else:
        raise NameError("PHS power must be a positive integer.")            
示例#7
0
def g_rhot(rho_, t_=0.9):
    #dim-less gibbs free energy as function of (t,rho,p)
    p_ = p_rhot(rho_, t_)
    g_ = -3.0*rho_ - 8.0/3.0*t_*plab.log(3.0/rho_ - 1.0) + p_/rho_
    return g_
示例#8
0
def make_mask(data_file,mask_file='none'):
   global key, x, y,lc,data,im,xy,mymask,xx,yy,px,lx,lm,default_mask,maskfig
   # determine if a point is inside a given polygon or not
   # Polygon is a list of (x,y) pairs.
      
   
   #read input parameters
   print "masking the edf file"
   print "use mouse to select a region"
   print "m - to mask selected region"
   print "u - to unmask selected region"
   print "a - to cancel selected region"
   print "w - to save mask and exit"
   #print "e - to exit"
   
   data=loadedf(data_file)
   lx,ly=shape(data)
   if os.path.exists(mask_file) is True:
      mymask=loadedf(mask_file,0)
      automask=loadedf(mask_file,1)
      if shape(mymask)!=shape(data):
         mymask=zeros((lx,ly))
   else:
      mymask=zeros((lx,ly))
      automask=zeros((lx,ly))

   #points=[]
   #for i in range(lx):
   #    for j in range(ly):
   #     points.append([i,j]) 
   
   x, y = meshgrid(arange(lx), arange(ly))
   x, y = x.flatten(), y.flatten()
   points = vstack((x,y)).T

   key=[]
   x=0
   y=0
   xy=[]
   xx=[]
   yy=[]
   print "Make Mask"
  
   def on_click(event):
       print "On click"
       global key, x, y,lc,data,im,xy,mymask,xx,yy,px,lx,lm,default_mask,maskfig
       if not event.inaxes: 
           xy=[]
           return
       x,y=int(event.xdata), int(event.ydata)
       xx.append([x])
       yy.append([y])
       xy.append([y,x])
       lc.set_data(xx,yy)
       p.draw()
   def on_click_key(event):
       global key, x, y,lc,data,im,xy,mymask,xx,yy,px,lx,lm,default_mask,maskfig
       key=event.key
       if not event.inaxes: 
          xy=[]
          return
       if key=='a':
          xx=[]
          yy=[]
          xy=[]  
          x=0
          y=0
          lc.set_data(xx,yy)
          lm.set_data(xx,yy)
          p.draw()
       if key=='m':
           #print 'masking'
           xx.append(xx[0])#xx[-1]=xx[0]
           yy.append(yy[0])#yy[-1]=yy[0]
           xy.append(xy[0])#xy[-1]=xy[0]
           ind=points_inside_poly(points,xy).reshape(lx,ly).T
           mymask[ind]=1
           data=masked_array(data,mymask+automask)
           im.set_data(data)
           xx=[]
           yy=[]
           xy=[] 
           lc.set_data(xx,yy)
           lm.set_data(xx,yy)
           p.draw()
           x=0
           y=0 
           print "key m pressed"
       if key=='u': 
           xx.append(xx[0])#xx[-1]=xx[0]
           yy.append(yy[0])#yy[-1]=yy[0]
           xy.append(xy[0])#xy[-1]=xy[0]
           ind=points_inside_poly(points,xy).reshape(lx,ly).T
           mymask[ind]=0
           data=masked_array(data,mymask+automask)
           im.set_data(data)
           xx=[]
           yy=[]
           xy=[] 
           lc.set_data(xx,yy)
           lm.set_data(xx,yy)
           p.draw()
           x=0
           y=0
           print "key u pressed"
       if key=='r':
          mymask=0*mymask
          mymask=mymask.reshape(lx,ly)
          data=masked_array(data,mymask+automask)
          im.set_data(data)
          xx=[]
          yy=[]
          xy=[] 
          lc.set_data(xx,yy)
          lm.set_data(xx,yy)
          p.draw()
          x=0
          y=0 
          print "key r pressed"
       if key=='w':
          p.close()
          saveedf(mask_file,mymask,0)
          saveedf(mask_file,automask,1)
          print "key w pressed, CLOSING"
          return
   
   def on_move(event):
       #print"On move"
       global lm,x,y
       if not event.inaxes: return
       xm,ym=int(event.xdata), int(event.ydata)
       # update the line positions
       if x!=0: 
           lm.set_data((x,xm),(y,ym))
           p.draw()
   p.rc('image',origin = 'lower')
   p.rc('image',interpolation = 'nearest')
   p.figure()
   px=p.subplot(111)
   data=p.log(data+1)
   im=p.imshow(masked_array(data,mymask+automask))
   p.title("Select a ROI. Press m to mask or u to unmask it \n w to write mask and exit")
   lc,=px.plot((0,0),(0,0),'-+m',linewidth=1,markersize=8,markeredgewidth=1)
   lm,=px.plot((0,0),(0,0),'-+m',linewidth=1,markersize=8,markeredgewidth=1)
   px.set_xlim(0,ly)
   px.set_ylim(0,lx)
   #p.ion()
   cidb=p.connect('button_press_event',on_click)
   cidk=p.connect('key_press_event',on_click_key)
   cidm=p.connect('motion_notify_event',on_move)
   p.show()
示例#9
0
 def view_function(self,v):
     return pylab.log(v)
示例#10
0
def integrate_them(o):
    """ 
    Process a series of files
    o = options object 
    o.parfile gives name of parameter file (fit2d or poni format)
    o.dark overrides to supply dark filename
    o.flood overrides to supply flood filename
    o.mask  overrides to supply mask filename
    o.backcalc asks for the back computation of the image
    o.npts gives number of output points
    """
    # pyFAI.load( ponifile )
    integrator = AzimuthalIntegrator()
    #integrator.tth = integrator.newtth
    # integrator.setChiDiscAtZero()
    ptype = determineparfile(o.parfile)
    if ptype == "pyfai":
        integrator.load(o.parfile)
        if o.dark is not None:
            print("Using dark from command line", o.dark)
        if o.flood is not None:
            print("Using dark from command line", o.flood)
    elif ptype == "fit2d":
        f2d = fit2dcakepars(o.parfile)
        if f2d["SPATIAL DIS."][0] not in ["Y", "y"]:
            # Set to None. Spatial is from parfile
            f2d["SD FILE"] = None
        integrator.setFit2D(float(f2d["DISTANCE"]),
                            float(f2d["X-BEAM CENTRE"]),
                            float(f2d["Y-BEAM CENTRE"]),
                            tilt=float(f2d["ANGLE OF TILT"]),
                            tiltPlanRotation=float(f2d["TILT ROTATION"]),
                            pixelX=float(f2d["X-PIXEL SIZE"]),
                            pixelY=float(f2d["Y-BEAM CENTRE"]),
                            splineFile=f2d["SD FILE"])
        integrator.rot3 = 0
        integrator.reset()
        print(integrator.param, integrator.detector.pixel1)
        # First choice is command line. Then from pars if supplied
        if o.dark is None:
            if f2d["DARK CURRENT"][0] in ["Y", "y"]:
                o.dark = f2d["DC FILE"]
                print("Using dark from fit2d parameter file", o.dark)
        else:
            print("Using dark from command line", o.dark)
        if o.flood is None:
            if f2d["FLAT-FIELD"][0] in ["Y", "y"]:
                o.flood = f2d["FF FILE"]
                print("Using flood from fit2d parameter file", o.flood)
        else:
            print("Using flood from command line", o.flood)
    # Should be in fabio utilities
    df = darkflood(o.dark, o.flood)
    # Should be in fabio
    fs = edffilenameseries(o.stem, o.first, o.last, o.glob, o.extn)
    #    integrator.polarization( factor = 1, shape=(2048,2048) )
    # Command line is first priority for make
    if o.mask is not None:
        mask = fabio.open(o.mask).data
        print("Using mask", o.mask)
        # assume poni file deals with this independently?
    elif ptype == "fit2d":  # try in fit2d parfile
        if f2d["USE MASK"][0] in ['y', 'Y']:
            mask = fabio.open(f2d["MASK FILE"]).data
            print("Using mask", f2d["MASK FILE"])
        else:
            mask = None
    if mask is not None:
        print("mask mean:", mask.mean())
    integrator.write(os.path.splitext(o.parfile)[0] + ".poni")
    for f in fs:
        print("Processing", f, end=' ')
        try:
            fo = df.correct(fabio.open(f))
        except:

            continue
        if ptype == "fit2d":
            outFile = f.replace(f2d["input_extn"], f2d["output_extn"])
        else:
            outFile = f.replace(o.extn, ".dat")
        global SOLID_ANGLE
        if 0:
            from matplotlib.pylab import imshow, figure, show, log, plot
            #imshow(log(fo.data))
            #figure()
            if mask is not None:
                imshow(log(fo.data * (1 - mask)),
                       vmin=log(10),
                       vmax=log(30000))
            else:
                imshow(log(fo.data), vmin=log(100), vmax=log(3000))
            #        show()
        if o.npts is None:
            npts = min(fo.data.shape)
        else:
            npts = int(o.npts)
        tth, I = integrator.integrate1d(
            fo.data,
            nbPt=npts,
            filename=outFile,
            correctSolidAngle=SOLID_ANGLE,
            mask=mask,  # 1 for valid
            unit="q_A^-1",
            #dummy=dummy, # mask pixels == dummy
            #delta_dummy=delta_dummy # precision of dummy
        )

        print("wrote", outFile)
        if o.backcalc:
            calcimage = calcfrom1d(integrator, tth, I,
                                   fo.data.shape) * integrator._polarization
            err = (calcimage - fo.data) * (1 - mask) / (calcimage + mask)
            e = fabio.edfimage.edfimage(data=err.astype(numpy.float32))
            e.write(outFile + ".edf")
            fitcen(fo.data, calcimage, (1 - mask))


#            from matplotlib.pylab import imshow, show
#            imshow( integrator._polarization )
#            show()

    if o.display:
        if mask is not None:
            display(tth, I,
                    (calcimage - fo.data) * (1 - mask) / (calcimage + 1))
        else:
            display(tth, I, (calcimage - fo.data) / (calcimage + 1))
示例#11
0
                                    QE_gain_variation)
line_frame = cs.create_line_noise_fusion(np.shape(frame), 1,
                                         fixed_pattern_noise)

noise_data = np.zeros(np.size(list_excitations))
mean_data = np.zeros(np.size(list_excitations))
count = 0
for number_of_photons in list_excitations:
    ## Build illumination pattern
    illumination_frame = np.ones((X_size, Y_size)) * number_of_photons
    print(number_of_photons)
    # Create the read noise map, this is different for each frame.
    frame_out = cs.create_readnoise(np.shape(frame), readnoise)

    # create shot noise, this is different for each frame.
    #    illumination_frame = illumination_frame * fixed_pattern_map
    shot_noise_frame = cs.create_shot_noise(illumination_frame)
    #near_final_frame = shot_noise_frame  + illumination_frame
    near_final_frame = shot_noise_frame + frame_out + illumination_frame

    #frame_out_counts = np.int32(near_final_frame)
    noise_data[count] = np.std(
        np.reshape(near_final_frame, [X_size * Y_size, 1]))
    mean_data[count] = np.mean(
        np.reshape(near_final_frame, [X_size * Y_size, 1]))
    count = count + 1

plt.log(mean_data, noise_data)
print(np.shape(mean_data))
plt.show()
示例#12
0
 def view_function(self, a):
     '''We display populations in log scale so they look better'''
     return pylab.log(a)
示例#13
0
 def view_function(self,v):
     return pylab.log(v)
示例#14
0
 def view_function(self,a):
     '''We display populations in log scale so they look better'''
     return pylab.log(a)
示例#15
0
文件: makemask.py 项目: zxmxyh/pyXPCS
def make_mask(data_file, mask_file='none'):
    global key, x, y, lc, data, im, xy, mymask, xx, yy, px, lx, lm, default_mask, maskfig
    # determine if a point is inside a given polygon or not
    # Polygon is a list of (x,y) pairs.

    #read input parameters
    print "masking the edf file"
    print "use mouse to select a region"
    print "m - to mask selected region"
    print "u - to unmask selected region"
    print "a - to cancel selected region"
    print "w - to save mask and exit"
    #print "e - to exit"

    data = loadedf(data_file)
    lx, ly = shape(data)
    if os.path.exists(mask_file) is True:
        mymask = loadedf(mask_file, 0)
        automask = loadedf(mask_file, 1)
        if shape(mymask) != shape(data):
            mymask = zeros((lx, ly))
    else:
        mymask = zeros((lx, ly))
        automask = zeros((lx, ly))

    #points=[]
    #for i in range(lx):
    #    for j in range(ly):
    #     points.append([i,j])

    x, y = meshgrid(arange(lx), arange(ly))
    x, y = x.flatten(), y.flatten()
    points = vstack((x, y)).T

    key = []
    x = 0
    y = 0
    xy = []
    xx = []
    yy = []
    print "Make Mask"

    def on_click(event):
        print "On click"
        global key, x, y, lc, data, im, xy, mymask, xx, yy, px, lx, lm, default_mask, maskfig
        if not event.inaxes:
            xy = []
            return
        x, y = int(event.xdata), int(event.ydata)
        xx.append([x])
        yy.append([y])
        xy.append([y, x])
        lc.set_data(xx, yy)
        p.draw()

    def on_click_key(event):
        global key, x, y, lc, data, im, xy, mymask, xx, yy, px, lx, lm, default_mask, maskfig
        key = event.key
        if not event.inaxes:
            xy = []
            return
        if key == 'a':
            xx = []
            yy = []
            xy = []
            x = 0
            y = 0
            lc.set_data(xx, yy)
            lm.set_data(xx, yy)
            p.draw()
        if key == 'm':
            #print 'masking'
            xx.append(xx[0])  #xx[-1]=xx[0]
            yy.append(yy[0])  #yy[-1]=yy[0]
            xy.append(xy[0])  #xy[-1]=xy[0]
            #ind=points_inside_poly(points,xy).reshape(lx,ly).T
            ind = contains_path(points, xy).reshape(lx, ly).T
            mymask[ind] = 1
            data = masked_array(data, mymask + automask)
            im.set_data(data)
            xx = []
            yy = []
            xy = []
            lc.set_data(xx, yy)
            lm.set_data(xx, yy)
            p.draw()
            x = 0
            y = 0
            print "key m pressed"
        if key == 'u':
            xx.append(xx[0])  #xx[-1]=xx[0]
            yy.append(yy[0])  #yy[-1]=yy[0]
            xy.append(xy[0])  #xy[-1]=xy[0]
            ind = points_inside_poly(points, xy).reshape(lx, ly).T
            mymask[ind] = 0
            data = masked_array(data, mymask + automask)
            im.set_data(data)
            xx = []
            yy = []
            xy = []
            lc.set_data(xx, yy)
            lm.set_data(xx, yy)
            p.draw()
            x = 0
            y = 0
            print "key u pressed"
        if key == 'r':
            mymask = 0 * mymask
            mymask = mymask.reshape(lx, ly)
            data = masked_array(data, mymask + automask)
            im.set_data(data)
            xx = []
            yy = []
            xy = []
            lc.set_data(xx, yy)
            lm.set_data(xx, yy)
            p.draw()
            x = 0
            y = 0
            print "key r pressed"
        if key == 'w':
            p.close()
            saveedf(mask_file, mymask, 0)
            saveedf(mask_file, automask, 1)
            print "key w pressed, CLOSING"
            return

    def on_move(event):
        #print"On move"
        global lm, x, y
        if not event.inaxes: return
        xm, ym = int(event.xdata), int(event.ydata)
        # update the line positions
        if x != 0:
            lm.set_data((x, xm), (y, ym))
            p.draw()

    p.rc('image', origin='lower')
    p.rc('image', interpolation='nearest')
    p.figure()
    px = p.subplot(111)
    data = p.log(data + 1)
    im = p.imshow(masked_array(data, mymask + automask))
    p.title(
        "Select a ROI. Press m to mask or u to unmask it \n w to write mask and exit"
    )
    lc, = px.plot((0, 0), (0, 0),
                  '-+m',
                  linewidth=1,
                  markersize=8,
                  markeredgewidth=1)
    lm, = px.plot((0, 0), (0, 0),
                  '-+m',
                  linewidth=1,
                  markersize=8,
                  markeredgewidth=1)
    px.set_xlim(0, ly)
    px.set_ylim(0, lx)
    #p.ion()
    cidb = p.connect('button_press_event', on_click)
    cidk = p.connect('key_press_event', on_click_key)
    cidm = p.connect('motion_notify_event', on_move)
    p.show()
示例#16
0
def cross_entropy_error(y, t):
    delta = 1e-7
    ret = -sum(t * log(y + delta))
    return ret