Пример #1
0
def getEazyPz(idx,
              MAIN_OUTPUT_FILE='photz',
              OUTPUT_DIRECTORY='./OUTPUT',
              CACHE_FILE='Same'):
    """
    zgrid, pz = getEazyPz(idx, \
                      MAIN_OUTPUT_FILE='photz', \
                      OUTPUT_DIRECTORY='./OUTPUT', \
                      CACHE_FILE='Same')
                      
    Get Eazy p(z) for object #idx.
    """
    tempfilt, coeffs, temp_seds, pz = readEazyBinary(MAIN_OUTPUT_FILE=MAIN_OUTPUT_FILE, \
                                                    OUTPUT_DIRECTORY=OUTPUT_DIRECTORY, \
                                                    CACHE_FILE = CACHE_FILE)

    if pz is None:
        return None, None

    ###### Get p(z|m) from prior grid
    kidx = pz['kidx'][idx]
    #print kidx, pz['priorzk'].shape
    if (kidx > 0) & (kidx < pz['priorzk'].shape[1]):
        prior = pz['priorzk'][:, kidx]
    else:
        prior = pylab.ones(pz['NZ'])

    ###### Convert Chi2 to p(z)
    pzi = pylab.exp(
        -0.5 * (pz['chi2fit'][:, idx] - min(pz['chi2fit'][:, idx]))) * prior
    if pylab.sum(pzi) > 0:
        pzi /= pylab.trapz(pzi, tempfilt['zgrid'])

    ###### Done
    return tempfilt['zgrid'], pzi
Пример #2
0
def broadgauss(x, y, sigma):
    '''Gaussian function for broadening
    '''

    bla = True
    plot = False
    c = 299792458.

    if bla:
        print " sigma = ", round(sigma, 4), " km/s"

    sigma = sigma * 1.0e3/c * pl.mean(x)   # sigma in Å

    if bla:
        print " sigma = ", round(sigma, 3), "  Å "

    xk = x - pl.mean(x)

    g = make_gauss(1, 0, sigma)
    yk = [g(i) for i in xk]

    if bla:
        print " Integral of the gaussian function: ", pl.trapz(yk, xk).__format__('5.3')

    if plot:
        pl.figure(2)
        pl.plot(xk, yk, '+-')
        pl.show()
    #if bla: print" size y:", y.size
    y = pl.convolve(y, yk, mode='same')
    #if bla: print" size y:", y.size

    return y/max(y)
Пример #3
0
def broadgauss(x, y, sigma):
    '''Gaussian function for broadening
    '''

    bla = True
    plot = False
    c = 299792458.

    if bla:
        print " sigma = ", round(sigma, 4), " km/s"

    sigma = sigma * 1.0e3 / c * pl.mean(x)  # sigma in Å

    if bla:
        print " sigma = ", round(sigma, 3), "  Å "

    xk = x - pl.mean(x)

    g = make_gauss(1, 0, sigma)
    yk = [g(i) for i in xk]

    if bla:
        print " Integral of the gaussian function: ", pl.trapz(
            yk, xk).__format__('5.3')

    if plot:
        pl.figure(2)
        pl.plot(xk, yk, '+-')
        pl.show()
    #if bla: print" size y:", y.size
    y = pl.convolve(y, yk, mode='same')
    #if bla: print" size y:", y.size

    return y / max(y)
Пример #4
0
def px_smooth(idx, e, x, idx_table, N_HE0, N_US, N_US_HE, WC):
    """Over sample, smooth and undersample photoionization cross-sections
    """
    i, nmin, ntot, m, l, p, pos = idx_table[idx]

    try:
        # case of TOPBASE data
        nmin.index(".")
        nmin = pl.nan
    except ValueError:
        nmin = int(nmin)

    # Keep sampling for high energy values where the variation follow Kramer's law
    if isinstance(int(ntot) - nmin, int):
        N_HE = int(ntot) - nmin
    else:
        N_HE = N_HE0

    if N_HE >= e.size:
        N_HE = -e.size
        print("Warning: N_HE is larger than photoionization table, select all the table.")

    e_sel = e[:-N_HE]
    e_sel_log = pl.log10(e_sel)
    x_sel = x[:-N_HE]

    # Interpolate and smooth data
    # e_i = pl.linspace(min(e_sel), max(e_sel), 10000)
    e_i_log = pl.linspace(min(e_sel_log), max(e_sel_log), 10000)
    e_i = 10 ** e_i_log
    x_i = pl.interp(e_i, e_sel, x_sel)
    x_is = smooth(x_i, WC)
    e_us = pl.concatenate([e_i[0:10], e_i[::N_US], e[int(ntot) - N_HE :: N_US_HE]])
    x_us = pl.concatenate([x_is[0:10], x_is[::N_US], x[int(ntot) - N_HE :: N_US_HE]])

    if x_us.any() == 0.0:
        print("x_us = 0")
        quit(1)

    # Conservation of area
    # area = pl.trapz( x_Mb, e_eV)   # total
    # area = pl.trapz( e_sel, x_sel) # selected
    area_i = pl.trapz(x_i, e_i)  # selected interpolated
    area_is = pl.trapz(x_is, e_i)  # selected interpolated and sampled
    # area_us = pl.trapz(x_us, e_us)

    return e_us, x_us, area_i, area_is
Пример #5
0
 def func(T):
     x0  = de * Cst.Q / Cst.K / T
     #x_vec = pl.linspace(0.0001, 100, 300)
     x_vec_log = pl.linspace(-5, 4, 100)
     x_vec = 10**x_vec_log
     q_vec = [sigma_seaton(i+x0, ion, line, T) for i in x_vec]
     ups_cst = line.lower.g * Cst.K * T / Cst.RYD / Cst.H / Cst.C 
     return ups_cst * pl.trapz(q_vec*(x_vec+x0)*pl.exp(-(x_vec)), x_vec)
Пример #6
0
def average_gaunt_fisher(dn, Eo, T):
    ''' Compute the maxwellian average of the Gaunt factor for electron collision
        Only for neutral species
    '''
    x_vec = pl.linspace(0., 8., 50) # sqrt(E/Eo)
    x0 = Eo * Cst.Q / (Cst.K * T)    # Eo/kT
    g_func = make_gaunt_fisher(dn)
    g_vec = [g_func(x) * pl.exp(-x**2*x0) for x in x_vec]
    return pl.trapz(g_vec, x_vec**2*x0)
