def PLOT_DATA(arr, Xplot, Yplot, lb, Ef, Log, Plot_Label):
    p.xlabel(Plot_Label[Xplot][1] + Plot_Label[Xplot][3])
    p.ylabel(Plot_Label[Yplot][1] + Plot_Label[Yplot][3])
    p.title(Plot_Label[Yplot][1] + " as a function of " + Plot_Label[Xplot][1])
    if Xplot == "E":
        x = (arr[:, Plot_Label[Xplot][0]] - Ef) * float(Plot_Label[Xplot][4])
    else:
        x = arr[:, Plot_Label[Xplot][0]] * float(Plot_Label[Xplot][4])
    if Yplot == "E":
        y = (arr[:, Plot_Label[Yplot][0]] - Ef) * float(Plot_Label[Yplot][4])
    else:
        y = arr[:, Plot_Label[Yplot][0]] * float(Plot_Label[Yplot][4])
    if Xplot == "N" and Log == "y":
        indn = p.where(x < 0)
        indp = p.where(x > 0)
        pp = raw_input("Plot electron or hole ? > ")
        if pp == "electron":
            p.semilogx(abs(x[indn]), y[indn], label=lb + "$ ; n-Type$")
        elif pp == "hole":
            p.semilogx(x[indp], y[indp], label=lb + "$ ; p-Type $")
    elif Yplot == "N" and Log == "y":
        indn = p.where(y < 0)
        indp = p.where(y > 0)
        pp = raw_input("Plot electron or hole ?? > ")
        if pp == "electron":
            p.semilogy(x[indn], abs(y[indn]), label=lb + "$ ; n-Type$")
        elif pp == "hole":
            p.semilogy(x[indp], y[indp], label=lb + "$ ; p-Type$")
    else:
        p.plot(x, y, label=lb)
Esempio n. 2
0
def initialJet(nx, y, beta, g):
    "Calculate the initial conditions for the geostrophically balanced jet"
    "for h, u and v at nx x-locations and at locations y"
    
    # Initial jet parameters
    yc = 6e6          # Centre of jet
    jetw = 3e6        # Jet half width
    yS = yc - jetw    # Southern extent of the jet
    yN = yc - jetw    # Northern extent of the jet
    umax = 20.        # Maximum jet velocity

    # derived jet parameters
    yhat = (y - yc)/jetw
    hS = 16./35*jetw*beta*umax*yc/g  # southern h
    hN = -hS                     # northern h
    
    ny = len(y)
    
    # initialise u, v and h
    u = py.zeros([nx, ny])
    v = py.zeros([nx, ny])
    h = py.zeros([nx, ny])

    # jet velocity
    u[:,:] = py.where((abs(yhat) > 1), 0., \
             umax*(1 - 3*yhat**2 + 3*yhat**4 - yhat**6))

    # jet height
    h[:,:] = py.where((yhat < -1), hS, py.where((yhat > 1), hN, \
                hS - jetw*beta*umax/g*(\
             yc*(16./35+yhat-yhat**3+3./5*yhat**5-1./7*yhat**7)\
           + jetw*(-1./8+0.5*yhat**2-0.75*yhat**4+0.5*yhat**6-1./8*yhat**8))))

    return [h,u,v]
def mixed(x):
    "A flat peak in one location and a cosine bell in another"
    return pl.where((x >= 0.2) & (x <= 0.3), 1, \
                    pl.where((x >= 0.4) & (x <= 0.8), \
                    0.5*(1 + pl.cos(5*pl.pi*(x-0.6))),
                    pl.where((x > 0.1) & (x < 0.2), 10*(x-0.1),
                    pl.where((x > 0.3) & (x < 0.35), 20*(0.35-x), 0))))
Esempio n. 4
0
def fProximity(A, B=None, zeroDiag=True):
    ''' Return the proximity (similarity x correlation) as :
    - 2D nparray scalar between 2D nparray vectors (filled with zeros for diagonal and symetrix terms)
    - 1D nparray scalar between 1D nparray vector and 2D nparray vectors
    - 2D nparray scalar between 2D nparray vectors
    '''
    sA = A.shape
    sB = B.shape

    if B == None:
        corr = zeros((sA[0], sA[0]))
        for i in range(sA[0]):
            corr[i, i + 1:] = fProximity(A[i], A[i + 1:])
        return corr

    elif A.ndim == 1:
        dif = 1. - abs(A - B).sum(axis=-1) / (1. * sA[0])
        sim = B.dot(A) / (A**2).sum(axis=-1)**(0.5) / (B**
                                                       2).sum(axis=-1)**(0.5)
        return where(isfinite(sim), dif * sim, dif)

    elif B.ndim == 1:
        dif = 1. - abs(A - B).sum(axis=-1) / (1. * sB[0])
        sim = A.dot(B) / (A**2).sum(axis=-1)**(0.5) / (B**
                                                       2).sum(axis=-1)**(0.5)
        return where(isfinite(sim), dif * sim, dif)

    else:
        corr = zeros((sA[0], sB[0]))
        for i in range(sA[0]):
            corr[i] = fProximity(A[i], B)
        return corr - zeroDiag * diag(diag(corr))
Esempio n. 5
0
  def solve(self):
    """
    """
    s    = "::: solving density, overburden stress, and grain radius :::"
    text = colored(s, 'cyan')
    print text
    
    firn   = self.firn
    config = self.config

    # newton's iterative method :
    solve(self.delta == 0, firn.U, bcs=self.bcs, J=self.J, 
          solver_parameters=config['enthalpy']['solver_params'])
    firn.rho, firn.sigma, firn.r = firn.U.split(True)

    rhop = firn.rho.vector().array()

    # update kc term in drhodt :
    # if rho >  550, kc = kcHigh
    # if rho <= 550, kc = kcLow
    # with parameterizations given by ligtenberg et all 2011
    A                   = firn.rhoi/firn.rhow * 1e3 * firn.adot
    rhoCoefNew          = ones(firn.n)
    rhoHigh             = where(rhop >  550)[0]
    rhoLow              = where(rhop <= 550)[0]
    rhoCoefNew[rhoHigh] = firn.kcHh * (2.366 - 0.293*ln(A))
    rhoCoefNew[rhoLow]  = firn.kcLw * (1.435 - 0.151*ln(A))
    firn.assign_variable(firn.rhoCoef, rhoCoefNew)
    firn.assign_variable(firn.drhodt,  project(self.drhodt))
    
    #firn.assign_variable(firn.rho, rhop)
    firn.print_min_max(firn.rho,   'rho')
    firn.print_min_max(firn.sigma, 'sigma')
    firn.print_min_max(firn.r,     'r^2')
Esempio n. 6
0
def test_TS_LSTM(path, data, sc, intervalle=[]):

    dataX, dataY = data
    print(dataX)
    print(dataY)

    lstm = load_model_TS(path)
    lstm.eval()
    train_predict = lstm(dataX)

    data_predict = train_predict.cpu().data.numpy()
    dataY_plot = dataY.cpu().data.numpy()

    borne_inf, borne_sup = intervalle
    dif = data_predict - dataY_plot
    anomalyX = append(where(dif < borne_inf), where(dif > borne_sup))
    #print("anoma",list(anomalyX))

    data_predict = sc.inverse_transform(data_predict)
    dataY_plot = sc.inverse_transform(dataY_plot)
    anomalyY = data_predict[anomalyX]

    #plt.axvline(x=3000, c='r', linestyle='--')

    plt.plot(dataY_plot)
    plt.plot(data_predict)
    plt.scatter(anomalyX, anomalyY, color="red")
    plt.suptitle('Time-Series Prediction')
    plt.show()
