#!/usr/bin/env python # coding: utf-8 # In[1]: fname = 'data/pax-2017-05-23_08_12d.hdf5' # In[2]: from pathlib import Path fname = Path(fname) assert fname.is_file(), 'File not found.' mlabel = '_'.join(fname.stem.replace('pax-', '').replace('alex-', '').split('_')[:4]) mlabel # In[3]: get_ipython().system('date') # # Imports # In[4]: import os from pathlib import Path import numpy as np from IPython.display import display, HTML, Math import pandas as pd import matplotlib as mpl mpl.rcParams['font.sans-serif'].insert(0, 'Arial') get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt from heatmap import heatmap48, spotsh, spotsv import pybroom as br # In[5]: from fretbursts import * # In[6]: sns = init_notebook(apionly=True) # In[7]: plt.rcParams['font.size'] = 14 # In[8]: get_ipython().run_line_magic('config', "InlineBackend.figure_format = 'retina'") # In[9]: # Instead of importing, "load" some utility functions using `%run -i`. # This gives these functions access to variables defined in the notebok (e.g. mlabel). # Defines: savefig, save_name, info_html, cal_phrate, make_df_spots, make_df_bursts # cal_phrate_alex, make_df_bursts_alex get_ipython().run_line_magic('run', '-i utils.py') # In[10]: plot_timetraces = True skip_ch = (12, 13) # In[11]: save_figures = True savefigdir = 'figures' highres = True # # Load Data # In[12]: d = loader.photon_hdf5(str(fname), ondisk=True) # In[13]: info_html(d) # In[14]: fig, ax = plt.subplots(figsize=(12, 8)) bpl.plot_alternation_hist_usalex(d, ax=ax, bins=np.arange(0, 4097, 16)) # In[15]: get_ipython().run_cell_magic('timeit', '-n1 -r1', 'loader.alex_apply_period(d)\n') # # Analysis # ## Timetraces # In[16]: if plot_timetraces: num_time_points = 5 kws = dict(figsize=(24, 8), xrotation=45) # Timepoints equally distributed along the measurement time_points = np.round(np.linspace(d.time_min+1, d.time_max-2, num=num_time_points)) for i in time_points: dplot(d, timetrace, tmin=i, tmax=i+1, **kws); plt.ylim(-100, 100) savefig("%s_timetrace_t=%d-%d.png" % (mlabel, i, i+1)) # ## Background # In[17]: d.calc_bg_cache(bg.exp_fit, time_s=5, tail_min_us='auto', F_bg=1.7) # In[18]: ax = dplot(d, timetrace_bg, show_da=True, hspace=0, wspace=0.08, plot_style=dict(marker=None), title='right top', title_kws=dict(fontsize=16), xrotation=45); plt.xlim(0) plt.ylim(0, 8); # In[19]: bg_AexDem = d.bg_from(Ph_sel(Aex='Dem')) bg_AexAem = d.bg_from(Ph_sel(Aex='Aem')) bg_DexDem = d.bg_from(Ph_sel(Dex='Dem')) bg_DexAem = d.bg_from(Ph_sel(Dex='Aem')) # In[20]: fig, ax = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True) fig.suptitle("BG A-excitation period") t = np.arange(len(bg_AexDem[0])) * 5 ax[0].plot(t, np.array(bg_AexDem).T, color='g', alpha=0.2); ax[1].plot(t, np.array(bg_AexAem).T, color='r', alpha=0.2); plt.setp(ax, xlabel='Time (s)', ylabel='cps') plt.subplots_adjust(wspace=0.07) ax[0].text(0.05, 0.95, '$A_{EX}D_{EM}$', va='top', fontsize=16, transform=ax[0].transAxes) ax[1].text(0.95, 0.95, '$A_{EX}A_{EM}$', va='top', ha='right', fontsize=16, transform=ax[1].transAxes); # In[21]: fig, ax = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True) fig.suptitle("BG D-excitation period") t = np.arange(len(bg_DexDem[0])) * 5 ax[0].plot(t, np.array(bg_DexDem).T, color='g', alpha=0.2); ax[1].plot(t, np.array(bg_DexAem).T, color='r', alpha=0.2); plt.setp(ax, xlabel='Time (s)', ylabel='cps') plt.subplots_adjust(wspace=0.07) ax[0].text(0.05, 0.95, '$D_{EX}D_{EM}$', va='top', fontsize=16, transform=ax[0].transAxes) ax[1].text(0.95, 0.95, '$D_{EX}A_{EM}$', va='top', ha='right', fontsize=16, transform=ax[1].transAxes); # ## Burst search # In[22]: d.burst_search(min_rate_cps=50e3, pax=True) # In[23]: kws = dict(skip_ch=skip_ch, hspace=0, wspace=0, top=0.96, title_bg=False, title_nbursts=False, title='in') # In[24]: dplot(d, hist_size_all, **kws); plt.xlim(0, 120) plt.legend(); # In[25]: kws.update(title_nbursts=False) ax = dplot(d, hist_size, which='na', **kws); dplot(d, hist_size, which='naa', AX=ax, **kws); plt.xlim(0, 100); plt.legend(['$n_a$', '$n_{DA_{ex}A_{em}}$'], fontsize=18, loc='upper right') # In[26]: size_th = 60 size_sel_kws = dict(ph_sel=Ph_sel(Dex='DAem', Aex='DAem'), na_comp=False, naa_comp=False, naa_aexonly=False) dplot(d, hist_size, vline=size_th, **size_sel_kws, **kws); plt.xlim(-10, 300) plt.legend(fontsize=15, loc='upper right'); Math(d._burst_sizes_pax_formula(**size_sel_kws)) # In[27]: bs_mean = [] for i in range(48): sizes_ch = d.burst_sizes_pax_ich(i, **size_sel_kws) tail = sizes_ch > size_th bs_mean.append(sizes_ch[tail].mean() - size_th) # In[28]: heatmap48(bs_mean, cmap='viridis', skip_ch=skip_ch, title="Mean Burst Sizes", vmin=10) savefig('heatmap_burst_sizes') # In[29]: width_th = 0.4 dplot(d, hist_width, vline=width_th, **kws); plt.xlim(-0.5, 4) # In[30]: bw_mean = [] for i in range(48): widths_ch = d.mburst[i].width * d.clk_p * 1e3 tail = widths_ch > width_th bw_mean.append(widths_ch[tail].mean() - width_th) # In[31]: heatmap48(bw_mean, cmap='viridis', skip_ch=skip_ch, title="Mean Burst Width (ms)", vmin=0.1) savefig('heatmap_burst_widths') # In[32]: # Use recompute=True if changing burst-search parameters recompute = False phrates = {} streams = ('all', 'DexDem', 'AexDem', 'DexAem', 'AexAem', ) for stream in streams: print(' - Computing peak photon rates for %6s stream.' % str(Ph_sel.from_str(stream))) cal_phrate(d, stream=Ph_sel.from_str(stream), phrates=phrates, recompute=recompute) # In[33]: kws = dict(figsize=(18, 6), skip_ch=skip_ch, wspace=0, hspace=0, grid=True, title=None, title_nbursts=False, title_bg=False, xrotation=45) ax = dplot(phrates['AexAemB'], hist_burst_phrate, plot_style=dict(color=bpl.purple, ms=3), **kws); dplot(phrates['AexDemB'], hist_burst_phrate, plot_style=dict(color='C0', ms=3), AX=ax, **kws); dplot(phrates['DexDemB'], hist_burst_phrate, plot_style=dict(color=bpl.green, ms=3), AX=ax, **kws); kws.update(vline=60, title='in', title_nbursts=False, title_bg=False,) dplot(phrates['DexAemB'], hist_burst_phrate, plot_style=dict(color=bpl.red, ms=3), AX=ax, **kws); plt.xlim(-50, 1150); plt.setp(ax[:, 0], ylabel='PDF') plt.setp(ax[-1], xlabel='kcps') savefig('peak_phrate_all') # In[34]: phr_th = 60e3 for stream in streams: phrates[stream]['mean'] = 0 phr = phrates[stream+'B'] for ich in range(48): valid = ~pd.isnull(phr[ich]) phr_valid = phr[ich][valid] m = phr_valid[phr_valid >= phr_th].mean() - phr_th phrates[stream].loc[ich, 'mean'] = np.round(m*1e-3, 1) # In[35]: streams # In[36]: cmaps = ('bone', 'Greens_r', 'Blues_r', 'Reds_r', 'Purples_r') label = {'DexDem': '$D_{ex}D_{em}$', 'DexAem': '$D_{ex}A_{em}$', 'AexDem': '$DA_{ex}D_{em}$', 'AexAem': '$DA_{ex}A_{em}$', 'all': 'all-photons'} with plt.rc_context({'font.size': 15}): for stream, cmap in zip(streams, cmaps): heatmap48(phrates[stream]['mean'], cmap=cmap, skip_ch=skip_ch, title=r"Peak Photon Rate in %s (kcps)" % label[stream]) savefig('peak_phrate_heatmap_%s' % stream) # ## Burst selection # In[37]: size_th = 80 size_sel_kws = dict(ph_sel=Ph_sel(Dex='DAem', Aex='Dem'), gamma=0.5, na_comp=True, naa_comp=False, naa_aexonly=False) ds = d.select_bursts(select_bursts.size, th1=size_th, **size_sel_kws) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds.nd, ds.na, ds.naa)] ds.add(Su=Su) Su2 = [(nd + nda + na)/(nd + nda + na + naa) for nd, na, nda, naa in zip(ds.nd, ds.na, ds.nda, ds.naa)] ds.add(Su2=Su2) Math(d._burst_sizes_pax_formula(**size_sel_kws)) # In[38]: heatmap48(ds.num_bursts) # In[39]: ax = dplot(ds, hist_fret, pdf=False, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts'); # plt.ylim(0, 600); savefig('48spot hist E all-bursts') # In[40]: dplot(ds, hist_burst_data, data_name='Su', pdf=False, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts'); savefig('48spot hist Su') # In[41]: ax = dplot(ds, hexbin_alex, vmax=10, skip_ch=skip_ch, wspace=0, hspace=0, gridsize=60, title='bottom left', title_bg=False); ax0 = ax[0,0] fig = ax0.figure cax = fig.add_axes([0.97, 0.25, 0.01, 0.5]) plt.colorbar(cax=cax) plt.setp(ax[:, 0], ylabel='S'); ax0.set_xticks([0, 0.5, 1]) ax0.set_yticks([0, 0.5, 1]); ax0.set_xlim(-0.2, 1) ax0.set_ylim(0, 1.2); savefig('48spot alex hist S all-bursts') # In[42]: ax = dplot(ds, hexbin_alex, S_name='Su', vmax=15, skip_ch=skip_ch, xrotation=0, wspace=0, hspace=0, gridsize=60, title='bottom left', title_bg=False); ax0 = ax[0, 0] fig = ax0.figure cax = fig.add_axes([0.97, 0.25, 0.01, 0.5]) plt.setp(ax[:, 0], ylabel='$S_u$'); plt.setp(ax[-1], xlabel='$E_{PR}$'); plt.colorbar(cax=cax) ax0.set_xticks([0, 0.5, 1]) ax0.set_xticklabels(['0', '0.5', '1']) ax0.set_yticks([0, 0.5, 1]); ax0.set_xlim(-0.2, 1) ax0.set_ylim(0, 1.2); plt.suptitle('') savefig('48spot alex hist Su all-bursts') # In[43]: ax = dplot(ds, hexbin_alex, S_name='Su2', vmax=15, skip_ch=skip_ch, wspace=0, hspace=0, gridsize=60, title='bottom left', title_bg=False); ax0 = ax[0, 0] fig = ax0.figure cax = fig.add_axes([0.97, 0.25, 0.01, 0.5]) plt.setp(ax[:, 0], ylabel='$S_{up}$'); plt.setp(ax[-1], xlabel='$E_{PR}$'); plt.colorbar(cax=cax) ax0.set_xticks([0, 0.5, 1]) ax0.set_yticks([0, 0.5, 1]); ax0.set_xlim(-0.2, 1) ax0.set_ylim(0, 1.2); # In[44]: size_Dex = 80 size_sel_kws = dict(ph_sel=Ph_sel(Dex='DAem', Aex='Dem'), gamma=0.5, na_comp=True, naa_comp=False, naa_aexonly=False) Math(d._burst_sizes_pax_formula(**size_sel_kws)) # In[45]: size_Aex = 40 naa_sel_kws = dict(ph_sel=Ph_sel(Aex='Aem'), gamma=0.5, na_comp=False, naa_comp=False, naa_aexonly=False) Math(d._burst_sizes_pax_formula(**naa_sel_kws)) # In[46]: ds1 = d.select_bursts(select_bursts.size, th1=size_Dex, **size_sel_kws) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds1.nd, ds1.na, ds1.naa)] ds1.add(Su=Su) dss = ds1.select_bursts(select_bursts.size, th1=size_Aex, **naa_sel_kws) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dss.nd, dss.na, dss.naa)] dss.add(Su=Su) # In[47]: g = alex_jointplot(ds1, i=30, vmax=35, S_name='Su') g.ax_joint.set_ylabel('$S_u$') g = alex_jointplot(dss, i=30, vmax=35, S_name='Su') g.ax_joint.set_ylabel('$S_u$') # In[48]: ax = dplot(dss, hexbin_alex, S_name='Su', vmax=15, skip_ch=skip_ch, xrotation=0, wspace=0, hspace=0, gridsize=60, title='bottom left', title_bg=False); ax0 = ax[0, 0] fig = ax0.figure cax = fig.add_axes([0.97, 0.25, 0.01, 0.5]) plt.setp(ax[:, 0], ylabel='$S_u$'); plt.setp(ax[-1], xlabel='$E_{PR}$'); plt.colorbar(cax=cax) ax0.set_xticks([0, 0.5, 1]) ax0.set_xticklabels(['0', '0.5', '1']) ax0.set_yticks([0, 0.5, 1]); ax0.set_xlim(-0.2, 1) ax0.set_ylim(0, 1.2); plt.suptitle('') savefig('48spot alex hist Su naa AND size selection') # ## Fitting # ### FRET population fit # In[49]: ds_fret = dss # In[50]: E_fitter = bext.bursts_fitter(ds_fret, 'E', skip_ch=skip_ch) model = mfit.factory_gaussian() model.set_param_hint('center', value=0.5, min=0, max=1) model.set_param_hint('sigma', value=0.1, min=0.03, max=0.2) model.print_param_hints() # In[51]: E_fitter.fit_histogram(model, pdf=False, method='mealder') # In[52]: ax = dplot(ds_fret, hist_fret, pdf=False, show_model=True, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts') savefig('FRET pop - E hist fit') # In[53]: S_fitter = bext.bursts_fitter(ds_fret, 'S', skip_ch=skip_ch) model = mfit.factory_gaussian() model.set_param_hint('center', value=0.5, min=0, max=1) model.set_param_hint('sigma', value=0.1, min=0.03, max=0.2) model.print_param_hints() # In[54]: S_fitter.fit_histogram(model, pdf=False, method='mealder') # In[55]: ax = dplot(ds_fret, hist_S, pdf=False, show_model=True, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts') savefig('FRET pop - E hist fit'); # In[56]: Su_fitter = bext.bursts_fitter(ds_fret, 'Su', skip_ch=skip_ch) model = mfit.factory_gaussian() model.set_param_hint('center', value=0.5, min=0, max=1) model.set_param_hint('sigma', value=0.1, min=0.03, max=0.2) model.print_param_hints() # In[57]: Su_fitter.fit_histogram(model, pdf=False, method='mealder') # In[58]: ax = dplot(ds_fret, hist_burst_data, data_name='Su', pdf=False, show_model=True, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts') savefig('FRET pop - Su hist fit'); # ### FRET population tidy # In[59]: def make_ES_fit_dataframe(E_df_fitt, S_df_fitt, Su_df_fitt, dx, skip_ch=skip_ch): pop_peak = E_df_fitt.query('name == "center"').loc[:,('value', 'spot')].set_index('spot') pop_peak.columns = ['E'] pop_peak['S'] = S_df_fitt.query('name == "center"').loc[:,('value', 'spot')].set_index('spot') pop_peak['Su'] = Su_df_fitt.query('name == "center"').loc[:,('value', 'spot')].set_index('spot') pop_peak['num_bursts'] = dx.num_bursts pop_peak['E_sigma'] = E_df_fitt.query('name == "sigma"').loc[:,('value', 'spot')].set_index('spot') pop_peak['S_sigma'] = S_df_fitt.query('name == "sigma"').loc[:,('value', 'spot')].set_index('spot') pop_peak['Su_sigma'] = Su_df_fitt.query('name == "sigma"').loc[:,('value', 'spot')].set_index('spot') pop_peak['E_err'] = pop_peak['E_sigma'] / np.sqrt(pop_peak['num_bursts']) pop_peak['S_err'] = pop_peak['S_sigma'] / np.sqrt(pop_peak['num_bursts']) pop_peak['Su_err'] = pop_peak['Su_sigma'] / np.sqrt(pop_peak['num_bursts']) pop_peak.loc[skip_ch, (c for c in pop_peak.columns if c != 'num_bursts')] = np.nan # Add categorical column for pixel grouping # spotv is a global var from heatmatp.py pixel_groups = { 'center': spotsv[4:8], 'top': spotsv[:4], 'bottom': spotsv[-4:]} cat = {} for i, row in pop_peak.iterrows(): for group, group_vals in pixel_groups.items(): if i in group_vals: cat[i] = group pop_peak['Pixel'] = pd.Series(cat, dtype="category") return pop_peak # In[60]: Efret_fitg = br.glance(E_fitter.fit_res, var_names='spot') Sfret_fitg = br.glance(S_fitter.fit_res, var_names='spot') Sufret_fitg = br.glance(Su_fitter.fit_res, var_names='spot') # In[61]: Efret_fitg.head(3) # In[62]: Sfret_fitg.head(3) # In[63]: Sufret_fitg.head(3) # In[64]: Efret_fitt = br.tidy(E_fitter.fit_res, var_names='spot') Sfret_fitt = br.tidy(S_fitter.fit_res, var_names='spot') Sufret_fitt = br.tidy(Su_fitter.fit_res, var_names='spot') Efret_fitt.head() # In[65]: FRET_peak = make_ES_fit_dataframe(Efret_fitt, Sfret_fitt, Sufret_fitt, ds_fret) # In[66]: FRET_peak.head() # ### FRET population plots # In[67]: binwidth = 0.02 bins= np.arange(-0.1, 1.1, binwidth) + 0.5*binwidth labels = sorted(set(FRET_peak.Pixel)) Efret_groups = [np.array(FRET_peak.E[FRET_peak.Pixel == grp]) for grp in labels] plt.hist(Efret_groups, bins=bins, range=(bins.min(), bins.max()), histtype='bar', stacked=True); plt.xlabel('E') plt.ylabel('# Spots') plt.xlim(-0.1, 1.1) plt.title('Distributions of FRET peak positions') plt.legend(labels, title='Pixel group') print('FRET pop: E mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (FRET_peak.E.mean(), FRET_peak.E.min(), FRET_peak.E.max(), FRET_peak.E.std())) savefig('FRET_peaks_distributions_grouped.png') # In[68]: binwidth = 0.02 bins= np.arange(-0.1, 1.1, binwidth) + 0.5*binwidth labels = sorted(set(FRET_peak.Pixel)) Sfret_groups = [np.array(FRET_peak.S[FRET_peak.Pixel == grp]) for grp in labels] plt.hist(Sfret_groups, bins=bins, range=(bins.min(), bins.max()), histtype='bar', stacked=True); plt.xlabel('S') plt.ylabel('# Spots') plt.xlim(-0.1, 1.1) plt.title('Distributions of S peak positions') plt.legend(labels, title='Pixel group') print('FRET pop: S mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (FRET_peak.S.mean(), FRET_peak.S.min(), FRET_peak.S.max(), FRET_peak.S.std())) savefig('S_peaks_distributions_grouped.png') # In[69]: binwidth = 0.02 bins= np.arange(-0.1, 1.1, binwidth) + 0.5*binwidth labels = sorted(set(FRET_peak.Pixel)) Sufret_groups = [np.array(FRET_peak.Su[FRET_peak.Pixel == grp]) for grp in labels] plt.hist(Sufret_groups, bins=bins, range=(bins.min(), bins.max()), histtype='bar', stacked=True); plt.xlabel('$S_u$') plt.ylabel('# Spots') plt.xlim(-0.1, 1.1) plt.title('Distributions of $S_u$ peak positions') plt.legend(labels, title='Pixel group') print('FRET pop: Su mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (FRET_peak.Su.mean(), FRET_peak.Su.min(), FRET_peak.Su.max(), FRET_peak.Su.std())) savefig('Su_peaks_distributions_grouped.png') # In[70]: num_bursts = FRET_peak.num_bursts x = FRET_peak.index y = FRET_peak.E yerr = np.array(Efret_fitt.query('name == "sigma"')['value']) / np.sqrt(num_bursts) sort = num_bursts.argsort() # In[71]: plt.figure(figsize=(10, 4)) x = FRET_peak.index / FRET_peak.index.max() * 100 y = FRET_peak.E yerr = FRET_peak.E_err plt.errorbar(x, y.loc[sort], yerr.loc[sort], lw=0, elinewidth=2, marker='o', label='Fitted E') plt.xlabel('Fraction of spots (%)') plt.ylabel('Fitted E') plt.ylim(0, 1) plt.legend(frameon=False, loc='upper left') delta = 0.05 for ich, (spot, yi, xi) in enumerate(zip(np.arange(48)[sort], y.loc[sort], x)): delta *= -1 plt.annotate('%d' % spot, (xi, yi + delta), ha='center', va='center') ax2 = plt.twinx() ax2.plot(x, num_bursts[sort], color='C1', ls='--', label='# bursts') ax2.set_ylabel('# Bursts') ax2.grid(True) plt.legend(frameon=False, loc='upper right'); savefig('E fit vs num_bursts') # In[72]: y = np.array(FRET_peak.E).reshape(4, 12) yerr = np.array(FRET_peak.E_err).reshape(4, 12) x = np.arange(12) # In[73]: fig, ax = plt.subplots(1, 1, figsize=(10, 6), squeeze=True, sharex=True, sharey=True) for irow in range(4): ax.errorbar(x, y[irow], yerr[irow], lw=0, elinewidth=2, marker='o', label='Pixel Row %d' % irow) ax.set_xticks(range(0, 12, 1)) ax.set_ylabel('Fitted E') ax.set_xlabel('Pixel Column') plt.legend() plt.grid(True, axis='y') plt.title('Fitted E vs Pixel Column') savefig('Fitted E vs Pixel Column') # In[74]: fig, ax = plt.subplots(figsize=(5,5)) ax.plot(FRET_peak.E, FRET_peak.S, lw=0, marker='+', mew=1, label='FRET peak') #ax.plot(FRET_peak.E, FRET_peak.S, lw=0, marker='o', label='FRET peak') ax.set_xlim(0, 1) ax.set_ylim(0, 1) # ### D-only population fit # In[75]: ddo = d.copy() # In[76]: ddo.burst_search(min_rate_cps=50e3, pax=True, ph_sel=Ph_sel(Dex='Dem', Aex='Dem')) # In[77]: ds_dox = ddo.select_bursts(select_bursts.size, ph_sel=Ph_sel(Dex='Dem', Aex='Dem'), th1 = 40) ds_do2 = ds_dox.select_bursts(select_bursts.size, ph_sel=Ph_sel(Aex='Aem'), naa_aexonly=False, th2=4, th1=-10) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(ds_do2.nd, ds_do2.na, ds_do2.naa)] ds_do2.add(Su=Su) # In[78]: g = alex_jointplot(ds_do2, i=30, S_name='Su', vmax_fret=False, marginal_kws=dict(show_kde=False)) g.ax_joint.set_ylabel('$S_u$') # In[79]: ax = dplot(ds_do2, hexbin_alex, vmax=30, S_name='Su', skip_ch=skip_ch, wspace=0, hspace=0, gridsize=60, title='bottom left', title_bg=False); ax0 = ax[0, 0] fig = ax0.figure cax = fig.add_axes([0.97, 0.25, 0.01, 0.5]) plt.setp(ax[:, 0], ylabel='$S_u$'); plt.colorbar(cax=cax) ax0.set_xticks([0, 0.5, 1]) ax0.set_yticks([0, 0.5, 1]); ax0.set_xlim(-0.2, 1) ax0.set_ylim(0, 1.2); savefig('48spot alex hist Su DonlyBS') # In[80]: ds_do = ds_do2 # In[81]: E_fitter = bext.bursts_fitter(ds_do, 'E', skip_ch=skip_ch) model = mfit.factory_gaussian() model.set_param_hint('center', value=0.05, min=-0.2, max=0.4) model.set_param_hint('sigma', value=0.1, min=0.03, max=0.2) model.print_param_hints() # In[82]: E_fitter.fit_histogram(model, pdf=False, method='mealder') # In[83]: ax = dplot(ds_do, hist_fret, pdf=False, show_model=True, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts') savefig('DO pop - E hist fit') # In[84]: S_fitter = bext.bursts_fitter(ds_do, 'S', skip_ch=skip_ch) model = mfit.factory_gaussian() model.set_param_hint('center', value=0.9, min=0, max=1.1) model.set_param_hint('sigma', value=0.1, min=0.03, max=0.3) model.print_param_hints() # In[85]: S_fitter.fit_histogram(model, pdf=False, method='mealder') # In[86]: ax = dplot(ds_do, hist_S, pdf=False, show_model=True, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts') savefig('DO pop - S hist fit'); # In[87]: Su_fitter = bext.bursts_fitter(ds_do, 'Su', skip_ch=skip_ch) model = mfit.factory_gaussian() model.set_param_hint('center', value=0.9, min=0, max=1.2) model.set_param_hint('sigma', value=0.1, min=0.03, max=0.2) model.print_param_hints() # In[88]: Su_fitter.fit_histogram(model, pdf=False, method='mealder') # In[89]: ax = dplot(ds_do, hist_burst_data, data_name='Su', pdf=False, show_model=True, skip_ch=skip_ch); plt.setp(ax[:, 0], ylabel='# Bursts') savefig('DO pop - Su hist fit'); # ### D-only population tidy # In[90]: Edo_fitg = br.glance(E_fitter.fit_res, var_names='spot') Sdo_fitg = br.glance(S_fitter.fit_res, var_names='spot') Sudo_fitg = br.glance(Su_fitter.fit_res, var_names='spot') # In[91]: Edo_fitg.head(3) # In[92]: Sdo_fitg.head(3) # In[93]: Sudo_fitg.head(3) # In[94]: Edo_fitt = br.tidy(E_fitter.fit_res, var_names='spot') Sdo_fitt = br.tidy(S_fitter.fit_res, var_names='spot') Sudo_fitt = br.tidy(Su_fitter.fit_res, var_names='spot') Edo_fitt.head() # In[95]: DO_peak = make_ES_fit_dataframe(Edo_fitt, Sdo_fitt, Sudo_fitt, ds_do) # In[96]: DO_peak.head() # ### D-only population plots # In[97]: binwidth = 0.02 bins= np.arange(-0.1, 1.1, binwidth) + 0.5*binwidth labels = sorted(set(DO_peak.Pixel)) Edo_groups = [np.array(DO_peak.E[DO_peak.Pixel == grp]) for grp in labels] plt.hist(Edo_groups, bins=bins, histtype='bar', range=(bins.min(), bins.max()), stacked=True); plt.hist(Efret_groups, bins=bins, histtype='bar', range=(bins.min(), bins.max()), stacked=True, color=('C0', 'C1', 'C2')); plt.xlabel('E') plt.ylabel('# Spots') plt.xlim(-0.1, 1.1) plt.title('Distributions of E peak centers for FRET and D-only populations') plt.legend(labels) print('DO pop : E mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (DO_peak.E.mean(), DO_peak.E.min(), DO_peak.E.max(), DO_peak.E.std())) print('FRET pop: E mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (FRET_peak.E.mean(), FRET_peak.E.min(), FRET_peak.E.max(), FRET_peak.E.std())) savefig('FRET_vs_DO_peaks_distributions_grouped.png') # In[98]: binwidth = 0.02 bins= np.arange(-0.1, 1.1, binwidth) + 0.5*binwidth labels = sorted(set(DO_peak.Pixel)) Sudo_groups = [np.array(DO_peak.Su[DO_peak.Pixel == grp]) for grp in labels] plt.hist(Sudo_groups, bins=bins, histtype='bar', range=(bins.min(), bins.max()), stacked=True); plt.hist(Sufret_groups, bins=bins, histtype='bar', range=(bins.min(), bins.max()), stacked=True, color=('C0', 'C1', 'C2')); plt.xlabel('$S_u$') plt.ylabel('# Spots') plt.xlim(-0.1, 1.1) plt.title('Distributions of $S_u$ peak centers for FRET and D-only populations') plt.legend(labels) print('FRET pop: Su mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (FRET_peak.Su.mean(), FRET_peak.Su.min(), FRET_peak.Su.max(), FRET_peak.Su.std())) print('DO : Su mean [min, max] = %5.3f [%5.3f, %5.3f], σ = %5.3f' % (DO_peak.Su.mean(), DO_peak.Su.min(), DO_peak.Su.max(), DO_peak.Su.std())) savefig('Su_peaks_distributions_grouped.png') # In[99]: fig, ax = plt.subplots(figsize=(8, 5)) ax.plot(FRET_peak.E, FRET_peak.S, lw=0, marker='+', mew=1, label='FRET peak') ax.plot(DO_peak.E, DO_peak.S, lw=0, marker='x', mew=1, label='D-only peak') ax.set_xlim(-0.1, 1) ax.set_ylim(0.4, 1.1) plt.setp(ax, xlabel='E', ylabel='S') plt.title('FRET vs DO fitted Epr-S peak position') plt.legend() savefig('FRET vs DO fitted Epr-S peak position') # In[100]: fig, ax = plt.subplots(figsize=(8, 5)) ax.plot(FRET_peak.E, FRET_peak.Su, lw=0, marker='+', mew=1, label='FRET peak') ax.plot(DO_peak.E, DO_peak.Su, lw=0, marker='x', mew=1, label='D-only peak') ax.set_xlim(-0.1, 1) ax.set_ylim(0.4, 1.1) plt.setp(ax, xlabel='E', ylabel='$S_u$') plt.title('Fitted $E_{PR}$-$S_u$ peak centers in different spots') plt.legend() plt.grid() savefig('FRET vs DO fitted Epr-Su peak position') # # Global plots # In[101]: def figure48(figsize=(18, 6), annotate=True, sharex=True, sharey=True, text_fmt='%d', text_x=0.85, text_y=0.9, text_data=spotsh, text_va='top', text_ha='right', ): fig, axes = plt.subplots(4, 12, figsize=figsize, sharex=sharex, sharey=sharey) plt.subplots_adjust(hspace=0, wspace=0) if annotate: for irow, axrow in enumerate(axes): for icol, ax in enumerate(axrow): ax.text(text_x, text_y, text_fmt % text_data[irow, icol], transform=ax.transAxes, ha=text_ha, va=text_va) return fig, axes # In[102]: err_kws = dict(capsize=6, capthick=1.5, marker='o', ms=6) # In[103]: x = np.array(FRET_peak.E).reshape(4, 12) xerr = np.array(FRET_peak.E_sigma).reshape(4, 12) y = np.array(FRET_peak.Su).reshape(4, 12) yerr = np.array(FRET_peak.Su_sigma).reshape(4, 12) xmean = np.nanmean(x) ymean = np.nanmean(y) delta = np.max([np.nanmax(yerr), np.nanmax(xerr)])*1.5 # In[104]: fig, axes = figure48(text_x=0.85, text_y=0.88, text_ha='right', sharey=False) for irow, axrow in enumerate(axes): for icol, ax in enumerate(axrow): ax.errorbar(x[irow, icol], y[irow, icol], xerr=xerr[irow,icol], yerr=yerr[irow,icol], **err_kws) ax.plot(xmean, ymean, marker='o', zorder=10) sns.despine(left=True, bottom=True) #plt.setp(axes[0], xlim=(np.round(xmean - delta, 1), np.round(xmean + delta, 1))); #plt.setp(axes[:,0], ylim=(ymean - delta, ymean + delta)); plt.setp(axes[0], xlim=(0.15, 0.85)); plt.setp(axes.ravel(), ylim=(0.25, 0.75)); plt.setp(axes[-1], xlabel='$E_{PR}$') plt.setp(axes[:, 0], ylabel='$S_u$'); for ax in axes[-1]: sns.despine(bottom=False, left=True, trim=True, ax=ax) plt.setp(ax.get_xticklabels(), rotation=45) for ax in axes[:, 0]: sns.despine(left=False, bottom=True, trim=True, ax=ax) sns.despine(left=False, bottom=False, trim=True, ax=axes[-1,0]) for ax in axes[:-1].ravel(): ax.tick_params(bottom=False) for ax in axes[:, 1:].ravel(): ax.tick_params(left=False) for ax in axes[:-1, 0]: ax.set_yticklabels([' ', '0.5', '0.75']) for ax in axes[:, 1:].ravel(): plt.setp(ax.get_yticklabels(), visible=False) plt.suptitle('Deviation of fitted $E_{PR}$ and $S_u$ peak from mean values for the 48 spots', fontsize=14, va='top'); savefig('48-spot Epr-Su peak position deviation from mean') # # Calibration # # For convenience we reporte the formula for the relative-$\gamma$ factor $\chi_{ch}$ # and the expression of $E$ as a function of $E_{PR}$: # # $$ # \chi_{ch} = \frac{\frac{1}{\langle E_{PR\,ch} \rangle_{ch}} - 1}{\frac{1}{E_{PR\,ch}} - 1} # $$ # # $$ # E = \frac{1}{1 + \gamma \left( \frac{1}{E_{PR}} - 1 \right)} # $$ # In[105]: chi_ch = (1/FRET_peak.E.mean() - 1) / (1/FRET_peak.E - 1) E_corr = 1 / (1 + chi_ch * (1/FRET_peak.E - 1)) E_corr.head() # In[106]: assert (E_corr.max() - E_corr.min()) < 1e-9 # Test that E_corr are all "equal" # In[107]: ds_fret.num_bursts # In[108]: ds_fret.chi_ch = np.ones(48) # In[109]: bursts0 = bext.burst_data(ds_fret) bursts0.shape # In[110]: ds_fret.chi_ch = chi_ch # In[111]: heatmap48(chi_ch, annot=True, fmt='.2f') # In[112]: chi_ch.hist(bins=np.arange(0.5, 1.5, 0.025)) # In[113]: bursts = bext.burst_data(ds_fret) bursts.shape # In[114]: #plt.rcParams['font.size'] = 12 kw = dict(bins=np.arange(-0.1,1.2, 0.02), histtype='step', lw=2, range=(-0.1, 1.1)) plt.hist(bursts0.E, label='raw', **kw); plt.hist(bursts.E, label='calibrated', **kw); plt.legend(); plt.title('48-spot E histogram, raw vs per-ch calibration'); plt.xlabel('$E_{PR}$') savefig('48-spot calibration') #plt.rcParams['font.size'] = 14 # In[115]: bursts.head() # In[116]: ich = 33 chi_ch[ich] # In[117]: ds_fret.chi_ch = np.ones(48) # In[118]: E0 = ds_fret.E[ich] # In[119]: ds_fret.chi_ch = chi_ch # In[120]: E = ds_fret.E[ich] # In[121]: kw = dict(bins=np.arange(-0.1,1.1, 0.0333), histtype='step', lw=2) plt.hist(E0, label='raw', **kw); plt.hist(E, label='calibrated', **kw); plt.legend(); plt.title('Spot %d ($\chi_i$ = %.2f) E histogram, raw vs calibrated' % (ich, chi_ch[ich])); # In[122]: ich # In[123]: E0i = bursts0.E.loc[bursts0.spot == ich] Ei = bursts.E.loc[bursts.spot == ich] # In[124]: E0a = bursts0.E.loc[~pd.isnull(bursts0.E)] Ea = bursts.E.loc[~pd.isnull(bursts.E)] # In[125]: #plt.rcParams['font.size'] = 12 fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(10, 4)) kw = dict(bins=np.arange(-0.1,1.1, 0.0333), histtype='step', lw=2) ax0.hist(E0a, label='raw', **kw); ax0.hist(Ea, label='calibrated', **kw); ax1.hist(E0i, label='raw', **kw); ax1.hist(Ei, label='calibrated', **kw); ax1.legend(); ax1.set_title('Spot %d, $\chi_i$ = %.2f' % (ich, chi_ch[ich])); ax0.set_title('All Spots'); ax1.set_title('Spot %d, $\chi_i$ = %.2f' % (ich, chi_ch[ich])); #plt.rcParams['font.size'] = 14 # In[126]: (bursts.spot == ich).sum() # In[127]: bursts.spot.shape # In[128]: bursts0.E.loc[bursts.spot == ich].shape # In[129]: E.shape, bursts.E.loc[bursts.spot == ich].shape # In[130]: bursts.sample(10).sort_index() # In[131]: bursts.t_start.max() # # Collapse # In[132]: df = d.fuse_bursts(ms=0) # In[133]: dc = d.collapse(update_gamma=False, skip_ch=skip_ch) ## check that chi_ch is set to 1 here # In[134]: d.num_bursts.sum() # In[135]: def sel_raw_naa(d, ich=0, th1=20, th2=np.inf, gamma=1., beta=1., donor_ref=True): assert th1 <= th2, 'th1 (%.2f) must be <= of th2 (%.2f)' % (th1, th2) kws = dict(ich=ich, gamma=gamma, beta=beta, donor_ref=donor_ref) naa_term = d.naa[ich] bursts_mask = (naa_term >= th1) * (naa_term <= th2) return bursts_mask, '' def sel_ndex_na(d, ich=0, th1=20, th2=np.inf, gamma=1): assert th1 <= th2, 'th1 (%.2f) must be <= of th2 (%.2f)' % (th1, th2) size = d.nd[ich] + d.nda[ich] + d.na[ich] / gamma bursts_mask = (size >= th1) * (size <= th2) return bursts_mask, '' # In[136]: naa_sel_kws = dict(ph_sel=Ph_sel(Aex='Aem'), gamma=1, na_comp=False, naa_comp=False, naa_aexonly=False) Math(d._burst_sizes_pax_formula(**naa_sel_kws)) # In[137]: size_sel_kws = dict(ph_sel=Ph_sel(Dex='DAem', Aex='Dem'), gamma=0.5, na_comp=True, naa_comp=False, naa_aexonly=False) Math(d._burst_sizes_pax_formula(**size_sel_kws)) # In[138]: dcs_th1 = d.select_bursts(select_bursts.size, th1=10, **naa_sel_kws) dcs_th2 = dcs_th1.select_bursts(select_bursts.size, th1=20, **size_sel_kws) dcs_th = dcs_th2.collapse(update_gamma=False, skip_ch=skip_ch) # In[139]: Th = (10, 20, 40, 80) Th_naa = np.array((0, 10, 20, 40)) fig, AX = plt.subplots(len(Th), len(Th_naa), figsize=(3.5*len(Th_naa), 3.5*len(Th)), sharex=True, sharey=True) for irow, th in enumerate(Th): for icol, th_naa in enumerate(Th_naa): ax = AX[irow,icol] dcs_th1 = d.select_bursts(select_bursts.size, th1=th_naa, **naa_sel_kws) dcs_th2 = dcs_th1.select_bursts(select_bursts.size, th1=th, **size_sel_kws) dcs_th = dcs_th2.collapse(update_gamma=False, skip_ch=skip_ch) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dcs_th.nd, dcs_th.na, dcs_th.naa)] dcs_th.add(Su=Su) E = dcs_th.E[0] E_std = E[E > 0.2].std() dplot(dcs_th, hexbin_alex, S_name='Su', gridsize=60, ax=ax) ax.text(0.05, 0.05, '$th_D$ = %d, $th_A$ = %d, σ = %.3f\n#B = %d' % (th, th_naa, E_std, dcs_th.num_bursts), va='bottom', ha='left', transform=ax.transAxes, fontsize=12) ax.set_title('') plt.setp(AX[:-1], xlabel='') plt.setp(AX[:, 1:], ylabel='') plt.subplots_adjust(hspace=0, wspace=0) plt.text(0.5, 0.89, 'ACBS', transform=fig.transFigure, fontsize=16); # In[140]: Th = (10, 20, 40, 80) Th_naa = np.array((0, 10, 20, 40)) fig, AX = plt.subplots(len(Th), len(Th_naa), figsize=(3.5*len(Th_naa), 3.5*len(Th)), sharex=True, sharey=True) for irow, th in enumerate(Th): for icol, th_naa in enumerate(Th_naa): ax = AX[irow,icol] dcs_th1 = d.select_bursts(select_bursts.size, th1=th_naa, **naa_sel_kws) dcs_th2 = dcs_th1.select_bursts(select_bursts.size, th1=th, **size_sel_kws) dcs_th = dcs_th2.collapse(update_gamma=False, skip_ch=skip_ch) dplot(dcs_th, hist_fret, ax=ax) E = dcs_th.E[0] E_std = E[E > 0.2].std() ax.text(0.05, 0.95, '$th_D$ = %d, $th_A$ = %d, σ = %.3f\n#B = %d' % (th, th_naa, E_std, dcs_th.num_bursts), va='top', ha='left', transform=ax.transAxes, fontsize=12) ax.set_title('') plt.setp(AX[:-1], xlabel='') plt.setp(AX[:, 1:], ylabel='') plt.subplots_adjust(hspace=0, wspace=0) plt.text(0.5, 0.89, 'ACBS', transform=fig.transFigure, fontsize=16) # In[141]: d2 = bext.burst_search_and_gate(d, min_rate_cps=(50e3, 25e3)) # In[142]: Th = (20, 40, 60, 80, 160) fig, AX = plt.subplots(1, len(Th), figsize=(3.5*len(Th), 3.5), sharex=True, sharey=True) for irow, th in enumerate(Th): ax = AX[irow] dcs_th2 = d2.select_bursts(select_bursts.size, th1=th, **size_sel_kws) dcs_th = dcs_th2.collapse(update_gamma=False, skip_ch=skip_ch) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dcs_th.nd, dcs_th.na, dcs_th.naa)] dcs_th.add(Su=Su) dplot(dcs_th, hexbin_alex, S_name='Su', gridsize=60, ax=ax) E = dcs_th.E[0] E_std = E[E > 0.2].std() ax.text(0.05, 0.95, '$th_D$ = %d, σ = %.3f\n#B = %d' % (th, E_std, dcs_th.num_bursts), va='top', ha='left', transform=ax.transAxes, fontsize=12) ax.set_title('') plt.setp(AX[1:], ylabel='') AX[0].set_ylabel('$S_u$') plt.subplots_adjust(hspace=0, wspace=0) plt.text(0.5, 0.89, 'DCBS', transform=fig.transFigure, fontsize=16); # In[143]: Th = (20, 40, 60, 80, 160) fig, AX = plt.subplots(1, len(Th), figsize=(3.5*len(Th), 3.5), sharex=True, sharey=True) for irow, th in enumerate(Th): ax = AX[irow] dcs_th2 = d2.select_bursts(select_bursts.size, th1=th, **size_sel_kws) dcs_th = dcs_th2.collapse(update_gamma=False, skip_ch=skip_ch) dplot(dcs_th, hist_fret, ax=ax) E = dcs_th.E[0] E_std = E[E > 0.2].std() ax.text(0.05, 0.95, '$th_D$ = %d, σ = %.3f\n#B = %d' % (th, E_std, dcs_th.num_bursts), va='top', ha='left', transform=ax.transAxes, fontsize=12) ax.set_title('') plt.setp(AX[1:], ylabel='') plt.subplots_adjust(hspace=0, wspace=0) plt.text(0.5, 0.89, 'DCBS', transform=fig.transFigure, fontsize=16); # In[144]: dc = d.collapse(update_gamma=False, skip_ch=skip_ch) # In[145]: size_th = 80 size_sel_kws = dict(ph_sel=Ph_sel(Dex='DAem', Aex='Dem'), gamma=0.5, na_comp=True, naa_comp=False, naa_aexonly=False) dcs = dc.select_bursts(select_bursts.size, th1=size_th, **size_sel_kws) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dcs.nd, dcs.na, dcs.naa)] dcs.add(Su=Su) Math(d._burst_sizes_pax_formula(**size_sel_kws)) # In[146]: g = alex_jointplot(dcs, S_name='Su', vmax_fret=True, gridsize=80) g.ax_joint.set_ylabel('$S_u$'); # In[147]: size_th = 40 size_sel_kws = dict(ph_sel=Ph_sel(Dex='DAem'), gamma=0.5, na_comp=False, naa_comp=False, naa_aexonly=False) dcs = dc.select_bursts(select_bursts.size, th1=size_th, **size_sel_kws) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dcs.nd, dcs.na, dcs.naa)] dcs.add(Su=Su) Math(d._burst_sizes_pax_formula(**size_sel_kws)) # In[148]: g = alex_jointplot(dcs, S_name='Su', vmax_fret=True, gridsize=80) g.ax_joint.set_ylabel('$S_u$'); # In[149]: dcs15 = dcs.select_bursts(select_bursts.time, time_s1=0, time_s2=15) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dcs15.nd, dcs15.na, dcs15.naa)] dcs15.add(Su=Su) # In[150]: g = alex_jointplot(dcs15, S_name='Su') g.ax_joint.set_ylabel('$S_u$'); # In[151]: dcs5 = dcs.select_bursts(select_bursts.time, time_s1=0, time_s2=5) Su = [(nd + na)/(nd + na + naa) for nd, na, naa in zip(dcs5.nd, dcs5.na, dcs5.naa)] dcs5.add(Su=Su) # In[152]: g = alex_jointplot(dcs5, S_name='Su') g.ax_joint.set_ylabel('$S_u$'); # In[ ]: