Exemplo n.º 1
0
def RTTplot_high_res(f,grid_cell_size=1.,burnin=0,max_age=0):
	wd = "%s" % os.path.dirname(f)
	name_file=os.path.splitext(os.path.basename(f))[0]
	t=loadtxt(f, skiprows=max(1,burnin))
	head = next(open(f)).split() # should be faster
	sp_ind = [head.index(s) for s in head if "lambda_" in s]
	ex_ind = [head.index(s) for s in head if "mu_" in s]
	root_ind  = head.index("root_age")
	death_ind = head.index("death_age")
	sp_shift_ind = [head.index(s) for s in head if "shift_sp_" in s]
	ex_shift_ind = [head.index(s) for s in head if "shift_ex_" in s]
	min_root_age = min(t[:,root_ind])
	if max_age> 0: min_root_age=max_age
	max_death_age= max(t[:,death_ind])
	n_bins= int((min_root_age-max_death_age)/grid_cell_size)
	grid = np.linspace(min_root_age,max_death_age,n_bins)
	n_samples = np.shape(t)[0]
	m_sp_matrix = np.zeros((n_samples,n_bins))
	m_ex_matrix = np.zeros((n_samples,n_bins))
	print "Extracting marginal rates..."
	for i in range(n_samples):
		l_shift_times = np.array([min_root_age]+ list(t[i,sp_shift_ind])+[max_death_age])
		l_rates = t[i,sp_ind]
		m_sp_matrix[i] = get_marginal_rates(l_shift_times,l_rates,grid)
		m_shift_times = np.array([min_root_age]+ list(t[i,ex_shift_ind])+[max_death_age])
		m_rates = t[i,ex_ind]
		m_ex_matrix[i] = get_marginal_rates(m_shift_times,m_rates,grid)

	res = np.zeros((n_bins,7)) # times, l, lM, lm, m, mM, mm
	print "Calculating HPDs..."
	for i in range(n_bins):
		l_HPD  = list(util.calcHPD(m_sp_matrix[:,i],0.95))
		l_mean = mean(m_sp_matrix[:,i])
		m_HPD  = list(util.calcHPD(m_ex_matrix[:,i],0.95))
		m_mean = mean(m_ex_matrix[:,i])
		res[i] = np.array([grid[i],l_mean]+l_HPD+[m_mean]+m_HPD)
	
	Rfile=r_plot_code(res,wd,name_file)
	out="%s/%s_RTT.r" % (wd,name_file)
	newfile = open(out, "w") 
	newfile.writelines(Rfile)
	newfile.close()
	print "\nAn R script with the source for the RTT plot was saved as: %sRTT.r\n(in %s)" % (name_file, wd)
	if platform.system() == "Windows" or platform.system() == "Microsoft":
		cmd="cd %s; Rscript %s\%s_RTT.r" % (wd,wd,name_file)
	else: 
		cmd="cd %s; Rscript %s/%s_RTT.r" % (wd,wd,name_file)
	os.system(cmd)
	print "done\n"


# args
#burnin=0
#grid_cell_size=.1 # in Myr (approximated)
#f= "/Users/daniele/Desktop/try/catalina_temp/par_est/combined_10mcmc_files.log"
#max_age=23.03
#RTTplot_high_res(f,grid_cell_size,burnin,max_age)
Exemplo n.º 2
0
def get_marginal_rates(f_name, min_age, max_age, nbins=0, burnin=0.2):
    # returns a list of 5 items:
    # 1. a vector of times (age of each marginal rate)
    # 2-4. mean, min and max marginal rates (95% HPD)
    # 5. a vector of times of rate shift
    f = open(f_name, 'U')
    if nbins == 0:
        nbins = int(max_age - 0)
    post_rate = f.readlines()
    bins_histogram = np.linspace(min_age, max_age, nbins + 1)
    marginal_rates_list = []
    times_of_shift = []

    if burnin < 1:  # define burnin as a fraction
        burnin = min(int(burnin * len(post_rate)), int(0.9 * len(post_rate)))
    else:
        burnin = int(burnin)

    for i in range(burnin, len(post_rate)):
        row = np.array(post_rate[i].split()).astype(float)

        if len(row) == 0:
            continue
        elif len(row) == 1:
            marginal_rates = np.zeros(nbins) + row[0]
        else:
            ind_rates = np.arange(0, int(np.ceil(len(row) / 2.)))
            ind_shifts = np.arange(int(np.ceil(len(row) / 2.)), len(row))
            rates = row[ind_rates]
            shifts = row[ind_shifts]
            #shifts = shifts[shifts>min_age]
            h = np.histogram(row[ind_shifts], bins=bins_histogram)[0][::-1]
            marginal_rates = rates[np.cumsum(h)]
            times_of_shift += list(shifts)

        marginal_rates_list.append(marginal_rates)

    marginal_rates_list = np.array(marginal_rates_list)
    mean_rates = np.median(marginal_rates_list, axis=0)
    min_rates, max_rates = [], []
    for i in range(nbins):
        hpd = util.calcHPD(marginal_rates_list[:, i], 0.95)
        min_rates += [hpd[0]]
        max_rates += [hpd[1]]

    time_frames = bins_histogram - abs(bins_histogram[1] -
                                       bins_histogram[0]) / 2.
    #print time_frames, min(times_of_shift), min_age
    #quit()
    time_frames = time_frames[1:]
    #print len(time_frames),len(mean_rates),
    n_mcmc_samples = len(
        post_rate
    ) - burnin  # number of samples used to normalize frequencies of rate shifts
    return [
        time_frames, mean_rates,
        np.array(min_rates),
        np.array(max_rates),
        np.array(times_of_shift), n_mcmc_samples, marginal_rates_list
    ]