Пример #7
0
def average_gaunt_vanregemorter2(deg, Eo, T):
    ''' Compute the maxwellian average of the Gaunt factor for electron collision
    '''
    x_vec = pl.linspace(0.2, 8., 50) # sqrt(E/Eo)
    x0 = Eo * Cst.Q / (Cst.K * T)    # Eo/kT
    g_func = make_gaunt_vanregemorter(deg)
    g_vec = [g_func(x) * pl.exp(-x**2*x0) for x in x_vec]

    return pl.trapz(g_vec, x_vec**2*x0)
Пример #8
0
def kappa_integration(sections):
    r = []
    kappa = []
    for i_sec in range(1, sections["n_sec"]):
        r.append(sections[i_sec]["r_start"])
        kappa.append(sections[i_sec]["kappa"])
    r.append(sections[i_sec]["r_end"])
    kappa.append(0)

    r = py.array(r)
    r = r - r[0]
    kappa = py.array(kappa)
    theta_fun = lambda i_in: py.trapz(kappa[:i_in], r[:i_in])
    w_new = []
    theta_new = []
    for i_in in range(1, len(r) + 1):
        theta_new.append(theta_fun(i_in))
        w_new.append(py.trapz(theta_new, r[:i_in]))
    return (r, w_new)
Пример #9
0
 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
Пример #10
0
def tomczak_sampler(pdf, N=1):
    '''
    Draws random samples from the provided PDF.

    Parameters
    ----------

    pdf : array
        2d array of the PDF. The first and second columns
        must be x and P(x) respectively. P(x) is not required
        to be normalized beforehand.
    N : int
        Number of desired samplings.

    Returns
    -------
    sample : array
        Random samplings from the provided PDF.
    '''

    x, px = pdf[:, 0], pdf[:, 1]
    px = px / pylab.trapz(px, x)

    u = []
    for i in range(len(x)):
        xprime = x[pylab.arange(i)]
        pxprime = px[pylab.arange(i)]
        uprime = pylab.trapz(pxprime, xprime)
        u.append(uprime)
    u = pylab.array(u)

    sample = []
    urand = pylab.rand(N)

    for ui in urand:

        du = abs(u - ui)
        ind = pylab.where(du == min(du))[0][0]
        sample.append(x[ind])

    return pylab.array(sample)
Пример #11
0
def auroc(x1, x2, N=40, limits=None):
  """Area under the ROC curve. Given scalar data from two groups (x1,x2) what is the probability that an ideal
  observer will be able to correctly classify the groups?

  >>> x1 = pylab.zeros(10)
  >>> x2 = pylab.zeros(10) + 1
  >>> print auroc(x1,x2)[0]
  1.0
  >>>
  >>> x1 = pylab.zeros(10)
  >>> x2 = pylab.zeros(10) - 1
  >>> print auroc(x1,x2)[0]
  0.0
  >>> pylab.seed(0)
  >>> x1 = pylab.randn(10000)
  >>> x2 = pylab.randn(10000)
  >>> print round(auroc(x1,x2)[0],3)
  0.509
  >>> pylab.seed(0)
  >>> x1 = pylab.randn(100000)+1
  >>> x2 = pylab.randn(100000)
  >>> print round(auroc(x1,x2)[0],3)
  0.241
  >>> pylab.seed(5)
  >>> x1 = pylab.randn(100000)-1.5
  >>> x2 = pylab.randn(100000)
  >>> print round(auroc(x1,x2)[0],3)
  0.853
  """
  n1 = float(x1.size)
  n2 = float(x2.size)
  if limits is None:
    st = min(x1.min(), x2.min())
    nd = max(x1.max(), x2.max())
    ra = nd - st
    if ra == 0:
      ra = .1 #this happens if all our readings are identical (we should actually get cp = 0.5 for this
    st -= .01*ra
    nd += .01*ra
  else:
    st = limits[0]
    nd = limits[1]
  cri = pylab.linspace(nd,st,N)
  roc = [pylab.zeros(N), pylab.zeros(N)]
  for n in xrange(N):
    roc[1][n] = pylab.find(x2 > cri[n]).size/n2
    roc[0][n] = pylab.find(x1 > cri[n]).size/n1

  return pylab.trapz(roc[1], roc[0]), roc
Пример #12
0
def auroc(x1, x2, N=40, limits=None):
    """Area under the ROC curve. Given scalar data from two groups (x1,x2) what is the probability that an ideal
  observer will be able to correctly classify the groups?

  >>> x1 = pylab.zeros(10)
  >>> x2 = pylab.zeros(10) + 1
  >>> print auroc(x1,x2)[0]
  1.0
  >>>
  >>> x1 = pylab.zeros(10)
  >>> x2 = pylab.zeros(10) - 1
  >>> print auroc(x1,x2)[0]
  0.0
  >>> pylab.seed(0)
  >>> x1 = pylab.randn(10000)
  >>> x2 = pylab.randn(10000)
  >>> print round(auroc(x1,x2)[0],3)
  0.509
  >>> pylab.seed(0)
  >>> x1 = pylab.randn(100000)+1
  >>> x2 = pylab.randn(100000)
  >>> print round(auroc(x1,x2)[0],3)
  0.241
  >>> pylab.seed(5)
  >>> x1 = pylab.randn(100000)-1.5
  >>> x2 = pylab.randn(100000)
  >>> print round(auroc(x1,x2)[0],3)
  0.853
  """
    n1 = float(x1.size)
    n2 = float(x2.size)
    if limits is None:
        st = min(x1.min(), x2.min())
        nd = max(x1.max(), x2.max())
        ra = nd - st
        if ra == 0:
            ra = .1  #this happens if all our readings are identical (we should actually get cp = 0.5 for this
        st -= .01 * ra
        nd += .01 * ra
    else:
        st = limits[0]
        nd = limits[1]
    cri = pylab.linspace(nd, st, N)
    roc = [pylab.zeros(N), pylab.zeros(N)]
    for n in xrange(N):
        roc[1][n] = pylab.find(x2 > cri[n]).size / n2
        roc[0][n] = pylab.find(x1 > cri[n]).size / n1

    return pylab.trapz(roc[1], roc[0]), roc
Пример #13
0
def qqplot ( th, D, interval=(0,1) ):
    """Create a q-q plot of theta with respect to density D

    :Parameters:
        *th*
            a sample that might be from the density D
        *D*
            a density object
    """
    th = th.copy()
    th.sort()
    nq = len(th)
    q = (pl.arange ( 0, nq, dtype='d' )+1)/nq
    x = pl.mgrid[interval[0]:interval[1]:1j*4*nq]
    f = D(x)
    qD = pl.cumsum(f)*pl.diff(x)[0]/pl.trapz(f,x)
    th_ = []
    for q_ in q:
        i = pl.where ( qD<=q_ )[0]
        th_.append ( x[i[-1]] )

    pl.plot ( th, th_, '.' )
    pl.plot ( [th[0],th[-1]],[th[0],th[-1]], 'k:' )
