Пример #1
0
def plotStats(numBars,numTrials,trialStats):
    f = pltUtil.pFigure()
    tickLabelX = np.arange(0,numBars)
    colorCycler = pltUtil.cycleColors()
    ax = plt.subplot(1,1,1)
    axRSQ = pltUtil.secondAxis(ax,'RSQ',[0,1.0])
    wid = 1/(numTrials*numBars)
    plt.title('Comparison of Surface Modeling Parameters')
    barDict = dict(elinewidth=4, ecolor='m')
    for t in range(numTrials):
        # XXX fix this to make it more general...
        RSQ = trialStats[t,-1]
        numMeanStd = numParams-1
        middle = int(numMeanStd/2)
        meanVals = trialStats[t,0:middle]
        stdVals = trialStats[t,middle:numMeanStd] 
        numVals = len(meanVals)
        xVals = tickLabelX+wid*t
        mColor = next(colorCycler)
        mLabel = trials[t]
        ax.bar(xVals[0:numVals],meanVals,yerr=0.2*stdVals,
               color=mColor,error_kw = barDict,align='center',label=mLabel)
        axRSQ.bar(xVals[numVals],RSQ,width=wid,color=mColor,align='center',
                  label=mLabel)
        # only the second time, add a third axis
    ax.legend(loc='upper left')
    ax.set_ylabel('Tau value (seconds)')
    plt.xlim([-0.5,max(xVals)*1.1])
    plt.xticks(tickLabelX, statsLabel)
    pltUtil.saveFigure(f,"Comparison")
    return  (-b*t/(t+a))

CRTD = [1.0 , 0.48386793340231393, 0.23356222368544821, 0.14250776032358203, 0.093876399209858019, 0.066973944125670259, 0.05041858715078551, 0.038848650174019395, 0.03235819772363846, 0.028689681121249255, 0.0242686482927289, 0.021917035086069125, 0.018718841125011876, 0.016649421503151296, 0.015050324522622671, 0.012980904900762091, 0.012040259618098181, 0.011381807920233467, 0.010535227165835992, 0.010158969052770472, 0.0092183237701065623, 0.0079014203743771327, 0.0075251622613116131, 0.0071489041482460935, 0.0066785815069141385, 0.0061141943373157481, 0.0056438716959837931, 0.0053616781111845979, 0.0051735490546518381, 0.0050794845263854027, 0.0049854199981189673, 0.0047972909415862075, 0.0044210328285206879, 0.0040447747154551683, 0.0039507101871887329, 0.0038566456589222975, 0.0035744520741231023, 0.0033863230175903425, 0.0032922584893239071, 0.0031981939610574717, 0.0031041294327910363, 0.0030100649045246008, 0.0029160003762581654, 0.00282193584799173, 0.0027278713197252946, 0.0026338067914588592, 0.0025397422631924238, 0.0024456777349259884, 0.0022575486783932286, 0.0021634841501267932, 0.0020694196218603578, 0.001881290565327598, 0.0016931615087948382, 0.0015990969805284028, 0.0015050324522619674, 0.0014109679239955319, 0.0013169033957290965, 0.0011287743391963367, 0.00094064528266357694, 0.00084658075439714153, 0.00075251622613070612, 0.00065845169786427071, 0.0005643871695978353, 0.00047032264133139989, 0.00037625811306496448, 0.00028219358479852907, 0.00018812905653209366]
times  = [0.0, 0.10000000000000001, 0.20000000000000001, 0.30000000000000004, 0.40000000000000002, 0.5, 0.60000000000000009, 0.70000000000000007, 0.80000000000000004, 0.90000000000000002, 1.0, 1.1000000000000001, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 1.6000000000000001, 1.7000000000000002, 1.8, 1.9000000000000001, 2.0, 2.1000000000000001, 2.2000000000000002, 2.3000000000000003, 2.4000000000000004, 2.5, 2.6000000000000001, 2.7000000000000002, 2.8000000000000003, 2.9000000000000004, 3.0, 3.1000000000000001, 3.2000000000000002, 3.4000000000000004, 3.5, 3.6000000000000001, 4.0, 4.1000000000000005, 4.2000000000000002, 4.2999999999999998, 4.4000000000000004, 4.5, 4.7000000000000002, 4.8000000000000007, 4.9000000000000004, 5.0, 5.3000000000000007, 5.4000000000000004, 5.5, 5.7000000000000002, 6.1000000000000005, 6.2000000000000002, 6.7000000000000002, 6.9000000000000004, 7.8000000000000007, 9.0, 9.4000000000000004, 9.6000000000000014, 9.9000000000000004, 11.0, 11.4, 11.700000000000001, 12.0, 12.100000000000001, 12.700000000000001, 16.400000000000002, 21.700000000000003]

