<jupyter_start><jupyter_text>Finding Gamma and BetaThis notebook is for calculating the gamma and beta correction factors from multiple loaded datasets. It is possible to find these parameters from a single data set so long as the distribution of apparent FRET efficiencies is dominated by a breadth of real underlying distances, however in this case we load multiple data sets each containing a single FRET state.Gamma describes the inequality between how well the donor and acceptor fluorophores can be detected by your system, whereas beta describes the inequality between how well they can be excited. They are both found by an equation which describes the slope and height (in S) of the doubly labelled population. You should find the alpha and delta factors first before this step.  Import packages<jupyter_code>from fretbursts import *
sns = init_notebook()
import lmfit
import phconvert
import os
from fretbursts.burstlib_ext import burst_search_and_gate<jupyter_output><empty_output><jupyter_text>Name and Load in dataName the data files, note that it will look for files starting from the folder this notebook is in.<jupyter_code>files = ["definitiveset/1a1.hdf5", "definitiveset/1a2.hdf5",
         "definitiveset/1b1.hdf5", "definitiveset/1b2.hdf5",
         "definitiveset/1c1.hdf5", "definitiveset/1c2.hdf5"]<jupyter_output><empty_output><jupyter_text>Set the spectral cross talk parameters, load them, and sort them<jupyter_code>alpha = 0.081
delta = 0.076

datasets = []
for file in files:
    datasets.append(loader.photon_hdf5(file))
for dataset in datasets:
    dataset.leakage = alpha
    dataset.dir_ex = delta
    for i in range(0, len(dataset.ph_times_t)): #sorting code
        indices = dataset.ph_times_t[i].argsort()
        dataset.ph_times_t[i], dataset.det_t[i] = dataset.ph_times_t[i][indices], dataset.det_t[i][indices]
datasets<jupyter_output><empty_output><jupyter_text>Apply alternation cycle<jupyter_code>for dataset in datasets:
    #dataset.add(det_donor_accept = (0, 1),
    #alex_period = 10000,
    #offset = 0,
    #D_ON = (0, 4500),
    #A_ON = (5000, 9500))
    loader.alex_apply_period(dataset)<jupyter_output># Total photons (after ALEX selection):     2,003,013
#  D  photons in D+A excitation periods:    1,141,024
#  A  photons in D+A excitation periods:      861,989
# D+A photons in  D  excitation period:     1,471,746
# D+A photons in  A  excitation period:       531,267

# Total photons (after ALEX selection):     2,067,432
#  D  photons in D+A excitation periods:    1,161,138
#  A  photons in D+A excitation periods:      906,294
# D+A photons in  D  excitation period:     1,501,952
# D+A photons in  A  excitation period:       565,480

# Total photons (after ALEX selection):     3,266,191
#  D  photons in D+A excitation periods:    2,027,118
#  A  photons in D+A excitation periods:    1,239,073
# D+A photons in  D  excitation period:     2,530,526
# D+A photons in  A  excitation period:       735,665

# Total photons (after ALEX selection):     3,189,530
#  D  photons in D+A excitation periods:    1,997,792
#  A  photons in D+A excitation periods:    1,191,738
# D+A photons in  D  exci[...]<jupyter_text>Background Estimation We background correct all datasets as before<jupyter_code>threshold = 1500
recalctime = 300
for dataset in datasets:
    dataset.calc_bg(bg.exp_fit, time_s = recalctime, tail_min_us=(threshold))
    dplot(dataset, hist_bg, show_fit=True)<jupyter_output>- Calculating BG rates ... [DONE]
 - Calculating BG rates ... [DONE]
 - Calculating BG rates ... [DONE]
 - Calculating BG rates ... [DONE]
 - Calculating BG rates ... [DONE]
 - Calculating BG rates ... [DONE]<jupyter_text>Now let's find bursts and plot them. We will use a dual channel burst search this time as we are looking for strictly doubly labelled bursts.<jupyter_code>colourscheme= "viridis_r"
burstsets = []
for dataset in datasets:
    dataset = burst_search_and_gate(dataset, F=45, m=10, mute=True)
    dataset = dataset.select_bursts(select_bursts.size, th1=50)
    burstsets.append(dataset)
for burstset in burstsets:
    alex_jointplot(burstset, cmap=colourscheme, marginal_color=20, vmax_fret=False)<jupyter_output>C:\Users\chp17ba\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\axisgrid.py:1847: UserWarning: JointGrid annotation is deprecated and will be removed in a future release.
  warnings.warn(UserWarning(msg))<jupyter_text>Now let's extract the E and S positions from these plots with gaussian fits. We could plot the fitted curves, but here the graphs are not shown to save space.<jupyter_code>def ESgauss(dataset):
    dataset.E_fitter.fit_histogram(model=mfit.factory_gaussian(), verbose=False, pdf=False)
    params = dataset.E_fitter.params
    Efit = params.to_dict()
    E = Efit['center'][0]
    dataset.S_fitter.fit_histogram(model=mfit.factory_gaussian(), verbose=False, pdf=False)
    params = dataset.S_fitter.params
    Sfit = params.to_dict()
    S = Sfit['center'][0]
    return(E, S)
x = []
y = []
for dataset in burstsets:
    E, S = ESgauss(dataset)
    x.append(E)
    y.append(S)<jupyter_output><empty_output><jupyter_text>Now we can fit to the equation:    S = 1/(1+bg+(1-g)*b*E)<jupyter_code>model = lmfit.models.ExpressionModel('1 / ( 1 + b * g + ( 1 - g ) * b * E )',
                             independent_vars=["E"], nan_policy = "omit")
params = model.make_params(b=0.5, g=1.0)
fit = model.fit(y, params, E=E)
fit.plot_fit(0)
print(fit.fit_report())
pars = fit.params
beta = pars['b']
beta=beta.value
gamma = pars['g']
gamma=gamma.value
print("gamma = ", gamma)
print("beta = ", beta)<jupyter_output>gamma =  0.8563232142630293
beta =  0.8480748600548473
{'fraction_non_alphanumeric': 0.1015693881149709, 'fraction_numerical': 0.04232057838123788, 'mean_word_length': 4.320825515947467, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '503344', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2091, 'n_tokens_neox': 1878, 'n_words': 567, 'path': 'Jnotebooks/Correction Factor Finder Gamma-Beta.ipynb', 'repo_name': 'BenjaminAmbrose/smfBox', 'size': '5671'}