Пример #14
0
 
for CpFile in CpFiles:
    alpha = -10+0.2*count
    print "reading Cp file" , CpFile
    data = readTwoColumnFile(CpFile)
 
    x, y, Cp, L, T, N, midx, midy, midcp, Tnode, Nnode, xnew, newCpUpper, newCpLower = postprocessCpData(data, geodata, 100)
 
    # panel Cp x and y components
    midcpx = midcp*N[:,0]
    midcpy = midcp*N[:,1]
 
    dx = pl.diff(x)
    dy = pl.diff(y)
 
    forceX = pl.trapz(midcpx*L)
    forceY = pl.trapz(midcpy*L)
    momentY = pl.trapz((midx-0.25)*midcpy*L)
    AoA = alpha*pl.pi/180.
    forceD = +forceX*pl.cos(AoA)+forceY*pl.sin(AoA)
    forceL = -forceX*pl.sin(AoA)+forceY*pl.cos(AoA)
    postprocessResult.write( "%12.6f %12.6f %12.6f %12.6f %12.6f %12.6f\n" % (AoA,forceX,forceY,momentY,forceD, forceL) )
 
    ForceMoment.append( [AoA,forceX,forceY,momentY,forceD,forceL])
 
    # plot upper and lower profile parts in different colors
    pl.figure()
    pl.plot( x[dx<0] ,data[dx<0] ,'r.-',lw=2)     
    pl.plot( x[dx>=0],data[dx>=0],'b.-',lw=2)
    pl.legend(['upper $C_p$','lower $C_p$'])
    pl.title(r'$C_p @ \alpha -10$')