Exemplo n.º 3
0
def plot_net_rate(resS,resE,col,min_age,max_age,plot_title,n_bins):
	#computes and plots net RATES
	marginal_rates_list	= resS[6]-resE[6]
	mean_rates= np.mean(marginal_rates_list,axis=0)
	min_rates,max_rates=[],[]
	for i in range(n_bins):
		hpd = util.calcHPD(marginal_rates_list[:,i],0.95)
		min_rates += [hpd[0]]
		max_rates += [hpd[1]]

	out_str = "\n#Net Diversification Rate"

	out_str += util.print_R_vec("\ntime",resS[0]-min_age)
	minXaxis,maxXaxis= max_age-min_age,min_age-min_age

	#right now I don't have support for log, but I think this is less likely to be needed for net rates
	out_str += util.print_R_vec("\nnet_rate",mean_rates[::-1])
	out_str += util.print_R_vec("\nnet_minHPD",np.array(min_rates[::-1]))
	out_str += util.print_R_vec("\nnet_maxHPD",np.array(max_rates[::-1]))
	out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = 'Net Rate', xlab = 'Time',lwd=2, main='%s', col= '%s' )" \
			% (min(0,1.1*np.nanmin(min_rates)),1.1*np.nanmax(max_rates),minXaxis,maxXaxis,plot_title,col) 
	out_str += "\npolygon(c(time, rev(time)), c(net_maxHPD, rev(net_minHPD)), col = alpha('%s',0.3), border = NA)" % (col)
	out_str += "\nlines(time,net_rate, col = '%s', lwd=2)" % (col)
	out_str += "\nabline(h=0,lty=2)\n"
		
	return out_str
Exemplo n.º 4
0
def plot_net_rate(resS, resE, col, min_age, max_age, plot_title, n_bins):
    #computes and plots net RATES
    marginal_rates_list = resS[6] - resE[6]
    mean_rates = np.mean(marginal_rates_list, axis=0)
    min_rates, max_rates = [], []
    for i in range(n_bins):
        hpd = util.calcHPD(marginal_rates_list[:, i], 0.95)
        min_rates += [hpd[0]]
        max_rates += [hpd[1]]

    out_str = "\n#Net Diversification Rate"

    out_str += util.print_R_vec("\ntime", resS[0] - min_age)
    minXaxis, maxXaxis = max_age - min_age, min_age - min_age

    #right now I don't have support for log, but I think this is less likely to be needed for net rates
    out_str += util.print_R_vec("\nnet_rate", mean_rates[::-1])
    out_str += util.print_R_vec("\nnet_minHPD", np.array(min_rates[::-1]))
    out_str += util.print_R_vec("\nnet_maxHPD", np.array(max_rates[::-1]))
    out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = 'Net Rate', xlab = 'Time',lwd=2, main='%s', col= '%s' )" \
      % (min(0,1.1*np.nanmin(min_rates)),1.1*np.nanmax(max_rates),minXaxis,maxXaxis,plot_title,col)
    out_str += "\npolygon(c(time, rev(time)), c(net_maxHPD, rev(net_minHPD)), col = alpha('%s',0.3), border = NA)" % (
        col)
    out_str += "\nlines(time,net_rate, col = '%s', lwd=2)" % (col)
    out_str += "\nabline(h=0,lty=2)\n"

    return out_str
