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
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
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
def r_plot_code(res, wd, name_file, alpha=0.5, plot_title="RTT plot"): data = "library(scales)\ntrans=%s" % (alpha) if platform.system() == "Windows" or platform.system() == "Microsoft": wd_forward = os.path.abspath(wd).replace('\\', '/') data += "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*9, height=0.6*14)\npar(mfrow=c(2,1))" % ( wd_forward, name_file) # 9 else: data += "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*9, height=0.6*14)\npar(mfrow=c(2,1))" % ( wd, name_file) # 9 data += util.print_R_vec('\nage', -res[:, 0]) data += util.print_R_vec('\nL_mean', res[:, 1]) data += util.print_R_vec('\nL_hpd_m', res[:, 2]) data += util.print_R_vec('\nL_hpd_M', res[:, 3]) data += util.print_R_vec('\nM_mean', res[:, 4]) data += util.print_R_vec('\nM_hpd_m', res[:, 5]) data += util.print_R_vec('\nM_hpd_M', res[:, 6]) max_x_axis, min_x_axis = -max(res[:, 0]), -min(res[:, 0]) data += "\nplot(age,age,type = 'n', ylim = c(0, %s), xlim = c(%s,%s), ylab = 'Speciation rate', xlab = 'Ma',main='%s' )" \ % (1.1*max(res[:,3]),max_x_axis,min_x_axis,plot_title) data += """\nlines(rev(age), rev(L_mean), col = "#4c4cec", lwd=3)""" data += """\npolygon(c(age, rev(age)), c(L_hpd_M, rev(L_hpd_m)), col = alpha("#4c4cec",trans), border = NA)""" data += "\nplot(age,age,type = 'n', ylim = c(0, %s), xlim = c(%s,%s), ylab = 'Extinction rate', xlab = 'Ma' )" \ % (1.1*max(res[:,6]),max_x_axis,min_x_axis) data += """\nlines(rev(age), rev(M_mean), col = "#e34a33", lwd=3)""" data += """\npolygon(c(age, rev(age)), c(M_hpd_M, rev(M_hpd_m)), col = alpha("#e34a33",trans), border = NA)""" data += "\nn <- dev.off()" return data
def r_plot_code(res,wd,name_file,alpha=0.5,plot_title="RTT plot"): data = "library(scales)\ntrans=%s" % (alpha) if platform.system() == "Windows" or platform.system() == "Microsoft": wd_forward = os.path.abspath(wd).replace('\\', '/') data+= "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*9, height=0.6*14)\npar(mfrow=c(2,1))" % (wd_forward,name_file) # 9 else: data+= "\n\npdf(file='%s/%s_RTT.pdf',width=0.6*9, height=0.6*14)\npar(mfrow=c(2,1))" % (wd,name_file) # 9 data += util.print_R_vec('\nage', -res[:,0]) data += util.print_R_vec('\nL_mean', res[:,1]) data += util.print_R_vec('\nL_hpd_m',res[:,2]) data += util.print_R_vec('\nL_hpd_M',res[:,3]) data += util.print_R_vec('\nM_mean', res[:,4]) data += util.print_R_vec('\nM_hpd_m',res[:,5]) data += util.print_R_vec('\nM_hpd_M',res[:,6]) max_x_axis,min_x_axis = -max(res[:,0]),-min(res[:,0]) data += "\nplot(age,age,type = 'n', ylim = c(0, %s), xlim = c(%s,%s), ylab = 'Speciation rate', xlab = 'Ma',main='%s' )" \ % (1.1*max(res[:,3]),max_x_axis,min_x_axis,plot_title) data += """\nlines(rev(age), rev(L_mean), col = "#4c4cec", lwd=3)""" data += """\npolygon(c(age, rev(age)), c(L_hpd_M, rev(L_hpd_m)), col = alpha("#4c4cec",trans), border = NA)""" data += "\nplot(age,age,type = 'n', ylim = c(0, %s), xlim = c(%s,%s), ylab = 'Extinction rate', xlab = 'Ma' )" \ % (1.1*max(res[:,6]),max_x_axis,min_x_axis) data += """\nlines(rev(age), rev(M_mean), col = "#e34a33", lwd=3)""" data += """\npolygon(c(age, rev(age)), c(M_hpd_M, rev(M_hpd_m)), col = alpha("#e34a33",trans), border = NA)""" data += "\nn <- dev.off()" return data
def get_r_plot(res,col,parameter,min_age,max_age,plot_title,plot_log,run_simulation=1, plot_shifts=1, line_wd=2): times = res[0] rates = res[1][::-1] rates_m = res[2][::-1] rates_M = res[3][::-1] shifts = res[4] indx = np.intersect1d((times>=min_age).nonzero()[0], (times<=max_age).nonzero()[0]) times = times[indx] #print indx,times, rates rates = rates[indx] rates_m = rates_m[indx] rates_M = rates_M[indx] try: shifts= shifts[shifts>min_age] shifts= shifts[shifts<max_age] except: plot_shifts=0 out_str = "\n" out_str += util.print_R_vec("\ntime",-times) out_str += util.print_R_vec("\nrate",rates) out_str += util.print_R_vec("\nminHPD",rates_m) out_str += util.print_R_vec("\nmaxHPD",rates_M) if plot_log==0: out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = '%s', xlab = 'Time',main='%s' )" \ % (0,1.1*np.nanmax(rates_M),-max_age,-min_age,parameter,plot_title) out_str += "\npolygon(c(time, rev(time)), c(maxHPD, rev(minHPD)), col = alpha('%s',0.3), border = NA)" % (col) out_str += "\nlines(time,rate, col = '%s', lwd=%s)" % (col, line_wd) else: out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = 'Log10 %s', xlab = 'Time',main='%s' )" \ % (np.nanmin(np.log10(0.9*rates_m)),np.nanmax(np.log10(1.1*rates_M)),-max_age,-min_age,parameter,plot_title) out_str += "\npolygon(c(time, rev(time)), c(log10(maxHPD), rev(log10(minHPD))), col = alpha('%s',0.3), border = NA)" % (col) out_str += "\nlines(time,log10(rate), col = '%s', lwd=%s)" % (col, line_wd) if plot_shifts: # add barplot rate shifts bins_histogram = np.linspace(0,max_age,len(res[0])) if len(shifts)>1: # rate shift sampled at least once h = np.histogram(shifts,bins =bins_histogram) #,density=1) else: h = [np.zeros(len(bins_histogram)-1),bins_histogram] a = h[1] mids = (a-a[1]/2.)[1:] out_str += util.print_R_vec("\nmids",-mids) out_str += util.print_R_vec("\ncounts",h[0]/float(res[5])) out_str += "\nplot(mids,counts,type = 'h', xlim = c(%s,%s), ylim=c(0,%s), ylab = 'Frequency of rate shift', xlab = 'Time',lwd=5,col='%s')" \ % (-max_age,-min_age,max(max(h[0]/float(res[5])),0.2),col) # get BFs if run_simulation==1: BFs = get_prior_shift(min_age,max_age,bins_histogram) out_str += "\nbf2 = %s\nbf6 = %s" % (BFs[1],BFs[2]) out_str += "\nabline(h=bf2, lty=2)" out_str += "\nabline(h=bf6, lty=2)" return out_str
def get_r_plot(res, col, parameter, min_age, max_age, plot_title, plot_log, run_simulation=1): out_str = "\n" out_str += util.print_R_vec("\ntime", -res[0]) out_str += util.print_R_vec("\nrate", res[1][::-1]) out_str += util.print_R_vec("\nminHPD", res[2][::-1]) out_str += util.print_R_vec("\nmaxHPD", res[3][::-1]) if plot_log == 0: out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = '%s', xlab = 'Time',main='%s' )" \ % (0,1.1*np.nanmax(res[3]),-max_age,-min_age,parameter,plot_title) out_str += "\npolygon(c(time, rev(time)), c(maxHPD, rev(minHPD)), col = alpha('%s',0.3), border = NA)" % ( col) out_str += "\nlines(time,rate, col = '%s', lwd=2)" % (col) else: out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = 'Log10 %s', xlab = 'Time',main='%s' )" \ % (np.nanmin(np.log10(0.9*res[2])),np.nanmax(np.log10(1.1*res[3])),-max_age,-min_age,parameter,plot_title) out_str += "\npolygon(c(time, rev(time)), c(log10(maxHPD), rev(log10(minHPD))), col = alpha('%s',0.3), border = NA)" % ( col) out_str += "\nlines(time,log10(rate), col = '%s', lwd=2)" % (col) # add barplot rate shifts bins_histogram = np.linspace(0, max_age, len(res[0])) if len(res[4]) > 1: # rate shift sampled at least once h = np.histogram(res[4], bins=bins_histogram) #,density=1) else: h = [np.zeros(len(bins_histogram) - 1), bins_histogram] a = h[1] mids = (a - a[1] / 2.)[1:] out_str += util.print_R_vec("\nmids", -mids) out_str += util.print_R_vec("\ncounts", h[0] / float(res[5])) out_str += "\nplot(mids,counts,type = 'h', xlim = c(%s,%s), ylim=c(0,%s), ylab = 'Frequency of rate shift', xlab = 'Time',lwd=5,col='%s')" \ % (-max_age,-min_age,max(max(h[0]/float(res[5])),0.2),col) # get BFs if run_simulation == 1: BFs = get_prior_shift(min_age, max_age, bins_histogram) out_str += "\nbf2 = %s\nbf6 = %s" % (BFs[1], BFs[2]) out_str += "\nabline(h=bf2, lty=2)" out_str += "\nabline(h=bf6, lty=2)" return out_str
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) r_script += print_R_vec('\nL_hpd_m', hpd_array_L[0, :]) r_script += print_R_vec('\nL_hpd_M', hpd_array_L[1, :]) r_script += print_R_vec('\nM_hpd_m', hpd_array_M[0, :]) r_script += print_R_vec('\nM_hpd_M', hpd_array_M[1, :]) r_script += """ par(mfrow=c(2,1)) plot(speciation ~ time,type="l",col="#4c4cec", lwd=3,main="Speciation rates", ylim = c(0,max(c(L_hpd_M,M_hpd_M))),xlab="Time",ylab="speciation rate",xlim=c(min(time),0)) polygon(c(time, rev(time)), c(L_hpd_M, rev(L_hpd_m)), col = alpha("#4c4cec",0.3), border = NA) plot(extinction ~ time,type="l",col="#e34a33", lwd=3,main="Extinction rates", ylim = c(0,max(c(L_hpd_M,M_hpd_M))),xlab="Time",ylab="extinction",xlim=c(min(time),0))
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) r_script += print_R_vec('\nL_hpd_m',hpd_array_L[0,:]) r_script += print_R_vec('\nL_hpd_M',hpd_array_L[1,:]) r_script += print_R_vec('\nM_hpd_m',hpd_array_M[0,:]) r_script += print_R_vec('\nM_hpd_M',hpd_array_M[1,:]) r_script += """ par(mfrow=c(2,1)) plot(speciation ~ time,type="l",col="#4c4cec", lwd=3,main="Speciation rates", ylim = c(0,max(c(L_hpd_M,M_hpd_M))),xlab="Time",ylab="speciation rate",xlim=c(min(time),0)) polygon(c(time, rev(time)), c(L_hpd_M, rev(L_hpd_m)), col = alpha("#4c4cec",0.3), border = NA)
def get_r_plot(res, col, parameter, min_age, max_age, plot_title, plot_log, run_simulation=1, plot_shifts=1, line_wd=2): times = res[0] rates = res[1][::-1] rates_m = res[2][::-1] rates_M = res[3][::-1] shifts = res[4] indx = np.intersect1d((times >= min_age).nonzero()[0], (times <= max_age).nonzero()[0]) times = times[indx] #print indx,times, rates rates = rates[indx] rates_m = rates_m[indx] rates_M = rates_M[indx] try: shifts = shifts[shifts > min_age] shifts = shifts[shifts < max_age] except: plot_shifts = 0 out_str = "\n" out_str += util.print_R_vec("\ntime", -times) out_str += util.print_R_vec("\nrate", rates) out_str += util.print_R_vec("\nminHPD", rates_m) out_str += util.print_R_vec("\nmaxHPD", rates_M) if plot_log == 0: out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = '%s', xlab = 'Time',main='%s' )" \ % (0,1.1*np.nanmax(rates_M),-max_age,-min_age,parameter,plot_title) out_str += "\npolygon(c(time, rev(time)), c(maxHPD, rev(minHPD)), col = alpha('%s',0.3), border = NA)" % ( col) out_str += "\nlines(time,rate, col = '%s', lwd=%s)" % (col, line_wd) else: out_str += "\nplot(time,time,type = 'n', ylim = c(%s, %s), xlim = c(%s,%s), ylab = 'Log10 %s', xlab = 'Time',main='%s' )" \ % (np.nanmin(np.log10(0.9*rates_m)),np.nanmax(np.log10(1.1*rates_M)),-max_age,-min_age,parameter,plot_title) out_str += "\npolygon(c(time, rev(time)), c(log10(maxHPD), rev(log10(minHPD))), col = alpha('%s',0.3), border = NA)" % ( col) out_str += "\nlines(time,log10(rate), col = '%s', lwd=%s)" % (col, line_wd) if plot_shifts: # add barplot rate shifts bins_histogram = np.linspace(0, max_age, len(res[0])) if len(shifts) > 1: # rate shift sampled at least once h = np.histogram(shifts, bins=bins_histogram) #,density=1) else: h = [np.zeros(len(bins_histogram) - 1), bins_histogram] a = h[1] mids = (a - a[1] / 2.)[1:] out_str += util.print_R_vec("\nmids", -mids) out_str += util.print_R_vec("\ncounts", h[0] / float(res[5])) out_str += "\nplot(mids,counts,type = 'h', xlim = c(%s,%s), ylim=c(0,%s), ylab = 'Frequency of rate shift', xlab = 'Time',lwd=5,col='%s')" \ % (-max_age,-min_age,max(max(h[0]/float(res[5])),0.2),col) # get BFs if run_simulation == 1: BFs = get_prior_shift(min_age, max_age, bins_histogram) out_str += "\nbf2 = %s\nbf6 = %s" % (BFs[1], BFs[2]) out_str += "\nabline(h=bf2, lty=2)" out_str += "\nabline(h=bf6, lty=2)" return out_str