Esempio n. 7
0
 def v(current_data):
     from pylab import where,sqrt
     q = current_data.q
     h = q[0,:]
     dry_tol = 0.001
     u = where(h>dry_tol, q[1,:]/h, 0.)
     v = where(h>dry_tol, q[2,:]/h, 0.)
     return v
 def v(current_data):
     from pylab import where, sqrt
     q = current_data.q
     h = q[0, :]
     dry_tol = 0.001
     u = where(h > dry_tol, q[1, :] / h, 0.)
     v = where(h > dry_tol, q[2, :] / h, 0.)
     return v
Esempio n. 9
0
def fwhm(x, y):
	hm = pl.amax(y/2.0);
	y_diff = pl.absolute(y-hm);
	y_diff_sorted = pl.sort(y_diff);
	i1 = pl.where(y_diff==y_diff_sorted[0]);
	i2 = pl.where(y_diff==y_diff_sorted[1]);
	fwhm = pl.absolute(x[i1]-x[i2]);
	return hm, fwhm
Esempio n. 10
0
    def noShadowing(self):
        "Select the non flagged (non shadowed) antennas from the FLAG and the index... Still bugged !!"

        indexNoFlag = pl.concatenate(
            (pl.where(self.f == 0)[0], pl.where(self.ff[0, 0, ] == False)[0],
             pl.where(self.ff[1, 0, ] == False)[0]))

        return (indexNoFlag)
Esempio n. 11
0
 def speed(current_data):
     from pylab import where, sqrt
     q = current_data.q
     h = q[0, :]
     dry_tol = 0.001
     u = where(h > dry_tol, q[1, :] / h, 0.)
     v = where(h > dry_tol, q[2, :] / h, 0.)
     s = sqrt(u**2 + v**2)
     return s
Esempio n. 12
0
 def speed(current_data):
     from pylab import where,sqrt
     q = current_data.q
     h = q[0,:]
     dry_tol = 0.001
     u = where(h>dry_tol, q[1,:]/h, 0.)
     v = where(h>dry_tol, q[2,:]/h, 0.)
     s = sqrt(u**2 + v**2)
     return s
    def plot_topo_xsec(current_data):
        from pylab import plot, cos, sin, where, legend, nan
        t = current_data.t

        x = numpy.linspace(0.0, domain_x, 201)
        #y = 0.
        B = where(x > 40.0, where(x < 40.34, 7.0, 0.0), 0.0)
        plot(x, B, 'g', label="bathymetry")
        legend()
Esempio n. 14
0
def ctc_align_targets(outputs,
                      targets,
                      threshold=100.0,
                      verbose=0,
                      debug=0,
                      lo=1e-5):
    """Perform alignment between the `outputs` of a neural network
    classifier and some targets. The targets themselves are a time sequence
    of vectors, usually a unary representation of each target class (but
    possibly sequences of arbitrary posterior probability distributions
    represented as vectors)."""

    outputs = maximum(lo, outputs)
    outputs = outputs * 1.0 / sum(outputs, axis=1)[:, newaxis]

    # first, we compute the match between the outputs and the targets
    # and put the result in the log domain
    match = dot(outputs, targets.T)
    lmatch = log(match)

    if debug:
        figure("ctcalign")
        clf()
        subplot(411)
        imshow(outputs.T, interpolation='nearest', cmap=cm.hot)
        subplot(412)
        imshow(lmatch.T, interpolation='nearest', cmap=cm.hot)
    assert not isnan(lmatch).any()

    # Now, we compute a forward-backward algorithm over the matches between
    # the input and the output states.
    both = forwardbackward(lmatch)

    # We need posterior probabilities for the states, so we need to normalize
    # the output. Instead of keeping track of the normalization
    # factors, we just normalize the posterior distribution directly.
    epath = exp(both - amax(both))
    l = sum(epath, axis=0)[newaxis, :]
    epath /= where(l == 0.0, 1e-9, l)

    # The previous computation gives us an alignment between input time
    # and output sequence position as posteriors over states.
    # However, we actually want the posterior probability distribution over
    # output classes at each time step. This dot product gives
    # us that result. We renormalize again afterwards.
    aligned = maximum(lo, dot(epath, targets))
    l = sum(aligned, axis=1)[:, newaxis]
    aligned /= where(l == 0.0, 1e-9, l)

    if debug:
        subplot(413)
        imshow(epath.T, cmap=cm.hot, interpolation='nearest')
        subplot(414)
        imshow(aligned.T, cmap=cm.hot, interpolation='nearest')
        ginput(1, 0.01)
    return aligned
Esempio n. 15
0
def plotIt(x, db='Pawsey', title_timestr=None, data_name="MWA LTA"):

    x = np.int64(x)
    ttsize = np.sum(x) / 1024.0**5
    ttlen = len(x)
    """
    for xi in x:
        print "{0}:{1}".format(xi, type(xi))
    """
    x[where(x == np.NaN)] = 1
    x[where(x == 0)] = 1
    x = np.log10(x)
    print "caculating the histogram..."
    hist, bins = np.histogram(x, bins=60)
    width = 0.7 * (bins[1] - bins[0])
    center = (bins[:-1] + bins[1:]) / 2
    y2 = hist.astype('float')
    y2 = y2 / np.sum(y2) * 100
    fig = plt.figure()
    dt = datetime.datetime.now()
    if (title_timestr is None):
        timestr = dt.strftime('%Y-%m-%dT%H:%M:%S')
    else:
        timestr = title_timestr
    fig.suptitle(
        "{4} {0} at {3}, # of files: {1}, total size: {2:.2f} PB".format(
            db, ttlen, round(ttsize, 2), timestr, data_name),
        fontsize=18)
    ax = fig.add_subplot(111)
    ax.set_ylabel('Frequency', fontsize=16)
    ax.set_xlabel('log(file size [B])', fontsize=16)
    ax.set_yscale('log')
    ax.tick_params(axis='both', which='major', labelsize=14)
    ax.tick_params(axis='both', which='minor', labelsize=12)
    plt.xticks(np.arange(int(min(center)), int(max(center)) + 1, 1))
    plt.bar(center, hist, align='center', width=width)
    ax2 = ax.twinx()
    ax2.set_ylabel('Frequency %', fontsize=16)
    ax2.set_yscale('log')
    ax2.tick_params(axis='both', which='major', labelsize=14)
    ax2.tick_params(axis='both', which='minor', labelsize=12)
    ax2.plot(center, y2, color='r', linestyle=':', label='percentage')

    ymax = ax.get_ylim()[1]
    three = math.log10(1024)
    six = math.log10(1024**2)
    nine = math.log10(1024**3)
    ax.vlines(three, 0, ymax, colors='k', linestyles=':')
    ax.vlines(six, 0, ymax, colors='k', linestyles=':')
    ax.vlines(nine, 0, ymax, colors='k', linestyles=':')

    ax.text(three, ymax + 110, 'K', fontsize=16)
    ax.text(six, ymax + 110, 'M', fontsize=16)
    ax.text(nine, ymax + 110, 'G', fontsize=16)

    plt.show()