Exemplo n.º 5
0
def get_marginal_rates(f_name,min_age,max_age,nbins=0,burnin=0.2):
	# returns a list of 5 items:
	# 1. a vector of times (age of each marginal rate)
	# 2-4. mean, min and max marginal rates (95% HPD)
	# 5. a vector of times of rate shift
	f = file(f_name,'U')
	if nbins==0:
		nbins = int(max_age-0)
	post_rate=f.readlines()
	bins_histogram = np.linspace(0,max_age,nbins+1)	
	marginal_rates_list = []
	times_of_shift = []
	
	if burnin<1: # define burnin as a fraction
		burnin=min(int(burnin*len(post_rate)),int(0.9*len(post_rate)))
	else: burnin = int(burnin)
	
	for i in range(burnin,len(post_rate)):
		row = np.array(post_rate[i].split()).astype(float)
		
		if len(row)==0:
			continue	
		elif len(row)==1: 
			marginal_rates = np.zeros(nbins)+row[0]
		else:
			ind_rates = np.arange(0,int(np.ceil(len(row)/2.)))
			ind_shifts = np.arange(int(np.ceil(len(row)/2.)),len(row))
			rates = row[ind_rates]
			shifts = row[ind_shifts]
			#shifts = shifts[shifts>min_age]
			h = np.histogram(row[ind_shifts],bins =bins_histogram)[0][::-1]
			marginal_rates = rates[np.cumsum(h)]
			times_of_shift += list(shifts)
		
		marginal_rates_list.append(marginal_rates)
	
	marginal_rates_list = np.array(marginal_rates_list)
	mean_rates= np.median(marginal_rates_list,axis=0)
	min_rates,max_rates=[],[]
	for i in range(nbins):
		hpd = util.calcHPD(marginal_rates_list[:,i],0.95)
		min_rates += [hpd[0]]
		max_rates += [hpd[1]]
	
	time_frames = bins_histogram-bins_histogram[1]/2.
	#print time_frames, min(times_of_shift), min_age
	#quit()
	time_frames = time_frames[1:]
	#print len(time_frames),len(mean_rates), 
	n_mcmc_samples = len(post_rate)-burnin # number of samples used to normalize frequencies of rate shifts
	return [time_frames,mean_rates,np.array(min_rates),np.array(max_rates),np.array(times_of_shift),n_mcmc_samples, marginal_rates_list]
Exemplo n.º 6
0
def plot_net_rate(resS, resE, col, min_age, max_age, plot_title, n_bins):
    #computes and plots net RATES
    resS_marginal_rate = resS[6]
    resE_marginal_rate = resE[6]
    # in case they have different number of samples
    max_indx = np.min(
        [resS_marginal_rate.shape[0], resE_marginal_rate.shape[0]])
    marginal_rates_list = resS_marginal_rate[
        0:max_indx, :] - resE_marginal_rate[0:max_indx, :]

    mean_rates = np.mean(marginal_rates_list, axis=0)
    min_rates, max_rates = [], []
    time_ax = []
    for i in range(n_bins):
        hpd = util.calcHPD(marginal_rates_list[:, i], 0.95)
        min_rates += [hpd[0]]
        max_rates += [hpd[1]]

    times = abs(resS[0])
    indx = np.intersect1d((times >= min_age).nonzero()[0],
                          (times <= max_age).nonzero()[0])
    times = times[indx]

    out_str = "\n#Net Diversification Rate"
    out_str += util.print_R_vec("\ntime", -times)
    minXaxis, maxXaxis = min_age, max_age

    mean_rates = np.array(mean_rates)[::-1]
    min_rates = np.array(min_rates)[::-1]
    max_rates = np.array(max_rates)[::-1]

    mean_rates = mean_rates[indx]
    min_rates = min_rates[indx]
    max_rates = max_rates[indx]

    out_str += util.print_R_vec("\nnet_rate", mean_rates)
    out_str += util.print_R_vec("\nnet_minHPD", np.array(min_rates))
    out_str += util.print_R_vec("\nnet_maxHPD", np.array(max_rates))
    out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = 'Net Rate', xlab = 'Time',lwd=2, main='%s', col= '%s' )" \
      % (min(0,1.1*np.nanmin(min_rates)),1.1*np.nanmax(max_rates),-maxXaxis,-minXaxis,plot_title,col)
    out_str += "\npolygon(c(time, rev(time)), c(net_maxHPD, rev(net_minHPD)), col = alpha('%s',0.3), border = NA)" % (
        col)
    out_str += "\nlines(time,net_rate, col = '%s', lwd=2)" % (col)
    out_str += "\nabline(h=0,lty=2)\n"

    return out_str
Exemplo n.º 7
0
                                                   Temp_at_events, shift_ind,
                                                   root_age)
        marginal_L.append(l_vec)
        marginal_M.append(m_vec)

    marginal_L = np.array(marginal_L)
    marginal_M = np.array(marginal_M)

    l_vec = np.zeros(np.shape(marginal_L)[1])
    m_vec = np.zeros(np.shape(marginal_L)[1])
    hpd_array_L = np.zeros((2, np.shape(marginal_L)[1]))
    hpd_array_M = np.zeros((2, np.shape(marginal_L)[1]))
    for i in range(np.shape(marginal_L)[1]):
        l_vec[i] = np.mean(marginal_L[:, i])
        m_vec[i] = np.mean(marginal_M[:, i])
        hpd_array_L[:, i] = calcHPD(marginal_L[:, i])
        hpd_array_M[:, i] = calcHPD(marginal_M[:, i])
    print "done"
    # write R file
    print "\ngenerating R file...",
    out = "%s/%s_RTT.r" % (output_wd, name_file)
    newfile = open(out, "wb")
    if platform.system() == "Windows" or platform.system() == "Microsoft":
        wd_forward = os.path.abspath(output_wd).replace('\\', '/')
        r_script = "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*20, height=0.6*20)\nlibrary(scales)\n" % (
            wd_forward, name_file)
    else:
        r_script = "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*20, height=0.6*20)\nlibrary(scales)\n" % (
            output_wd, name_file)

    r_script += print_R_vec("\n\nt", age_vec)