Пример #15
0
def tailfit(filename,
            display=None,
            start_point=None,
            direction='down',
            output_jpegs=False,
            plotlengths=False,
            tail_startpoint=None,
            scale=0.5):
    # avi, 1st time:   fittedtail,startpoint,  direction, FPS, numframes  = tailfit(videopath,(first or not displayonlyfirst) and display ,startpoints)
    # fittedtail,startpoint,  direction, FPS, numframes  = tailfit(videopath,(first or not displayonlyfirst) and display ,startpoints[i])
    # Question. Keep eyes on how does start_point work?
    '''
    Takes an avi filepath, fits the tail of the fish
    Display sets if the fit is shown as it is processed (slower)
    Start point is where fitting begins, if None the user is queried
    Direction is which direction the fit happens
    '''
    '''1ST PART. INITIATE THE PARAMETERS AND READ THE FRAME'''
    directions = {
        "up": [0, -1],
        "down": [0, 1],
        "left": [-1, 0],
        "right": [1, 0]
    }
    # Question. up and down are inversed?
    fitted_tail = []

    ##  print filename, os.path.exists(filename)
    cap = cv2.VideoCapture(filename)  ########DT error here...tag
    if not cap.isOpened():
        print "Error with video or path!"
        raise Exception('Issues opening video file!')

    frame = cap.read()[1]
    frame = cv2.resize(
        frame, (0, 0), fx=scale, fy=scale,
        interpolation=cv2.INTER_CUBIC)  #[ADJUST] resize-tag #resize the frame!

    # Question. abt the grammar? why not start from [0]
    # Answer. cv2.VideoCapture.read([image]) returns tuple (retval, image), so frame only takes the returned image
    cv2.destroyAllWindows()

    max_points = 200  # mostly in case it somehow gets stuck in a loop, and to preallocate the result array
    # Question. meaning of this max_points?

    frame_fit = np.zeros((max_points, 2))
    # frame_fit is a 2d array, tuple inside the np.zeros() defines the shape of frame

    first_frame = True
    # first_frame just told the program if it is processing the first frame
    widths, convolveresults = [], []
    test, slices = [], []
    '''2ND PART. ANALYSIS FRAME ONE BY ONE'''
    while type(frame) != type(None):
        # LOOP NO.1 while is the biggest loop, it continutes until analysis of the 1st frame.
        # Question. how to break this loop? Answer. At the end, when next read of frame returns None

        if display:
            # display in main-function is boolean
            frame_display = frame.copy()
        if direction:
            guess_vector = np.array(directions[direction])
            # guess_vector represent which direction the fit happens
        else:
            raise Exception('Need to define a direction!')  #could ask here
        '''2-1. IF FIRST FRAME'''
        '''This 2-1. session is only implemented one time during the 1st frame'''
        if first_frame:
            # Note, first_frame=True, this is defined outside the while loops

            #TODO try to ID the head?
            #predict tail pos, and dir?
            #then query user for ok
            #detect things aligned the same as before, by looking for big shifts between first frames of video?
            '''2-1.1 SET THE STARTPOINT'''
            #SET THE STARTPOINT. if we don't have a start point, query user for one
            if type(start_point) == type(np.array(
                [])) or type(start_point) is list:
                current = np.array(start_point)
                point = current
            elif str(type(tail_startpoint)) != "<type 'NoneType'>":
                start_point = tail_startpoint
                point = start_point
                current = start_point
            else:
                handlec = handleclick
                cv2.namedWindow('first')
                cv2.imshow("first", frame)
                cv2.moveWindow('first', 0, 0)
                #would be nice to raise window, since it doesn't always spawn top

                cv2.waitKey(10)
                point = np.array([-1, -1])
                cv2.setMouseCallback("first", handlec, point)
                # cv2.setMouseCallback(windowName, onMouse[, param])
                print "Click on start of the fish's tail"
                cv2.waitKey(
                    10)  # Question. difference between 0 and 10?    #tag
                while (point == np.array([-1, -1])).all(
                ):  # Question. this all() is strange ... Answer. point is a list/array
                    cv2.waitKey(10)
                current = point
                start_point = current
                print 'start point is ', start_point
                # the start_point is set here.... it seems to be correlated to threshold as well?
                cv2.destroyWindow('first')
                # TODO Simplify the code, too redunctant

            # NOTE. CONFIRMED. current can be accessed outside the if & else
            # DT-Code: print 'current can be accessed outside the if & else, like: ', current
            '''2-1.2 ILLUMINATION ANALYSIS FOR BG & FISH'''
            # BUILD THE HISTOGRAM, frame is np.ndarray, 2D-gray scale, 3D-RGB
            if frame.ndim == 2:
                hist = np.histogram(frame[:, :], 10, (0, 255))
                # Question. 255 can't be divided by 10?   A. 10 means the number of bars, not the interval
                # hist/returned value of np.histogram is a tuple, the first item is occurrence in each bin, the second item is bin
                # numpy.histogram(a, bins=10, range=None, normed=False, weights=None, density=None)
                # If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths.
            elif frame.ndim == 3:
                # Task. maybe this loop for RGB vedios, try this later
                hist = np.histogram(frame[:, :, 0], 10, (0, 255))
                # Question. the meaning of this sentence?
                # Question. if frame is 3D then it should be converted into gray scale??? Can't just take R to construct histogram
            else:
                raise Exception('Unknown video format!')

            # find background - 10 bin hist of frame, use most common as background
            background = hist[1][hist[0].argmax()] / 2 + hist[1][min(
                hist[0].argmax() + 1, len(hist[0]))] / 2
            # DT-CODE. print 'hist: ', hist
            # Note. CONFIRMED. hist[1] should have one more item than hist[0].
            # Note. CONFIRMED. this histogram is only calculated one time for 1st frame
            # Q. why add len(hist[0]) A. to avoid argmax()+1 bigger than len
            # Q. why divided by 2?  A. they take the middle value of the most frequent bar
            # np.argmax(), Returns the indices of the maximum values along an axis.

            # find fish luminosity - area around point
            if frame.ndim == 2:
                fish = frame[point[1] - 2:point[1] + 2,
                             point[0] - 2:point[0] + 2].mean()
                # TASK-DONE. I want to draw and see how big is this chosen area?
                # A. The drawn area is quite small and is within the contour of the fish
                # Note. CONFIRMED. point is the start point set by user
                # Question. why point[1] is the first dimension? Why reverse the x and y axis?
                # fish is like the average grayscale/brightness of the fish image
                # numpy.ndarray.mean(), Returns the average of the array elements along given axis.
            elif frame.ndim == 3:
                fish = frame[point[1] - 2:point[1] + 2,
                             point[0] - 2:point[0] + 2, 0].mean()
            '''2-1.3 BUILD THE GAUSSIAN KERNEL & SET DISPLAY '''
            print "Starting tailfit on:  ", filename
            FPS = cap.get(cv2.cv.CV_CAP_PROP_FPS)
            # CV_CAP_PROP_FPS represent Frame rate
            numframes = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
            # total frame number

            guess_line_width = 51  ###PARAMETERS
            # Question. the meaning of guess_line_width?
            # Question. why bother to divide 4?

            # gaussian kernel, used to find middle of tail  #tag
            normpdf = pylab.normpdf(
                np.arange((-guess_line_width + 1) / 4 + 1,
                          (guess_line_width - 1) / 4), 0, 8)  ###PARAMETERS
            # JULIE-Question. what if I chagnge the mu and sigma? What is the function of this? Why it can be used to find middle of tail?
            # [ADJUST] !!! I think the parameters related to this Gaussian has some relationship with
            # return a 1-D numpy.array with 24 items, delineate the contour of the Gaussian
            # "Return the normal pdf evaluated at *x*; args provides *mu*, *sigma*"
            # return 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-0.5 * (1./sigma*(x - mu))**2)
            # numpy.arange([start, ]stop, [step, ]dtype=None), Return a numpy.ndarray, evenly spaced values within a given interval. The default step size is 1. If step is specified, start must also be given.

            # Display sets if the fit is shown as it is processed (slower)
            if display:
                cv2.namedWindow("frame_display")
                cv2.moveWindow("frame_display", 0, 0)

            starttime = time.time()
            # Return the current time in seconds since the Epoch.

        else:  #task. temporarily skip
            # Question. the if above did so many thing while this else only did this????
            current = fitted_tail[-1][0, :]
            # Question. why there could be [-1]?
            # Question. when is fitted_tail filled? what does this means?
        '''2-2. SET SPACING'''
        # change this multiplier to change the point spacing
        tailpoint_spacing = 5
        '''2-3.FIT THE TAIL WITH CIRCILES IN THIS FRAME(BIG FOR COUNT LOOPS)'''
        # Question. so the whole for loops simply just analysis one tail in one frame?
        # Question. Come on! the meaning of guess?
        # A. should be the point that delineate the contour of tail

        for count in range(max_points):
            # Note. this big for loop breaks when count meets the tail_length, which is the total circles drawn to fit the tail.
            # Question. difficult. But when is tail_length defined?
            '''2-3.1 SET THE GUESS POINT/GUESS_LINE'''
            '''2-3.1.1 GUESS IS THE NEXT FITTED POINTS'''
            if count == 0:
                guess = current  ###tag
                # Question. what is current? A. should be the startpoint
                # task. temp skip the following case
            elif count == 1:
                # DT-CODE:
                # print 'count: ', count
                # print 'guess_vector: ', guess_vector
                # print 'current: ', current
                # print 'before calculation guess is: ', guess
                guess = current + guess_vector * tailpoint_spacing  #can't advance guess vector, since we didn't move from our previous point
                # DT-CODE: print 'after calculation guess is: ', guess
                # Question. what is the difference between guess and new_point?
                # A. it's like guess just confirm the location of guess_slice, guess can be close to new_point, but may not be the same
                # A. new_point is the accurate fit based on the estimation of illumination
            else:
                guess_vector = guess_vector / (
                    ((guess_vector**2).sum())**.5)  #normalize guess vector
                # Question. what if you did not normalize it?
                # A. you will get only two point with large interval
                guess = current + guess_vector * tailpoint_spacing
            '''2-3.1.2 DRAW THE START AND END'''
            # TASK-DIFFICULT TO UNDERSTAND THE SEMANTIC
            # NOTE. start and end is a line vertical to the direction of tail with length of guess_line_width
            guess_line_start = guess + np.array([
                -guess_vector[1], guess_vector[0]
            ]) * guess_line_width / 2  #####tag
            # QUESTION. DIDN'T GET IT... WHEN DID WE GET THIS?
            # directions={"up":[0,-1],"down":[0,1],"left":[-1,0],"right":[1,0]}
            # guess here for the very first time is just start point
            guess_line_end = guess + np.array(
                [guess_vector[1], -guess_vector[0]]) * guess_line_width / 2
            # Question. I understand the grammar above, but not the sematic

            x_indices = np.int_(
                np.linspace(guess_line_start[0], guess_line_end[0],
                            guess_line_width))
            y_indices = np.int_(
                np.linspace(guess_line_start[1], guess_line_end[1],
                            guess_line_width))
            # default interval for np.linspace should be 1
            # 51 = guess_line_width items in x_indices
            # returned x_indices & y_indices are type 'numpy.ndarray'
            # numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
            # np.linspace, Return evenly spaced numbers over a specified interval.
            # num, Number of samples to generate. Default is 50. Must be non-negative.
            # NumPy knows that int refers to np.int_
            '''2-3.1.3 JUDGE IF THE CLIP IS PROPER'''
            # TASK. I am not sure how useful this session would be. wait and see
            if max(y_indices) >= frame.shape[0] or min(y_indices) < 0 or max(
                    x_indices) >= frame.shape[1] or min(x_indices) < 0:
                # Question. I don't understand why is the x and y reversed? x should correspond to frame[0]
                # Answer. IT IS NOT REVERSED! INNER DIMENSION CORRESOPOND TO X!
                y_indices = np.clip(y_indices, 0, frame.shape[0] - 1)
                x_indices = np.clip(x_indices, 0, frame.shape[1] - 1)
                print "Tail got too close to the edge of the frame, clipping search area!"
                #TODO if too many values are clipped, break?
            '''2-3.1.4 DRAW THE GUESS_SLICE'''
            # y_indices and x_indices are np.ndarray!!!!!!
            guess_slice = frame[y_indices, x_indices]
            # DT-code.
            # print 'frame: ', frame
            # print 'shape of frame: ', frame.shape
            # print 'x_indices: ', x_indices
            # print 'y_indices: ', y_indices
            # print 'shape of x_indices: ', x_indices.shape
            # print 'shape of y_indices: ', y_indices.shape
            # print 'guess_slice: ', guess_slice
            # print 'shape of guess_slice: ', guess_slice.shape
            # DT-CODE: frame_display[y_indices, x_indices] = 255  # right way to modify the frame

            # guess_slice is a line that is vertical to the direction of the tail with centre as start_point with two ends as start and end
            # guess_slice is the line with each point coordinate as x & y represented by y_indices & x_indices
            # TASK DIFFICULT! the frame is transposed compared to what might be expected Question. how does this transposition work?
            # JULIE-Question. y and x always inverse?
            # Answer. IT IS NOT REVERSED! INNER DIMENSION CORRESOPOND TO X!

            # S-Question. what is the meaning of this if session?
            if guess_slice.ndim == 2:
                guess_slice = guess_slice[:, 0]
                # Question. why only keep the first row in guess_slice
            else:
                guess_slice = guess_slice[:]
                # enter else for the 1st frame in avi situation
            '''2-3.2 BASELINE SUBSTRACTION'''
            if fish < background:
                # fish is like the average grayscale/brightness of the fish image
                guess_slice = (background - guess_slice)
            else:
                guess_slice = (guess_slice - background)
                # Julie-Question. why do the substraction???

            slices += [guess_slice]  ######tag
            # Question. real meaning?
            # Note. Question. difficult. they create lot of 's' to collect variables, how are they going to be used?

            hist = np.histogram(guess_slice, 10)
            # numpy.histogram(a, bins=10, range=None, normed=False, weights=None, density=None)
            # range is naturally set

            #DT-CODE
            #print 'guess_slice, before: ', guess_slice
            #print 'hist: ', hist
            #print '(hist[1][hist[0].argmax()] <= guess_slice): ', hist[1][hist[0].argmax()] <= guess_slice
            #print 'guess_slice<hist[1][hist[0].argmax()+1]: ', guess_slice<hist[1][hist[0].argmax()+1]
            #print 'guess_slice[((hist[1][hist[0].argmax()] <= guess_slice)&(guess_slice<hist[1][hist[0].argmax()+1]))]: ', guess_slice[((hist[1][hist[0].argmax()] <= guess_slice)&(guess_slice<hist[1][hist[0].argmax()+1]))]

            #DT-CODE
            #plt.plot(guess_slice)
            #plt.ylabel('before')
            #plt.show()
            guess_slice = guess_slice - guess_slice[(
                (hist[1][hist[0].argmax()] <= guess_slice) &
                (guess_slice < hist[1][hist[0].argmax() + 1]))].mean()  #tag
            # QUESTION. WHAT IS THE MEANING OF THIS PROCESSING?
            # Note. CONFIRMED. Baseline substraction, build a histogram and substract the the mean value of smallest bin
            # Question. principles underlying this ?
            # A. baseline substraction. draw a histogram based on guess_slice and choose the interval with most items and get the mean of these items and substracted it
            # & means only keep the items that is both true in either formula
            # only the item corresponds to true could be selected out to remain
            # Question. <=, hist[1][index] is integer, can't be comparied with guess_slice, a  right????
            # A. return value is going to be a matrix containing boolean, same shape of guess_slice
            '''2-3.3 FILTER! SMOOTH THE GUESS_SLICE '''
            # Note. this seems to do a nice job of smoothing out while not moving edges too much
            sguess = scipy.ndimage.filters.percentile_filter(
                guess_slice, 50, 5)
            #QUESTION. does this has to be length of 51??1

            # WHAT DOES THIS sguess actually do?
            # QUESTION. WILL THIS NUMBER 50 OR 5 EFFECTS THE FITTING?
            # Task. DIFFICULT! JULIE-Question. I want to know the principle of this filter and exactly how this sguess is calculated. why you choose this one
            # Note. CONFIRMED. type of sguess is <type 'numpy.ndarray'>, with 51 items, this 'filter' is not constant, change every frame?
            # scipy.ndimage.filters.percentile_filter(input, percentile, size=None, footprint=None, output=None, mode='reflect', cval=0.0, origin=0)

            # DT-CODE
            # plt.plot(guess_slice)
            # plt.plot(sguess)
            # plt.ylabel('before')
            # plt.show()
            '''2-3.4, 1ST FRAME-1, DELINEATE ALL THE NEWPOINT'''
            if first_frame:
                # first time through, profile the tail

                # DT-CODE
                # print 'sguess>(sguess.max()*.25: ', sguess>(sguess.max()*.25)
                # print 'np.diff(sguess>(sguess.max()*.25)): ', np.diff(sguess>(sguess.max()*.25))
                # print 'np.where(np.diff(sguess>(sguess.max()*.25))): ', np.where(np.diff(sguess>(sguess.max()*.25)))
                '''2-3.4.1 DEFINE THE EDGE OF TAIL AND FIND THE MID-POINT'''
                tailedges_threshold = 0.45
                # the smaller the value, the tighter the tailfitting would be, less dots
                tailedges = np.where(
                    np.diff(sguess > (sguess.max() * tailedges_threshold)))[0]
                # [ADJUSTMENT] The original value is .25 ....
                # QUESTION. WHY SOMETIMES TAILEDGES COULD HAVE MULTIPLE COMPONENTS?
                # Note. Use 1/4 of max to define the edge of tail!
                # Note. returned tailedges is (array([17, 32])) for one frame, item correspond to the index in list
                # numpy.where(condition[, x, y]), Return elements, either from x or y, depending on condition.
                # numpy.diff(a, n=1, axis=-1), Calculate the n-th order discrete difference along given axis.
                # numpy.diff would return the value correspond to True, if the two items are Boolean and different

                if len(tailedges) >= 2:
                    # Question. Normally, we won't see this len(tailedges) bigger than 2, right?
                    # DT-CODE: print 'tailedges: ', tailedges
                    # DT-CODE: print 'tailedges-len(sguess)/2.0: ', tailedges-len(sguess)/2.0

                    tailedges = tailedges - len(sguess) / 2.0
                    # S-Question. why minus this one, len(sguess)/2.0?
                    # DT-CODE: print 'np.argsort(np.abs(tailedges)): ', np.argsort(np.abs(tailedges))
                    # DT-CODE: print 'tailedges[np.argsort(np.abs(tailedges))[0:2]]: ', tailedges[np.argsort(np.abs(tailedges))[0:2]]

                    tailindexes = tailedges[np.argsort(np.abs(tailedges))[0:2]]
                    # so this operation just sorted the tailedges, and only keep the biggest two items
                    # np.argsort, Returns the indices that would sort an array.
                    # DT-CODE: print '(tailindexes).mean()+len(sguess)/2.0: ', (tailindexes).mean()+len(sguess)/2.0

                    result_index_new = (tailindexes).mean() + len(sguess) / 2.0
                    # QUESTION. WHY ADD len(sguess)?
                    # Note. result_index_new is the mid point of tailedges
                    # Note. this complex calculation can be replaced with: result_index_new = tailedges.mean()

                    widths += [abs(tailindexes[0] - tailindexes[1])]
                    # Note. widths of the tail
                else:
                    result_index_new = None
                    tail_length = count
                    # Note. means the stop of guessing!
                    break
                '''2-3.4.2 CONVOLUTION & NEWPOINT'''
                results = np.convolve(normpdf, guess_slice, "valid")
                # Note. so normpdf here acts like a kernel to process the guess_slice?
                # Note. results looks like Gaussian after processing
                # 'valid', Mode 'valid' returns output of length max(M, N) - min(M, N)+1. The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect.

                convolveresults += [results]
                ##              test+=[guess_slice.mean()]

                result_index = results.argmax(
                ) - results.size / 2 + guess_slice.size / 2
                #Q. why is result_index calculated this way?
                #A. yeap, result_index is the peak, here they want find the orginal position of the peak in guess_slice
                #Q. the result_index caculated for the first frame, is it going to be used?
                #A. I think it is going to be accessed in else part, in other frame.

                newpoint = np.array([
                    x_indices[int(result_index_new)],
                    y_indices[int(result_index_new)]
                ])  #DT-modification: add int() here
                # QUESTION. WHEN DOES THE NEWPOINT ACTUALLY GOES ONE STEP MORE???
                #!!! NOTES... newpoint is actually the final output of this tailfitting
                #QUESTION. what is the function for result_index/convolveresults/result?

                #####################################################################################################################
                # DT CODE: print newpoint, ' is the newpoint of ', count, 'count'
                # Note. CONFIRMED: each time only one newpoint is produced.
                # Question. what is the meaning of newpoint?
                # Answer. IT iS the new point growing along the axis?

            else:  ############task. temp omit#################### ##########must come back and figure this out!#############
                results = np.convolve(tailfuncs[count], guess_slice, "valid")
                result_index = results.argmax(
                ) - results.size / 2 + guess_slice.size / 2
                newpoint = np.array(
                    [x_indices[result_index], y_indices[result_index]])
            '''2-3.5, 1ST FRAME-2, FUNCTION UNKNOWN'''
            if first_frame:
                '''2-3.5.1 CHECK FITTING SESSION, BREAK IF NECCESSARY'''
                # task. don't really understand the principles of this session but... let it be
                if count > 10:
                    #@ SCALE FIT GOODNESS WITH CONTRAST
                    trapz = [
                        pylab.trapz(result - result.mean())
                        for result in convolveresults
                    ]  #tag
                    # Integrate along the given axis using the composite trapezoidal rule. Integrate y(x) along given axis.
                    # numpy.trapz(y, x=None, dx=1.0, axis=-1)
                    # x: array_like, optional. The sample points corresponding to the y values. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.
                    # dx: scalar, optional. The spacing between sample points when x is None. The default is 1.
                    # Question. what is the real meaning of this...?
                    # Answer. trapz should be a list storing the 'area under curve' of convolveresults
                    # Question. why calculate sth like this?

                    slicesnp = np.vstack(slices)
                    # slices, a list storing guess_slice
                    # np.vstack, Stack arrays in sequence vertically (row wise). Just put the arrays together without changing the shape or any items.

                    if np.array(trapz[-3:]).mean() < .2:  #tag
                        # Question. what is the meaning of this array?
                        ##                        pdb.set_trace()
                        tail_length = count
                        break
                    elif slicesnp[-1, result_index - 2:result_index +
                                  2].mean() < 5:
                        # slicenp is a 2-d array, using -1 to access the last row, then using result_index-2:result+2 to access the corresponding part of array
                        ##                    elif -np.diff(sliding_average(slicesnp.mean(1)),4).min()<0:
                        ##                    elif np.diff(scipy.ndimage.filters.percentile_filter(trapz,50,4)).min()<-20:
                        ##                        print np.abs(np.diff(trapz))
                        ##                        pdb.set_trace()
                        tail_length = count
                        break