Esempio n. 16
0
File: UVW.py Progetto: nodarai/acdc
 def shadowing(self):
     "Select the shadowed antennas from the FLAG column and return the index of the shadowed measurement and the percentage of shadowing "
     
     indexFlag=pl.concatenate((pl.where(self.f==1)[0],pl.where(self.ff[0,0,])[0],pl.where(self.ff[1,0,])[0]))
     indexNoFlag=pl.concatenate((pl.where(self.f==0)[0],pl.where(self.ff[0,0,]==False)[0],pl.where(self.ff[1,0,]==False)[0]))
     
     Ntot=len(indexFlag)+len(indexNoFlag)
     fractionShadow=100.*len(indexFlag)/Ntot
     
     return(indexFlag,fractionShadow)
 def gauge_speed(current_data):
     # different than speed function because q is function of time, not
     # x,y at the gauges.
     from numpy import where, sqrt
     h = q[0,:]
     dry_tol = 0.001
     u = where(h>dry_tol, q[1,:]/h, 0.)
     v = where(h>dry_tol, q[2,:]/h, 0.)
     s = sqrt(u**2 + v**2)
     return s
Esempio n. 18
0
def fbank(signal,samplerate=16000,winlen=0.025,winstep=0.01,
          nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97):
    """Compute Mel-filterbank energy features from an audio signal.

    :param signal: the audio signal from which to compute features. Should be an N*1 array
    :param samplerate: the samplerate of the signal we are working with.
    :param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)    
    :param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)    
    :param nfilt: the number of filters in the filterbank, default 26.
    :param nfft: the FFT size. Default is 512.
    :param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
    :param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
    :param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97. 
    :returns: 2 values. The first is a numpy array of size (NUMFRAMES by nfilt) containing features. Each row holds 1 feature vector. The
        second return value is the energy in each frame (total energy, unwindowed)
    """          
    highfreq= highfreq or samplerate/2
    print "preemph %s"%(preemph)
    signal = sigproc.preemphasis(signal,preemph)
    frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate)
    matchframes(frames[0], frames[1])
    pspec = sigproc.powspec(frames,nfft)
    energy = pylab.sum(pspec,1) # this stores the total energy in each frame
    energy = pylab.where(energy == 0, pylab.finfo(float).eps, energy) # if energy is zero, we get problems with log
    fb = get_filterbanks(nfilt, nfft, samplerate, lowfreq, highfreq)
    print "len(fb) %s"%(len(fb))
    colour = "k-"
    for i in range(len(fb)):
        if colour == "k-":
            colour = "r-"
        else:
            colour = "k-"
        startedplot = False
        midpoint = 0
        for j in range(len(fb[i])):
            if fb[i][j] > 0:
                if startedplot == False:
                    startedplot = j
                if j > 0:
                    pylab.plot([j-1, j], [fb[i][j-1], fb[i][j]], colour)
                    if fb[i][j] == 1.0:
                        midpoint = j
            else:
                if not startedplot == False:
                    pylab.plot([j-1, j], [fb[i][j-1], 0], colour)
                    try:
                        print "slope to midpoint %.3f, slope from midpoint %.3f"%(1.0/float(midpoint-startedplot), 1.0/float(midpoint-j+1))
                    except:
                        pass
                    break
    pylab.show()
    feat = pylab.dot(pspec, fb.T) # compute the filterbank energies
    feat = pylab.where(feat == 0, pylab.finfo(float).eps, feat) # if feat is zero, we get problems with log
    return feat, energy
Esempio n. 19
0
    def transform_vector(self,uin,vin,lons,lats,nx,ny,returnxy=False,preserve_magnitude=True):
        """
 transform a vector field (uin,vin) from a lat/lon grid with longitudes
 lons and latitudes lats to a (ny,nx) native map projection grid.
 The input vector field is defined in spherical coordinates (it
 has eastward and northward components) while the output
 vector field is defined in map projection coordinates (relative
 to x and y).
 if returnxy=True, the x and y values of the native map projection grid
 are also returned (default False).
 if preserve_magnitude=True (default), the vector magnitude is preserved
 (so that length of vectors represents magnitude of vector relative to
 spherical coordinate system, not map projection coordinates). 

 vectors on a lat/lon grid must be transformed to map projection coordinates
 before they be plotted on the map (with the quiver class method).
        """
        lonsout, latsout, x, y = self.makegrid(nx,ny,returnxy=True)
        # interpolate to map projection coordinates.
        uin = interp(uin,lons,lats,lonsout,latsout)
        vin = interp(vin,lons,lats,lonsout,latsout)
        if preserve_magnitude:
            # compute original magnitude.
            mag = pylab.sqrt(uin**2+vin**2)
        rad2dg = 180./math.pi
        tiny = 1.e-5
        delta = 0.1
        coslats = pylab.cos(latsout/rad2dg)
        # use dx/dlongitude, dx/dlatitude, dy/dlongitude and dy/dlatitude
        # to transform vector to map projection coordinates.
        # dlongitude is delta degrees at equator, dlatitude is delta degrees.
        xn,yn = self(lonsout,pylab.where(latsout+delta<90.,latsout+delta,latsout))
        # at poles, derivs w/respect to longitude will be zero.
        lonse = pylab.where(coslats>tiny,lonsout+(delta/coslats),lonsout)
        xe,ye = self(lonse,latsout)
        uout = uin*(xe-x)*(coslats/delta) + vin*(xn-x)/delta
        vout = uin*(ye-y)*(coslats/delta) + vin*(yn-y)/delta
        # make sure uout, vout not too small (quiver will raise
        # an exception when trying to rescale vectors).
        uout = pylab.where(pylab.fabs(uout)<tiny,tiny,uout)
        vout = pylab.where(pylab.fabs(vout)<tiny,tiny,vout)
        # fix units. 
        if self.projection != 'cyl':
            uout = uout*rad2dg/self.rsphere
            vout = vout*rad2dg/self.rsphere
        # rescale magnitude.
        if preserve_magnitude:
            magout = pylab.sqrt(uout**2+vout**2)
            uout = uout*mag/magout
            vout = vout*mag/magout
        if returnxy:
            return uout,vout,x,y
        else:
            return uout,vout
def calzetti(wavelength, R_v=4.05):

	k_lam = pylab.zeros(len(wavelength))
	x     = wavelength / (1.e4)  # convert wavelength to microns
	x1    = pylab.where(x <  0.63)[0]
	x2    = pylab.where(x >= 0.63)[0]

	k_lam[x1] = 2.659 * (-2.156 + 1.509/x[x1] - 0.198/(x[x1]**2) + 0.011/(x[x1]**3)) + R_v
	k_lam[x2] = 2.659 * (-1.857 + 1.040/x[x2]) + R_v

	return k_lam / R_v
Esempio n. 21
0
 def speed(current_data):
     from pylab import where, sqrt
     q = current_data.q
     h = q[0, :]
     dry_tol = 0.001
     u = where(h > dry_tol, q[1, :] / h, 0.)
     v = where(h > dry_tol, q[2, :] / h, 0.)
     s = sqrt(u**2 + v**2)
     #s = s / sqrt(9.81/0.97)  # so comparable to eta
     s = s / 10.  # as in Figure 5 of paper
     return s
Esempio n. 22
0
    def plot_topo_xsec(current_data):
        from pylab import plot, cos, sin, where, legend, nan
        t = current_data.t

        x = numpy.linspace(0.0, domain_x, 201)
        #y = 0.
        B = where(x > 40.0, where(x < 40.50, 7.0, 0.0), 0.0)
        plot(x, B, 'g', label="bathymetry")
        legend()
        pylab.xticks(fontsize=21, fontname="Tex Gyre Pagella")
        pylab.yticks(fontsize=21, fontname="Tex Gyre Pagella")