Exemplo n.º 8
0
        l_vec = np.zeros(np.shape(marginal_L)[1])
        m_vec = np.zeros(np.shape(marginal_L)[1])
        hpd_array_L = np.zeros((2, np.shape(marginal_L)[1]))
        hpd_array_M = np.zeros((2, np.shape(marginal_L)[1]))
        hpd_array_L50 = np.zeros((2, np.shape(marginal_L)[1]))
        hpd_array_M50 = np.zeros((2, np.shape(marginal_L)[1]))

        if i >= 0:
            l_vec = np.mean(marginal_L, axis=0)  # get_mode
            m_vec = np.mean(marginal_M, axis=0)  # get_mode
        else:
            for ii in range(
                    np.shape(marginal_L)[1]):  # loop over marginal rates
                l_vec[ii] = np.mean(marginal_L[:, ii])  # get_mode
                m_vec[ii] = np.mean(marginal_M[:, ii])  # get_mode
                hpd_array_L[:, ii] = calcHPD(marginal_L[:, ii])
                hpd_array_M[:, ii] = calcHPD(marginal_M[:, ii])
                hpd_array_L50[:, ii] = calcHPD(marginal_L[:, ii], 0.75)
                hpd_array_M50[:, ii] = calcHPD(marginal_M[:, ii], 0.75)

        r_script += lib_utilities.print_R_vec("\n\nt", all_events)
        r_script += "\ntime = -t"
        r_script += lib_utilities.print_R_vec("\nspeciation", l_vec)
        if i == -1:
            r_script += lib_utilities.print_R_vec("\nsp_hdp_m", hpd_array_L[0])
            r_script += lib_utilities.print_R_vec("\nsp_hdp_M", hpd_array_L[1])
            r_script += lib_utilities.print_R_vec("\nsp_hdp_m50",
                                                  hpd_array_L50[0])
            r_script += lib_utilities.print_R_vec("\nsp_hdp_M50",
                                                  hpd_array_L50[1])
        r_script += lib_utilities.print_R_vec("\nextinction", m_vec)
Exemplo n.º 9
0
		l_vec= np.zeros(np.shape(marginal_L)[1])
		m_vec= np.zeros(np.shape(marginal_L)[1])
		hpd_array_L= np.zeros((2,np.shape(marginal_L)[1]))
		hpd_array_M= np.zeros((2,np.shape(marginal_L)[1]))
		hpd_array_L50= np.zeros((2,np.shape(marginal_L)[1]))
		hpd_array_M50= np.zeros((2,np.shape(marginal_L)[1]))
		
		if i>=0:
			l_vec = np.mean(marginal_L, axis=0) # get_mode
			m_vec = np.mean(marginal_M, axis=0) # get_mode
		else:		
			for ii in range(np.shape(marginal_L)[1]): # loop over marginal rates
				l_vec[ii] = np.mean(marginal_L[:,ii]) # get_mode
				m_vec[ii] = np.mean(marginal_M[:,ii]) # get_mode
				hpd_array_L[:,ii] = calcHPD(marginal_L[:,ii])
				hpd_array_M[:,ii] = calcHPD(marginal_M[:,ii])
				hpd_array_L50[:,ii] = calcHPD(marginal_L[:,ii],0.75)
				hpd_array_M50[:,ii] = calcHPD(marginal_M[:,ii],0.75)

		r_script  = lib_utilities.print_R_vec("\n\nt",all_events)
		r_script += "\ntime = -t"
		r_script += lib_utilities.print_R_vec("\nspeciation",l_vec)
		if i==-1:
			r_script += lib_utilities.print_R_vec("\nsp_hdp_m",hpd_array_L[0])
			r_script += lib_utilities.print_R_vec("\nsp_hdp_M",hpd_array_L[1])
			r_script += lib_utilities.print_R_vec("\nsp_hdp_m50",hpd_array_L50[0])
			r_script += lib_utilities.print_R_vec("\nsp_hdp_M50",hpd_array_L50[1])
		r_script += lib_utilities.print_R_vec("\nextinction",m_vec)
		if i==-1:
			r_script += lib_utilities.print_R_vec("\nex_hdp_m",hpd_array_M[0])
