def analyseFile(filename): print(filename) seriesName = os.path.splitext(os.path.split(filename)[-1])[0] PL.ExtendContext({'seriesName': seriesName}) try: pipe = Pipeline(filename) except RuntimeError: print(('Error opening %s' % filename)) PL.PopContext() return #only look at first 7k frames #pipe.filterKeys['t'] = (0, 7000) pipe.Rebuild() autoDrift.correctDrift(pipe) imb = visHelpers.ImageBounds.estimateFromSource(pipe) jitVals = 1.0 * pipe['error_x'] im = visHelpers.rendJitTriang(pipe['x'], pipe['y'], 20, jitVals, 1, imb, 5) pipe.CloseFiles() PL.PopContext()
def colfcnwrap(pipeline): colourFilter = pipeline.colourFilter metadata = pipeline.mdh chans = colourFilter.getColourChans() if USE_GUI: figure(os.path.split(pipeline.filename)[-1] + ' - ' + fcn.__name__) if len(chans) == 0: fcn(colourFilter, metadata) else: curChan = colourFilter.currentColour if 'rng' in args: nPh = getPhotonNums(colourFilter, metadata) rng = 6 * nPh.mean() chanNames = chans[:] if 'Sample.Labelling' in metadata.getEntryNames(): lab = metadata.getEntry('Sample.Labelling') for i in range(len(lab)): if lab[i][0] in chanNames: chanNames[chanNames.index(lab[i][0])] = lab[i][1] for ch, i in zip(chans, range(len(chans))): colourFilter.setColour(ch) PL.ExtendContext({'chan': chanNames[i]}) if 'rng' in args: fcn(colourFilter, metadata, chanNames[i], i, rng) else: fcn(colourFilter, metadata, chanNames[i], i) PL.PopContext() colourFilter.setColour(curChan)
def fitOnTimes(colourFilter, metadata, channame='', i=0): numPerClump = colourFilter['clumpSize'] n, bins = np.histogram(numPerClump, np.arange(20) + .001) n = n / np.arange(1, 20) cycTime = metadata.getEntry('Camera.CycleTime') res = FitModelWeighted(eimod, [n[0], .2], n[1:], 1. / (np.sqrt(n[1:]) + 1), bins[2:] * cycTime) #res = FitModelPoisson(eimod, [n[0], .2], n[1:], bins[2:]*cycTime) ch2, mse = chi2_mse(eimod, n[1:], res, bins[2:] * cycTime) #mse = (res[2]['fvec']**2).mean() res2 = FitModelWeighted(ei2mod, [n[0], .2], n[1:], 1. / (np.sqrt(n[1:]) + 1), bins[2:] * cycTime, cycTime) ch22, mse2 = chi2_mse(ei2mod, n[1:], res2, bins[2:] * cycTime, cycTime) PL.AddRecord('/Photophysics/OnTimes/eimod', munge_res(eimod, res, mse=mse)) PL.AddRecord('/Photophysics/OnTimes/ei2mod', munge_res(ei2mod, res2, mse=mse2)) #print res[0] #figure() if USE_GUI: pylab.semilogy() pylab.bar(bins[:-1] * cycTime, n, width=cycTime, alpha=0.4, fc=colours[i]) pylab.plot(np.linspace(1, 20, 50) * cycTime, ei2mod(res2[0], np.linspace(1, 20, 50) * cycTime, cycTime), colours[i], lw=3, ls='--') pylab.plot(np.linspace(1, 20, 50) * cycTime, eimod(res[0], np.linspace(1, 20, 50) * cycTime), colours[i], lw=3) pylab.ylabel('Events') pylab.xlabel('Event Duration [s]') pylab.ylim((1, pylab.ylim()[1])) pylab.title( 'Event Duration - CAUTION: unreliable if $\\tau <\\sim$ exposure time' ) pylab.figtext(.6, .8 - .05 * i, channame + '\t$\\tau = %3.4fs$' % (res[0][1], ), size=18, color=colours[i]) return 0
def fitDecay(colourFilter, metadata, channame='', i=0): #get frames in which events occured and convert into seconds t = colourFilter['t'] * metadata.getEntry('Camera.CycleTime') n, bins = histogram(t, 100) b1 = bins[:-1] Nm = n.max() res = FitModel(e2mod, [Nm * 2, 15, -3, Nm * 3, n[1] / 1], n[1:], b1[1:], Nm) #mse = (res[2]['fvec']**2).mean() #ch2 = chi2(res, n[1:]) #print ch2 ch2, mse = chi2_mse(e2mod, n[1:], res, b1[1:], Nm) PL.AddRecord('/Photophysics/Decay/e2mod', munge_res(e2mod, res, mse=mse, ch2=ch2)) res2 = FitModelPoisson(emod, [Nm * 2, 15, -3, Nm * 3, n[1] / 2], n[1:], b1[1:], Nm) #[0] ch2, mse = chi2_mse(hmod, n[1:], res2, b1[1:], Nm) PL.AddRecord('/Photophysics/Decay/emod', munge_res(emod, res2, mse=mse, ch2=ch2)) res3 = FitModelPoisson(hmod, [Nm * 2, 15, -3, Nm * 3, n[1] / 2], n[1:], b1[1:], Nm) #[0] ch2, mse = chi2_mse(hmod, n[1:], res3, b1[1:], Nm) PL.AddRecord('/Photophysics/Decay/hmod', munge_res(hmod, res3, mse=mse, ch2=ch2)) r4 = FitModelPoisson(e2mod, [Nm * 2, 15, -3, Nm * 3, n[1] / 2], n[1:], b1[1:], Nm) ch2, mse = chi2_mse(e2mod, n[1:], r4, b1[1:], Nm) PL.AddRecord('/Photophysics/Decay/e2mod_p', munge_res(e2mod, r4, mse=mse, ch2=ch2)) if USE_GUI: bar(b1 / 60, n, width=(b1[1] - b1[0]) / 60, alpha=0.4, fc=colours[i]) plot(b1 / 60, e2mod(res[0], b1, Nm), colours[i], lw=3) plot(b1 / 60, emod(res2[0], b1, Nm), colours[i], lw=2, ls='--') plot(b1 / 60, hmod(res3[0], b1, Nm), colours[i], lw=2, ls=':') plot(b1 / 60, e2mod(r4[0], b1, Nm), colours[i], lw=1) ylabel('Events') xlabel('Acquisition Time [mins]') title('Event Rate') b = 0.5 * (1 + erf(res[0][2])) * Nm figtext(.4, .8 - .05 * i, channame + '\t$\\tau = %3.2fs,\\;b = %3.2f$' % (res[0][1], b / res[0][0]), size=18, color=colours[i])
def analyseFile(filename): print(filename) seriesName = os.path.splitext(os.path.split(filename)[-1])[0] PL.ExtendContext({'seriesName':seriesName}) try: pipe = Pipeline(filename) except RuntimeError: print(('Error opening %s' % filename)) PL.PopContext() return #only look at first 7k frames pipe.filterKeys['t'] = (0, 7000) pipe.Rebuild() trackUtils.findTracks(pipe, 'error_x', 2, 20) pipe.Rebuild() extraParams = {} extraParams['cycleTime'] = pipe.mdh['Camera.CycleTime'] nPhot = kinModels.getPhotonNums(pipe.colourFilter, pipe.mdh) extraParams['MedianPhotons'] = np.median(nPhot) extraParams['MeanPhotons'] = np.mean(nPhot) extraParams['NEvents'] = len(nPhot) extraParams['MeanBackground'] = pipe['fitResults_background'].mean() - pipe.mdh['Camera.ADOffset'] extraParams['MedianBackground'] = np.median(pipe['fitResults_background']) - pipe.mdh['Camera.ADOffset'] extraParams['MeanClumpSize'] = pipe['clumpSize'].mean() extraParams['MeanClumpPhotons'] = (pipe['clumpSize']*nPhot).mean() PL.AddRecord('/Photophysics/ExtraParams', dictToRecarray(extraParams)) kinModels.fitDecay(pipe) kinModels.fitFluorBrightness(pipe) #kinModels.fitFluorBrightnessT(pipe) #max_off_ts = [3,5,10,20,40] #max_off_ts = [20] #for ot in max_off_ts: #PL.ExtendContext({'otMax':ot}) #find molecules appearing across multiple frames kinModels.fitOnTimes(pipe) #PL.PopContext() pipe.CloseFiles() PL.PopContext()
def processDir(dataPath, outfile): #set the logging backend to the PyTables file TB = TablesBackend(outfile) PL.SetBackend(TB) #walk the path and analyse all .h5r for (path, dirs, files) in os.walk(dataPath): for f in files: if os.path.splitext(f)[1] in ['.h5r']: filename = os.path.join(path, f) analyseFile(filename)
def fitFluorBrightness(colourFilter, metadata, channame='', i=0, rng=None, quiet=False): #nPh = (colourFilter['A']*2*math.pi*(colourFilter['sig']/(1e3*metadata.getEntry('voxelsize.x')))**2) #nPh = nPh*metadata.getEntry('Camera.ElectronsPerCount')/metadata.getEntry('Camera.TrueEMGain') nPh = getPhotonNums(colourFilter, metadata) if rng is None: rng = nPh.mean() * 6 n, bins = np.histogram(nPh, np.linspace(0, rng, 100)) NEvents = len(colourFilter['t']) bins = bins[:-1] res = FitModel(fImod, [n.max(), bins[n.argmax()] / 2, 100, nPh.mean()], n, bins) mse = (res[2]['fvec']**2).mean() if not quiet: PL.AddRecord('/Photophysics/FluorBrightness/fImod', munge_res(fImod, res, mse=mse)) #figure() #semilogy() if USE_GUI: plt.bar(bins, n, width=bins[1] - bins[0], alpha=0.4, fc=colours[i]) plt.plot(bins, fImod(res[0], bins), colours[i], lw=3) plt.ylabel('Events') plt.xlabel('Intensity [photons]') #ylim((1, ylim()[1])) plt.title( 'Event Intensity - CAUTION - unreliable if evt. duration $>\\sim$ exposure time' ) #print res[0][2] plt.figtext( .4, .8 - .15 * i, channame + '\t$N_{det} = %3.0f\\;\\lambda = %3.0f$\n\t$Ph.mean = %3.0f$' % (res[0][1], res[0][3], nPh.mean()), size=18, color=colours[i], verticalalignment='top', horizontalalignment='left') return [channame, res[0][3], NEvents]
def fitFluorBrightnessT(colourFilter, metadata, channame='', i=0, rng=None): #nPh = (colourFilter['A']*2*math.pi*(colourFilter['sig']/(1e3*metadata.getEntry('voxelsize.x')))**2) #nPh = nPh*metadata.getEntry('Camera.ElectronsPerCount')/metadata.getEntry('Camera.TrueEMGain') #from mpl_toolkits.mplot3d import Axes3D nPh = getPhotonNums(colourFilter, metadata) t = (colourFilter['t'].astype('f') - metadata['Protocol.DataStartsAt'] ) * metadata.getEntry('Camera.CycleTime') NEvents = len(t) if rng is None: rng = nPh.mean() * 3 Nco = nPh.min() n, xbins, ybins = np.histogram2d( nPh, t, [np.linspace(0, rng, 50), np.linspace(0, t.max(), 20)]) bins = xbins[:-1] xb = xbins[:-1][:, None] * np.ones([1, ybins.size - 1]) yb = ybins[:-1][None, :] * np.ones([xbins.size - 1, 1]) res0 = FitModel( fITmod2, [n.max() * 3, 1, np.median(nPh), 20, 1e2, 1e2, 100], n, xb, yb, Nco) print((res0[0])) PL.AddRecord('/Photophysics/FluorBrightness/fITmod2', munge_res(fITmod2, res0)) A, Ndet, lamb, tauI, a, Acrit, bg = res0[0] #Ndet = Ndet**2 #NDetM = NDetM**2 #Acrit = Acrit**2 #a = (1+erf(a))/2 Ndet = np.sqrt(Ndet**2 + 1) - 1 #+ve bg = np.sqrt(bg**2 + 1) - 1 Acrit = np.sqrt(Acrit**2 + 1) - 1 a = (1 + erf(a)) / 2 # [0,1] #bg = sqrt(bg**2 + 1) - 1 #k = sqrt(k**2 + 1) - 1 NDetM = bg rr = fITmod2(res0[0], xb, yb, Nco) if USE_GUI: pylab.figure() pylab.subplot(131) pylab.imshow(n, interpolation='nearest') pylab.colorbar() pylab.subplot(132) pylab.imshow(rr, interpolation='nearest') pylab.colorbar() pylab.subplot(133) pylab.imshow(n - rr, interpolation='nearest') pylab.colorbar() pylab.title(channame) pylab.figure() t_ = np.linspace(t[0], t[-1], 100) #sc = (lamb/(ybins[1] - ybins[0])) #sc = len(ybins) sc = 1. / (1 - np.exp(-(ybins[1] - ybins[0]) / lamb)) print(('sc = ', sc)) y1 = sc * A / ((t_ / tauI)**a + 1) pylab.plot(t_, y1) pylab.plot(t_, sc * (Ndet / ((t_ / tauI)**a + 1) + NDetM)) pylab.bar(ybins[:-1], n.sum(0), width=ybins[1] - ybins[0], alpha=0.5) pylab.plot(ybins[:-1], rr.sum(0), lw=2) pylab.title(channame) pylab.xlabel('Time [s]') pylab.figtext( .2, .7, '$A = %3.0f\\;N_{det} = %3.2f\\;\\lambda = %3.0f\\;\\tau = %3.0f$\n$\\alpha = %3.3f\\;A_{crit} = %3.2f\\;N_{det_0} = %3.2f$' % (A, Ndet, lamb, tauI, a, Acrit, NDetM), size=18) return [channame, lamb, NEvents]