Esempio n. 23
0
 def construct(self):
     first = pymc.Bernoulli('F', .6, value=pl.ones(self.obs))
     p_first = pymc.Lambda('p_F',
                           lambda R=R: pl.where(R, .005, .8),
                           doc='Pr[S|R]')
     second = pymc.Bernoulli('S', p_first, value=pl.ones(self.obs))
     p_G = mc.Lambda('p_G',
                     lambda S=S, R=R: pl.where(S, pl.where(R, .99, .9),
                                               pl.where(R, .8, 0.)),
                     doc='Pr[G|S,R]')
     G = mc.Bernoulli('G', p_G, value=G_obs, observed=True)
Esempio n. 24
0
def risetime(t, y, limit=[0.1, 0.9]):
    y = (y - y.min()) / (y - y.min()).max()

    i0 = pl.where(y > limit[0])
    i1 = pl.where(y > limit[1])

    t0 = t[i0][0]
    t1 = t[i1][0]

    t_rise = t1 - t0
    return t_rise
 def int_peak(self,fitrange=None, intrange=None, normalize=False, plot=False, npoints=10):
     """
     Fits a linear background, subtracts the background, and integrates. Intended to be used for integrating peaks.
     
     wavelen : list
         list of wavelengths in nm. Can be sorted from low to high or high to low
     lum : list
         list of luminescence
     fitrange : 2-element list, optional
         Defaults to the span of the data. Input: [low nm, high nm]
     intrange : 2-element list, optional
         Defaults to the span of the data or fitrange (if given). Input: [low nm, high nm]
     normalize : boolean, optional
         Default is False
     plot : boolean, optional
         Default is False. Plots the original data, the linear background, and the data with the background subtracted
     npoints : int
         Default is 10. Number of points above and below the given fitrange point to average over.
     """
     if fitrange is None:
         fitindex=[0+npoints/2, len(self._wavelen)-1-npoints/2]
     else:
         fitindex=[0, 0]
         fitindex[0]=py.where(self._wavelen>fitrange[0])[0][0]
         fitindex[1]=py.where(self._wavelen>fitrange[1])[0][0]
     
     wavelenfit=py.concatenate((self._wavelen[fitindex[0]-npoints/2:fitindex[0]+npoints/2], 
                            self._wavelen[fitindex[1]-npoints/2:fitindex[1]+npoints/2]))
     lumfit=py.concatenate((self._lum[fitindex[0]-npoints/2:fitindex[0]+npoints/2], 
                         self._lum[fitindex[1]-npoints/2:fitindex[1]+npoints/2]))
     linearfit = py.polyfit(wavelenfit, lumfit, 1)
     linear_bg = py.polyval( linearfit, self._wavelen[fitindex[0]:fitindex[1]+1] )
     wavelen_bg = self._wavelen[fitindex[0]:fitindex[1]+1].copy()
     lum_bg = self._lum[fitindex[0]:fitindex[1]+1].copy()
     lum_bg -= linear_bg
     
     if plot is True:
         py.plot(self._wavelen,self._lum,'k')
         py.plot(wavelen_bg,linear_bg,'k:')
         py.plot(wavelen_bg,lum_bg,'r')
         py.show()
     
     intindex=[0,0]
     if intrange is None:
         wavelen_int = wavelen_bg
         lum_int = lum_bg  
     else:
         intindex[0]=py.where(wavelen_bg>intrange[0])[0][0]
         intindex[1]=py.where(wavelen_bg>intrange[1])[0][0]    
         wavelen_int = wavelen_bg[intindex[0]:intindex[1]+1]
         lum_int = lum_bg[intindex[0]:intindex[1]+1]
     
     peak_area = py.trapz(lum_int, x=wavelen_int)
     return peak_area
Esempio n. 26
0
 def speed(current_data):
     from pylab import where,sqrt
     q = current_data.q
     h = q[0,:]
     dry_tol = 0.001
     u = where(h>dry_tol, q[1,:]/h, 0.)
     v = where(h>dry_tol, q[2,:]/h, 0.)
     s = sqrt(u**2 + v**2)
     #s = s / sqrt(9.81/0.97)  # so comparable to eta
     s = s / 10.  # as in Figure 5 of paper
     return s
Esempio n. 27
0
 def save_sonogram(self, replace=False, n_fft=settings.N_FFT, \
     min_freq=settings.MIN_FREQ, \
     max_freq=settings.MAX_FREQ, \
     dpi=100,
     width=1000,
     height=350,
     max_framerate=settings.MAX_FRAMERATE):
     filename = self.get_sonogram_name()
     name = os.path.join(settings.SONOGRAM_DIR, filename)
     path = os.path.join(settings.MEDIA_ROOT, name)
     try:
         if not os.path.exists(path):
             replace = True
     except (ValueError, SuspiciousOperation, AttributeError):
         replace = True
     if replace:
         audio, framerate = self.get_audio(max_framerate=max_framerate)
         Pxx, freqs, bins, im = specgram(audio, NFFT=n_fft, Fs=framerate)
         f = where(logical_and(freqs > min_freq, freqs <= max_freq))[0]
         Pxx[where(Pxx > percentile(Pxx[f].flatten(), 99.99))] = percentile(
             Pxx[f].flatten(), 99.99)
         Pxx[where(Pxx < percentile(Pxx[f].flatten(), 0.01))] = percentile(
             Pxx[f].flatten(), 0.01)
         clf()
         fig = figure(figsize=(float(width) / dpi, float(height) / dpi),
                      dpi=dpi)
         imshow(flipud(10 * log10(Pxx[f, ])),
                extent=(bins[0], bins[-1], freqs[f][0], freqs[f][-1]),
                aspect='auto',
                cmap=cm.gray)
         gca().set_ylabel('Frequency (Hz)')
         gca().set_xlabel('Time (s)')
         axis_pixels = gca().transData.transform(
             np.array((gca().get_xlim(), gca().get_ylim())).T)
         st, created = SonogramTransform.objects.get_or_create(
             n_fft=n_fft,
             framerate=framerate,
             min_freq=min_freq,
             max_freq=max_freq,
             duration=self.duration,
             width=width,
             height=height,
             dpi=dpi,
             top_px=max(axis_pixels[:, 1]),
             bottom_px=min(axis_pixels[:, 1]),
             left_px=min(axis_pixels[:, 0]),
             right_px=max(axis_pixels[:, 0]),
         )
         savefig(open(path, 'wb'), format='jpg', dpi=dpi)
         sonogram, created = Sonogram.objects.get_or_create(snippet=self,
                                                            transform=st,
                                                            path=name)
         close()