##            elif count > 1 and pylab.trapz(results-results.mean())<.3: #lower means higher contrast threshold
            elif count > tail_length * .8 and np.power(
                    newpoint - current, 2).sum()**.5 > tailpoint_spacing * 1.5:
                # Question. I mean what f**k does the above function means???
                # let's assume newpoint is the new growing point from the recognized tail?
                # semantically, current should be the current-point (start point when first time)
                # np.power, First array elements raised to powers from second array, element-wise.
                ##                print count, ' Point Distance Break', np.power(newpoint-current,2).sum()**.5
                break

            elif count == tail_length:
                # Question. difficult. when is tail_length defined?
                break  #should be end of the tail
#threshold changes with tail speed?
#also could try overfit, and seeing where the elbow is
            '''2-3.6 DRAW THE CIRCLES ALONG THE TAIL, UPDATE VECTORS AND THEN CURRENT'''
            if display:
                cv2.circle(frame_display, (int(newpoint[0]), int(newpoint[1])),
                           2, (0, 0, 0))  #tag
                # DT CODE: print 'newpoint: ', newpoint
                # Note. CONFIRMED: circle is drawed one by one, newpoint is simple list consists of two items
                # cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]), returns img
                # frame_display is defined by this: frame_display=frame.copy()
##                frame_display[y_indices,x_indices]=0

            frame_fit[count, :] = newpoint
            # frame_fit=np.zeros((max_points,2))
            # put the newpoint into the frame_fit array, a 2D array

            if count > 0:
                guess_vector = newpoint - current
            # Question. function of this if block?
            # A. guess_vector gives the direction of guess, current is old point

            current = newpoint  #####################################################################tag################################################
            # update the current with the newpoint

            #@ autoscale guess line width and then regen normpdf