Exemplo n.º 10
0
		Garray = np.array([Gl,Gm])*curve_scale_factor	
		age_vec,l_vec,m_vec = get_marginal_rates(args.m,L0,M0,Garray,Temp_at_events,shift_ind,root_age)
		marginal_L.append(l_vec)
		marginal_M.append(m_vec)
	
	marginal_L = np.array(marginal_L)
	marginal_M = np.array(marginal_M)
	
	l_vec= np.zeros(np.shape(marginal_L)[1])
	m_vec= np.zeros(np.shape(marginal_L)[1])
	hpd_array_L= np.zeros((2,np.shape(marginal_L)[1]))
	hpd_array_M= np.zeros((2,np.shape(marginal_L)[1]))
	for i in range(np.shape(marginal_L)[1]):
		l_vec[i] = np.mean(marginal_L[:,i])
		m_vec[i] = np.mean(marginal_M[:,i])
		hpd_array_L[:,i] = calcHPD(marginal_L[:,i])
		hpd_array_M[:,i] = calcHPD(marginal_M[:,i])
	print "done"	
	# write R file
	print "\ngenerating R file...",
	out="%s/%s_RTT.r" % (output_wd,name_file)
	newfile = open(out, "wb") 	
	if platform.system() == "Windows" or platform.system() == "Microsoft":
		wd_forward = os.path.abspath(output_wd).replace('\\', '/')
		r_script= "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*20, height=0.6*20)\nlibrary(scales)\n" % (wd_forward,name_file)
	else: r_script= "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*20, height=0.6*20)\nlibrary(scales)\n" % (output_wd,name_file)

	r_script += print_R_vec("\n\nt",  age_vec)
	r_script += "\ntime = -t"
	r_script += print_R_vec("\nspeciation",l_vec)
	r_script += print_R_vec("\nextinction",m_vec)
Exemplo n.º 11
0
def RTTplot_high_res(f, grid_cell_size=1., burnin=0, max_age=0):
    wd = "%s" % os.path.dirname(f)
    name_file = os.path.splitext(os.path.basename(f))[0]
    t = loadtxt(f, skiprows=max(1, int(burnin)))
    head = np.array(next(open(f)).split())  # should be faster
    #print np.where(head=="beta")[0], np.where(head=="temperature")[0]
    if "temperature" in head or "beta" in head:
        if "temperature" in head:
            temp_index = np.where(head == "temperature")[0][0]
        else:
            temp_index = np.where(head == "beta")[0][0]
        temp_values = t[:, temp_index]
        t = t[temp_values == 1, :]
        print("removed heated chains:", np.shape(t))

    head = list(head)
    sp_ind = [head.index(s) for s in head if "lambda_" in s]
    ex_ind = [head.index(s) for s in head if "mu_" in s]
    root_ind = head.index("root_age")
    death_ind = head.index("death_age")
    sp_shift_ind = [head.index(s) for s in head if "shift_sp_" in s]
    ex_shift_ind = [head.index(s) for s in head if "shift_ex_" in s]
    min_root_age = min(t[:, root_ind])
    if max_age > 0: min_root_age = max_age
    max_death_age = max(t[:, death_ind])
    n_bins = int((min_root_age - max_death_age) / grid_cell_size)
    grid = np.linspace(min_root_age, max_death_age, n_bins)
    n_samples = np.shape(t)[0]
    m_sp_matrix = np.zeros((n_samples, n_bins))
    m_ex_matrix = np.zeros((n_samples, n_bins))
    print("Extracting marginal rates...")
    for i in range(n_samples):
        l_shift_times = np.array([min_root_age] + list(t[i, sp_shift_ind]) +
                                 [max_death_age])
        l_rates = t[i, sp_ind]
        m_sp_matrix[i] = get_marginal_rates_plot3(l_shift_times, l_rates, grid)
        m_shift_times = np.array([min_root_age] + list(t[i, ex_shift_ind]) +
                                 [max_death_age])
        m_rates = t[i, ex_ind]
        m_ex_matrix[i] = get_marginal_rates_plot3(m_shift_times, m_rates, grid)

    res = np.zeros((n_bins, 7))  # times, l, lM, lm, m, mM, mm
    print("Calculating HPDs...")
    for i in range(n_bins):
        l_HPD = list(util.calcHPD(m_sp_matrix[:, i], 0.95))
        l_mean = mean(m_sp_matrix[:, i])
        m_HPD = list(util.calcHPD(m_ex_matrix[:, i], 0.95))
        m_mean = mean(m_ex_matrix[:, i])
        res[i] = np.array([grid[i], l_mean] + l_HPD + [m_mean] + m_HPD)

    Rfile = r_plot_code(res, wd, name_file)
    out = "%s/%s_RTT.r" % (wd, name_file)
    newfile = open(out, "w")
    newfile.writelines(Rfile)
    newfile.close()
    print(
        "\nAn R script with the source for the RTT plot was saved as: %sRTT.r\n(in %s)"
        % (name_file, wd))
    if platform.system() == "Windows" or platform.system() == "Microsoft":
        cmd = "cd %s & Rscript %s_RTT.r" % (wd, name_file)
    else:
        cmd = "cd %s; Rscript %s/%s_RTT.r" % (wd, wd, name_file)
    os.system(cmd)

    save_HR_log_file = 1
    if save_HR_log_file == 1:
        print("Saving log file...")
        out = "%s/%s_HR_marginal.log" % (wd, name_file)
        logfile = open(out, "w")
        head = ["iteration"] + ["l_%s" % (i) for i in range(n_bins)]
        head += ["m_%s" % (i) for i in range(n_bins)]
        wlog = csv.writer(logfile, delimiter='\t')
        wlog.writerow(head)
        for i in range(n_samples):
            l = [i] + list(m_sp_matrix[i, :]) + list(m_ex_matrix[i, :])
            wlog.writerow(l)
            logfile.flush()

    print("done\n")