Esempio n. 28
0
 def save_sonogram(
     self,
     replace=False,
     n_fft=settings.N_FFT,
     min_freq=settings.MIN_FREQ,
     max_freq=settings.MAX_FREQ,
     dpi=100,
     width=1000,
     height=350,
     max_framerate=settings.MAX_FRAMERATE,
 ):
     filename = self.get_sonogram_name()
     name = os.path.join(settings.SONOGRAM_DIR, filename)
     path = os.path.join(settings.MEDIA_ROOT, name)
     try:
         if not os.path.exists(path):
             replace = True
     except (ValueError, SuspiciousOperation, AttributeError):
         replace = True
     if replace:
         audio, framerate = self.get_audio(max_framerate=max_framerate)
         Pxx, freqs, bins, im = specgram(audio, NFFT=n_fft, Fs=framerate)
         f = where(logical_and(freqs > min_freq, freqs <= max_freq))[0]
         Pxx[where(Pxx > percentile(Pxx[f].flatten(), 99.99))] = percentile(Pxx[f].flatten(), 99.99)
         Pxx[where(Pxx < percentile(Pxx[f].flatten(), 0.01))] = percentile(Pxx[f].flatten(), 0.01)
         clf()
         fig = figure(figsize=(float(width) / dpi, float(height) / dpi), dpi=dpi)
         imshow(
             flipud(10 * log10(Pxx[f,])),
             extent=(bins[0], bins[-1], freqs[f][0], freqs[f][-1]),
             aspect="auto",
             cmap=cm.gray,
         )
         gca().set_ylabel("Frequency (Hz)")
         gca().set_xlabel("Time (s)")
         axis_pixels = gca().transData.transform(np.array((gca().get_xlim(), gca().get_ylim())).T)
         st, created = SonogramTransform.objects.get_or_create(
             n_fft=n_fft,
             framerate=framerate,
             min_freq=min_freq,
             max_freq=max_freq,
             duration=self.duration,
             width=width,
             height=height,
             dpi=dpi,
             top_px=max(axis_pixels[:, 1]),
             bottom_px=min(axis_pixels[:, 1]),
             left_px=min(axis_pixels[:, 0]),
             right_px=max(axis_pixels[:, 0]),
         )
         savefig(open(path, "wb"), format="jpg", dpi=dpi)
         sonogram, created = Sonogram.objects.get_or_create(snippet=self, transform=st, path=name)
         close()
Esempio n. 29
0
def compare(features, targets, columns):
    q0mask = pyl.where(targets == 0)[0]
    q1mask = pyl.where(targets == 1)[0]
    q2mask = pyl.where(targets == 2)[0]

    pyl.scatter(features[:, columns[0]][q0mask],
                features[:, columns[1]][q0mask], c='r', label='0')
    pyl.scatter(features[:, columns[0]][q1mask],
                features[:, columns[1]][q1mask], c='g', label='1')
    pyl.scatter(features[:, columns[0]][q2mask],
                features[:, columns[1]][q2mask], c='b', label='2')

    pyl.legend(loc='best')
    pyl.show()
Esempio n. 30
0
def index_approx(v, value):
    """Find index of element closest to value"""
    if isinstance(v, (list, tuple)):
        a = min([abs(i-value) for i in v])
        try:
            return v.index(value + a)
        except ValueError:
            return v.index(value - a)
    elif isinstance(v, pylab.ndarray):
        a = min(abs(v-value))
        if pylab.where(v == value + a)[0].size:
            return pylab.where(v == value + a)[0][0]
        else:
            return pylab.where(v == value - a)[0][0]
Esempio n. 31
0
 def gs(current_data):
     q = current_data.q
     # different than speed function because q is function of time, not
     # x,y at the gauges.
     from numpy import where, sqrt
     h = q[0, :]
     #print('shape of h ' +  str(h.shape))
     dry_tol = 0.001
     u = where(h > dry_tol, q[1, :] / h, 0.)
     v = where(h > dry_tol, q[2, :] / h, 0.)
     ssq = sqrt(u * u + v * v)
     #s = sqrt(u**2 + v**2)
     s = sqrt(ssq)
     return ssq
Esempio n. 32
0
 def __setitem__(self, wavelength, intensity):
     index, = pylab.where(self.wavelengths == wavelength)
     if pylab.any(index.shape):
         self.intensities[index] = intensity
     else:
         index, = pylab.where(self.wavelengths < wavelength)
         if pylab.any(index.shape):
             self.wavelengths = pylab.insert(self.wavelengths, index[-1] + 1,
                                             wavelength)
             self.intensities = pylab.insert(self.intensities, index[-1] + 1,
                                             intensity)
         else:
             self.wavelengths = pylab.insert(self.wavelengths, 0, wavelength)
             self.intensities = pylab.insert(self.intensities, 0, intensity)
Esempio n. 33
0
 def gs(current_data):
     q = current_data.q
     # different than speed function because q is function of time, not
     # x,y at the gauges.
     from numpy import where, sqrt
     h = q[0,:]
     #print('shape of h ' +  str(h.shape))
     dry_tol = 0.001
     u = where(h>dry_tol, q[1,:]/h, 0.)
     v = where(h>dry_tol, q[2,:]/h, 0.)
     ssq = sqrt(u*u+v*v)
     #s = sqrt(u**2 + v**2)
     s = sqrt(ssq)
     return ssq
Esempio n. 34
0
    def draw_rand_gaussian_pos(self, min_r=pl.array([])):
        '''optional min_r, array or tuple of arrays on the form
        array([[r0,r1,...,rn],[z0,z1,...,zn]])'''

        x = pl.normal(0, self.radius, self.n)
        y = pl.normal(0, self.radius, self.n)
        z = pl.normal(0, self.radius, self.n)

        min_r_z = {}
        if pl.size(min_r) > 0:  # != False:
            if type(min_r) == type(()):
                for j in xrange(pl.shape(min_r)[0]):
                    min_r_z[j] = pl.interp(z, min_r[j][0, ], min_r[j][1, ])
                    if j > 0:
                        [w] = pl.where(min_r_z[j] < min_r_z[j - 1])
                        min_r_z[j][w] = min_r_z[j - 1][w]
                    minrz = min_r_z[j]

            else:
                minrz = pl.interp(z, min_r[0], min_r[1])

            R_z = pl.sqrt(x**2 + y**2)
            [u] = pl.where(R_z < minrz)

            while len(u) > 0:
                for i in xrange(len(u)):
                    x[u[i]] = pl.normal(0, self.radius, 1)
                    y[u[i]] = pl.normal(0, self.radius, 1)
                    z[u[i]] = pl.normal(0, self.radius, 1)
                    if type(min_r) == type(()):
                        for j in xrange(pl.shape(min_r)[0]):
                            min_r_z[j][u[i]] = pl.interp(
                                z[u[i]], min_r[j][0, ], min_r[j][1, ])
                            if j > 0:
                                [w] = pl.where(min_r_z[j] < min_r_z[j - 1])
                                min_r_z[j][w] = min_r_z[j - 1][w]
                            minrz = min_r_z[j]
                    else:
                        minrz[u[i]] = pl.interp(z[u[i]], min_r[0, ],
                                                min_r[1, ])
                R_z = pl.sqrt(x**2 + y**2)
                [u] = pl.where(R_z < minrz)

        soma_pos = {
            'xpos': x,
            'ypos': y,
            'zpos': z,
        }
        return soma_pos
def add_fts_scan(fts, ints=[], length=[]):

  '''
  IF N_PARAMS() EQ 0 then begin
    print, 'out = add_fts(fft_structure, int=int)'
    print, 'where /int adds the interferrograms, not the spectra'
    return, 0
  ENDIF
  '''
  print fts
  if type(fts['scan_length'])==int:
    return 0 
  if len(length) == 0 : length = 0
  #print ints
  if length == 0 : t = pl.where(fts['scan_length'] == max(fts['scan_length']))[0]
  if length != 0 : t = pl.where(fts['fft_length'] == length)[0]
  nscans = len(t)

  if len(ints) == 0:   #add the spectra, not the interferrograms. 
    freq = fts['freq'][:,t[0]]
    real = fts['real_FFT'][:,t[0]]
    im = fts['im_FFT'][:,t[0]]
    stdev = pl.zeros(len(freq))

    if nscans > 1:
      for i in range(1,len(t)):
        real = real + fts['real_FFT'][:,t[i]]
        im = im + fts['im_FFT'][:,t[i]]

      for j in range(len(freq)):
        stdev[j] = (fts['real_FFT'][j,t]).std()/pl.sqrt(nscans-1)
        #std dev of mean: std_Dev/sqrt(N-1)


    out = {'freq': freq, 'real': real/nscans, 'im': im/nscans, 'stdev': stdev}

  else:
    intf = fts['intf'][t[0]:]
    stdev = pl.zeros(len(intf))
    if nscans > 1:
      for i in range(1,len(t)):
        intf = intf + fts['intf'][:,t[i]]
      for j in range(len(intf)):
        stdev[j] = (fts['intf'][j,t]).std()/pl.sqrt(nscans-1)

    out = {'xint': reform(fts['xint'][t[0]:]), 'intf': reform(intf)/nscans, 'stdev': stdev}


  return out
