def test_potential(cell='NIH3T3', temperature=37): """ Compute the potential associated to the coupling function in the system of coupled oscillators. Parameters ---------- cell : string Cell condition. temperature : integer Temperature condition. """ ##################### 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) ##################### CREATE HIDDEN VARIABLES ################## theta_var_coupled, amplitude_var, background_var \ = create_hidden_variables(l_parameters = l_parameters ) ##################### COMPUTE POTENTIAL ################## Fbis = F + w_theta F_temp = np.hstack((Fbis, Fbis, Fbis)) F_temp = np.vstack((F_temp, F_temp, F_temp)) f = interpolate.interp2d(np.linspace(-2 * np.pi, 4 * np.pi, 3 * F.shape[0], endpoint=False), np.linspace(-2 * np.pi, 4 * np.pi, 3 * F.shape[1], endpoint=False), F_temp.T, kind='cubic', bounds_error=True) U = np.zeros((N_theta, N_phi)) for idx_theta in range(N_theta): for idx_phi in range(N_phi): print(idx_theta, idx_phi) U[idx_theta, idx_phi] = dblquad( f, 0, theta_var_coupled.domain[idx_theta], lambda x: 0, lambda x: theta_var_coupled.codomain[idx_phi])[0] path = "../Results/Potential/U_" + str(temperature) + "_" + cell + '.p' pickle.dump(U, open(path, "wb")) plt.figure(figsize=(5 * 1.2, 5 * 1.2)) ax = plt.gca() im = plt.imshow(U.T, cmap='bwr', vmin=-0.3, vmax=0.3, interpolation='spline16', origin='lower') #add_colorbar(im, label = r'Acceleration ($rad.h^{-1}$)') plt.xlabel(r'Circadian phase $\theta$') plt.ylabel(r'Cell-cycle phase $\phi$') plt.tight_layout() plt.savefig("../Results/Potential/U_" + str(temperature) + "_" + cell + '.pdf') plt.show() plt.close()
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) pi = None #to start with distributions at equilibrium W_init = copy.copy(W) #save to make a plot in the end """""" """""" """""" """ DISPLAY PARAMETERS """ """""" """""" "" display_parameters_from_file(path, show=True) """""" """""" """""" """ 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] """""" """""" """""" """ COMPUTE NB_TRACE_MAX """ """""" """""" "" if cell == 'NIH3T3': path = "Data/NIH3T3.ALL.2017-04-04/ALL_TRACES_INFORMATION.p" dataClass = LoadData(path, nb_traces, temperature=temperature, division=False) else: path = "Data/U2OS-2017-03-20/ALL_TRACES_INFORMATION_march_2017.p" dataClass = LoadData(path, nb_traces,
#amplitude parameters N_amplitude_theta = 30 mu_amplitude_theta = mu_A std_amplitude_theta = std_A l_boundaries_amplitude_theta = (mu_amplitude_theta - 5 * std_amplitude_theta, mu_amplitude_theta + 5 * std_amplitude_theta) #background parameters N_background_theta = 30 mu_background_theta = mu_B std_background_theta = std_B l_boundaries_background_theta = (mu_background_theta - 5 * std_background_theta, mu_background_theta + 5 * std_background_theta) """""" """""" """""" """ WRAP PARAMETERS """ """""" """""" "" path = 'Parameters/Real/init_parameters_nodiv_' + str( temperature) + "_" + cell + ".p" with open(path, 'wb') as f: pickle.dump([ 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 ], f) """""" """""" """""" """ DISPLAY PARAMETERS """ """""" """""" "" display_parameters_from_file(path, show=False)
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')
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)
def F_by_cell_cell_cycle_period(cell = 'NIH3T3', nb_traces = 10000, size_block = 100, nb_iter = 15): """ Optimize and plot the coupling function and phase-space density at different temperatures, but for a fixed distribution of cell-cycle periods. Parameters ---------- cell : string Cell condition. nb_traces : int How many traces to run the experiment on. size_block : integer Size of the traces chunks (to save memory). nb_iter : int Number of EM iterations. """ temperature = None ##################### LOAD COLORMAP ################## c = mcolors.ColorConverter().to_rgb bwr = make_colormap( [c('blue'), c('white'), 0.48, c('white'), 0.52,c('white'), c('red')]) ##################### LOAD OPTIMIZED PARAMETERS ################## path = '../Parameters/Real/opt_parameters_nodiv_'+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 TRACES ################## dic_traces = {34:{}, 37:{}, 40:{}} cell_cycle_space = list(range(12,40)) for T_cell_cycle in cell_cycle_space: for temperature in [34,37,40]: ##################### 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_tot_flat, ll_signal_tot_flat, ll_nan_circadian_factor_tot_flat, ll_obs_phi_tot_flat, ll_peak, ll_idx_cell_cycle_start, T_theta, std_T_theta, T_phi, std_T_phi)\ = dataClass.load(period_phi = T_cell_cycle, load_annotation = True, force_temperature = True) dic_traces[temperature][T_cell_cycle] = [ll_area_tot_flat, ll_signal_tot_flat, ll_nan_circadian_factor_tot_flat, ll_obs_phi_tot_flat, ll_peak, ll_idx_cell_cycle_start, T_theta, std_T_theta, T_phi, std_T_phi] #check that there is the same number of traces for a given temperature and #given cell-cycle for T_cell_cycle in cell_cycle_space: max_nb = min(len(dic_traces[34][T_cell_cycle][0]), len(dic_traces[37][T_cell_cycle][0]), len(dic_traces[40][T_cell_cycle][0])) dic_traces[34][T_cell_cycle] = [l[:max_nb] \ if type(l)==list else l for l in dic_traces[34][T_cell_cycle]] dic_traces[37][T_cell_cycle] = [l[:max_nb] \ if type(l)==list else l for l in dic_traces[37][T_cell_cycle]] dic_traces[40][T_cell_cycle] = [l[:max_nb] \ if type(l)==list else l for l in dic_traces[40][T_cell_cycle]] print(len(dic_traces[34][T_cell_cycle][0]), len(dic_traces[37][T_cell_cycle][0]), len(dic_traces[40][T_cell_cycle][0])) #merge all traces for a given temperature for temperature in [34,37,40]: print("Temperature : ", temperature) (ll_area_tot_flat, ll_signal_tot_flat, ll_nan_circadian_factor_tot_flat, ll_obs_phi_tot_flat, ll_peak, ll_idx_cell_cycle_start) \ = [], [], [], [], [], [] for T_cell_cycle in cell_cycle_space: ll_area_tot_flat.extend(dic_traces[temperature][T_cell_cycle][0]) ll_signal_tot_flat.extend(dic_traces[temperature][T_cell_cycle][1]) ll_nan_circadian_factor_tot_flat.extend( dic_traces[temperature][T_cell_cycle][2]) ll_obs_phi_tot_flat.extend(dic_traces[temperature][T_cell_cycle][3]) ll_peak.extend(dic_traces[temperature][T_cell_cycle][4]) ll_idx_cell_cycle_start.extend( dic_traces[temperature][T_cell_cycle][5]) ##################### SPECIFY F OPTIMIZATION CONDITIONS ################## #makes algorithm go faster only_F_and_pi = True #we don't know the inital condiion when traces divide pi = None #we start with a random empty F F = (np.random.rand( N_theta, N_phi)-0.5)*0.01 #regularization lambda_parameter = 2*10e-6 lambda_2_parameter = 0.005 ##################### CORRECT INFERENCE BIAS ################## try: F_no_coupling = pickle.load(open("../Parameters/Misc/F_no_coupling_"\ +str(37)+"_"+cell+'.p', "rb" ) ) for idx_theta in range(N_theta): F_no_coupling[idx_theta,:] = np.mean(F_no_coupling[idx_theta,:]) except: print("F_no_coupling not found, no bias correction applied") F_no_coupling = None ##################### CORRECT PARAMETERS ACCORDINGLY ################## 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] ##################### 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 ################## def buildObsPhiFromIndex(ll_index): ll_obs_phi = [] for l_index in ll_index: l_obs_phi = [] for index in l_index: if index==-1: l_obs_phi.append(-1) else: l_obs_phi.append(index/N_theta*2*np.pi) ll_obs_phi.append(l_obs_phi) return ll_obs_phi lP=0 l_lP=[] l_idx_to_remove = [] for it in range(nb_iter): print("Iteration :", it) 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 = [] ### CLEAN TRACES AFTER FIRST ITERATION ### if it==1: ll_signal_tot_flat = ll_signal_hmm_clean ll_idx_phi_tot_flat = ll_idx_phi_hmm_clean ll_nan_circadian_factor_tot_flat = \ ll_nan_circadian_factor_hmm_clean print("nb traces apres 1ere iteration : ", len(ll_signal_tot_flat)) ll_signal_tot = [] ll_idx_phi_tot = [] ll_nan_circadian_factor_tot = [] ll_obs_phi_tot = [] first= True zp = zip(enumerate(ll_signal_tot_flat), ll_idx_phi_tot_flat, ll_nan_circadian_factor_tot_flat) for (index, l_signal), l_idx_phi, l_nan_circadian_factor in zp: if index%size_block==0: if not first: ll_signal_tot.append(ll_signal) ll_idx_phi_tot.append(ll_idx_phi) ll_nan_circadian_factor_tot.append( ll_nan_circadian_factor) ll_obs_phi_tot.append( buildObsPhiFromIndex(ll_idx_phi)) else: first = False ll_signal = [l_signal] ll_idx_phi = [l_idx_phi] ll_nan_circadian_factor = [l_nan_circadian_factor] else: ll_signal.append(l_signal) ll_idx_phi.append(l_idx_phi) ll_nan_circadian_factor.append(l_nan_circadian_factor) #get remaining trace ll_signal_tot.append(ll_signal) ll_idx_phi_tot.append(ll_idx_phi) ll_nan_circadian_factor_tot.append(ll_nan_circadian_factor) ll_obs_phi_tot.append( buildObsPhiFromIndex(ll_idx_phi) ) zp = zip(ll_signal_tot, ll_obs_phi_tot, ll_nan_circadian_factor_tot) for ll_signal, ll_obs_phi, ll_nan_circadian_factor in zp: ### 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] 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) t_l_jP = (l_jP_phase, l_jP_amplitude,l_jP_background) ### REMOVE BAD TRACES IF FIRST ITERATION### if it==0: Plim = np.percentile(l_logP, 10) #Plim = -10000 for idx_trace, P in enumerate(l_logP): if P<=Plim: l_idx_to_remove.append(idx_trace) for index in sorted(l_idx_to_remove, reverse=True): del t_l_jP[0][index] del t_l_jP[1][index] del t_l_jP[2][index] del l_gamma[index] del l_logP[index] del l_gamma_0[index] del ll_signal_hmm[index] del ll_idx_phi_hmm[index] del ll_nan_circadian_factor_hmm[index] ll_signal_hmm_clean = copy.deepcopy(ll_signal_hmm) ll_idx_phi_hmm_clean = copy.deepcopy(ll_idx_phi_hmm) ll_nan_circadian_factor_hmm_clean \ = copy.deepcopy(ll_nan_circadian_factor_hmm) ### PARAMETERS UPDATE ### [F_up, pi_up, std_theta_up, sigma_em_circadian_up, ll_coef, std_amplitude_theta_up, std_background_theta_up, mu_amplitude_theta_up, mu_background_theta_up, W_up] = EM.run_EM(l_gamma_0, l_gamma, t_l_jP, theta_var_coupled, ll_idx_phi_hmm, F, ll_signal_hmm, amplitude_var, background_var, W, ll_idx_coef = F_no_coupling, only_F_and_pi = only_F_and_pi, lambd_parameter = lambda_parameter, lambd_2_parameter = lambda_2_parameter) if np.mean(l_logP)-lP<10**-9: print("diff:", np.mean(l_logP)-lP) print(print("average lopP:", np.mean(l_logP))) break else: lP = np.mean(l_logP) print("average lopP:", lP) l_lP.append(lP) ### CHOOSE NEW PARAMETERS ### F = F_up pi_up = pi 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] theta_var_coupled, amplitude_var, background_var = \ create_hidden_variables(l_parameters = l_parameters) ### PLOT COUPLING FUNCTION ### plt.pcolormesh(theta_var_coupled.domain, theta_var_coupled.codomain, F.T, cmap=bwr, vmin=-0.3, vmax=0.3) plt.xlim([0, 2*np.pi]) plt.ylim([0, 2*np.pi]) plt.colorbar() plt.xlabel("theta") plt.ylabel("phi") plt.show() plt.close() plt.plot(l_lP) #plt.savefig("../Parameters/Real/opt_parameters_div_"+str(temperature) #+"_"+cell+'_'+str(T_cell_cycle)+'.pdf') plt.show() plt.close() ##################### PLOT FINAL COUPLING ################## def add_colorbar(im, aspect=20, pad_fraction=0.5, **kwargs): ###Add a vertical color bar to an image plot.### divider = axes_grid1.make_axes_locatable(im.axes) width = axes_grid1.axes_size.AxesY(im.axes, aspect=1./aspect) pad = axes_grid1.axes_size.Fraction(pad_fraction, width) current_ax = plt.gca() cax = divider.append_axes("right", size=width, pad=pad) plt.sca(current_ax) return im.axes.figure.colorbar(im, cax=cax, **kwargs) plt.figure(figsize=(5*1.2,5*1.2)) im = plt.imshow(F.T, cmap=bwr, vmin=-0.3, vmax=0.3, interpolation='spline16', origin='lower', extent=[0, 2*np.pi,0, 2*np.pi]) add_colorbar(im, label = r'Acceleration ($rad.h^{-1}$)') plt.xlabel(r'Circadian phase $\theta$') plt.ylabel(r'Cell-cycle phase $\phi$') #plt.colorbar() plt.xlabel(r'$\theta$') plt.ylabel(r'$\phi$') plt.title('T = ' + str(temperature)) plt.tight_layout() plt.savefig("../Results/PhaseSpace/Coupling_"+str(temperature)\ +"_"+cell+'_demoind.pdf') plt.close() ##################### PLOT PHASE SPACE DENSITY ################## plot_phase_space_density(l_var, l_gamma, ll_idx_phi_tot_flat, F_superimpose = None, save = True, cmap = bwr, temperature = temperature, cell = cell, period = T_cell_cycle, folder = '../Results/PhaseSpace/' ) path = "../Results/PhaseSpace/Coupling_"+str(temperature)+"_"\ +cell+'_demoind.p' with open(path, 'wb') as f: pickle.dump(F, f)
def figure_1(cell='NIH3T3', temperature=37, nb_traces=1000, size_block=100, division=True): """ Create a nice plot to show data and fits. 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). division : bool Plot dividing traces (True) or not (False). """ 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=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=division, several_cell_cycles=False, remove_odd_traces=False) (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) 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=False) l_gamma_div, l_logP_div = hmm.run(project=False) #keep only long traces to_keep = [ i for i, l_signal in enumerate(ll_signal) if len(l_signal) > 100 ] ll_signal = [ll_signal[i][:100] for i in to_keep] ll_area = [ll_area[i][:100] for i in to_keep] if division: ll_nan_circadian_factor =[ll_nan_circadian_factor[i][:100] \ for i in to_keep] ll_obs_phi = [ll_obs_phi[i][:100] for i in to_keep] ll_peak = [ll_peak[i][:100] for i in to_keep] ll_idx_cell_cycle_start = [ [j for j in ll_idx_cell_cycle_start[i] \ if j<100] for i in to_keep] ll_idx_peak = [[idx for idx, v in enumerate(l_peak) if v>0] \ for l_peak in ll_peak] else: ll_nan_circadian_factor = [[np.nan] * 100 for i in to_keep] ll_obs_phi = [[np.nan] * 100 for i in to_keep] ll_peak = [[np.nan] * 100 for i in to_keep] ll_idx_cell_cycle_start = [[np.nan] for i in to_keep] ll_idx_peak = [[np.nan] for l_peak in ll_peak] l_gamma_div = [l_gamma_div[i][:100] for i in to_keep] ##################### PLOT ##################### waveform = interpolate.interp1d( np.linspace(0, 2 * np.pi, len(W), endpoint=True), W) current_palette = sn.color_palette() for (idx, l_signal), l_nan_circadian_factor, l_idx_peak, l_idx_cc, gamma \ in zip(enumerate(ll_signal), ll_nan_circadian_factor, ll_idx_peak, ll_idx_cell_cycle_start, l_gamma_div): if idx % 2 == 0: plt.figure(figsize=(4, 3)) tspan = np.linspace(0, len(l_signal) / 2, len(l_signal)) plt.subplot(210 + idx % 2 + 1) plt.plot(tspan, l_signal, '.', color = current_palette[0] \ if division else current_palette[1]) l_phase = [] l_A = [] l_B = [] l_model = [] for gamma_t in gamma: phase = np.angle( np.sum( np.multiply( np.sum(gamma_t, axis=(1, 2)), np.exp(1j * np.array(theta_var_coupled.domain))))) % ( 2 * np.pi) A = np.sum( np.multiply(np.sum(gamma_t, axis=(0, 2)), amplitude_var.domain)) B = np.sum( np.multiply(np.sum(gamma_t, axis=(0, 1)), background_var.domain)) l_phase.append(phase) l_A.append(A) l_B.append(B) l_model.append(signal_model([phase, A, B], waveform)) #plt.plot(tspan, [phase/(2*np.pi) for phase in l_phase], '--', #color = 'grey') #plt.plot(tspan, l_model, '-', color = current_palette[2]) for d in l_idx_cc: plt.axvline(d / 2, color='black') plt.ylim(-0.1, 1.1) if idx % 2 == 1: plt.tight_layout() plt.savefig('../Results/Fits/GroupedFit_div_'+str(idx)+'_'\ +str(temperature)+"_"+cell+'.pdf' if division \ else '../Results/Fits/GroupedFit_nodiv_'+str(idx)\ +'_'+str(temperature)+"_"+cell+'.pdf') plt.show() plt.close() '''
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()
def deterministic_attractor_by_temperature(cell='NIH3T3'): """ Compute and plot the evolution of the deterministic attractor with temperature. Parameters ---------- cell : string Cell condition. """ lll_theta = [] lll_phi = [] l_temperature = [34, 37, 40] l_color = ['lightblue', 'grey', 'orange'] for idx_t, temperature in enumerate(l_temperature): 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) detSim = DetSim(l_parameters, cell, temperature) ll_phase_theta, ll_phase_phi = detSim.plot_trajectory(ti=800, tf=1600, rand=True, save=False) lll_theta.append(ll_phase_theta) lll_phi.append(ll_phase_phi) plt.figure(figsize=(5, 5)) for idx_t, (ll_phase_theta, ll_phase_phi) in enumerate(zip(lll_theta, lll_phi)): first = True for l_phase_theta, l_phase_phi in zip(ll_phase_theta, ll_phase_phi): if first: plt.plot(np.array(l_phase_theta) / (2 * np.pi), np.array(l_phase_phi) / (2 * np.pi), color=l_color[idx_t], label=str(l_temperature[idx_t])) first = False else: plt.plot(np.array(l_phase_theta) / (2 * np.pi), np.array(l_phase_phi) / (2 * np.pi), color=l_color[idx_t]) #plot identity x_domain = np.linspace(0, 2 * np.pi, 100) f = (lll_phi[1][2][0] - 0.05 + np.linspace(0, 2 * np.pi, 100)) % (2 * np.pi) abs_d_data_x = np.abs(np.diff(f)) mask_x = np.hstack( [abs_d_data_x > abs_d_data_x.mean() + 3 * abs_d_data_x.std(), [False]]) masked_data_x = np.array( [x if not m else np.nan for x, m in zip(f, mask_x)]) #plt.plot(x_domain/(2*np.pi),masked_data_x/(2*np.pi) , '--' ) plt.xlim(0, 1) plt.ylim(0, 1) plt.xlabel(r'Circadian phase $\theta$') plt.ylabel(r'Cell-cycle phase $\phi$') #plt.plot([0,1],[0.22,0.22], '--', color = 'grey') #plt.text(x = 0.35, y = 0.14, s='G1', color = 'grey', fontsize=12) #plt.text(x = 0.36, y = 0.27, s='S', color = 'grey', fontsize=12) #plt.plot([0,1],[0.84,0.84], '--', color = 'grey') #plt.text(x = 0.25, y = 0.84-0.08, s='S/G2', color = 'grey', fontsize=12) #plt.text(x = 0.26, y = 0.84+0.05, s='M', color = 'grey', fontsize=12) plt.legend(loc=5) plt.tight_layout() plt.savefig('../Results/DetSilico/attractor_by_temperature.pdf') plt.show() plt.close()
def stochastic_attractor_by_temperature(cell='NIH3T3', nb_traces=500, size_block=100, expected=True): """ Compute and plot the evolution of the stochastic attractor with temperature. Parameters ---------- cell : string Cell condition. """ ll_theta = [] ll_phi = [] l_temperature = [34, 37, 40] l_color = ['lightblue', 'grey', 'orange'] for idx_t, temperature in enumerate(l_temperature): ##################### 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=temperature, division=True, several_cell_cycles=True, 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 ################## 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) if expected: ##################### GET EXPECTED PHASE ################## M_at = plot_phase_space_density(l_var, l_gamma_div, ll_idx_obs_phi, F_superimpose=F, save=False) l_theta = [] l_phi = [] for line, phi in zip(M_at.T, theta_var_coupled.domain): l_theta.append( np.angle( np.sum( np.multiply( line,np.exp(1j*\ np.array(theta_var_coupled.domain) ))))%(2*np.pi) ) l_phi.append(phi) else: ##################### GET MOST LIKELY PHASE ################## M_at = plot_phase_space_density(l_var, l_gamma_div, ll_idx_obs_phi, F_superimpose=F, save=False) l_theta = [] l_phi = [] for line, phi in zip(M_at.T, theta_var_coupled.domain): idx_best_phase = np.argmax(line) l_theta.append(theta_var_coupled.domain[idx_best_phase]) l_phi.append(phi) ######### REMOVE VERTICAL LINES AT BOUNDARIES ######### abs_d_data_x = np.abs(np.diff(l_theta)) mask_x = np.hstack([ abs_d_data_x > abs_d_data_x.mean()\ +3*abs_d_data_x.std(), [False]]) masked_l_theta = np.array([x if not m else np.nan \ for x,m in zip(l_theta, mask_x)]) abs_d_data_x = np.abs(np.diff(l_phi)) mask_x = np.hstack([ abs_d_data_x > abs_d_data_x.mean()\ +3*abs_d_data_x.std(), [False]]) masked_l_phi = np.array([x if not m else np.nan \ for x,m in zip(l_phi, mask_x) ]) ll_theta.append(masked_l_theta) ll_phi.append(masked_l_phi) ######### COMPUTE IDENDITIY ######### #plot identity x_domain = np.linspace(0, 2 * np.pi, 100) f = (1.15 + np.linspace(0, 2 * np.pi, 100)) % (2 * np.pi) abs_d_data_x = np.abs(np.diff(f)) mask_x = np.hstack([ abs_d_data_x > abs_d_data_x.mean()\ +3*abs_d_data_x.std(), [False]]) masked_identity = np.array([x if not m else np.nan \ for x, m in zip(f, mask_x)]) ######### PLOT ######### plt.figure(figsize=(10, 10)) for idx_t, (l_phase_theta, l_phase_phi) in enumerate(zip(ll_theta, ll_phi)): plt.plot(l_phase_theta, l_phase_phi, color=l_color[idx_t], label=str(l_temperature[idx_t])) plt.plot(x_domain, masked_identity, '--') plt.xlim(10**-2, 2 * np.pi - 10**-2) plt.ylim(10**-2, 2 * np.pi - 10**-2) plt.xlabel("Circadian phase") plt.ylabel("Cell-cycle phase") plt.legend() plt.savefig('../Results/PhaseSpace/stochastic_attractor_by_temperature_' \ +str(expected)+'.pdf') plt.show() plt.close()
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()