Exemplo n.º 12
0
def RTTplot_Q(f, q_shift_file, burnin=0, max_age=0):
    wd = "%s" % os.path.dirname(f)
    name_file = os.path.splitext(os.path.basename(f))[0]
    t = loadtxt(f, skiprows=max(1, int(burnin)))
    head = np.array(next(open(f)).split())  # should be faster
    #print np.where(head=="beta")[0], np.where(head=="temperature")[0]
    if "temperature" in head or "beta" in head:
        if "temperature" in head:
            temp_index = np.where(head == "temperature")[0][0]
        else:
            temp_index = np.where(head == "beta")[0][0]
        temp_values = t[:, temp_index]
        t = t[temp_values == 1, :]
        print("removed heated chains:", np.shape(t))

    head = list(head)
    q_ind = [head.index(s) for s in head if "q_" in s]
    root_ind = head.index("root_age")
    death_ind = head.index("death_age")
    min_root_age = min(t[:, root_ind])
    if max_age > 0: min_root_age = max_age
    max_death_age = max(t[:, death_ind])

    try:
        times_q_shift = np.sort(np.loadtxt(q_shift_file))[::-1]
    except:
        times_q_shift = np.array([np.loadtxt(q_shift_file)])

    times_q_shift = times_q_shift[times_q_shift > max_death_age]
    times_q_shift = times_q_shift[times_q_shift < min_root_age]
    times_q_shift = np.sort(
        np.array(list(times_q_shift) + [max_death_age, min_root_age]))[::-1]
    print(times_q_shift)

    means = []
    hpdM = []
    hpdm = []
    data = "library(scales)\n"
    if platform.system() == "Windows" or platform.system() == "Microsoft":
        wd_forward = os.path.abspath(wd).replace('\\', '/')
        data += "\n\npdf(file='%s/%s_RTT_Qrates.pdf',width=0.6*9, height=0.6*7)\n" % (
            wd, name_file)  # 9
    else:
        data += "\n\npdf(file='%s/%s_RTT_Qrates.pdf',width=0.6*9, height=0.6*7)\n" % (
            wd, name_file)  # 9

    max_y_axis, max_x_axis, min_x_axis = np.max(
        t[:, q_ind]), -np.max(times_q_shift), -np.min(times_q_shift)

    for i in range(len(q_ind)):
        qtemp = t[:, q_ind[i]]
        hpdtemp = util.calcHPD(qtemp, 0.95)
        #means.append(np.mean(qtemp),)
        #hpdM.append(hpdtemp[1],hpdtemp[1])
        #hpdm.append(hpdtemp[0],hpdtemp[0])
        time_slice = np.array([times_q_shift[i], times_q_shift[i + 1]])
        data += '\nage = c(%s, %s)' % (-time_slice[0], -time_slice[1])
        data += '\nQ_mean = %s' % np.mean(qtemp)
        data += '\nQ_hpd_m = %s' % hpdtemp[0]
        data += '\nQ_hpd_M = %s' % hpdtemp[1]
        if i == 0:
            data += "\nplot(age,age,type = 'n', ylim = c(0, %s), xlim = c(%s,%s), ylab = 'Preservation rate', xlab = 'Ma',main='%s' )" \
             % (max_y_axis,max_x_axis,min_x_axis,"Preservation rates")
        else:
            data += """\nsegments(x0=age[1], y0 = %s, x1 = age[1], y1 = Q_mean, col = "#756bb1", lwd=3)""" % (
                Q_mean_previous)
        Q_mean_previous = np.mean(qtemp)
        data += """\nsegments(x0=age[1], y0 = Q_mean, x1 = age[2], y1 = Q_mean, col = "#756bb1", lwd=3)"""
        data += """\npolygon( c(age, rev(age)), c(Q_hpd_m, Q_hpd_m, Q_hpd_M, Q_hpd_M), col = alpha("#756bb1",0.5), border = NA)"""
    data += "\nn <- dev.off()"

    out = "%s/%s_RTT_Qrates.r" % (wd, name_file)
    newfile = open(out, "w")
    newfile.writelines(data)
    newfile.close()
    print(
        "\nAn R script with the source for the RTT plot was saved as: %s_RTT_Qrates.r\n(in %s)"
        % (name_file, wd))
    if platform.system() == "Windows" or platform.system() == "Microsoft":
        cmd = "cd %s & Rscript %s_RTT_Qrates.r" % (wd, name_file)
    else:
        cmd = "cd %s; Rscript %s/%s_RTT_Qrates.r" % (wd, wd, name_file)
    os.system(cmd)

    print("done\n")