Esempio n. 36
0
def fwhm_2gauss(x, y, dx=0.001):
	'''
	Finds the FWHM for the profile y(x), with accuracy dx=0.001
	Uses a 2-Gauss 1D fit.
	'''
	popt, pcov = curve_fit(gauss2, x, y);
	xx = pl.arange(pl.amin(x), pl.amax(x)+dx, dx);
	ym = gauss2(xx, popt[0], popt[1], popt[2], popt[3], popt[4], popt[5])
	hm = pl.amax(ym/2.0);
	y_diff = pl.absolute(ym-hm);
	y_diff_sorted = pl.sort(y_diff);
	i1 = pl.where(y_diff==y_diff_sorted[0]);
	i2 = pl.where(y_diff==y_diff_sorted[1]);
	fwhm = pl.absolute(xx[i1]-xx[i2]);
	return hm, fwhm, xx, ym
Esempio n. 37
0
def preprocessing(input_image, scale, factor):
    """Given an image object, return the difference of gaussian matrix
    
    """
    var1 = scale / sqrt(2)
    var2 = scale * sqrt(2)
    image_array = (asarray(input_image)).astype(float64)/255. * scale
    threshold = image_array.max() * factor
    image_array[where(image_array < threshold)] = 0
    image_array[where(image_array >= threshold)] = 255
    
    image_gaussian = filters.gaussian_filter(image_array, sigma=(var1,var1)) - \
                     filters.gaussian_filter(image_array, sigma=(var2,var2))
    
    return image_gaussian    
Esempio n. 38
0
    def xsec(current_data):
        # Return x value and surface depth at this point, along y=0
        from pylab import where, ravel
        x = current_data.x
        y = ravel(current_data.y)
        dy = current_data.dy
        q = current_data.q

        ij = where((y <= dy / 1.) & (y > -dy / 1.))
        x_slice = ravel(x)[ij]
        ij1 = where((x_slice > 40.40) | (x_slice < 40.0))
        x_slice = x_slice[ij1]
        depth_slice = ravel(q[0, :, :])[ij]
        depth_slice = depth_slice[ij1]
        return x_slice, depth_slice
Esempio n. 39
0
    def plot_topo_xsec(current_data):
        from pylab import plot, cos, sin, where, legend, nan
        t = current_data.t

        x = np.linspace(0.0, domain_x, 201)
        #y = 0.
        B = where(x > 40.0, where(x < 40.40, 7.0, 0.0), 0.0)
        plot(x, B, 'g', label="internal walls")
        legend()
        pylab.legend(fontsize=18)

        pylab.xticks(fontsize=18, fontname="Tex Gyre Pagella")
        pylab.yticks(fontsize=18, fontname="Tex Gyre Pagella")
        t = current_data.t
        pylab.title("Run-up at time t = %10.4e" % t, fontsize=16)
Esempio n. 40
0
    def rank_by_distance_bhatt(self, qkeys, ikeys, rkeys, dists):
        """
        ::

            Reduce timbre-channel distances to ranks list by ground-truth key indices
            Bhattacharyya distance on timbre-channel probabilities and Kullback distances
        """
        # timbre-channel search using pre-computed distances
        ranks_list = []
        t_keys, t_lens = self.get_adb_lists(0) 
        rdists=pylab.ones(len(t_keys))*float('inf')
        qk = self._get_probs_tc(qkeys)
        for i in range(len(ikeys[0])): # number of include keys
            ikey=[]
            dk = pylab.zeros(self.timbre_channels)
            for t_chan in range(self.timbre_channels): # timbre channels
                ikey.append(ikeys[t_chan][i])
                try: 
                    # find dist of key i for query
                    i_idx = rkeys[t_chan].index( ikey[t_chan] ) # dataset include-key match
                    # the reduced distance function in include_keys order
                    # distance is Bhattacharyya distance on probs and dists
                    dk[t_chan] = dists[t_chan][i_idx]
                except:
                    print "Key not found in result list: ", ikey, "for query:", qkeys[t_chan]
                    raise error.BregmanError()
            rk = self._get_probs_tc(ikey)
            a_idx = t_keys.index( ikey[0] ) # audiodb include-key index
            rdists[a_idx] = distance.bhatt(pylab.sqrt(pylab.absolute(dk)), pylab.sqrt(pylab.absolute(qk*rk)))
        #search for the index of the relevant keys
        rdists = pylab.absolute(rdists)
        sort_idx = pylab.argsort(rdists)   # Sort fields into database order
        for r in self.ground_truth: # relevant keys
            ranks_list.append(pylab.where(sort_idx==r)[0][0]) # Rank of the relevant key
        return ranks_list, rdists
Esempio n. 41
0
def maximize_icsd_correlation_brute(m,
                                    exps=pl.log10(pl.linspace(1E-5, 1E-3,
                                                              100))):
    '''
    Return correlation coefficient between iCSD estimates and ground truth CSD
    for a series of assumed source diameters
    '''
    if m != 'delta' and m != 'step' and m != 'spline':
        raise ValueError, 'Must have iCSD method'
    diams = 10**exps
    corrcoefs = pl.zeros(diams.size)
    if __name__ == '__main__':
        freeze_support()
        task_queue = Queue()
        done_queue = Queue()

        TASKS = pl.arange(diams.size)

        for task in TASKS:
            task_queue.put(int(task))
        for i in xrange(NUMBER_OF_PROCESSES):
            Process(target=__csd_correlation_thr,
                    args=(task_queue, diams, m, done_queue)).start()
        for n in xrange(TASKS.size):
            nn, corrcoef = done_queue.get()
            corrcoefs[nn] = corrcoef
        for i in xrange(NUMBER_OF_PROCESSES):
            task_queue.put('STOP')

        task_queue.close()
        done_queue.close()

        return corrcoefs, diams, diams[pl.where(corrcoef == corrcoef.max())]