times = np.array(times)
CRTD = np.array(CRTD)
logP = np.log(CRTD)
# fit to the normal CRTD
popt, pcov = curve_fit(hyperfit,times,CRTD)
paramsStd = np.sqrt(np.diag(pcov))
# get the predicted values
predict = hyperfit(times,*popt)
# get the string for the model label
modelStr,modelParams = fitStr(popt,paramsStd,predict,CRTD,True)
fig = pltUtil.pFigure()
numPlots = 3
plotCount = 1
xLabelStr = 'Time to unfold, t (seconds)'
# plot it all
timeConstant = popt[0]
timeLabel = modelParams[0]
ax = plt.subplot(numPlots,1,plotCount)
ax.axvline(timeConstant,color='k',label=timeLabel)
ax.plot(times,CRTD,'ro',label="Data")
ax.plot(times,predict,'b-',label=modelStr)
ax.set_ylim([min(CRTD),max(CRTD)])
ax.set_yscale('log')
plt.grid(b=True, which='major', color='b', linestyle='-')
plt.grid(b=True, which='minor', color='r', linestyle='--')
plt.legend(loc='best')
def GetPhysicsMain(goodTimes,goodFRET,goodDiff):
    source = 'Step2::GetPhysics'
    util.ReportMessage("Starting",source)
    colors = ['r','g','b','k','m','c','y','0.33','0.66']
    colorCycle = cycle(colors)
    count = 0
    fig = plotUtil.pFigure()
    # XXXfill all these in! based on video size
    frameRate = 0.1
    maxNumTimes = 30*10
    distances,times,definedDistancesIdx,nodeIdx =getDistances(goodFRET,goodTimes)
    # get just the 'nodes' with valid valued.
    # flatten the distances to get a notion of the 'all time'
    # distance information. We can use this and kmeans to find a 'folded
    # and unfolded sttae
    flattenDistances = np.concatenate(distances)
    # use two clusters; folded and unfolded
    numClusters = 2
    # lots of iterations (this number seems to work well; the 'smooth'
    # running time / convergence (?) of kmeans is polynomial
    # http://en.wikipedia.org/wiki/K-means_clustering
    numIters = int(1e3)
    clusters,ignore = cluster.kmeans(flattenDistances,numClusters,iter=numIters)
    # the clusters give us the 'folded' and 'unfolded' groups. between those, we have
    # a fairly undefined state.
    folded = min(clusters)
    unfolded = max(clusters)
    clusters = [unfolded,folded]
    
    folded = getMinimumTime(distances,times,folded,False)
    unfolded = getMinimumTime(distances,times,unfolded,True)
    diffTime, definedUnfoldingIdx = getDifferentialTime(folded,unfolded)

    goodDiff = util.takeSubset(goodDiff,
                                [nodeIdx,definedUnfoldingIdx])

    plt.xscale('log', nonposy='clip')
    plt.xlabel('Time since protein (seconds)')
    plt.ylabel('FRET d distance (arb)')
    fig = plotUtil.pFigure()
    numPlots = 2
    plotCount = 1
    fretLabel = 'FRET d Distance (arb)'
    ax = plt.subplot(numPlots,1,plotCount)
    plotUtil.histogramPlot(ax,fretLabel,'# Proteins',
                           'FRET Distance histogram',flattenDistances,
                           len(flattenDistances)/100,True,True)
    # plot guiding lines for the two clusters we found
    normalClusters = plotUtil.normalizeTo(flattenDistances,clusters)
    plt.axvline(normalClusters[0])
    plt.axvline(normalClusters[1])
    
    plotCount += 1
    ax = plt.subplot(numPlots,1,plotCount)
    plotUtil.histogramPlot(ax,'Unfolding time distribution','# Proteins',
                           'Unfolding time (seconds) ',diffTime,
                           len(diffTime)/100,True,True)


    plotUtil.saveFigure(fig,'tmp2')
    # return the good unfolding times and differential coefficients
    return diffTime,goodDiff