Exemplo n.º 13
0
def RTTplot_high_res(f,grid_cell_size=1.,burnin=0,max_age=0):
	wd = "%s" % os.path.dirname(f)
	name_file=os.path.splitext(os.path.basename(f))[0]
	t=loadtxt(f, skiprows=max(1,int(burnin)))
	head = np.array(next(open(f)).split()) # should be faster
	#print np.where(head=="beta")[0], np.where(head=="temperature")[0]
	if "temperature" in head or "beta" in head:
		if "temperature" in head: 
			temp_index = np.where(head=="temperature")[0][0]
		else: 
			temp_index = np.where(head=="beta")[0][0]
		temp_values = t[:,temp_index]
		t = t[temp_values==1,:]
		print "removed heated chains:",np.shape(t)
	
	head= list(head)
	sp_ind = [head.index(s) for s in head if "lambda_" in s]
	ex_ind = [head.index(s) for s in head if "mu_" in s]
	root_ind  = head.index("root_age")
	death_ind = head.index("death_age")
	sp_shift_ind = [head.index(s) for s in head if "shift_sp_" in s]
	ex_shift_ind = [head.index(s) for s in head if "shift_ex_" in s]
	min_root_age = min(t[:,root_ind])
	if max_age> 0: min_root_age=max_age
	max_death_age= max(t[:,death_ind])
	n_bins= int((min_root_age-max_death_age)/grid_cell_size)
	grid = np.linspace(min_root_age,max_death_age,n_bins)
	n_samples = np.shape(t)[0]
	m_sp_matrix = np.zeros((n_samples,n_bins))
	m_ex_matrix = np.zeros((n_samples,n_bins))
	print "Extracting marginal rates..."
	for i in range(n_samples):
		l_shift_times = np.array([min_root_age]+ list(t[i,sp_shift_ind])+[max_death_age])
		l_rates = t[i,sp_ind]
		m_sp_matrix[i] = get_marginal_rates_plot3(l_shift_times,l_rates,grid)
		m_shift_times = np.array([min_root_age]+ list(t[i,ex_shift_ind])+[max_death_age])
		m_rates = t[i,ex_ind]
		m_ex_matrix[i] = get_marginal_rates_plot3(m_shift_times,m_rates,grid)

	res = np.zeros((n_bins,7)) # times, l, lM, lm, m, mM, mm
	print "Calculating HPDs..."
	for i in range(n_bins):
		l_HPD  = list(util.calcHPD(m_sp_matrix[:,i],0.95))
		l_mean = mean(m_sp_matrix[:,i])
		m_HPD  = list(util.calcHPD(m_ex_matrix[:,i],0.95))
		m_mean = mean(m_ex_matrix[:,i])
		res[i] = np.array([grid[i],l_mean]+l_HPD+[m_mean]+m_HPD)
	
	Rfile=r_plot_code(res,wd,name_file)
	out="%s/%s_RTT.r" % (wd,name_file)
	newfile = open(out, "w") 
	newfile.writelines(Rfile)
	newfile.close()
	print "\nAn R script with the source for the RTT plot was saved as: %sRTT.r\n(in %s)" % (name_file, wd)
	if platform.system() == "Windows" or platform.system() == "Microsoft":
		cmd="cd %s & Rscript %s_RTT.r" % (wd,name_file)
	else: 
		cmd="cd %s; Rscript %s/%s_RTT.r" % (wd,wd,name_file)
	os.system(cmd)
	
	save_HR_log_file=1
	if save_HR_log_file==1:
		print "Saving log file..."
		out="%s/%s_HR_marginal.log" % (wd,name_file)
		logfile = open(out, "w") 
		head = ["iteration"]+["l_%s" % (i) for i in range(n_bins)]
		head +=["m_%s" % (i) for i in range(n_bins)]
		wlog=csv.writer(logfile, delimiter='\t')
		wlog.writerow(head)
		for i in range(n_samples):
			l = [i]+ list(m_sp_matrix[i,:]) + list(m_ex_matrix[i,:])
			wlog.writerow(l)
			logfile.flush()
			
	
	
	print "done\n"