Esempio n. 42
0
def ssc(signal,samplerate=16000,winlen=0.025,winstep=0.01,
          nfilt=26,nfft=512,lowfreq=0,highfreq=None,preemph=0.97):
    """Compute Spectral Subband Centroid features from an audio signal.

    :param signal: the audio signal from which to compute features. Should be an N*1 array
    :param samplerate: the samplerate of the signal we are working with.
    :param winlen: the length of the analysis window in seconds. Default is 0.025s (25 milliseconds)    
    :param winstep: the step between successive windows in seconds. Default is 0.01s (10 milliseconds)    
    :param nfilt: the number of filters in the filterbank, default 26.
    :param nfft: the FFT size. Default is 512.
    :param lowfreq: lowest band edge of mel filters. In Hz, default is 0.
    :param highfreq: highest band edge of mel filters. In Hz, default is samplerate/2
    :param preemph: apply preemphasis filter with preemph as coefficient. 0 is no filter. Default is 0.97. 
    :returns: A numpy array of size (NUMFRAMES by nfilt) containing features. Each row holds 1 feature vector. 
    """          
    highfreq= highfreq or samplerate/2
    signal = sigproc.preemphasis(signal,preemph)
    frames = sigproc.framesig(signal, winlen*samplerate, winstep*samplerate)
    pspec = sigproc.powspec(frames,nfft)
    pspec = pylab.where(pspec == 0,pylab.finfo(float).eps,pspec) # if things are all zeros we get problems
    
    fb = get_filterbanks(nfilt,nfft,samplerate,lowfreq,highfreq)
    feat = pylab.dot(pspec,fb.T) # compute the filterbank energies
    R = pylab.tile(pylab.linspace(1,samplerate/2,pylab.size(pspec,1)),(pylab.size(pspec,0),1))
    
    return pylab.dot(pspec*R,fb.T) / feat
    def addDataVectorAccessor(self, data_vector_accessor):
        self.__data_vectors_accessors__.append(data_vector_accessor)

        _sum = pl.sum(data_vector_accessor.signal)
        _min = pl.amin(data_vector_accessor.signal)
        _max = pl.amax(data_vector_accessor.signal)

        if self.__minimal_signal__ == None:
            self.__minimal_signal__ = _sum
            self.__minimal_data_vector_accessor__ = data_vector_accessor

            self.__min_signal__ = _min
            self.__max_signal__ = _max

        if _sum < self.__minimal_signal__:
            self.__minimal_data_vector_accessor__ = data_vector_accessor
            self.__minimal_signal__ = _sum

        if _min < self.__min_signal__:
            self.__min_signal__ = _min

        if _max > self.__max_signal__:
            self.__max_signal__ = _max

        #collects unique annotations (>0) as a set
        if not data_vector_accessor.annotation == None:
            unique_annotations = pl.unique(data_vector_accessor.annotation[
                                pl.where(data_vector_accessor.annotation > 0)])
            if len(unique_annotations) > 0:
                #union of sets
                self.__unique_annotations__ |= set(unique_annotations)
Esempio n. 44
0
def specgram_demo():
   '''
   the demo in matplotlib. But calls
   interactive.specgram
   '''
   from pylab import arange, sin, where, logical_and, randn, pi

   dt = 0.0005
   t = arange(0.0, 20.0, dt)
   s1 = sin(2*pi*100*t)
   s2 = 2*sin(2*pi*400*t)

   # create a transient "chirp"
   mask = where(logical_and(t>10, t<12), 1.0, 0.0)
   s2 = s2 * mask

   # add some noise into the mix
   nse = 0.01*randn(len(t))

   x = s1 + s2 + nse # the signal
   NFFT = 1024       # the length of the windowing segments
   Fs = int(1.0/dt)  # the sampling frequency

   from ifigure.interactive import figure, specgram, nsec, plot, isec, clog, hold

   figure()
   hold(True)
   nsec(2)
   isec(0)
   plot(t, x)
   isec(1)
   specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
   clog()
 def get_lumAtWavelen(self, wavelen):
     lum = None
     index = py.where(self._wavelen > wavelen)[0][0]
     lum = self._lum[index]
         
     assert lum != None, "wavelength out of bound"
     return lum
Esempio n. 46
0
def fast_CIC_deposit(x, mi, Ngrid=N, periodic=1):
    """cloud in cell density estimator
    """
    if ((np.size(mi)) < (np.size(x))):
        m = x.copy()
        m[:] = mi
    else:
        m = mi

    dx = 1. / Ngrid
    rho = np.zeros(Ngrid)

    left = x - 0.5 * dx
    right = left + dx
    xi = np.int32(left / dx)
    frac = (1. + xi - left / dx)
    ind = pyl.where(left < 0.)
    frac[ind] = (-(left[ind] / dx))
    xi[ind] = Ngrid - 1
    xir = xi.copy() + 1
    xir[xir == Ngrid] = 0
    rho = pyl.bincount(xi, weights=frac * m, minlength=Ngrid)
    rho2 = pyl.bincount(xir, weights=(1. - frac) * m, minlength=Ngrid)

    rho += rho2

    return rho * Ngrid
Esempio n. 47
0
    def rank_by_distance_bhatt(self, qkeys, ikeys, rkeys, dists):
        """
        ::

            Reduce timbre-channel distances to ranks list by ground-truth key indices
            Bhattacharyya distance on timbre-channel probabilities and Kullback distances
        """
        # timbre-channel search using pre-computed distances
        ranks_list = []
        t_keys, t_lens = self.get_adb_lists(0) 
        rdists=pylab.ones(len(t_keys))*float('inf')
        qk = self._get_probs_tc(qkeys)
        for i in range(len(ikeys[0])): # number of include keys
            ikey=[]
            dk = pylab.zeros(self.timbre_channels)
            for t_chan in range(self.timbre_channels): # timbre channels
                ikey.append(ikeys[t_chan][i])
                try: 
                    # find dist of key i for query
                    i_idx = rkeys[t_chan].index( ikey[t_chan] ) # dataset include-key match
                    # the reduced distance function in include_keys order
                    # distance is Bhattacharyya distance on probs and dists
                    dk[t_chan] = dists[t_chan][i_idx]
                except:
                    print("Key not found in result list: ", ikey, "for query:", qkeys[t_chan])
                    raise error.BregmanError()
            rk = self._get_probs_tc(ikey)
            a_idx = t_keys.index( ikey[0] ) # audiodb include-key index
            rdists[a_idx] = distance.bhatt(pylab.sqrt(pylab.absolute(dk)), pylab.sqrt(pylab.absolute(qk*rk)))
        #search for the index of the relevant keys
        rdists = pylab.absolute(rdists)
        sort_idx = pylab.argsort(rdists)   # Sort fields into database order
        for r in self.ground_truth: # relevant keys
            ranks_list.append(pylab.where(sort_idx==r)[0][0]) # Rank of the relevant key
        return ranks_list, rdists
Esempio n. 48
0
    def rank_by_distance_avg(self, qkeys, ikeys, rkeys, dists):
        """
        ::

            Reduce timbre-channel distances to ranks list by ground-truth key indices
            Kullback distances
        """
        # timbre-channel search using pre-computed distances
        ranks_list = []
        t_keys, t_lens = self.get_adb_lists(0) 
        rdists=pylab.ones(len(t_keys))*float('inf')
        for t_chan in range(self.timbre_channels): # timbre channels
            t_keys, t_lens = self.get_adb_lists(t_chan) 
            for i, ikey in enumerate(ikeys[t_chan]): # include keys, results
                try: 
                    # find dist of key i for query
                    i_idx = rkeys[t_chan].index( ikey ) # lower_bounded include-key index
                    a_idx = t_keys.index( ikey ) # audiodb include-key index
                    # the reduced distance function in include_keys order
                    # distance is the sum for now
                    if t_chan:
                        rdists[a_idx] += dists[t_chan][i_idx]
                    else:
                        rdists[a_idx] = dists[t_chan][i_idx]
                except:
                    print("Key not found in result list: ", ikey, "for query:", qkeys[t_chan])
                    raise error.BregmanError()
        #search for the index of the relevant keys
        rdists = pylab.absolute(rdists)
        sort_idx = pylab.argsort(rdists)   # Sort fields into database order
        for r in self.ground_truth: # relevant keys
            ranks_list.append(pylab.where(sort_idx==r)[0][0]) # Rank of the relevant key
        return ranks_list, rdists
