Esempio n. 1
0
def test_correlations(cell='NIH3T3', temperature=37, nb_traces=500):
    """
    Compute and plot the correlation between the model variabels : phase, noise,
    amplitude and background.

    Parameters
    ----------
    cell : string
        Cell condition.
    temperature : integer
        Temperature condition.
    nb_traces : integer
        How many traces to run the experiment on.
    """
    ##################### LOAD OPTIMIZED PARAMETERS ##################
    path = '../Parameters/Real/opt_parameters_div_'+str(temperature)\
                                                                +"_"+cell+'.p'
    with open(path, 'rb') as f:
        l_parameters = [
            dt, sigma_em_circadian, W, pi, N_theta, std_theta, period_theta,
            l_boundaries_theta, w_theta, N_phi, std_phi, period_phi,
            l_boundaries_phi, w_phi, N_amplitude_theta, mu_amplitude_theta,
            std_amplitude_theta, gamma_amplitude_theta,
            l_boundaries_amplitude_theta, N_background_theta,
            mu_background_theta, std_background_theta, gamma_background_theta,
            l_boundaries_background_theta, F
        ] = pickle.load(f)
        '''
        gamma = 0.005
        std_amplitude_theta = std_amplitude_theta \
                                            * (gamma/gamma_amplitude_theta)**0.5
        std_background_theta = std_background_theta \
                                           * (gamma/gamma_background_theta)**0.5
        gamma_amplitude_theta = gamma
        gamma_background_theta = gamma


        l_parameters = [dt, sigma_em_circadian, W, pi,
        N_theta, std_theta, period_theta, l_boundaries_theta, w_theta,
        N_phi, std_phi, period_phi, l_boundaries_phi, w_phi,
        N_amplitude_theta, mu_amplitude_theta, std_amplitude_theta,
        gamma_amplitude_theta, l_boundaries_amplitude_theta,
        N_background_theta, mu_background_theta, std_background_theta,
        gamma_background_theta, l_boundaries_background_theta,
        F]
        '''
    ##################### DISPLAY PARAMETERS ##################
    display_parameters_from_file(path, show=True)

    ##################### LOAD DATA ##################
    if cell == 'NIH3T3':
        path = "../Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p"
    else:
        path = "../Data/U2OS-2017-03-20/ALL_TRACES_INFORMATION_march_2017.p"
    dataClass = LoadData(path,
                         nb_traces,
                         temperature=temperature,
                         division=True,
                         several_cell_cycles=False,
                         remove_odd_traces=True)
    (ll_area, ll_signal, ll_nan_circadian_factor, ll_obs_phi, ll_peak,
    ll_idx_cell_cycle_start, T_theta, T_phi) \
                                    = dataClass.load(load_annotation = True)
    ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] \
                                                        for l_peak in ll_peak]
    print(len(ll_signal), " traces kept")

    ##################### CREATE HIDDEN VARIABLES ##################
    theta_var_coupled, amplitude_var, background_var \
                        = create_hidden_variables(l_parameters = l_parameters )
    l_var = [theta_var_coupled, amplitude_var, background_var]

    ##################### CREATE AND RUN HMM ##################
    hmm = HMM_SemiCoupled(l_var,
                          ll_signal,
                          sigma_em_circadian,
                          ll_obs_phi,
                          waveform=W,
                          ll_nan_factor=ll_nan_circadian_factor,
                          pi=pi,
                          crop=True)
    l_gamma_div, l_logP_div = hmm.run(project=False)

    ##################### REMOVE BAD TRACES #####################
    Plim = np.percentile(l_logP_div, 10)
    idx_to_keep = [i for i, logP in enumerate(l_logP_div) if logP > Plim]
    l_gamma_div = [l_gamma_div[i] for i in idx_to_keep]
    ll_signal = [ll_signal[i] for i in idx_to_keep]
    ll_area = [ll_area[i] for i in idx_to_keep]
    l_logP_div = [l_logP_div[i] for i in idx_to_keep]
    ll_obs_phi = [ll_obs_phi[i] for i in idx_to_keep]
    ll_idx_cell_cycle_start = [ll_idx_cell_cycle_start[i] for i in idx_to_keep]
    ll_idx_peak = [ll_idx_peak[i] for i in idx_to_keep]
    print("Kept traces with div: ", len(idx_to_keep))

    ##################### CROP SIGNALS FOR PLOTTING ##################

    l_first = [[it for it, obj in enumerate(l_obs_phi) \
                                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
    l_last = [[len(l_obs_phi)-it-1 for it, obj in enumerate(l_obs_phi[::-1]) \
                                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
    ll_signal = [l_signal[first:last+1] for l_signal, first, last \
                                            in zip(ll_signal, l_first, l_last)]
    ll_area = [l_area[first:last+1] for l_area, first, last \
                                            in zip(ll_area, l_first, l_last)]
    ll_obs_phi = [l_obs_phi[first:last+1] for l_obs_phi, first, last \
                                        in zip(ll_obs_phi, l_first, l_last)]
    ll_idx_cell_cycle_start = [ [v for v in l_idx_cell_cycle_start \
            if v>=first and v<=last  ] for l_idx_cell_cycle_start, first, last \
            in zip(ll_idx_cell_cycle_start, l_first, l_last)]
    ll_idx_peak = [ [v for v in l_idx_peak if v>=first and v<=last  ] \
                for l_idx_peak, first, last in zip(ll_idx_peak, l_first, l_last)]

    ##################### CREATE ll_idx_obs_phi and ll_val_phi##################
    ll_idx_obs_phi = []
    for l_obs in ll_obs_phi:
        l_idx_obs_phi = []
        for obs in l_obs:
            l_idx_obs_phi.append( int(round(obs/(2*np.pi)\
                                *len(theta_var_coupled.codomain )))\
                                %len(theta_var_coupled.codomain))
        ll_idx_obs_phi.append(l_idx_obs_phi)

    ##################### COMPUTE EXPECTED VALUE OF THE SIGNAL #################
    ll_model = []
    ll_phase = []
    ll_amplitude = []
    ll_background = []
    zp = zip(enumerate(ll_signal), l_gamma_div, l_logP_div, ll_area,
             ll_obs_phi, ll_idx_cell_cycle_start, ll_idx_peak)
    for ((idx, signal), gamma, logP, area, l_obs_phi, l_idx_cell_cycle_start,
         l_idx_peak) in zp:
        plt_result = PlotResults(gamma,
                                 l_var,
                                 signal_model,
                                 signal,
                                 waveform=W,
                                 logP=logP,
                                 temperature=temperature,
                                 cell=cell)
        E_model, E_theta, E_A, E_B \
                            = plt_result.plotEverythingEsperance(False, idx)
        ll_model.append(E_model)
        ll_phase.append(E_theta)
        ll_amplitude.append(E_A)
        ll_background.append(E_B)

    ##################### COMPUTE AND PLOT RESIDUALS ##################
    domain = list(range(N_theta))
    dic_phase_res = {}
    for phase in domain:
        dic_phase_res[phase] = []
    for (idx, l_signal), l_E_model, l_E_phase, in zip(enumerate(ll_signal),
                                                      ll_model, ll_phase):
        for signal, E_model, E_phase in zip(l_signal, l_E_model, l_E_phase):
            phase = int(round(E_phase * N_theta))
            if phase == N_theta:
                phase = 0
            dic_phase_res[phase].append(E_model - signal)

    l_mean = []
    l_std = []
    for phase in domain:
        l_mean.append(np.mean(dic_phase_res[phase]))
        l_std.append(np.std(dic_phase_res[phase]))

    plt.close()
    plt.errorbar(np.array(domain) / len(domain), l_mean, yerr=l_std, fmt='o')
    plt.xlabel(r"Circadian phase $\theta$")
    plt.ylabel(r"Residuals  $S_t-O_t$")
    plt.tight_layout()
    plt.savefig('../Results/Correlation/Residuals_'+cell+'_'\
                                                    +str(temperature)+'.pdf')
    plt.show()
    plt.close()

    ##################### COMPUTE AND PLOT PHASE/AMP CORRELATION ###############
    dic_phase_amp = {}
    for phase in domain:
        dic_phase_amp[phase] = []
    for (idx, l_signal), l_E_amp, l_E_phase, in zip(enumerate(ll_signal),
                                                    ll_amplitude, ll_phase):
        for signal, E_amp, E_phase in zip(l_signal, l_E_amp, l_E_phase):
            phase = int(round(E_phase * N_theta))
            if phase == N_theta:
                phase = 0
            dic_phase_amp[phase].append(E_amp)

    l_mean = []
    l_std = []
    for phase in domain:
        l_mean.append(np.mean(dic_phase_amp[phase]))
        l_std.append(np.std(dic_phase_amp[phase]))

    plt.errorbar(np.array(domain) / len(domain), l_mean, yerr=l_std, fmt='o')
    plt.xlabel(r"Circadian phase $\theta$")
    plt.ylabel(r"Amplitude Process $A_t$")
    plt.tight_layout()
    plt.savefig('../Results/Correlation/Amplitude_'+cell+'_'\
                                                       +str(temperature)+'.pdf')
    plt.show()
    plt.close()

    ##################### COMPUTE AND PLOT PHASE/BACK CORRELATION ##############
    dic_phase_bac = {}
    for phase in domain:
        dic_phase_bac[phase] = []
    for (idx, l_signal), l_E_bac, l_E_phase, in zip(enumerate(ll_signal),
                                                    ll_background, ll_phase):
        for signal, E_bac, E_phase in zip(l_signal, l_E_bac, l_E_phase):
            phase = int(round(E_phase * N_theta))
            if phase == N_theta:
                phase = 0
            dic_phase_bac[phase].append(E_bac)

    l_mean = []
    l_std = []
    for phase in domain:
        l_mean.append(np.mean(dic_phase_bac[phase]))
        l_std.append(np.std(dic_phase_bac[phase]))

    plt.errorbar(np.array(domain) / len(domain), l_mean, yerr=l_std, fmt='o')
    plt.xlabel(r"Circadian phase $\theta$")
    plt.ylabel(r"Background Process $B_t$")
    plt.tight_layout()
    plt.savefig('../Results/Correlation/Background_'+cell+'_'\
                                                      +str(temperature)+'.pdf')
    plt.show()
    plt.close()
def compute_phase_for_phase_delay(path=None, temperature=None):
    """
    Compute phase of cells treated with various cell-cycle inhibitors and
    write results in a text file to be analyzed with R.

    Parameters
    ----------
    path : string
        Data path (if no temperature is given)
    temperature : integer
        Temperature condition (if no path is given)
    """
    if path is None and temperature is None:
        print('A PATH OR A TEMPERATURE MUST BE SPECIFIED')
    ##################### LOAD OPTIMIZED PARAMETERS ##################
    path_par = '../Parameters/Real/opt_parameters_div_'+str(temperature)\
                                                                    +'_NIH3T3.p'
    with open(path_par, 'rb') as f:
        l_parameters = [
            dt, sigma_em_circadian, W, pi, N_theta, std_theta, period_theta,
            l_boundaries_theta, w_theta, N_phi, std_phi, period_phi,
            l_boundaries_phi, w_phi, N_amplitude_theta, mu_amplitude_theta,
            std_amplitude_theta, gamma_amplitude_theta,
            l_boundaries_amplitude_theta, N_background_theta,
            mu_background_theta, std_background_theta, gamma_background_theta,
            l_boundaries_background_theta, F
        ] = pickle.load(f)

    ##################### LOAD COLORMAP ##################
    c = mcolors.ColorConverter().to_rgb
    bwr = make_colormap(
        [c('blue'),
         c('white'), 0.48,
         c('white'), 0.52,
         c('white'),
         c('red')])

    ##################### DISPLAY PARAMETERS ##################
    display_parameters_from_file(path_par, show=True)

    ##################### LOAD DATA ##################
    if temperature is not None:
        path = "../Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p"
        dataClass = LoadData(path,
                             100000,
                             temperature=temperature,
                             division=True,
                             several_cell_cycles=False,
                             remove_odd_traces=True)
        (ll_area, ll_signal, ll_nan_circadian_factor, ll_obs_phi, ll_peak,
        ll_idx_cell_cycle_start, T_theta, T_phi)\
                    = dataClass.load(period_phi = None, load_annotation = True)
        ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] \
                                                        for l_peak in ll_peak]
        print(len(ll_signal), " traces kept")
        l_idx_trace = list(range(len(ll_area)))

    else:
        from os import listdir
        from os.path import isfile, join
        l_files = [f for f in listdir(path) if isfile(join(path, f))]

        ll_t = []
        ll_signal = []
        ll_area = []
        ll_peak = []
        ll_neb = []
        ll_cc_start = []
        ll_nan_circadian_factor = []
        ll_idx_cell_cycle_start = []
        l_idx_trace = []

        for file in l_files:
            l_t = []
            l_signal = []
            l_area = []
            l_peak = []
            l_neb = []
            l_cc_start = []
            with open(path + '/' + file, 'r') as f:
                for idx, line in enumerate(f):
                    if idx == 0: continue
                    t, signal, area, peak, cc_start, neb = line.split()
                    l_signal.append(float(signal))
                    l_area.append(float(area))
                    l_t.append(float(t))
                    l_peak.append(int(peak))
                    l_cc_start.append(int(cc_start))
                    l_neb.append(int(neb))

                l_signal = np.array(l_signal) - np.percentile(l_signal, 5)
                l_signal = l_signal / np.percentile(l_signal, 95)

            garbage, idx_trace, garbage = file.split('.')
            if np.sum(l_cc_start) >= 2:
                l_idx_trace.append(int(idx_trace))
                ll_t.append(l_t)
                ll_signal.append(l_signal)
                ll_area.append(l_area)
                ll_peak.append(l_peak)
                ll_neb.append(l_neb)
                ll_cc_start.append(l_cc_start)
                ll_peak.append(l_peak)

        ### NaN FOR CIRCADIAN SIGNAL ###
        for l_mitosis_start, l_cell_start, l_peak in zip(
                ll_neb, ll_cc_start, ll_peak):
            l_temp = [False] * len(l_mitosis_start)
            NaN = False
            for ind, (m, c) in enumerate(zip(l_mitosis_start, l_cell_start)):
                if m == 1:
                    NaN = True
                if c == 1:
                    NaN = False
                if NaN:
                    try:
                        l_temp[ind - 1] = True  #TO BE REMOVED POTENTIALLY
                    except:
                        pass
                    try:
                        l_temp[ind + 1] = True
                    except:
                        pass
                    l_temp[ind] = True
            ll_nan_circadian_factor.append(l_temp)

        ### COMPUTE IDX PHI ###
        zp = zip(enumerate(ll_neb), ll_cc_start)
        for (idx, l_mitosis_start), l_cell_cycle_start in zp:
            l_idx_cell_cycle_start = \
                       [idx for idx, i in enumerate(l_cell_cycle_start) if i==1]
            ll_idx_cell_cycle_start.append(l_idx_cell_cycle_start)

        ### GET PHI OBS ###
        ll_obs_phi = []
        for l_signal, l_idx_cell_cycle_start in zip(ll_signal,
                                                    ll_idx_cell_cycle_start):
            l_obs_phi = [-1] * l_idx_cell_cycle_start[0]
            first = True
            for idx_div_1, idx_div_2 in zip(l_idx_cell_cycle_start[:-1],
                                            l_idx_cell_cycle_start[1:]):
                if not first:
                    del l_obs_phi[-1]
                l_obs_phi.extend([i%(2*np.pi) for i \
                            in np.linspace(0,2*np.pi,idx_div_2-idx_div_1+1)])
                first = False

            l_obs_phi.extend( [-1] * \
                                  (len(l_signal)-l_idx_cell_cycle_start[-1]-1))
            ll_obs_phi.append(l_obs_phi)

        ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] \
                                                          for l_peak in ll_peak]

    ##################### CREATE HIDDEN VARIABLES ##################
    theta_var_coupled, amplitude_var, background_var = \
                            create_hidden_variables(l_parameters = l_parameters)
    l_var = [theta_var_coupled, amplitude_var, background_var]

    ##################### CREATE AND RUN HMM ##################
    hmm = HMM_SemiCoupled(l_var,
                          ll_signal,
                          sigma_em_circadian,
                          ll_obs_phi,
                          waveform=W,
                          ll_nan_factor=ll_nan_circadian_factor,
                          pi=pi,
                          crop=True)
    l_gamma_div, l_logP_div = hmm.run(project=False)

    ##################### CROP SIGNALS FOR PLOTTING ##################
    l_first = [[it for it, obj in enumerate(l_obs_phi) \
                                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
    l_last = [[len(l_obs_phi)-it-1 for it, obj \
        in enumerate(l_obs_phi[::-1]) if obj!=-1][0] for l_obs_phi in ll_obs_phi]
    ll_signal = [l_signal[first:last+1] for l_signal, first, last \
                                            in zip(ll_signal, l_first, l_last)]
    ll_area = [l_area[first:last+1] for l_area, first, last \
                                            in zip(ll_area, l_first, l_last)]
    ll_obs_phi = [l_obs_phi[first:last+1] for l_obs_phi, first, last \
                                            in zip(ll_obs_phi, l_first, l_last)]
    ll_idx_cell_cycle_start = [ [v for v in l_idx_cell_cycle_start \
        if v>=first and v<=last  ] for l_idx_cell_cycle_start, first, last \
        in zip(ll_idx_cell_cycle_start, l_first, l_last)]
    ll_idx_peak = [ [v for v in l_idx_peak if v>=first and v<=last  ] \
            for l_idx_peak, first, last in zip(ll_idx_peak, l_first, l_last)]

    ##################### CREATE ll_idx_obs_phi and ll_val_phi##################
    ll_idx_obs_phi = []
    for l_obs in ll_obs_phi:
        l_idx_obs_phi = []
        for obs in l_obs:
            l_idx_obs_phi.append( int(round(obs/(2*np.pi) * \
                                len(theta_var_coupled.codomain )))\
                                %len(theta_var_coupled.codomain )   )
        ll_idx_obs_phi.append(l_idx_obs_phi)

    ##################### PLOT FITS ##################
    ll_esp_theta = []
    for ((idx, signal), gamma, logP, area, l_obs_phi, l_idx_cell_cycle_start,
         l_idx_peak) in zip(enumerate(ll_signal), l_gamma_div, l_logP_div,
                            ll_area, ll_obs_phi, ll_idx_cell_cycle_start,
                            ll_idx_peak):
        plt_result = PlotResults(gamma,
                                 l_var,
                                 signal_model,
                                 signal,
                                 waveform=W,
                                 logP=logP,
                                 temperature=temperature,
                                 cell='NIH3T3')
        try:
            E_model, E_theta, E_A, E_B \
                                = plt_result.plotEverythingEsperance(False, idx)
            ll_esp_theta.append(E_theta)
        except:
            ll_esp_theta.append([-1] * len(signal))
            ('Trace ' + str(idx) + ' skipped !')

    ##################### RECORD INFERRED PHASES IN A TXT FILE ##################
    longest_trace = np.max([len(trace) for trace in ll_signal])
    print([len(trace) for trace in ll_signal])
    print(longest_trace)
    if temperature is None:
        appendix = path.split('/')[-1]
    else:
        appendix = str(temperature)
    with open('../Results/RawPhases/traces_theta_inferred_'\
                                            +appendix+'.txt', 'w') as f_theta:
        with open('../Results/RawPhases/traces_phi_inferred_'\
                                                +appendix+'.txt', 'w') as f_phi:
            zp = zip(l_idx_trace, ll_signal, ll_esp_theta, ll_obs_phi)
            for idx, signal, l_esp_theta, l_obs_phi in zp:
                f_theta.write(str(idx))
                f_phi.write(str(idx))
                for s in np.concatenate(
                    (l_esp_theta, [-1] * (longest_trace - len(signal)))):
                    if s != -1:
                        f_theta.write('\t' + str(s * 2 * np.pi))
                    else:
                        f_theta.write('\t' + str(s))
                for s in np.concatenate(
                    (l_obs_phi, [-1] * (longest_trace - len(signal)))):
                    f_phi.write('\t' + str(s))
                f_theta.write('\n')
                f_phi.write('\n')
Esempio n. 3
0
def before_after_opti(cell='NIH3T3',
                      temperature=37,
                      nb_traces=500,
                      size_block=100):
    """
    Plot a trace fit before and after optimization of the parameters.

    Parameters
    ----------
    cell : string
        Cell condition.
    temperature : integer
        Temperature condition.
    nb_traces : integer
        Number of traces from which the inference is made.
    size_block : integer
        Size of the traces chunks (to save memory).
    """

    path = 'Parameters/Real/opt_parameters_div_' + str(
        temperature) + "_" + cell + '.p'
    """""" """""" """""" """ LOAD OPTIMIZED PARAMETERS """ """""" """""" ""
    with open(path, 'rb') as f:
        l_parameters = [
            dt, sigma_em_circadian, W, pi, N_theta, std_theta, period_theta,
            l_boundaries_theta, w_theta, N_phi, std_phi, period_phi,
            l_boundaries_phi, w_phi, N_amplitude_theta, mu_amplitude_theta,
            std_amplitude_theta, gamma_amplitude_theta,
            l_boundaries_amplitude_theta, N_background_theta,
            mu_background_theta, std_background_theta, gamma_background_theta,
            l_boundaries_background_theta, F
        ] = pickle.load(f)
    """""" """""" """""" """ DISPLAY PARAMETERS """ """""" """""" ""
    display_parameters_from_file(path, show=False)
    """""" """""" """""" """ LOAD DATA """ """""" """""" ""
    if cell == 'NIH3T3':
        path = "Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p"
    else:
        path = "Data/U2OS-2017-03-20/ALL_TRACES_INFORMATION_march_2017.p"
    dataClass = LoadData(path,
                         nb_traces,
                         temperature=temperature,
                         division=True,
                         several_cell_cycles=False,
                         remove_odd_traces=True)
    (ll_area, ll_signal, ll_nan_circadian_factor, ll_obs_phi, ll_peak,
    ll_idx_cell_cycle_start, T_theta, T_phi) \
                    = dataClass.load(period_phi = None, load_annotation = True)
    ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] for \
                                                            l_peak in ll_peak]
    print(len(ll_signal), " traces kept")
    """""" """""" """""" """ CREATE HIDDEN VARIABLES """ """""" """""" ""
    theta_var_coupled, amplitude_var, background_var = \
                            create_hidden_variables(l_parameters = l_parameters)
    l_var = [theta_var_coupled, amplitude_var, background_var]
    """""" """""" """""" """ CREATE AND RUN HMM WITH OPTI """ """""" """""" ""
    hmm = HMM_SemiCoupled(l_var,
                          ll_signal,
                          sigma_em_circadian,
                          ll_obs_phi,
                          waveform=W,
                          ll_nan_factor=ll_nan_circadian_factor,
                          pi=pi,
                          crop=True)
    l_gamma_div_1, l_logP_div_1 = hmm.run(project=False)
    """""" """""" """""" """ SET F TO 0 """ """""" """""" ""
    l_parameters[-1] = np.zeros((N_theta, N_phi))
    """""" """""" """""" """ RECREATE HIDDEN VARIABLES """ """""" """""" ""
    theta_var_coupled, amplitude_var, background_var = \
                            create_hidden_variables(l_parameters = l_parameters)
    l_var = [theta_var_coupled, amplitude_var, background_var]
    """""" """""" """""" """ CREATE AND RUN HMM WITHOUT OPTI """ """""" """""" ""
    hmm = HMM_SemiCoupled(l_var,
                          ll_signal,
                          sigma_em_circadian,
                          ll_obs_phi,
                          waveform=W,
                          ll_nan_factor=ll_nan_circadian_factor,
                          pi=pi,
                          crop=True)
    l_gamma_div_2, l_logP_div_2 = hmm.run(project=False)
    """""" """""" """""" """ CROP SIGNALS FOR PLOTTING """ """""" """""" ""
    l_first = [[it for it, obj in enumerate(l_obs_phi) if obj!=-1][0] \
                                                    for l_obs_phi in ll_obs_phi]
    l_last = [[len(l_obs_phi)-it-1 for it, obj in enumerate(l_obs_phi[::-1]) \
                                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
    ll_signal = [l_signal[first:last+1] for l_signal, first, last \
                                            in zip(ll_signal, l_first, l_last)]
    ll_area = [l_area[first:last+1] for l_area, first, last \
                                            in zip(ll_area, l_first, l_last)]
    ll_obs_phi = [l_obs_phi[first:last+1] for l_obs_phi, first, last \
                                            in zip(ll_obs_phi, l_first, l_last)]
    ll_idx_cell_cycle_start = [ [v for v in l_idx_cell_cycle_start \
            if v>=first and v<=last  ] for l_idx_cell_cycle_start, first, last \
            in zip(ll_idx_cell_cycle_start, l_first, l_last)]
    ll_idx_peak = [ [v for v in l_idx_peak if v>=first and v<=last  ] \
            for l_idx_peak, first, last in zip(ll_idx_peak, l_first, l_last)]
    """""" """""" """""" """ PLOT FITS WITH """ """""" """""" ""
    idx2 = 0
    vmax = 0
    imax = 0
    zp = zip(enumerate(ll_signal), ll_area, ll_obs_phi,
             ll_idx_cell_cycle_start, ll_idx_peak)
    for (idx,
         signal), area, l_obs_phi, l_idx_cell_cycle_start, l_idx_peak in zp:
        plt_result = PlotResults(l_gamma_div_2[idx],
                                 l_var,
                                 signal_model,
                                 signal,
                                 waveform=W,
                                 logP=l_logP_div_2[idx],
                                 temperature=temperature,
                                 cell=cell)
        E_model1, E_theta, E_A, E_B = plt_result.plotEverythingEsperance(
            True, idx2)
        plt_result = PlotResults(l_gamma_div_1[idx],
                                 l_var,
                                 signal_model,
                                 signal,
                                 waveform=W,
                                 logP=l_logP_div_1[idx],
                                 temperature=temperature,
                                 cell=cell)
        E_model2, E_theta, E_A, E_B = plt_result.plotEverythingEsperance(
            True, idx2 + 1)
        vcur = np.sum(np.abs(np.array(E_model1)\
                                            -np.array(E_model2)))/len(E_model1)
        if vcur > vmax:
            vmax = vcur
            imax = idx
        idx2 += 2
    print(vmax, imax)
Esempio n. 4
0
l_gamma_div, l_logP_div = hmm.run(project=False)
"""""" """""" """""" """ CROP SIGNALS FOR PLOTTING """ """""" """""" ""
l_first = [[it for it, obj in enumerate(l_obs_phi) if obj!=-1][0] \
                                                    for l_obs_phi in ll_obs_phi]
l_last = [[len(l_obs_phi)-it-1 for it, obj in enumerate(l_obs_phi[::-1]) \
                                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
ll_signal = [l_signal[first:last+1] for l_signal, first, last \
                                            in zip(ll_signal, l_first, l_last)]
ll_obs_phi = [l_obs_phi[first:last+1] for l_obs_phi, first, last \
                                        in zip(ll_obs_phi, l_first, l_last)]
ll_idx_cell_cycle_start = [ [v for v in l_idx_cell_cycle_start \
                            if v>=first and v<=last  ] \
                            for l_idx_cell_cycle_start, first, last \
                            in zip(ll_idx_cell_cycle_start, l_first, l_last)]
"""""" """""" """""" """ COMPUTE CIRCADIAN SPEED """ """""" """""" ""
zp = zip(l_idx_trace, ll_signal, l_gamma_div, l_logP_div, ll_obs_phi,
         ll_idx_cell_cycle_start)
for idx, signal, gamma, logP, l_obs_phi, l_idx_cell_cycle_start in zp:
    plt_result = PlotResults(gamma,
                             l_var,
                             signal_model,
                             signal,
                             waveform=W,
                             logP=logP)
    l_E_model, l_E_theta, l_E_A, l_E_B = \
                                  plt_result.plotEverythingEsperance(False, idx)
    with open('../Results/H2B/' + str(idx) + '.txt', 'w') as f:
        f.write('PhaseTheta' + '\t' + 'PhasePhi' + '\n')
        for E_theta, phi in zip(l_E_theta, l_obs_phi):
            f.write(str(E_theta * 2 * np.pi) + '\t' + str(phi) + '\n')
        hmm=HMM_SemiCoupled(l_var, ll_signal, sigma_em_circadian,
                            ll_val_phi = [], waveform = W , pi = pi,
                            crop = False )
        (l_gamma_0_temp, l_gamma_temp ,l_logP_temp, ll_alpha, ll_beta, l_E,
            ll_cnorm, ll_idx_phi_hmm_temp, ll_signal_hmm_temp,
            ll_nan_circadian_factor_hmm_temp) = hmm.run_em()

        ll_mat_TR = [np.array( [theta_var_coupled.TR_no_coupling \
                        for i in gamma_temp]) for gamma_temp in l_gamma_temp ]


        """ PLOT TRACE EXAMPLE """
        zp = zip(enumerate(ll_signal_hmm_temp),l_gamma_temp, l_logP_temp)
        for (idx, signal), gamma, logP in zp:
            plt_result = PlotResults(gamma, l_var, signal_model, signal,
                                    waveform = W, logP = None,
                                    temperature = temperature, cell = cell)
            plt_result.plotEverythingEsperance(False, idx)
            if idx==0:
                break

        l_jP_phase_temp, l_jP_amplitude_temp,l_jP_background_temp \
            = EM.compute_jP_by_block(ll_alpha, l_E, ll_beta, ll_mat_TR,
                                    amplitude_var.TR, background_var.TR,
                                    N_theta, N_amplitude_theta,
                                    N_background_theta)
        l_jP_phase.extend(l_jP_phase_temp)
        l_jP_amplitude.extend(l_jP_amplitude_temp)
        l_jP_background.extend(l_jP_background_temp)
        l_gamma.extend(l_gamma_temp)
        l_logP.extend(l_logP_temp)
Esempio n. 6
0
def phase_delay(cell='NIH3T3', temperature=37, nb_traces=500, size_block=100):
    """
    Compute and plot how phase-delay evolves with the difference of intrinsic
    periods between the two oscillators.

    Parameters
    ----------
    cell : string
        Cell condition.
    temperature : integer
        Temperature condition.
    nb_traces : integer
        How many traces to run the experiment on.
    size_block : integer
        Size of the traces chunks (to save memory).
    """

    l_delay = []
    l_std_delay = []
    l_T = [14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34]
    l_real_T = []
    l_real_std_T = []
    #l_T = [20,22,24]
    for period in l_T:
        ##################### LOAD OPTIMIZED PARAMETERS ##################
        path = '../Parameters/Real/opt_parameters_div_'+str(temperature)\
                +"_"+cell+'.p'
        with open(path, 'rb') as f:
            l_parameters = [
                dt, sigma_em_circadian, W, pi, N_theta, std_theta,
                period_theta, l_boundaries_theta, w_theta, N_phi, std_phi,
                period_phi, l_boundaries_phi, w_phi, N_amplitude_theta,
                mu_amplitude_theta, std_amplitude_theta, gamma_amplitude_theta,
                l_boundaries_amplitude_theta, N_background_theta,
                mu_background_theta, std_background_theta,
                gamma_background_theta, l_boundaries_background_theta, F
            ] = pickle.load(f)

        ##################### DISPLAY PARAMETERS ##################
        display_parameters_from_file(path, show=True)

        ##################### LOAD DATA ##################
        if cell == 'NIH3T3':
            path = "../Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p"
        else:
            path = "../Data/U2OS-2017-03-20/ALL_TRACES_INFORMATION_march_2017.p"
        dataClass = LoadData(path,
                             nb_traces,
                             temperature=temperature,
                             division=True,
                             several_cell_cycles=False,
                             remove_odd_traces=True)
        (ll_area, ll_signal, ll_nan_circadian_factor, ll_obs_phi, ll_peak,
        ll_idx_cell_cycle_start, T_theta, std_T_theta, T_phi, std_T_phi) = \
                    dataClass.load(period_phi = period, load_annotation = True)
        l_real_T.append(24 - T_phi)
        l_real_std_T.append(std_T_phi)
        ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] \
                                                        for l_peak in ll_peak]
        print(len(ll_signal), " traces kept")

        ##################### CREATE HIDDEN VARIABLES ##################
        theta_var_coupled, amplitude_var, background_var = \
                            create_hidden_variables(l_parameters = l_parameters)
        l_var = [theta_var_coupled, amplitude_var, background_var]

        ##################### CREATE AND RUN HMM ##################
        hmm = HMM_SemiCoupled(l_var,
                              ll_signal,
                              sigma_em_circadian,
                              ll_obs_phi,
                              waveform=W,
                              ll_nan_factor=ll_nan_circadian_factor,
                              pi=pi,
                              crop=True)
        l_gamma_div, l_logP_div = hmm.run(project=False)

        ##################### REMOVE BAD TRACES #####################
        Plim = np.percentile(l_logP_div, 10)
        idx_to_keep = [i for i, logP in enumerate(l_logP_div) if logP > Plim]
        l_gamma_div = [l_gamma_div[i] for i in idx_to_keep]
        ll_signal = [ll_signal[i] for i in idx_to_keep]
        ll_area = [ll_area[i] for i in idx_to_keep]
        l_logP_div = [l_logP_div[i] for i in idx_to_keep]
        ll_obs_phi = [ll_obs_phi[i] for i in idx_to_keep]
        ll_idx_cell_cycle_start = [ll_idx_cell_cycle_start[i] \
                                                           for i in idx_to_keep]
        ll_idx_peak = [ll_idx_peak[i] for i in idx_to_keep]
        print("Kept traces with div: ", len(idx_to_keep))

        ##################### CROP SIGNALS FOR PLOTTING ##################
        l_first = [[it for it, obj in enumerate(l_obs_phi) if obj!=-1][0] \
                                                    for l_obs_phi in ll_obs_phi]
        l_last = [[len(l_obs_phi)-it-1 for it, obj in enumerate(l_obs_phi[::-1])\
                                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
        ll_signal = [l_signal[first:last+1] for l_signal, first, last \
                                            in zip(ll_signal, l_first, l_last)]
        ll_area = [l_area[first:last+1] for l_area, first, last \
                                              in zip(ll_area, l_first, l_last)]
        ll_obs_phi = [l_obs_phi[first:last+1] for l_obs_phi, first, last \
                                            in zip(ll_obs_phi, l_first, l_last)]
        ll_idx_cell_cycle_start = [ [v for v in l_idx_cell_cycle_start \
            if v>=first and v<=last  ] for l_idx_cell_cycle_start, first, last \
            in zip(ll_idx_cell_cycle_start, l_first, l_last)]
        ll_idx_peak = [ [v for v in l_idx_peak if v>=first and v<=last  ] \
            for l_idx_peak, first, last in zip(ll_idx_peak, l_first, l_last)]

        ##################### COMPUTE PHASE DELAY ##################
        l_phase_delay = []
        zp = zip(enumerate(ll_signal), l_gamma_div, l_logP_div, ll_area,
                 ll_obs_phi, ll_idx_cell_cycle_start, ll_idx_peak)
        for ((idx, signal), gamma, logP, area, l_obs_phi,
             l_idx_cell_cycle_start, l_idx_peak) in zp:
            plt_result = PlotResults(gamma,
                                     l_var,
                                     signal_model,
                                     signal,
                                     waveform=W,
                                     logP=logP,
                                     temperature=temperature,
                                     cell=cell)
            l_E_model, l_E_theta, l_E_A, l_E_B \
                            = plt_result.plotEverythingEsperance(False, idx)
            for phi, theta_unorm in zip(l_obs_phi, l_E_theta):
                theta = theta_unorm * 2 * np.pi
                if theta - phi > -np.pi and theta - phi < np.pi:
                    delay = theta - phi
                elif theta - phi < -np.pi:
                    delay = theta + 2 * np.pi - phi
                else:
                    delay = theta - phi - 2 * np.pi

                l_phase_delay.append(delay)
        l_delay.append(np.mean(l_phase_delay))
        l_std_delay.append(np.std(l_phase_delay))

    plt.errorbar(l_real_T,
                 l_delay,
                 xerr=l_real_std_T,
                 yerr=l_std_delay,
                 fmt='o')
    plt.xlabel(r'$T_\theta-T_\phi$')
    plt.ylabel("<" + r'$\theta$-$\phi$' + ">")
    plt.savefig('../Results/PhaseBehavior/PhaseDelay_'+str(temperature)\
                                                            +"_" + cell+'.pdf')
    plt.show()
    plt.close()
detSim = DetSim(l_parameters, cell, temperature)
l_theta, l_phi = detSim.plot_trajectory(ti=2500,
                                        tf=3000,
                                        rand=True,
                                        save=False)
print(T_phi)
"""""" """""" """""" """ PLOT FITS """ """""" """""" ""
ll_esp_theta = []
zp = zip(enumerate(ll_signal), l_gamma_div, l_logP_div, ll_area, ll_obs_phi,
         ll_idx_cell_cycle_start, ll_idx_peak)
for ((idx, signal), gamma, logP, area, l_obs_phi, l_idx_cell_cycle_start,
     l_idx_peak) in zp:
    plt_result = PlotResults(gamma,
                             l_var,
                             signal_model,
                             signal,
                             waveform=W,
                             logP=logP,
                             temperature=temperature,
                             cell=cell)
    E_model, E_theta, E_A, E_B \
        = plt_result.plotEverythingEsperance(True, idx,
                                             l_obs_phi = ll_obs_phi[idx])
    ll_esp_theta.append(E_theta)
    if idx < 20:
        plt_result.plot3D(save=True, index_trace=idx)
        plt_result.plotEsperancePhaseSpace(np.array(ll_idx_obs_phi[idx]) /
                                           N_phi * (2 * np.pi),
                                           save=True,
                                           index_trace=idx,
                                           tag=str(period),
                                           attractor=(l_theta, l_phi))
def test_sigma_theta(cell='NIH3T3',
                     temperature=37,
                     nb_traces=500,
                     size_block=100):
    """
    Compute how the diffusion coefficient (inferred) evolves with the phase.

    Parameters
    ----------
    cell : string
        Cell condition.
    temperature : integer
        Temperature condition.
    nb_traces : integer
        How many traces to run the experiment on.
    size_block : integer
        Size of the traces chunks (to save memory).
    """
    ##################### LOAD OPTIMIZED PARAMETERS ##################
    path = '../Parameters/Real/opt_parameters_div_'+str(temperature)+"_"\
                                                                    +cell+'.p'
    with open(path, 'rb') as f:
        l_parameters = [
            dt, sigma_em_circadian, W, pi, N_theta, std_theta, period_theta,
            l_boundaries_theta, w_theta, N_phi, std_phi, period_phi,
            l_boundaries_phi, w_phi, N_amplitude_theta, mu_amplitude_theta,
            std_amplitude_theta, gamma_amplitude_theta,
            l_boundaries_amplitude_theta, N_background_theta,
            mu_background_theta, std_background_theta, gamma_background_theta,
            l_boundaries_background_theta, F
        ] = pickle.load(f)

    ##################### DISPLAY PARAMETERS ##################
    display_parameters_from_file(path, show=False)

    ##################### LOAD DATA ##################
    if cell == 'NIH3T3':
        path = "../Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p"
    else:
        path = "../Data/U2OS-2017-03-20/ALL_TRACES_INFORMATION_march_2017.p"

    dataClass = LoadData(path,
                         nb_traces,
                         temperature=None,
                         division=True,
                         several_cell_cycles=False,
                         remove_odd_traces=True)
    (ll_area_tot_flat, ll_signal_tot_flat, ll_nan_circadian_factor_tot_flat,
     ll_obs_phi_tot_flat, T_theta, T_phi) = dataClass.load()
    print(len(ll_signal_tot_flat), " traces kept")

    ##################### SPECIFY F OPTIMIZATION CONDITIONS ##################
    #makes algorithm go faster
    only_F_and_pi = True

    ##################### CREATE HIDDEN VARIABLES ##################
    theta_var_coupled, amplitude_var, background_var \
                        = create_hidden_variables(l_parameters = l_parameters)

    ##################### CREATE BLOCK OF TRACES ##################
    ll_area_tot = []
    ll_signal_tot = []
    ll_nan_circadian_factor_tot = []
    ll_obs_phi_tot = []
    first = True
    zp = enumerate(
        zip(ll_area_tot_flat, ll_signal_tot_flat,
            ll_nan_circadian_factor_tot_flat, ll_obs_phi_tot_flat))
    for index, (l_area, l_signal, l_nan_circadian_factor, l_obs_phi) in zp:
        if index % size_block == 0:
            if not first:
                ll_area_tot.append(ll_area)
                ll_signal_tot.append(ll_signal)
                ll_nan_circadian_factor_tot.append(ll_nan_circadian_factor)
                ll_obs_phi_tot.append(ll_obs_phi)

            else:
                first = False
            ll_area = [l_area]
            ll_signal = [l_signal]
            ll_nan_circadian_factor = [l_nan_circadian_factor]
            ll_obs_phi = [l_obs_phi]
        else:
            ll_area.append(l_area)
            ll_signal.append(l_signal)
            ll_nan_circadian_factor.append(l_nan_circadian_factor)
            ll_obs_phi.append(l_obs_phi)
    #get remaining trace
    ll_area_tot.append(ll_area)
    ll_signal_tot.append(ll_signal)
    ll_nan_circadian_factor_tot.append(ll_nan_circadian_factor)
    ll_obs_phi_tot.append(ll_obs_phi)

    ##################### OPTIMIZATION ##################

    l_jP_phase = []
    l_jP_amplitude = []
    l_jP_background = []
    l_gamma = []
    l_gamma_0 = []
    l_logP = []
    ll_signal_hmm = []
    ll_idx_phi_hmm = []
    ll_nan_circadian_factor_hmm = []

    for ll_signal, ll_obs_phi, ll_nan_circadian_factor in zip(
            ll_signal_tot, ll_obs_phi_tot, ll_nan_circadian_factor_tot):

        ### INITIALIZE AND RUN HMM ###
        l_var = [theta_var_coupled, amplitude_var, background_var]
        hmm = HMM_SemiCoupled(l_var,
                              ll_signal,
                              sigma_em_circadian,
                              ll_val_phi=ll_obs_phi,
                              waveform=W,
                              ll_nan_factor=ll_nan_circadian_factor,
                              pi=pi,
                              crop=True)
        (l_gamma_0_temp, l_gamma_temp, l_logP_temp, ll_alpha, ll_beta, l_E,
         ll_cnorm, ll_idx_phi_hmm_temp, ll_signal_hmm_temp,
         ll_nan_circadian_factor_hmm_temp) = hmm.run_em()

        #crop and create ll_mat_TR
        ll_signal_hmm_cropped_temp =[[s for s, idx in zip(l_s, l_idx) if idx>-1]\
                for l_s, l_idx in  zip(ll_signal_hmm_temp ,ll_idx_phi_hmm_temp)]
        ll_idx_phi_hmm_cropped_temp = [ [idx for idx in l_idx if idx>-1] \
                                            for l_idx in  ll_idx_phi_hmm_temp  ]
        ll_mat_TR = [np.array( [theta_var_coupled.TR[:,idx_phi,:] for idx_phi \
            in l_idx_obs_phi]) for l_idx_obs_phi in ll_idx_phi_hmm_cropped_temp]

        ### PLOT TRACE EXAMPLE ###
        zp = zip(enumerate(ll_signal_hmm_cropped_temp), l_gamma_temp,
                 l_logP_temp)
        for (idx, signal), gamma, logP in zp:
            plt_result = PlotResults(gamma,
                                     l_var,
                                     signal_model,
                                     signal,
                                     waveform=W,
                                     logP=None,
                                     temperature=temperature,
                                     cell=cell)
            plt_result.plotEverythingEsperance(False, idx)
            if idx == 0:
                break


        l_jP_phase_temp, l_jP_amplitude_temp,l_jP_background_temp \
            = EM.compute_jP_by_block(ll_alpha, l_E, ll_beta, ll_mat_TR,
                                    amplitude_var.TR, background_var.TR,
                                    N_theta, N_amplitude_theta,
                                    N_background_theta, only_F_and_pi)
        l_jP_phase.extend(l_jP_phase_temp)
        l_jP_amplitude.extend(l_jP_amplitude_temp)
        l_jP_background.extend(l_jP_background_temp)
        l_gamma.extend(l_gamma_temp)
        l_logP.extend(l_logP_temp)
        l_gamma_0.extend(l_gamma_0_temp)
        ll_signal_hmm.extend(ll_signal_hmm_temp)
        ll_idx_phi_hmm.extend(ll_idx_phi_hmm_temp)
        ll_nan_circadian_factor_hmm.extend(ll_nan_circadian_factor_hmm_temp)

    ##################### COMPUTE SIGMA(THETA) ##################
    l_mean = []
    l_std = []
    dic_phase = {}
    theta_domain = theta_var_coupled.domain
    for idx_theta in range(N_theta):
        dic_phase[idx_theta] = []
    for jP, ll_idx_obs_phi_trace in zip(l_jP_phase, ll_idx_phi_hmm):
        for t, jPt in enumerate(jP):
            for idx_theta_i, theta_i in enumerate(theta_domain):
                norm = np.sum(jPt[idx_theta_i])
                if norm == 0:
                    continue
                jPt_i_norm = jPt[idx_theta_i] / norm

                theta_dest = theta_i + (
                    w_theta + F[idx_theta_i, ll_idx_obs_phi_trace[t]]) * dt

                var = 0
                for idx_theta_k, theta_k in enumerate(theta_domain):
                    var+= (min( abs( theta_dest - theta_k ),
                          abs( theta_dest - (theta_k+2*np.pi) ) ,
                          abs( theta_dest - (theta_k-2*np.pi) ) )**2) \
                                                    *jPt_i_norm[idx_theta_k]
                dic_phase[idx_theta_i].append((var, norm))

    for idx_theta in range(N_theta):
        p_theta = np.sum([tupl[1] for tupl in dic_phase[idx_theta]])
        sigma_theta = np.sum( [ (p_theta_t * sigma_theta_t)/p_theta \
                    for (p_theta_t, sigma_theta_t) in dic_phase[idx_theta] ])
        var_sigma_theta =  np.sum( [ (sigma_theta_t-sigma_theta)**2*p_theta_t \
                                                                      /p_theta \
                     for (p_theta_t, sigma_theta_t) in dic_phase[idx_theta] ])
        l_mean.append(sigma_theta)
        l_std.append(var_sigma_theta**0.5)

    plt.errorbar(theta_domain / (2 * np.pi), l_mean, yerr=l_std, fmt='o')
    plt.xlabel(r"Circadian phase $\theta$")
    plt.ylabel(r"Phase diffusion SD[$\theta$]")
    plt.tight_layout()
    plt.savefig('../Results/Correlation/Diffusion_'+cell+'_'\
                +str(temperature)+'.pdf')
    plt.show()
    plt.close()
Esempio n. 9
0
def test_gamma(cell = 'NIH3T3', temperature = 37, nb_trace = 500,
                size_block = 100, nb_iter = 15):

    ##################### LOAD COLORMAP ##################
    c = mcolors.ColorConverter().to_rgb
    bwr = (  [c('blue'), c('white'), 0.48, c('white'),
                0.52,c('white'),  c('red')])


    ### GUESS OR ESTIMATE PARAMETERS ON NON-DIVIDING TRACES ###
    wd = os.getcwd()[:-21]
    os.chdir(wd)

    #remove potential previous results
    path = 'Parameters/Real/init_parameters_nodiv_'+str(temperature)\
            +"_"+cell+".p"
    if os.path.isfile(path) :
        os.remove(path)
    #run script
    print("1_wrap_initial_parameters launched")
    os.system("python 1_wrap_initial_parameters.py " + cell + " "\
                + str(temperature) + \
                ' > TextOutputs/1_wrap_initial_parameters_'+cell\
                +'_'+str(temperature)+'.txt')
    #check if final result has been created
    if os.path.isfile(path) :
        print("Initial set of parameters successfully created")
    else:
        print("BUG, initial set of parameters not created")


    for gamma_reg in [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1]:
        print("gamma =", gamma_reg, " started ")
        ### REPLACE GAMMA AND STD OU  AND RECORD AS OPTIMIZED PARAMETERS NODIV###
        gamma_A = gamma_reg
        gamma_B = gamma_reg
        mu_A, std_A, mu_B, std_B = estimate_OU_par(cell, temperature, \
                                        gamma_A = gamma_A, gamma_B = gamma_B)

        path = 'Parameters/Real/init_parameters_nodiv_'+str(temperature)\
                                                                +"_"+cell+".p"
        with open(path, 'rb') as f:
            [dt, sigma_em_circadian, W, pi,
            N_theta, std_theta, period_theta, l_boundaries_theta, w_theta,
            N_phi, std_phi, period_phi, l_boundaries_phi, w_phi,
            N_amplitude_theta, mu_amplitude_theta, std_amplitude_theta,
            gamma_amplitude_theta, l_boundaries_amplitude_theta,
            N_background_theta, mu_background_theta, std_background_theta,
            gamma_background_theta, l_boundaries_background_theta,
            F] = pickle.load(f)

        gamma_amplitude_theta = gamma_A
        gamma_background_theta = gamma_B
        std_amplitude_theta = std_A
        std_background_theta = std_B

        l_parameters = [dt, sigma_em_circadian, W, pi,
        N_theta, std_theta, period_theta, l_boundaries_theta, w_theta,
        N_phi, std_phi, period_phi, l_boundaries_phi, w_phi,
        N_amplitude_theta, mu_amplitude_theta, std_amplitude_theta,
        gamma_amplitude_theta, l_boundaries_amplitude_theta,
        N_background_theta, mu_background_theta, std_background_theta,
        gamma_background_theta, l_boundaries_background_theta,
        F]

        ##################### WRAP PARAMETERS ##################
        path = "Parameters/Real/opt_parameters_nodiv_"+str(temperature)\
                                                                +"_"+cell+".p"
        pickle.dump( l_parameters, open( path, "wb" ) )


        ### COMPUTE WAVEFORM BIAS ###
        #remove potential previous results
        path = "Parameters/Misc/F_no_coupling_"+str(temperature)+"_"+cell+'.p'
        if os.path.isfile(path) :
            os.remove(path)
        #run script
        print("7_bias launched")
        os.system("python 7_validate_inference_dividing.py " + str(nb_iter) \
                    + " "+ str(nb_trace) + " "+  str(size_block) + " "+cell \
                    + " "+ str(temperature) + " True"+ ' > TextOutputs/7_bias_'\
                    +cell+'_'+str(temperature)+'.txt')
        #check if final result has been created
        if os.path.isfile(path) :
            print("Bias successfully computed")
        else:
            print("BUG, bias not computed")

        ### OPTIMIZE COUPLING ON DIVIDING TRACES ###
        #remove potential previous results
        path = "Parameters/Real/opt_parameters_div_"+str(temperature)+"_"\
                                                                    +cell+".p"
        if os.path.isfile(path) :
            os.remove(path)
        #run script
        print("4_optimize_parameters_dividing launched")
        os.system("python 4_optimize_parameters_dividing.py " + str(nb_iter) \
                    + " "+ str(nb_trace) + " "+  str(size_block) + " "+cell \
                    + " "+ str(temperature)\
                    + ' > TextOutputs/4_optimize_parameters_dividing_'+cell\
                    +'_'+str(temperature)+'.txt')
         #check if final result has been created
        if os.path.isfile(path) :
            print("Optimized set of parameters on dividing traces"\
                                                    +" successfully created")
        else:
            print("BUG, optimized set of parameters on"\
                                                +" dividing traces not created")


        ### COMPLUTE/PLOT COUPLING AND PHASE DENSITY ###
        ##################### LOAD OPTIMIZED PARAMETERS ##################
        path = 'Parameters/Real/opt_parameters_div_'+str(temperature)\
                                                                +"_"+cell+'.p'
        with open(path, 'rb') as f:
            l_parameters = [dt, sigma_em_circadian, W, pi,
            N_theta, std_theta, period_theta, l_boundaries_theta, w_theta,
            N_phi, std_phi, period_phi, l_boundaries_phi, w_phi,
            N_amplitude_theta, mu_amplitude_theta, std_amplitude_theta,
            gamma_amplitude_theta, l_boundaries_amplitude_theta,
            N_background_theta, mu_background_theta, std_background_theta,
            gamma_background_theta, l_boundaries_background_theta,
            F] = pickle.load(f)
        period = None

        ##################### LOAD DATA ##################
        if cell == 'NIH3T3':
            path =  "Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p"
        else:
            path = "Data/U2OS-2017-03-20/ALL_TRACES_INFORMATION_march_2017.p"
        dataClass=LoadData(path, nb_trace, temperature = temperature,
                            division = True, several_cell_cycles = False,
                            remove_odd_traces = True)
        if period is None:
            (ll_area, ll_signal, ll_nan_circadian_factor, ll_obs_phi, ll_peak,
            ll_idx_cell_cycle_start, T_theta, T_phi) \
                = dataClass.load(period_phi = period, load_annotation = True)
        else:
            (ll_area, ll_signal, ll_nan_circadian_factor, ll_obs_phi, ll_peak,
            ll_idx_cell_cycle_start, T_theta, std_T_theta, T_phi, std_T_phi) \
                = dataClass.load(period_phi = period, load_annotation = True)

        ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] for \
                                                            l_peak in ll_peak]
        print(len(ll_signal), " traces kept")

        ##################### CREATE HIDDEN VARIABLES ##################
        theta_var_coupled, amplitude_var, background_var = \
                            create_hidden_variables(l_parameters = l_parameters)
        l_var = [theta_var_coupled, amplitude_var, background_var]

        ##################### CREATE AND RUN HMM ##################
        hmm=HMM_SemiCoupled(l_var, ll_signal, sigma_em_circadian, ll_obs_phi,
                            waveform = W,
                            ll_nan_factor = ll_nan_circadian_factor,
                            pi = pi, crop = True )
        l_gamma_div, l_logP_div = hmm.run(project = False)

        ##################### REMOVE BAD TRACES #####################
        Plim = np.percentile(l_logP_div, 10)
        idx_to_keep = [i for i, logP in enumerate(l_logP_div) if logP>Plim ]
        l_gamma_div = [l_gamma_div[i] for i in idx_to_keep]
        ll_signal = [ll_signal[i] for i in idx_to_keep]
        ll_area = [ll_area[i] for i in idx_to_keep]
        l_logP_div = [l_logP_div[i] for i in idx_to_keep]
        ll_obs_phi = [ll_obs_phi[i] for i in idx_to_keep]
        ll_idx_cell_cycle_start = [ll_idx_cell_cycle_start[i] \
                                                        for i in idx_to_keep]
        ll_idx_peak = [ll_idx_peak[i] for i in idx_to_keep]
        print("Kept traces with div: ", len(idx_to_keep))


        ##################### CROP SIGNALS FOR PLOTTING ##################
        l_first = [[it for it, obj in enumerate(l_obs_phi) \
                    if obj!=-1][0] for l_obs_phi in ll_obs_phi]
        l_last = [[len(l_obs_phi)-it-1 for it, obj \
                    in enumerate(l_obs_phi[::-1]) if obj!=-1][0] \
                    for l_obs_phi in ll_obs_phi]
        ll_signal = [l_signal[first:last+1] for l_signal, first, last \
                                            in zip(ll_signal, l_first, l_last)]
        ll_area = [l_area[first:last+1] for l_area, first, last \
                                              in zip(ll_area, l_first, l_last)]
        ll_obs_phi = [l_obs_phi[first:last+1] for l_obs_phi, first, last \
                                        in zip(ll_obs_phi, l_first, l_last)]
        ll_idx_cell_cycle_start = [ [v for v in l_idx_cell_cycle_start \
            if v>=first and v<=last  ] for l_idx_cell_cycle_start, first, last \
            in zip(ll_idx_cell_cycle_start, l_first, l_last)]
        ll_idx_peak = [ [v for v in l_idx_peak if v>=first and v<=last  ] \
            for l_idx_peak, first, last in zip(ll_idx_peak, l_first, l_last)]


        ##################### CREATE ll_idx_obs_phi and ll_val_phi###############
        ll_idx_obs_phi = []
        for l_obs in ll_obs_phi:
            l_idx_obs_phi = []
            for obs in l_obs:
                l_idx_obs_phi.append( int(round(obs/(2*np.pi) *\
                                    len(theta_var_coupled.codomain )))\
                                    %len(theta_var_coupled.codomain )   )
            ll_idx_obs_phi.append(l_idx_obs_phi)


        ##################### PLOT FITS ##################
        zp = zip(enumerate(ll_signal),l_gamma_div, l_logP_div, ll_area,
            ll_obs_phi, ll_idx_cell_cycle_start, ll_idx_peak)
        for ((idx, signal), gamma, logP, area, l_obs_phi,
            l_idx_cell_cycle_start, l_idx_peak) in zp:
            plt_result = PlotResults(gamma, l_var, signal_model, signal,
                                    waveform = W, logP = logP,
                                    temperature = temperature, cell = cell)
            E_model, E_theta, E_A, E_B = \
                                plt_result.plotEverythingEsperance( True, idx)

        ##################### PLOT COUPLING AND PHASE SPACE DENSITY ############
        #plot phase density
        plot_phase_space_density(l_var, l_gamma_div, ll_idx_obs_phi,
                                F_superimpose = F, save = True, cmap = bwr,
                                temperature = temperature, cell = cell,
                                period = gamma_reg,
                                folder = 'Results/TestGamma' )

        #plot coupling
        plt.imshow(F.T, cmap=bwr, vmin=-0.3, vmax=0.3,
                    interpolation='nearest', origin='lower',
                    extent=[0, 2*np.pi,0, 2*np.pi])
        plt.colorbar()
        plt.xlabel(r'$\theta$')
        plt.ylabel(r'$\phi$')
        plt.title('Coupling Function')
        plt.savefig("Results/TestGamma/Coupling_"+str(temperature)+"_"\
                    +cell+'_'+str(gamma_reg)+'.pdf')
        plt.close()