Exemplo n.º 14
0
def RTTplot_Q(f,q_shift_file,burnin=0,max_age=0):
	wd = "%s" % os.path.dirname(f)
	name_file=os.path.splitext(os.path.basename(f))[0]
	t=loadtxt(f, skiprows=max(1,int(burnin)))
	head = np.array(next(open(f)).split()) # should be faster
	#print np.where(head=="beta")[0], np.where(head=="temperature")[0]
	if "temperature" in head or "beta" in head:
		if "temperature" in head: 
			temp_index = np.where(head=="temperature")[0][0]
		else: 
			temp_index = np.where(head=="beta")[0][0]
		temp_values = t[:,temp_index]
		t = t[temp_values==1,:]
		print "removed heated chains:",np.shape(t)
	
	head= list(head)
	q_ind = [head.index(s) for s in head if "q_" in s]
	root_ind  = head.index("root_age")
	death_ind = head.index("death_age")
	min_root_age = min(t[:,root_ind])
	if max_age> 0: min_root_age=max_age
	max_death_age= max(t[:,death_ind])

	try: times_q_shift=np.sort(np.loadtxt(q_shift_file))[::-1]
	except: times_q_shift=np.array([np.loadtxt(q_shift_file)])
	
	times_q_shift = times_q_shift[times_q_shift>max_death_age]
	times_q_shift = times_q_shift[times_q_shift<min_root_age]
	times_q_shift = np.sort(np.array(list(times_q_shift) + [max_death_age,min_root_age]))[::-1]
	print times_q_shift
	
	means = []
	hpdM  = []
	hpdm  = []
	data  = "library(scales)\n" 
	if platform.system() == "Windows" or platform.system() == "Microsoft":
		wd_forward = os.path.abspath(wd).replace('\\', '/')
		data+= "\n\npdf(file='%s/%s_RTT_Qrates.pdf',width=0.6*9, height=0.6*7)\n" % (wd,name_file) # 9
	else: 
		data+= "\n\npdf(file='%s/%s_RTT_Qrates.pdf',width=0.6*9, height=0.6*7)\n" % (wd,name_file) # 9
	
	
	max_y_axis,max_x_axis,min_x_axis = np.max(t[:,q_ind]),-np.max(times_q_shift),-np.min(times_q_shift)
	
	for i in range(len(q_ind)):
		qtemp = t[:,q_ind[i]]
		hpdtemp = util.calcHPD(qtemp,0.95)
		#means.append(np.mean(qtemp),)
		#hpdM.append(hpdtemp[1],hpdtemp[1])
	      #hpdm.append(hpdtemp[0],hpdtemp[0])
		time_slice = np.array([times_q_shift[i],times_q_shift[i+1]])
		data += '\nage = c(%s, %s)' % (-time_slice[0],-time_slice[1])
		data += '\nQ_mean = %s' %  np.mean(qtemp)
		data += '\nQ_hpd_m = %s' % hpdtemp[0]
		data += '\nQ_hpd_M = %s' % hpdtemp[1]
		if i==0:
			data += "\nplot(age,age,type = 'n', ylim = c(0, %s), xlim = c(%s,%s), ylab = 'Preservation rate', xlab = 'Ma',main='%s' )" \
				% (max_y_axis,max_x_axis,min_x_axis,"Preservation rates") 			
		else:
			data += """\nsegments(x0=age[1], y0 = %s, x1 = age[1], y1 = Q_mean, col = "#756bb1", lwd=3)""" % (Q_mean_previous)
		Q_mean_previous = np.mean(qtemp)
		data += """\nsegments(x0=age[1], y0 = Q_mean, x1 = age[2], y1 = Q_mean, col = "#756bb1", lwd=3)""" 
		data += """\npolygon( c(age, rev(age)), c(Q_hpd_m, Q_hpd_m, Q_hpd_M, Q_hpd_M), col = alpha("#756bb1",0.5), border = NA)""" 
	data += "\nn <- dev.off()" 
		
	out="%s/%s_RTT_Qrates.r" % (wd,name_file)
	newfile = open(out, "w") 
	newfile.writelines(data)
	newfile.close()
	print "\nAn R script with the source for the RTT plot was saved as: %s_RTT_Qrates.r\n(in %s)" % (name_file, wd)
	if platform.system() == "Windows" or platform.system() == "Microsoft":
		cmd="cd %s & Rscript %s_RTT_Qrates.r" % (wd,name_file)
	else: 
		cmd="cd %s; Rscript %s/%s_RTT_Qrates.r" % (wd,wd,name_file)
	os.system(cmd)
	
	print "done\n"