Esempio n. 49
0
 def vvel(current_data):
     from numpy import where, sqrt
     q = current_data.q
     h = q[0,:]
     dry_tol = 0.001
     v = where(h>dry_tol, q[2,:]/h, 0.)
     return v
Esempio n. 50
0
    def rank_by_distance_avg(self, qkeys, ikeys, rkeys, dists):
        """
        ::

            Reduce timbre-channel distances to ranks list by ground-truth key indices
            Kullback distances
        """
        # timbre-channel search using pre-computed distances
        ranks_list = []
        t_keys, t_lens = self.get_adb_lists(0) 
        rdists=pylab.ones(len(t_keys))*float('inf')
        for t_chan in range(self.timbre_channels): # timbre channels
            t_keys, t_lens = self.get_adb_lists(t_chan) 
            for i, ikey in enumerate(ikeys[t_chan]): # include keys, results
                try: 
                    # find dist of key i for query
                    i_idx = rkeys[t_chan].index( ikey ) # lower_bounded include-key index
                    a_idx = t_keys.index( ikey ) # audiodb include-key index
                    # the reduced distance function in include_keys order
                    # distance is the sum for now
                    if t_chan:
                        rdists[a_idx] += dists[t_chan][i_idx]
                    else:
                        rdists[a_idx] = dists[t_chan][i_idx]
                except:
                    print "Key not found in result list: ", ikey, "for query:", qkeys[t_chan]
                    raise error.BregmanError()
        #search for the index of the relevant keys
        rdists = pylab.absolute(rdists)
        sort_idx = pylab.argsort(rdists)   # Sort fields into database order
        for r in self.ground_truth: # relevant keys
            ranks_list.append(pylab.where(sort_idx==r)[0][0]) # Rank of the relevant key
        return ranks_list, rdists
Esempio n. 51
0
def minimize_icsd_error_brute(m, exps=pl.log10(pl.linspace(1E-5, 1E-3, 100))):
    '''
    Return summed squared difference between iCSD estimates and ground truth CSD
    for a series of assumed source diameters
    '''
    if m != 'delta' and m != 'step' and m != 'spline':
        raise ValueError, 'Must have iCSD method'
    diams = 10**exps
    errors = pl.zeros(diams.size)
    if __name__ == '__main__':
        freeze_support()
        task_queue = Queue()
        done_queue = Queue()

        TASKS = pl.arange(diams.size)

        for task in TASKS:
            task_queue.put(int(task))
        for i in xrange(NUMBER_OF_PROCESSES):
            Process(target=__csd_error_thr,
                    args=(task_queue, diams, m, done_queue)).start()
        for n in xrange(TASKS.size):
            nn, error = done_queue.get()
            errors[nn] = error
        for i in xrange(NUMBER_OF_PROCESSES):
            task_queue.put('STOP')

        task_queue.close()
        done_queue.close()

        return errors, diams, diams[pl.where(errors == errors.min())]
 def activate(self, inputs):
   assert len(inputs) == NN.ni, 'incorrect number of inputs'
   self.ai[:] = np.tanh(inputs).copy()
   self.ah[:] = np.tanh(np.sum(self.wi.T * self.ai, axis=1))
   self.ao[:] = np.sum(self.wo.T * self.ah, axis=1) + 0.5
   self.ao[:] = where(self.ao == max(self.ao), 1.0, 0.0)
   return self.ao.copy()
Esempio n. 53
0
def beat_track(x, feature=LogFrequencySpectrum, **kwargs):
    """
    Scheirer beat tracker. Use output of comb filter bank on filterbank
                           sub-bands to estimate tempo, and comb filter state
                           to track beats.
    inputs:
       x        - the audio signal or filename to analyze
       feature  - the feature class to use [LogFrequencySpectrum]
       **kwargs - parameters to the feature extractor [nbpo=1, nhop=441]
    outputs:
       z      - per-tempo comb filter summed outputs
       tempos - the range of tempos in z
       D      - the differentiated half-wave rectified octave-band filterbank
                outputs
    """
    kwargs.setdefault('nhop', 441)
    kwargs.setdefault('nbpo', 1)
    F = feature(x, **kwargs)
    frame_rate = F.sample_rate / float(F.nhop)
    D = diff(F.X, axis=1)
    D[where(D < 0)] = 0
    tempos = range(40, 200, 4)
    z = zeros((len(tempos), D.shape[0]))
    for i, bpm in enumerate(tempos):  # loop over tempos to test
        t = int(round(frame_rate * 60. / bpm))  # num frames per beat
        alpha = 0.5**(2.0/t)
        b = [1 - alpha]
        a = zeros(t)
        a[0] = 1.0
        a[-1] = alpha
        z[i, :] = lfilter(b, a, D).sum(1)  # filter and sum sub-band onsets
    return z, tempos, D
Esempio n. 54
0
	def testWeightsAreNotTheSame(self):
		"""Compares each weight against all others but could be sped
		up don't need to compare twice"""
		for i in range(len(self.pop)):
			for j in range(len(self.pop)):
				if i != j:
					wi1 = array(self.pop[i].wi)
					wi2 = array(self.pop[j].wi)
					comparisons = where( wi1 == wi2, True, False)
					for c in comparisons:
						self.assertFalse(c.all())					

					wo1 = array(self.pop[i].wo)
					wo2 = array(self.pop[j].wo)
					comparisons = where( wo1 == wo2, True, False)
					for c in comparisons:
						self.assertFalse(c.all())
def percentAcc(all_aos, targets):
  correct = 0
  for i, trg in enumerate(targets):
    sample_res = where(trg == array(all_aos[i]), True, False)
    if sample_res.all():
      correct += 1
  total = len(all_aos)
  return float(correct) / total
Esempio n. 56
0
def get_data(ifname):
    '''Get spectrum for a given input filename
    '''

    bla = False

    i = ifname.rfind('.')

    if i == -1:
        name = ifname
        ext = ''
    else:
        name = ifname[:i]
        ext = ifname[i+1:]

    if bla:
        print name, ext

    if ext in ['dat', 'txt', 'convol', 'spec', 'tmp', 'cvl', '']:
        data = pl.loadtxt(ifname)
    elif ext == 'bin':
        data = []
        ifile = open(ifname, 'rb')
        while True:
            record = ifile.read(16)
            if len(record) != 16:
                break
            record = struct.unpack('dd', record)
            data.append(record)
        data = pl.asarray(data)
    elif ext in ['fits', 'fit']:
        data = load_fits(ifname, False, extn)
    else:
        try: 
            data = pl.loadtxt(ifname)
        except:
            print "Unknown format of input spectra."
            quit(1)

    # Remove possible nan in the input data
    idx, = pl.where(data[:, 1] == data[:, 1]) # idx is a tuple
    data = data[idx, :]
    idx, = pl.where(data[:, 0] == data[:, 0])
    data = data[idx, :]

    return data
 def activate(self, inputs):
   assert len(inputs) == NN.ni, 'incorrect number of inputs'
   for i in range(NN.ni):
       self.ai[i] = np.tanh(inputs[i])
   self.ah = np.tanh(np.sum(self.wi.T * self.ai, axis=1))
   self.ao = np.sum(self.wo.T * self.ah, axis=1)
   self.ao = where(self.ao > 0.5, 1.0, 0.0)
   return self.ao