##        trapz = [pylab.trapz(result-result.mean()) for result in convolveresults]
##        pylab.scatter(range(len(trapz)),trapz)
##        pylab.figure()
##        td = sliding_average(np.abs(np.diff(trapz)),5)
##
##        pylab.scatter(range(len(td)),td,c='r');pylab.show()
##        pylab.axhline()

##        pylab.plot(trapz)
####        pylab.plot(np.abs(np.diff(scipy.ndimage.filters.percentile_filter(trapz,50,4))))
##        pylab.plot(scipy.ndimage.filters.percentile_filter(trapz,50,4))
##        pylab.plot(test)
####        pylab.plot(slices
##        slices = np.vstack(slices)
##        pylab.show()
##
##        pylab.plot(sliding_average(slices.mean(1)));
##        pylab.plot(np.abs(np.diff(sliding_average(slices.mean(1),8))));
##        pylab.plot(-np.diff(sliding_average(slicesnp.mean(1)))[:45]);pylab.show()
##        pdb.set_trace()
        '''2-4. STRANGE SWIDTHS, FINALLY! JUMP OUT OF FOR-COUNT'''
        if first_frame:
            # first_frame just told the program if it is processing the first frame

            swidths = scipy.ndimage.filters.percentile_filter(
                widths, 50, 8)  #task...temporarily just keep it
            # Julie-Question. meaning of this? and width, pleasssssssssssssse
            # DT code

            swidths = np.lib.pad(swidths, [0, 5], mode='edge')  #tag bug
            # Note. Bug. IndexError: index -1 is out of bounds for axis 0 with size 0
            # np.lib.pad, choose the last item of swidths and add
            # Question. why pads the fish?
            # numpy.pad(array, pad_width, mode, **kwargs), Pads an array

            tailfuncs = [
                tail_func2(
                    np.arange((-guess_line_width + 1) / 4 + 1,
                              (guess_line_width - 1) / 4), 0, swidth, 1, 0)
                for swidth in swidths
            ]  #tag
            # Note. guess_line_width = 51
            # Note. def tail_func2(x, mu, sigma, scale, offset)
            # Question. so swidth is going to be sigma? why is that????
        '''2-5. APPEND FITTED_TAIL'''
        fitted_tail.append(np.copy(frame_fit[:count]))
        # DT-CODE: print 'fitted_tail looks like this: ', fitted_tail
        # NOTE. CONFIRMED. fitted_tail is the final result of the whole program.
        # NOTE. CONFIRMED. it is a list, storing arrays with the total number of total frame analyzed/read
        # NOTE. CONFIRMED. each correspond to one frame, storing x number of points (x=tail_lengh/count)
        # NOTE. CONFIRMED. points is the fitted result_point(mid point of tail edge), it is the coordinate in each frame
        # NOTE. CONFIRMED. count would be the final number of total circles.
        '''2-6. DISPLAY THE FRAME!'''
        if display:
            cv2.putText(frame_display, str(count), (340, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.0, (225, 10, 20))
            cv2.putText(frame_display, str(len(fitted_tail) - 1), (15, 25),
                        cv2.FONT_HERSHEY_SIMPLEX, 1.0, (25, 10, 20))
            #-1 because the current frame has already been appended
            cv2.imshow("frame_display", frame_display)
            # cv2.waitKey(0)  #DT-CODE: Manual control to analyze the frame one by one
            if first_frame:
                delaytime = 1
                # Question. unit ms?
            else:
                minlen = min(
                    [fitted_tail[-2].shape[0], fitted_tail[-1].shape[0]]) - 1
                delaytime = int(
                    min(
                        max((np.abs(
                            (fitted_tail[-2][minlen, :] -
                             fitted_tail[-1][minlen, :])**2).sum()**.5)**1.2 *
                            3 - 1, 1), 500))


##              # Question. why calculate delay time in a such trouble way?
            cv2.waitKey(delaytime)
        '''2-7. OUTPUT JPEG'''
        #task. temp omit
        if output_jpegs:
            if first_frame:
                jpegs_dir = pickdir()
                if not os.path.exists(jpegs_dir):
                    os.makedirs(jpegs_dir)
            jpg_out = Image.fromarray(frame_display)
            jpg_out.save(
                os.path.normpath(jpegs_dir + '\\' + str(len(fitted_tail) - 1) +
                                 '.jpg'))
        '''2-8. FALSE 1ST FRAME AND READ NEXT FRAME'''
        first_frame = False
        # cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,float(len(fitted_tail)) );  #workaround for raw videos crash, but massively (ie 5x) slower
        s, frame = cap.read()
        if s:  # Only process valid image frames
            frame = cv2.resize(
                frame, (0, 0),
                fx=scale,
                fy=scale,
                interpolation=cv2.INTER_CUBIC)  #resize-tag #resize the frame!

        # turn off the first_frame and update the frame with next frame in video
        # cv2.VideoCapture.read([image]) returns tuple (retval, image), so frame only takes the returned image
    print "Done in %.2f seconds" % (time.time() - starttime)
    '''3RD PART. WARNING SYSTEM'''
    'DT-NOTE FOLLOWING IS LIKE WARNING SYSTEM, TEMPORARILLY SKIP THEM'
    fit_lengths = np.array([len(i) for i in fitted_tail])  ########tag########
    if np.std(fit_lengths) > 3 or plotlengths:
        print 'Abnormal variances in tail length detected, check results: ', filename
        pylab.plot(range(0, len(fitted_tail)), fit_lengths)
        pylab.ylim((0, 5 + max(fit_lengths)))
        pylab.xlabel("Frame")
        pylab.ylabel('Tail points')
        pylab.title('Tail fit lengths')
        print 'Close graph to continue!'
        pylab.show()

    if any(fit_lengths < 25):
        print "Warning - short tail detected in some frames - min: ", min(
            fit_lengths)

    if len(fitted_tail) != int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)):
        print "Warning - number of frames processed doesn't match number of video frames - can happen with videos over 2gb!"
        print "Frames processed: ", len(fitted_tail)
        print "Actual frames according to video header: ", int(
            cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    '''4TH PART. DESTROYWINDOW AND RETURN!'''
    cv2.destroyAllWindows()
    return fitted_tail, start_point, direction, FPS, numframes, tailedges_threshold
Пример #16
0
    convolution = numpy.fft.ifft(
        pl.multiply(ftilda, gtilda)
    )  #Convolution using properties of fourier transforms and convolution
    return pl.divide(convolution, len(ftilda)) * T


a = tophat(t, 3, 5)
b = gaussian(t).tolist()
c = split_gaussian(t)

pl.figure("h(t)")
pl.plot(t, a, label="h(t)")
pl.plot(t, b, label="g(t)")
#pl.figure("convolution")
pl.plot(t, convolve(a, c), label="h(t)*g(t)")
pl.legend()

print(pl.trapz(convolve(a, c), t,
               dx=0.0001))  #finds area under convolution curve

pl.figure()
pl.plot(pl.fftfreq(N, d=dT),
        numpy.fft.fft(a),
        label=r"$\mathcal{F}(h(t))(\omega)$")
pl.legend()
pl.figure()
pl.plot(pl.fftfreq(N, d=dT),
        numpy.fft.fft(b),
        label=r"$\mathcal{F}(g(t))(\omega)$")
pl.legend()
Пример #17
0
*  This script is free software: you can redistribute it and/or modify      *
*  it under the terms of the GNU General Public License as published by     *
*  the Free Software Foundation, either version 3 of the License, or        *
*  (at your option) any later version.                                      *
*                                                                           *
*  This program is distributed in the hope that it will be useful,          *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
*  GNU General Public License for more details.                             *
*                                                                           *
*  You should have received a copy of the GNU General Public License        *
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
*                                                                           *
**************************************************************************"""
from pylab import trapz, plot, title, xlabel, ylabel, show
import sys
from out_proc_util import get_isi_distrib

if len(sys.argv) >= 3:
    filename = sys.argv[1]
    colnum   = int(sys.argv[2])
    xvalues, distribution = get_isi_distrib(filename, colnum)

    S = trapz(distribution, x=xvalues)
    print("Area under the curve: {}".format(S))

    plot(xvalues, distribution)
    title("Interspike interval density")
    xlabel("ISI length")
    ylabel("Probability density")
    show()
Пример #18
0
def calc_trace_align(flatim, biasim, fibers1, fiber_folder, params):

    imdat, imhead = pf.getdata(flatim), pf.getheader(flatim)
    pass2 = pf.getdata(flatim)*0
    weight_im = pf.getdata(flatim)*0
    pass3 = pf.getdata(flatim)*0
    fibers_y = open('fibertrace_ys.txt','w')

    print '\n\tBEGINNING TRACE-ALIGNMENT'
    regfile = open('trace.reg','w')
    for fib in fibers1:

        fib.tracey = (len(fib.xy[0][1])+1)*fib.id+len(fib.xy[0][1])/2
        fibers_y.write( str(fib.id)+'  '+str(fib.tracey)+'\n' )

        if 0<fib.id<1000: tracename = 'fibertrace_id'+spec.filenum(fib.id,3)
        else: raise ValueError('Fiber ID out of range, must be <1000')

        tracename += str(fib.id)+'.txt'
        tracetxt = open(tracename,'w')
        tracetxt.write('#  id  x_ind  [y_inds]  [rescale]\n')


# update 2014
        regx, regy = [],[]
        for xy in fib.xy:
            regx.append(xy[0])
            regy.append( (xy[1][0]+xy[1][-1])/2. )
        regx = pl.array(regx)*1.0
        regy = pl.array(regy)*1.0
        fit = opt.curve_fit( parabola, regx, regy, p0=[(2.0e-7), -(2.5e-3), 1000.0], sigma=pl.zeros(len(regx))+1 )

        for x in range(0, len(regx)-5, 5):
            regfile.write( 'line( '+str(x+1)+' , '+str(parabola(x,*fit[0])+1)+' , '+str(x+5+1)+' , '+str(parabola(x+5,*fit[0])+1)+' ) # line=0 0\n' )
# update 2014


        for xy in fib.xy:

            tracetxt.write('  '+str(fib.id)+'  '+str(xy[0]) )
            for y in xy[1]: tracetxt.write('  '+str(y))

            col = imdat[:,xy[0]]

            dy = (params.FIBER_WIDTH+1)/2
            inx = xy[1]
            iny = col[inx]

            fitx, fity, msh = gf.gaussfit(inx,iny)

            fib.mu.append(msh[0])
            fib.sig.append(msh[1])
            fib.h.append(msh[2])

            peaks_dif = inx[dy-1] - msh[0]
            fityvals = msh[2]*pl.exp( - ( ((inx-msh[0]-peaks_dif)/msh[1])**2 ) )
            rescale = fityvals/iny
            rescalenorm = rescale * pl.trapz(iny,inx) / pl.trapz(iny*rescale,inx)
            fib.rescale.append( rescalenorm )

            for y in range( len(iny) ):
                pass2[(len(iny)+1)*fib.id+y][xy[0]] = iny[y]*rescalenorm[y]
                weight_im[(len(iny)+1)*fib.id+y][xy[0]] = iny[y] / max(iny)
                pass3[(len(iny)+1)*fib.id+y][xy[0]] = fib.id
                tracetxt.write( '  '+str(rescalenorm[y]) )
            tracetxt.write('\n')

        tracetxt.close()
        shu.move(tracename,fiber_folder)
        if fib.id%(len(fibers1)/10)==0: print '\t  --> TRACE-ALIGNED ', fib.id, ' FIBERS OF ', fibers1[-1].id
    regfile.close()
    fibers_y.close()
    shu.move('fibertrace_ys.txt',fiber_folder)

    try: pf.writeto('trace_pass2.fits', pass2, header=imhead)
    except:
        os.remove('trace_pass2.fits')
        pf.writeto('trace_pass2.fits', pass2, header=imhead)

    try: pf.writeto('trace_weights.fits', weight_im, header=imhead)
    except:
        os.remove('trace_weights.fits')
        pf.writeto('trace_weights.fits', weight_im, header=imhead)

    try: pf.writeto('trace_pass3.fits', pass3, header=imhead)
    except:
        os.remove('trace_pass3.fits')
        pf.writeto('trace_pass3.fits', pass3, header=imhead)

    return fibers1
Пример #19
0
def area_under_the_curve(true_labels, predicted_labels, pos_class=1):
    fpr, tpr, thr = roc_curve(true_labels,
                              predicted_labels,
                              pos_class=pos_class)
    area = -pylab.trapz(tpr, x=fpr)
    return area