def AnalyzeTraces(velX,velY,times,numTimes,fretRatio,MSD,frameRate):
    util.ReportMessage("AnalyzeTraces")
    proteinYStr = '# Proteins'
    numProteins = len(numTimes)
    numBins = max(numTimes)
    fig = plotUtil.pFigure()
    titleStr = "Raw distribution of protein appearances"
    ax = fig.add_subplot(1,1,1)
    plotUtil.histogramPlot(ax,'Frame duration of protein',proteinYStr,
                           titleStr,numTimes,numBins)
    plotUtil.saveFigure(fig,"Protein_Distribution")

    fig = plotUtil.pFigure()
    # save the MSD and R^2 of the MSD
    numStats = 2
    msdMatrix = np.zeros((numProteins,numStats))
    plotCount = 1
    numPlots = 2
    msdAx = plt.subplot(numPlots,1,plotCount)
    allMSDs = np.concatenate(MSD)
    allTimes= np.concatenate(times)
    for i in range(numProteins):
        # get the X and Y values to fit...
        tmpMSD = MSD[i]
        # multiple the times by four, per the diffusion formulae
        tmpTimes = times[i]*2
        tmpTimes -= tmpTimes[0]
        if (len(tmpTimes) < 3):
            # nothing valid here. set everything to 0 and flag 0 RSQ later
            # we need at least three values to be able to get a Diffusion Coefficient
            # and an uncertainty.
            msdMatrix[i,:] = 0
            continue
        # linear fit
        deg = 1
        polyVals = np.polyfit(tmpTimes,tmpMSD,deg)
        polyFunc = np.poly1d(polyVals)
        fitVals = polyFunc(tmpTimes)
        slope, intercept, r_value, p_value, std_err = \
                        stats.linregress(fitVals,tmpMSD)
        rSquared = r_value**2
        # MSD (slope) is given by the slope, first coeff returned
        diffCoeff = polyVals[0]
        if (diffCoeff < 0):
            # ignore diffusion coefficients less than 0
            continue
        msdMatrix[i,0] = diffCoeff
        msdMatrix[i,1] = rSquared
    rawRsqVals = msdMatrix[:,1]
    goodIndices = np.where(rawRsqVals > 0)[0]
    goodMsds = np.take(msdMatrix,goodIndices,axis=0)
    msdVals =  goodMsds[:,0]
    # plot the histogram of MSDs
    ax = fig.add_subplot(numPlots,1,plotCount)
    numBins = max(msdVals)
    numProteins = len(msdVals)
    axTmp = plotUtil.histogramPlot(ax,'Diffusion Coeff (pixels^2/second)',
                                   proteinYStr,
                                   'Histogram of Protein Hiffusion Coeffs',
                                   msdVals,numBins)
    plotCount+=1
    # plot the rSquared values
    RSqVals = goodMsds[:,1]*100
    # RSQ is between 0 and 1, multiply by 100 and fit with 100 bins for 'simple' normalization
    numBinsRsq= 100 
    ax = fig.add_subplot(numPlots,1,plotCount)
    # use 100 bins for each of the RSq values
    plotUtil.histogramPlot(ax,'R Squared Coeff',proteinYStr,
                            'Histogram of Protein RSq',RSqVals,numBinsRsq)
    plotCount += 1
    # next, we plot a comparison of the 'raw', 'valid' (D with uncertainty),
    # and 'processed'
    # (D fit with RSQ > cutoffRsq)
    cutoffRsq = 0.8
    plt.tight_layout()
    plotUtil.saveFigure(fig,"MSD")
    
    titleStr = proteinYStr
    xStr = 'Frames Appearing'
    xLimit = [1,max(numTimes)]
    bestIndices = np.where(rawRsqVals > cutoffRsq)[0]
    # make labels for each of the indices ('Raw' is assumed...)
    compLabel = ["All Proteins","Valid Diffusion Coeffs",
                        ("RSQ > {:.3f}".format(cutoffRsq))]
    comparisonIndices = [goodIndices,bestIndices]
    plotUtil.comparisonPlot(xStr,proteinYStr,titleStr,xLimit,numTimes,
                            comparisonIndices,compLabel)
    # XXX TODO: compare other options (e.g. x velocity, y velcocity, etc)
    # XXX TODO: try for all files, need a better way to store
    # return all the diffusion coeffs, as well as the 'best' indices we found
    return msdMatrix[:,0],bestIndices