def plot_obs_pred(obs, pred, radius, loglog, ax = None, inset = False, sites = None):
    """Generic function to generate an observed vs predicted figure with 1:1 line"""
    if not ax:
        fig = plt.figure(figsize = (3.5, 3.5))
        ax = plt.subplot(111)

    axis_min = 0.9 * min(list(obs[obs > 0]) + list(pred[pred > 0]))
    if loglog:
        axis_max = 3 * max(list(obs)+list(pred))
    else:
        axis_max = 1.1 * max(list(obs)+list(pred))
    macroecotools.plot_color_by_pt_dens(np.array(pred), np.array(obs), radius, loglog=loglog, plot_obj = ax)      
    plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
    plt.xlim(axis_min, axis_max)
    plt.ylim(axis_min, axis_max)
    ax.tick_params(axis = 'both', which = 'major', labelsize = 6)
    if loglog:
        plt.annotate(r'$R^2$ = %0.2f' %macroecotools.obs_pred_rsquare(np.log10(obs[(obs != 0) * (pred != 0)]), np.log10(pred[(obs != 0) * (pred != 0)])),
                     xy = (0.05, 0.85), xycoords = 'axes fraction', fontsize = 7)
    else:
        plt.annotate(r'$R^2$ = %0.2f' %macroecotools.obs_pred_rsquare(obs, pred),
                     xy = (0.05, 0.85), xycoords = 'axes fraction', fontsize = 7)
    if inset:
        axins = inset_axes(ax, width="30%", height="30%", loc=4)
        if loglog:
            hist_mete_r2(sites[(obs != 0) * (pred != 0)], np.log10(obs[(obs != 0) * (pred != 0)]), 
                         np.log10(pred[(obs != 0) * (pred != 0)]))
        else:
            hist_mete_r2(sites, obs, pred)
        plt.setp(axins, xticks=[], yticks=[])
    return ax
def plot_numsp_obs_pred(sites, obs_ab, min_abundance, max_abundance):
    """Observed vs. predicted plot of the number of species in an abundance range

    Drops communities where there are 0 species that occur within the range so
    that the results can be displayed on log-scaled axes. Prints the number of
    dropped communities to the screen.
    
    """
    sites = np.array(sites)
    usites = np.unique(sites)
    obs_ab = np.array(obs_ab)
    pred = []
    obs = []
    for site in usites:
        site_abs = obs_ab[sites==site]
        site_range_abundances = site_abs[(site_abs >= min_abundance) &
                                            (site_abs <= max_abundance)]
        obs_richness = len(site_range_abundances)
        pred_richness = mete.get_mete_sad(len(site_abs), sum(site_abs),
                                          bin_edges=[min_abundance,
                                                     max_abundance + 1])
        obs.append(obs_richness)
        pred.append(pred_richness)
    pred = np.array(list(itertools.chain.from_iterable(pred)))
    obs = np.array(obs)
    obs_pred_data = np.column_stack((obs, pred))
    np.savetxt('temp_sp_obs_pred_data', obs_pred_data)
    num_dropped_communities = len(obs[obs==0])
    pred = pred[obs > 0]
    obs = obs[obs > 0]
    print("%s communities out of a total of %s communities were dropped because no species were observed in the given abundance range"
          % (num_dropped_communities, num_dropped_communities + len(obs)))
    macroecotools.plot_color_by_pt_dens(pred, obs, 3, loglog=1)
def plot_obs_pred_sad(datasets, data_dir='./data/', radius=2):
    """Multiple obs-predicted plotter"""
    fig = plt.figure()
    num_datasets = len(datasets)
    rows = (3 if num_datasets in (5, 6) else 2)
    for i, dataset in enumerate(datasets):
        obs_pred_data = import_obs_pred_data(data_dir + dataset + '_obs_pred.csv') 
        site = ((obs_pred_data["site"]))
        obs = ((obs_pred_data["obs"]))
        pred = ((obs_pred_data["pred"]))
        
        axis_min = 0.5 * min(obs)
        axis_max = 2 * max(obs)
        ax = fig.add_subplot(rows,2,i+1)
        macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1, 
                                            plot_obj=plt.subplot(rows,2,i+1))      
        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
        plt.xlim(axis_min, axis_max)
        plt.ylim(axis_min, axis_max)
        plt.subplots_adjust(left=0.2, bottom=0.12, right=0.8, top=0.92, 
                            wspace=0.29, hspace=0.21)  
        
        # Create inset for histogram of site level r^2 values
        axins = inset_axes(ax, width="30%", height="30%", loc=4)
        hist_mete_r2(site, np.log10(obs), np.log10(pred))
        plt.setp(axins, xticks=[], yticks=[])
        
    plt.savefig('obs_pred_plots.png', dpi=400, bbox_inches = 'tight', pad_inches=0)
Esempio n. 4
0
def plot_obs_pred(obs_pred_data, dest_file='./obs_pred.png'):
    plot_color_by_pt_dens(obs_pred_data['pred'],
                          obs_pred_data['obs'],
                          3,
                          loglog=1)
    plt.loglog([min(obs_pred_data['pred']),
                max(obs_pred_data['pred'])],
               [min(obs_pred_data['pred']),
                max(obs_pred_data['pred'])], 'k-')
    plt.savefig(dest_file, dpi=400)
Esempio n. 5
0
def figSuppp(figname = 'SuppFig3', data_dir=mydir, radius=2):
    fig = plt.figure()
    plot_dim = 2
    count = 0

    IN_Obs_Pred = importData.import_NSR2_data(mydir + \
        'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt')
    N = np.asarray(list(((IN_Obs_Pred["N"]))))
    S = np.asarray(list(((IN_Obs_Pred["S"]))))
    NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"]))))
    NmaxPred = []
    SPred = []
    for i in range(len(N)):
        NmaxPred_i = importPredictS.predictS(N[i], NmaxObs[i], predictNmax=True).getNmax()
        SPred_i = importPredictS.predictS(N[i], NmaxObs[i], predictNmax=True).getS()
        NmaxPred.append(NmaxPred_i)
        SPred.append(SPred_i)
    NmaxPred = np.asarray(NmaxPred)
    SPred = np.asarray(SPred)
    toIteratePred = [NmaxPred, SPred]
    toIterateObs = [NmaxObs, S]
    for x in range(2):
        axis_min = 0
        axis_max = 2 * max(toIteratePred[x])
        #print plot_dim
        ax = fig.add_subplot(plot_dim-1, plot_dim, count+1)
        if x == 0:
            ax.set_title(r"$\mathbf{N_{max}}$")
        else:
            ax.set_title(r"$\mathbf{S}$")

        macroecotools.plot_color_by_pt_dens(toIteratePred[x], toIterateObs[x], radius, loglog=1,
                        plot_obj=plt.subplot(plot_dim-1,plot_dim,count+1))
        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
        plt.xlim(axis_min, axis_max)
        plt.ylim(0, axis_max)
        r2_all = macroecotools.obs_pred_rsquare(np.log10(toIterateObs[x]), np.log10(toIteratePred[x]))
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2)
        plt.text(0.18, 0.93, r2text,  fontsize=10,
            horizontalalignment='center',
            verticalalignment='center',transform = ax.transAxes)
        plt.tick_params(axis='both', which='major', labelsize=7)
        plt.subplots_adjust(wspace=0.5, hspace=0.3)

        #axins = inset_axes(ax, width="30%", height="30%", loc=4)

        ax.set(adjustable='box-forced', aspect='equal')
        #plt.setp(axins, xticks=[], yticks=[])

        count += 1
    fig.text(0.50, 0.04, r'$Predicted$', ha='center', va='center')
    fig.text(0.05, 0.5, r'$Observed$', ha='center', va='center', rotation='vertical')
    fig_name = str(mydir + 'figures/' + figname + '.png')
    plt.savefig(fig_name, dpi=600)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 6
0
def plot_obs_pred(obs_pred_data, yvar, dest_file='./obs_pred.png'):
    plot_color_by_pt_dens(10**obs_pred_data[yvar + 'pred'],
                          10**obs_pred_data[yvar],
                          3,
                          loglog=1)
    plt.loglog(
        [min(10**obs_pred_data[yvar + 'pred']),
         max(10**obs_pred_data[yvar])],
        [min(10**obs_pred_data[yvar + 'pred']),
         max(10**obs_pred_data[yvar])], 'k-')
    plt.savefig(dest_file, dpi=400)
Esempio n. 7
0
def plot_obs_pred_sad(SADModels, data_dir, radius=2): 
    # TAKEN FROM THE mete_sads.py script used for White et al. (2012)
    # Used for Figure 3 Locey and White (2013)        ########################################################################################

    """Multiple obs-predicted plotter"""
    fig = plt.figure()

    for i, model in enumerate(SADModels):

        fig.add_subplot(2, 2, i+1)

        obs_pred_data = import_obs_pred_data(data_dir + model + '.txt') 
        site = ((obs_pred_data["site"]))
        obs = ((obs_pred_data["obs"]))
        pred = ((obs_pred_data["pred"]))

        axis_min = 0.5 * min(obs)
        axis_max = 2 * max(obs)

        macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1,
                        plot_obj=plt.subplot(2, 2, i+1))

        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
        plt.xlim(axis_min, axis_max)
        plt.ylim(axis_min, axis_max)

        plt.tick_params(axis='both', which='major', labelsize=8)
        plt.subplots_adjust(wspace=0.5, hspace=0.3)

        r2 = macroecotools.obs_pred_rsquare(np.log10(obs), np.log10(pred))
        print model, r2

        # Create inset for histogram of site level r^2 values
        #axins = inset_axes(ax, width="30%", height="30%", loc=4)
        #hist_mete_r2(site, np.log10(obs), np.log10(pred))
        #plt.setp(axins, xticks=[], yticks=[])

        plt.title(model)
        #plt.text(1, 2000,  r'$R^2$' + '='+ str(round(r2,3)))
        plt.ylabel('Observed abundance',rotation='90',fontsize=12)
        plt.xlabel('Predicted abundance',fontsize=12)

    plt.savefig(mydir+'/Results/obs_pred_plots.png', dpi=600)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.show()
Esempio n. 8
0
def plot_obs_pred_sad(methods, datasets, data_dir='/home/kenlocey/data1/', radius=2): # TAKEN FROM THE mete_sads.py script used for White et al. (2012)
    # Used for Figure 3 Locey and White (2013)        ########################################################################################

    """Multiple obs-predicted plotter"""
    for i, dataset in enumerate(datasets):
        for j, method in enumerate(methods):

            #if method == 'mete' and dataset == 'EMP': continue

            obs_pred_data = import_obs_pred_data(mydir+'/data/truedata/'+method+'_'+dataset+'_obs_pred.txt')
            #site = ((obs_pred_data["site"]))
            obs = ((obs_pred_data["obs"]))
            pred = ((obs_pred_data["pred"]))

            axis_min = 0.5 * min(obs)
            axis_max = 2 * max(obs)

            macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1,
                            plot_obj=plt.subplot(2, 2, j+1))

            plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
            plt.xlim(axis_min, axis_max)
            plt.ylim(axis_min, axis_max)

            plt.tick_params(axis='both', which='major', labelsize=8)
            plt.subplots_adjust(wspace=0.5, hspace=0.3)

            r2 = macroecotools.obs_pred_rsquare(np.log10(obs), np.log10(pred))
            print method, dataset, r2

            #Create inset for histogram of site level r^2 values
            #axins = inset_axes(ax, width="30%", height="30%", loc=4)
            #hist_mete_r2(site, np.log10(obs), np.log10(pred))
            #plt.setp(axins, xticks=[], yticks=[])

            if method == 'mete': plt.title("Log-series")
            else: plt.title("Geometric series")
            plt.text(1, 2000,  r'$R^2$' + '='+ str(round(r2,3)))
            plt.ylabel('Observed abundance',rotation='90',fontsize=12)
            plt.xlabel('Predicted abundance',fontsize=12)
    plt.savefig(mydir+'/obs_pred_plots.png', dpi=600)#, bbox_inches = 'tight')#, pad_inches=0)
def fig4(figname = 'Fig4', data_dir=mydir, radius=1.5, saveAs = 'png'):
    fig = plt.figure()
    fig.subplots_adjust(bottom= 0.15)
    plot_dim = 1
    count = 0
    models = ['geom', 'lognorm', 'mete', 'zipf']
    #modelSlopes = [0.647520323289, 0.942904468437, 0.769214774397, 0.954497727096]
    #modelInterepts = [0.116508916992, 0.292527611072, 0.19240314275, 0.189954627996]
    modelSlopes = []
    modelInterepts = []


    for g, model in enumerate(models):

        if model == 'geom':
            IN_Obs_Pred = importData.import_NSR2_data(mydir + \
                'data/NSR2/Stratified/geom_NSR2_stratify.txt')
            nsr2 = importData.import_NSR2_data(data_dir + \
            'data/NSR2/Stratified_Test/' + model + '_NSR2_stratify.txt')


        elif model == 'lognorm':
            IN_Obs_Pred = importData.import_NSR2_data(mydir + \
                'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt')
            nsr2 = importData.import_NSR2_data(data_dir + \
            'data/NSR2/Stratified_Test/' + model + '_'+  'pln' + '_NSR2_stratify.txt')


        elif model == 'mete':
            IN_Obs_Pred = importData.import_NSR2_data(mydir + \
                'data/NSR2/Stratified/mete_NSR2_stratify.txt')
            nsr2 = importData.import_NSR2_data(data_dir + \
            'data/NSR2/Stratified_Test/' + model + '_NSR2_stratify.txt')


        elif model == 'zipf':
            IN_Obs_Pred = importData.import_NSR2_data(mydir + \
                'data/NSR2/Stratified/zipf_mle_NSR2_stratify.txt')
            nsr2 = importData.import_NSR2_data(data_dir + \
            'data/NSR2/Stratified_Test/' + model + '_mle' + '_NSR2_stratify.txt')

        N = np.asarray(list(((IN_Obs_Pred["N"]))))
        N_All = np.asarray(list(((nsr2["N"]))))
        domSlope = np.mean(((nsr2["NmaxPredSlope"])))
        domIntercept =  10 ** np.mean(((nsr2["NmaxPredIntercept"])))

        NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"]))))
        NmaxObsAll = np.asarray(list(((nsr2["NmaxObs"]))))

        NmaxPred = []
        NmaxPredAll = []
        for i in range(len(N)):
            NmaxPred_i = mo.predictNmax(N[i]).getNmax(b = domIntercept, slope = domSlope)
            NmaxPred.append(NmaxPred_i)

        NmaxPred = np.asarray(NmaxPred)
        NmaxPred_obs = [k for k in zip(NmaxObs, NmaxPred) if k[0] < 200000 ]
        NmaxObs = np.asarray([k[0] for k in NmaxPred_obs])
        NmaxPred = np.asarray([k[1] for k in NmaxPred_obs])

        axis_min = 10
        axis_max = 1000000
        ax = fig.add_subplot(2, 2, count+1)

        if model == 'geom':
            ax.set_title("Broken-stick")
        elif model == 'lognorm':
            ax.set_title("Lognormal")
        elif model == 'mete':
            ax.set_title("Log-series")
        elif model == 'zipf':
            ax.set_title("Zipf")

        #plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1,
        #                plot_obj=plt.subplot(2,2,count+1))
        #if model == 'lognorm':
        #    radius =  1.3
        macroecotools.plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1,
                        plot_obj=plt.subplot(2,2,count+1))
        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
        plt.xlim(axis_min, axis_max)
        plt.ylim(axis_min, axis_max)
        ax.set_xlim(axis_min, axis_max)
        ax.set_ylim(axis_min, axis_max )
        r2_all = macroecotools.obs_pred_rsquare(np.log10(NmaxObs), np.log10(NmaxPred))
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2)
        plt.text(0.72, 0.12, r2text,  fontsize=13,
            horizontalalignment='center',
            verticalalignment='center',transform = ax.transAxes)
        plt.tick_params(axis='both', which='major', labelsize=12)
        plt.subplots_adjust(wspace=0.00001, hspace=0.3)
        ax.set(adjustable='box-forced', aspect='equal')
        count += 1

    fig.text(0.50, 0.055 , 'Predicted, ' +r'$log_{10}(N_{max})$', ha='center', va='center', fontsize = 19)
    fig.text(0.09, 0.5, 'Observed, ' +r'$log_{10}(N_{max})$', ha='center', va='center', rotation='vertical',\
        fontsize = 19)
    fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs)
    plt.savefig(fig_name, dpi=600, format = saveAs)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
def fig3(figname = 'Fig3', \
    zipfType = 'mle', lognormType = 'pln', Stratified = True, data_dir= mydir, \
    saveAs = 'eps'):
    methods = ['geom', 'lognorm', 'mete', 'zipf']
    fig = plt.figure()
    count  = 0
    params = ['N']
    removeSADs = []
    for i, param in enumerate(params):
        for j, method in enumerate(methods):
            if method == 'zipf':
                obs_pred_data = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/'+ method + '_'+ zipfType + '_NSR2_stratify.txt')
            elif method == 'lognorm':
                obs_pred_data = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/'+ method + '_'+ lognormType +'_NSR2_stratify.txt')
            else:
                obs_pred_data = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/'+ method +'_NSR2_stratify.txt')
            site = np.asarray(list(((obs_pred_data["site"]))))
            y = np.asarray(list(((obs_pred_data["R2"]))))
            x = np.log10(np.asarray(list(((obs_pred_data[param])))))
            #print "nmax" + str(np.mean(np.asarray(list(((obs_pred_data["NmaxObs"]))))))

            mean_x = np.mean(x)
            mean_y = np.mean(y)
            std_error = sp.stats.sem(y)
            print method, param
            print "mean modified r2 = " + str(mean_y)
            print "modified r2 standard error = " + str(std_error)
            print "mean " + param  + " is " + str(np.mean(np.asarray(list(((obs_pred_data[param]))))))
            ax = fig.add_subplot(2, 2, count+1)
            macroecotools.plot_color_by_pt_dens(x, y, 0.1, loglog=0,
                            plot_obj=plt.subplot(2, 2, count+1))
            slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
            print "slope is " + str(slope)
            print "r2-value is " + str(r_value **2)
            print "p-value is " + str(p_value)

            print "NmaxPred ", method
            NmaxPred = np.log10(np.asarray(list(((obs_pred_data["NmaxPred"])))))
            slope1, intercept1, r_value1, p_value1, std_err1 = stats.linregress(x,NmaxPred)

            diff1 = slope1  - 1
            p_diff1 = (np.absolute(diff1) /  ( 0.5 *(slope1 + 1) )) * 100
            print "percent difference " + str(p_diff1)

            print "evennessPred ",method
            evennessPred = np.log10(np.asarray(list(((obs_pred_data["evennessPred"])))))
            slope2, intercept2, r_value2, p_value2, std_err2 = stats.linregress(x,evennessPred)
            diff2 = slope2  - (-0.31)
            p_diff2 = (np.absolute(diff2)  /  ( 0.5 *(slope1 + 1) )) * 100


            print "percent difference " + str(p_diff2)

            print "skewnessPred ",method
            skewnessPred = np.log10(np.asarray(list(((obs_pred_data["skewnessPred"])))))
            slope3, intercept3, r_value3, p_value3, std_err3 = stats.linregress(x,skewnessPred)
            diff3 = slope3  - 0.13
            p_diff3 = (np.absolute(diff3)  /  ( 0.5 *(slope1 + 1) )) * 100

            print "percent difference " + str(p_diff3)
            plt.xlim(np.amin(x), np.amax(x))

            plt.ylim(-1.5,1.5)
            predict_y = intercept + slope * x
            pred_error = y - predict_y
            degrees_of_freedom = len(x) - 2
            residual_std_error = np.sqrt(np.sum(pred_error**2) / degrees_of_freedom)
            plt.plot(x, predict_y, 'k-')
            plt.axhline(linewidth=2, color='darkgrey',ls='--')
            plt.tick_params(axis='both', which='major', labelsize=10)

            if i == 0 and j == 0:
                ax.set_title("Broken-stick", fontsize = 15)
                ax.set_ylabel(r'$r^{2}_{m}$', fontsize = 22)
            if i == 0 and j == 1:
                ax.set_title("Lognormal", fontsize = 15)
            if i == 0 and j == 2:
                ax.set_title("Log-series", fontsize = 15)
                ax.set_xlabel('Abundance, ' +r'$log_{10}$', fontsize = 17)
                ax.set_ylabel(r'$r^{2}_{m}$', fontsize = 22)
            if i == 0 and j == 3:
                ax.set_title("Zipf", fontsize = 15)
                ax.set_xlabel('Abundance, ' +r'$log_{10}$', fontsize = 17)
            ax.tick_params(axis='x', labelsize=12)
            ax.tick_params(axis='y', labelsize=12)

            count += 1

    plt.tight_layout(pad=0.8, w_pad=0.8, h_pad=0.8)
    fig_name = str(mydir + 'figures/' + figname  + '_RGB.' + saveAs)
    plt.savefig(fig_name, bbox_inches = "tight", pad_inches = 0.4, dpi = 600, \
        format = saveAs)
    plt.close()
Esempio n. 11
0
def plot_obs_pred(obs_pred_data, dest_file='./obs_pred.png'):
    plot_color_by_pt_dens(obs_pred_data['pred'], obs_pred_data['obs'], 3, loglog=1)
    plt.loglog([min(obs_pred_data['pred']), max(obs_pred_data['pred'])], 
               [min(obs_pred_data['pred']), max(obs_pred_data['pred'])], 'k-')
    plt.savefig(dest_file, dpi = 400)    
Esempio n. 12
0
def plot_obs_pred(obs_pred_data, yvar, dest_file='./obs_pred.png'):
    plot_color_by_pt_dens(10**obs_pred_data[yvar + 'pred'], 10**obs_pred_data[yvar],
                          3, loglog=1)
    plt.loglog([min(10**obs_pred_data[yvar + 'pred']), max(10**obs_pred_data[yvar])], 
               [min(10**obs_pred_data[yvar + 'pred']), max(10**obs_pred_data[yvar])], 'k-')
    plt.savefig(dest_file, dpi = 400)
def plot_obs_pred_sad(methods, datasets, data_dir= mydir, radius=2): # TAKEN FROM THE mete_sads.py script used for White et al. (2012)
    # Used for Figure 3 Locey and White (2013)        ########################################################################################
    """Multiple obs-predicted plotter"""
    fig = plt.figure()

    #xs = [[60,1], [100,1], [20,1], [60,1], [40,1], [200,1], [800,1.5], [200,1.5]]
    #rs = ['0.93','0.77','0.84','0.81','0.78','0.83','0.58','0.76']
    count = 0
    #ax = fig.add_subplot(111)
    for i, dataset in enumerate(datasets):
        for j, method in enumerate(methods):
            #if method == 'mete' and dataset == 'EMP': continue
            #obs_pred_data = import_obs_pred_data(data_dir + 'ObsPred/' + method+'_'+dataset+'_obs_pred_test.txt')
            if str(dataset) == 'EMPclosed' or str(dataset) == 'EMPopen':
                obs_pred_data = import_obs_pred_data(data_dir + 'ObsPred/' + method+'_'+dataset+'_obs_pred_subset.txt')
            elif str(dataset) == 'HMP':
                obs_pred_data = import_obs_pred_data(data_dir + 'ObsPred/' + method+'_'+dataset+'_obs_pred.txt')
            else:
                obs_pred_data = import_obs_pred_data(data_dir + 'ObsPred/' + method + '_' + 'MGRAST' + dataset +'_obs_pred.txt')
            print method, dataset
            site = ((obs_pred_data["site"]))
            obs = ((obs_pred_data["obs"]))
            pred = ((obs_pred_data["pred"]))
            axis_min = 0.5 * min(obs)
            axis_max = 2 * max(obs)
            ax = fig.add_subplot(4, 2, count+1)
            if j == 0:
                if i == 0:
                    ax.set_ylabel("HMP", rotation=90, size=8)
                elif i == 1:
                    ax.set_ylabel("EMP closed", rotation=90, size=8)
                elif i == 2:
                    ax.set_ylabel("EMP open", rotation=90, size=8)
                elif i == 3:
                    ax.set_ylabel('MGRAST', rotation=90, size=8)
            if i == 0 and j == 0:
                ax.set_title("Broken-stick")
            elif i == 0 and j == 1:
                ax.set_title("METE")

            macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1,
                            plot_obj=plt.subplot(4,2,count+1))

            plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
            plt.xlim(axis_min, axis_max)
            plt.ylim(axis_min, axis_max)

            plt.tick_params(axis='both', which='major', labelsize=8)
            plt.subplots_adjust(wspace=0.5, hspace=0.3)

            #plt.text(xs[0][1],xs[0][0],dataset+'\n'+rs[0],fontsize=8)
            #xs.pop(0)
            #rs.pop(0)
            # Create inset for histogram of site level r^2 values
            axins = inset_axes(ax, width="30%", height="30%", loc=4)
            if str(dataset) == 'EMPclosed' or str(dataset) == 'EMPopen':
                INh2 = import_NSR2_data(data_dir + 'NSR2/' + method+'_'+dataset+'_NSR2.txt')
                r2s = ((INh2["R2"]))
                hist_r2 = np.histogram(r2s, range=(0, 1))
                xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0])
                xvals = xvals[0:len(xvals)-1]
                yvals = hist_r2[0]
                plt.plot(xvals, yvals, 'k-', linewidth=2)
                plt.axis([0, 1, 0, 1.1 * max(yvals)])
            else:
                hist_mete_r2(site, np.log10(obs), np.log10(pred))
            plt.setp(axins, xticks=[], yticks=[])
            count += 1
    #ax.set_xlabel(-8,-80,'Rank-abundance at the centre of the feasible set',fontsize=10)
    #ax.set_ylabel(-8.5,500,'Observed rank-abundance',rotation='90',fontsize=10)
    #ax.set_ylabel('Rank-abundance at the centre of the feasible set',rotation='90',fontsize=10)
    fig.text(0.06, 0.5, 'Observed rank-abundance', ha='center', va='center', rotation='vertical')
    fig.text(0.5, 0.04, 'Rank-abundance at the centre of the feasible set', ha='center', va='center')
    #ax.set_xlabel('Observed rank-abundance',fontsize=10)
    plt.savefig('obs_pred_plots.png', dpi=600)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 14
0
def fig4(figname="Fig4", data_dir=mydir, radius=2, saveAs="eps"):
    fig = plt.figure()
    fig.subplots_adjust(bottom=0.15)
    plot_dim = 1
    count = 0

    IN_Obs_Pred = importData.import_NSR2_data(mydir + "data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt")
    N = np.asarray(list(((IN_Obs_Pred["N"]))))
    S = np.asarray(list(((IN_Obs_Pred["S"]))))
    NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"]))))
    models = ["geom", "lognorm", "mete", "zipf"]
    modelSlopes = [0.647520323289, 0.942904468437, 0.769214774397, 0.954497727096]
    modelInterepts = [0.116508916992, 0.292527611072, 0.19240314275, 0.189954627996]
    for g, model in enumerate(models):
        NmaxPred = []
        SPred = []
        for i in range(len(N)):
            NmaxPred_i = mo.predictS(N[i], NmaxObs[i], predictNmax=True).getNmax(
                b=modelInterepts[g], slope=modelSlopes[g]
            )
            SPred_i = mo.predictS(N[i], NmaxObs[i], predictNmax=True).getS()
            NmaxPred.append(NmaxPred_i)
            SPred.append(SPred_i)
        NmaxPred = np.asarray(NmaxPred)
        SPred = np.asarray(SPred)
        axis_min = 0
        axis_max = 2 * max(NmaxObs)
        ax = fig.add_subplot(2, 2, count + 1)
        if model == "zipf":
            OUT2 = importData.import_NSR2_data(
                data_dir + "data/NSR2/Stratified_Test/" + model + "_mle_NSR2_stratify.txt"
            )
        elif model == "lognorm":
            OUT2 = importData.import_NSR2_data(
                data_dir + "data/NSR2/Stratified_Test/" + model + "_pln_NSR2_stratify.txt"
            )
        else:
            OUT2 = importData.import_NSR2_data(data_dir + "data/NSR2/Stratified_Test/" + model + "_NSR2_stratify.txt")

        NmaxObs_BS = np.asarray(list(((OUT2["NmaxObs"]))))
        NmaxPred_BS = np.asarray(list(((OUT2["NmaxPred"]))))

        if model == "geom":
            ax.set_title("Broken-stick")
        elif model == "lognorm":
            ax.set_title("Lognormal")
        elif model == "mete":
            ax.set_title("Log-series")
        elif model == "zipf":
            ax.set_title("Zipf")
        macroecotools.plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1, plot_obj=plt.subplot(2, 2, count + 1))
        plt.plot([axis_min, axis_max], [axis_min, axis_max], "k-")
        plt.xlim(axis_min, axis_max)
        plt.ylim(0, axis_max)
        r2_all = macroecotools.obs_pred_rsquare(np.log10(NmaxObs), np.log10(NmaxPred))
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format("r", r2_all, p=2)
        plt.text(
            0.72,
            0.12,
            r2text,
            fontsize=13,
            horizontalalignment="center",
            verticalalignment="center",
            transform=ax.transAxes,
        )
        plt.tick_params(axis="both", which="major", labelsize=12)
        plt.subplots_adjust(wspace=0.00001, hspace=0.3)
        ax.set(adjustable="box-forced", aspect="equal")

        count += 1
    fig.text(0.50, 0.055, "Predicted, " + r"$log_{10}(N_{max})$", ha="center", va="center", fontsize=19)
    fig.text(
        0.09, 0.5, "Observed, " + r"$log_{10}(N_{max})$", ha="center", va="center", rotation="vertical", fontsize=19
    )
    fig_name = str(mydir + "figures/" + figname + "_RGB." + saveAs)
    plt.savefig(fig_name, dpi=600, format=saveAs)  # , bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 15
0
def fig4(figname = 'Fig4', data_dir=mydir, radius=2):
    fig = plt.figure()
    plot_dim = 1
    count = 0

    IN_Obs_Pred = importData.import_NSR2_data(mydir + \
        'data/NSR2/Stratified/lognorm_pln_NSR2_stratify.txt')
    N = np.asarray(list(((IN_Obs_Pred["N"]))))
    S = np.asarray(list(((IN_Obs_Pred["S"]))))
    NmaxObs = np.asarray(list(((IN_Obs_Pred["NmaxObs"]))))
    # order
    models = ['geom', 'lognorm', 'mete', 'zipf']
    modelSlopes = [0.647520323289, 0.942904468437, 0.769214774397, 0.954497727096]
    modelInterepts = [0.116508916992, 0.292527611072, 0.19240314275, 0.189954627996]
    for g, model in enumerate(models):
        NmaxPred = []
        SPred = []
        for i in range(len(N)):
            NmaxPred_i = importPredictS.predictS(N[i], NmaxObs[i], \
                predictNmax=True).getNmax(b = modelInterepts[g], slope = modelSlopes[g])
            SPred_i = importPredictS.predictS(N[i], NmaxObs[i], predictNmax=True).getS()
            NmaxPred.append(NmaxPred_i)
            SPred.append(SPred_i)
        NmaxPred = np.asarray(NmaxPred)
        SPred = np.asarray(SPred)
        axis_min = 0
        axis_max = 2 * max(NmaxObs)
        ax = fig.add_subplot(2, 2, count+1)
        #ax.set_title(r"$\mathbf{N_{max}}$", y=1.03)
        if model == 'geom':
            ax.set_title(r"$\mathbf{Broken-stick}$")
        elif model == 'lognorm':
            ax.set_title(r"$\mathbf{Lognormal}$")
        elif model == 'mete':
            ax.set_title(r"$\mathbf{Log-series}$")
        elif model == 'zipf':
            ax.set_title(r"$\mathbf{Zipf}$")
        macroecotools.plot_color_by_pt_dens(NmaxPred, NmaxObs, radius, loglog=1,
                        plot_obj=plt.subplot(2,2,count+1))
        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
        plt.xlim(axis_min, axis_max)
        plt.ylim(0, axis_max)
        print max(NmaxPred)
        r2_all = macroecotools.obs_pred_rsquare(np.log10(NmaxObs), np.log10(NmaxPred))
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2)
        plt.text(0.22, 0.91, r2text,  fontsize=13,
            horizontalalignment='center',
            verticalalignment='center',transform = ax.transAxes)
        plt.tick_params(axis='both', which='major', labelsize=7)
        plt.subplots_adjust(wspace=0.5, hspace=0.3)

        #axins = inset_axes(ax, width="30%", height="30%", loc=4)

        ax.set(adjustable='box-forced', aspect='equal')
        #plt.setp(axins, xticks=[], yticks=[])

        count += 1
    fig.text(0.50, 0.04, r'$Predicted\, log_{10}(N_{max})$', ha='center', va='center', fontsize = 16)
    fig.text(0.04, 0.5, r'$Observed\,log_{10}(N_{max})$', ha='center', va='center', rotation='vertical',\
        fontsize = 16)
    fig_name = str(mydir + 'figures/' + figname + '.png')
    plt.savefig(fig_name, dpi=600)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 16
0
def Supp(figname = 'Supp', data_dir=mydir, radius=2):
    # TAKEN FROM THE mete_sads.py script used for White et al. (2012)
    # Used for Figure 3 Locey and White (2013)
    """Multiple obs-predicted plotter"""
    fig = plt.figure()
    count = 0

    plot_dim = 2
    IN_Obs_Pred = importData.import_obs_pred_data(mydir + \
        'data/ObsPred/Stratified/lognorm_75_25_obs_pred_stratify_test.txt')
    site = np.asarray(list(((IN_Obs_Pred["site"]))))
    obs = np.asarray(list(((IN_Obs_Pred["obs"]))))
    pred7525 = np.asarray(list(((IN_Obs_Pred["pred7525"]))))
    predPln = np.asarray(list(((IN_Obs_Pred["predPln"]))))
    toIterate = [pred7525, predPln]
    for x in range(2):
        axis_min = 0
        axis_max = 2 * max(obs)
        #print plot_dim
        ax = fig.add_subplot(plot_dim, plot_dim, count+1)
        if x == 0:
            ax.set_title(r"$\mathbf{75:25\, Simulation}$")
        else:
            ax.set_title(r"$\mathbf{Lognormal\, MLE}$")

        macroecotools.plot_color_by_pt_dens(toIterate[x], obs, radius, loglog=1,
                        plot_obj=plt.subplot(plot_dim,plot_dim,count+1))
        #
        #plt.text(0.1, 0.9,'matplotlib', ha='center', va='center', transform=ax.transAxes)


        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')

        plt.xlim(0, axis_max)
        plt.ylim(0, axis_max)
        #r2s = ((INh2["R2"]))
        #r2s = r2s.astype(float)
        # insert r2 of all data
        r2_all = macroecotools.obs_pred_rsquare(np.log10(obs), np.log10(toIterate[x]))
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2)

        plt.text(0.18, 0.93, r2text,  fontsize=10,
            horizontalalignment='center',
            verticalalignment='center',transform = ax.transAxes)
        plt.tick_params(axis='both', which='major', labelsize=7)
        plt.subplots_adjust(wspace=0.5, hspace=0.3)

        axins = inset_axes(ax, width="30%", height="30%", loc=4)

        #hist_r2 = np.histogram(r2s, range=(0, 1))
        #xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0])
        #xvals = xvals[0:len(xvals)-1]
        #yvals = hist_r2[0]
        #plt.plot(xvals, yvals, 'k-', linewidth=2)
        #plt.axis([0, 1, 0, 1.1 * max(yvals)])
        ax.set(adjustable='box-forced', aspect='equal')
        #plt.setp(axins, xticks=[], yticks=[])

        count += 1
    fig.text(0.50, 0.04, r'$Predicted \; rank-abundance$', ha='center', va='center')
    fig.text(0.05, 0.5, r'$Observed \; rank-abundance$', ha='center', va='center', rotation='vertical')
    fig_name = str(mydir + 'figures/' + figname + '.png')
    plt.savefig(fig_name, dpi=600)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 17
0
"""Multiple obs-predicted plotter"""
fig = plt.figure()

ax1 = fig.add_subplot(2, 2, 1)

filename = mydir+'/data/emp_mete_obs_pred.txt'
data = import_obs_pred_data(filename)
site = np.asarray(list(((data["site"]))))
obs = np.asarray(list(((data["obs"]))))
pred = np.asarray(list(((data["pred"]))))

axis_min = 0
axis_max = 2 * max(obs)

radius=2
mct.plot_color_by_pt_dens(pred, obs, radius, loglog=1, plot_obj=ax1)

plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')

plt.xlim(0, axis_max)
plt.ylim(0, axis_max)

r2_all = mct.obs_pred_rsquare(np.log10(obs), np.log10(pred))
r2text = r"${}^{{2}} = {:.{p}f} $".format('r',r2_all , p=2)

plt.text(2, 30000, r2text,  fontsize=14)
plt.text(28, 800000, 'Log-series',  fontsize=14)
plt.text(5, 0.1, 'Predicted rank-abundance', fontsize=10)
plt.text(0.1, 60000, 'Observed rank-abundance', rotation='vertical', fontsize=10)

plt.tick_params(axis='both', which='major', labelsize=7)
def NSR2_regression(methods, datasets, data_dir= mydir):
    fig = plt.figure()
    count  = 0
    test_count = 0
    for i, dataset in enumerate(datasets):
        for k, param in enumerate(params):
            for j, method in enumerate(methods):
                nsr2_data = import_NSR2_data(data_dir + 'NSR2/' + method+'_'+dataset+'_NSR2.txt')

                #nsr2_data[[~np.isnan(nsr2_data).any(axis=1)]]
                #nsr2_data[~np.isinf(nsr2_data).any(axis=1)]
                #nsr2_data[~np.isnan(nsr2_data).any(1)]
                list = ['nan', 'NAN', '-inf', 'inf']
                #for x in nsr2_data:
                #    print type(x)
                #    value = str(x[3])

                #if np.isinf(x[3]) == True:
                #    print "infinity"
                #mask = np.all(np.isinf(nsr2_data), axis=1)


                y = ((nsr2_data["R2"]))
                mean = np.mean(y)
                std_error = sp.stats.sem(y)
                print method, param, dataset
                print "mean = " + str(mean)
                print "standard error = " + str(std_error)

                #print method, dataset, param
                ax = fig.add_subplot(3, 8, count+1)
                if param == "N" or param == "S":
                    x = np.log10(((nsr2_data[param])))
                else:
                    N_count = ((nsr2_data["N"]))
                    S_count = ((nsr2_data["S"]))
                    print dataset, method
                    print "mean N is " + str(np.mean(N_count))
                    print "mean S is " + str(np.mean(S_count))
                    x = np.divide(N_count, S_count)
                    x = np.log10(x)

                #elif str(n_or_s).capitalize() == 'S'
                #x = ((nsr2_data["N"]))
                macroecotools.plot_color_by_pt_dens(x, y, 0.1, loglog=0,
                                plot_obj=plt.subplot(3, 8, count+1))
                slope, intercept, r_value, p_value, std_err = stats.linregress(x,y)
                #if param == 'N/S':
                #    plt.xlim(np.amin(x), 1000)
                #else:
                plt.xlim(np.amin(x), np.amax(x))
                plt.ylim(-1,1)

                predict_y = intercept + slope * x
                pred_error = y - predict_y
                degrees_of_freedom = len(x) - 2
                residual_std_error = np.sqrt(np.sum(pred_error**2) / degrees_of_freedom)
                plt.plot(x, predict_y, 'k-')
                plt.axhline(linewidth=2, color='lightgray')
                plt.subplots_adjust(wspace=0.2, hspace=0.3)
                # Plotting
                plt.xlabel(param)
                if j == 0 and k == 0:
                    plt.title('Broken-stick', fontsize = 'large')
                elif j == 1 and k == 0:
                    plt.title('METE', fontsize = 'large')

                #plt.ylabel(r'$r^{2}$',fontsize=16)
                #r_2 = "r2 =" + str(round(r_value,2))
                #p_s = "p =" + str(round(p_value,2))
                #plt.text(0, 1, r'$p$'+ ' = '+str(round(p_value,2)), fontsize=12)
                #plt.text(0, 1, r'$r_{2}$'+ ' = '+str(round(r_value,2)), fontsize=12)
                #ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props)
                leg = plt.legend(loc=1,prop={'size':10})
                #leg.draw_frame(False)
                #plt.legend(loc='upper left')
                print r_value, p_value

                count += 1
    #ax.set_ylabel('common ylabel')
    plt.tight_layout()
    fig.text(0.02, 0.5, r'$r^{2}$', ha='center', va='center', rotation='vertical', size = 'medium')
    #ax.set_ylabel('common ylabel')
    #fig.text(-8,-80,'Rank-abundance at the centre of the feasible set',fontsize=10)
    #plt.suptitle(-8.5,500,r'$r^{2}$',rotation='90',fontsize=10)
    fig_name = 'NSR2_GeomMete' + str(dataset) + '.png'
    plt.savefig(fig_name)
    #plt.xscale()
    plt.close()
def figS1(n=35289, figname = 'FigS1', data_dir=mydir, radius=2, zipfType = 'mle', \
    saveAs = 'eps', lognormType = 'pln'):
    methods = ['geom', 'lognorm', 'mete', 'zipf']
    datasets = ['95', '97', '99']
    fig = plt.figure()
    count = 0
    rows = len(datasets)
    columns = len(methods)
    for i, dataset in enumerate(datasets):
        for j, method in enumerate(methods):
            print count
            if method == 'zipf':
                obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+  zipfType+'_obs_pred_stratify.txt')
                INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_mle' + '_NSR2_stratify.txt')
            elif method == 'lognorm':
                obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+  lognormType+'_obs_pred_stratify.txt')
                INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_'+  lognormType + '_NSR2_stratify.txt')
            else:
                obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method +'_obs_pred_stratify.txt')
                INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_NSR2_stratify.txt')
            obs = np.asarray(list(((obs_pred_data["obs"]))))
            pred = np.asarray(list(((obs_pred_data["pred"]))))
            site = np.asarray(list(((obs_pred_data["site"]))))


            obs2 = []
            pred2 = []
            site2 = []

            obs_all = np.asarray(obs)
            pred_all = np.asarray(pred)
            site_all = np.asarray(site)

            if n == 'all' or len(obs) <= n:
                obs2 = list(obs)
                pred2 = list(pred)
                site2 = list(site)

            else:
                if len(obs) > n:
                    inds = np.random.choice(range(len(site)), size=n, replace=False)
                    for ind in inds:
                        obs2.append(obs[ind])
                        pred2.append(pred[ind])
                        site2.append(site[ind])

            obs = np.asarray(obs2)
            pred = np.asarray(pred2)
            site =  np.asarray(site2)

            print "number of points " + str(len(obs))
            if method == 'zipf':
                axis_min = 0
                axis_max = 2  * max(pred)
            else:
                axis_min = 0
                axis_max = 2 * max(obs)
            ax = fig.add_subplot(rows, columns, count+1)

            if i == 0 and j == 0:
                ax.set_title("Broken-stick")
            elif i == 0 and j == 1:
                ax.set_title("Lognormal")
            elif i == 0 and j == 2:
                ax.set_title("Log-series")
            elif i == 0 and j == 3:
                ax.set_title("Zipf")

            if j == 0:
                if dataset == '95':
                    ax.set_ylabel("MG-RAST 95%", rotation=90, size=12)
                elif dataset == '97':
                    ax.set_ylabel("MG-RAST 97%", rotation=90, size=12)
                elif dataset == '99':
                    ax.set_ylabel("MG-RAST 99%", rotation=90, size=12)

            macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1,
                            plot_obj=plt.subplot(rows,columns,count+1))

            plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
            if method == 'zipf':
                plt.xlim(0, axis_max)
                plt.ylim(0, axis_max)
            else:
                plt.xlim(0, axis_max)
                plt.ylim(0, axis_max)
            r2s = ((INh2["R2"]))
            r2s = r2s.astype(float)
            mean_r2s = np.mean(r2s)
            std_r2s = np.std(r2s)
            print method, dataset
            print "Mean r2 " + str(mean_r2s)
            print "Standard dev. " + str(std_r2s)
            if method == 'zipf':
                getR2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method  + '_mle_' +dataset +'_NSR2_stratify.txt')
            elif method == 'lognorm':
                getR2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method  + '_' + lognormType + '_' +dataset  +'_NSR2_stratify.txt')
            else:
                getR2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/SequenceSimilarity/'+ method + '_' +dataset +'_NSR2_stratify.txt')
            r2_mean = np.mean(((getR2["R2"])))
            if method == 'geom':
                r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_mean , p=3)
            else:
                r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_mean , p=2)
            if method == 'geom':
                plt.text(0.28, 0.90, r2text,  fontsize=10,
                    horizontalalignment='center',
                    verticalalignment='center',transform = ax.transAxes)
            else:
                plt.text(0.25, 0.90, r2text,  fontsize=10,
                    horizontalalignment='center',
                    verticalalignment='center',transform = ax.transAxes)
            plt.tick_params(axis='both', which='major', labelsize=8)
            plt.subplots_adjust(wspace=0.0000000001, hspace=0.5)

            axins = inset_axes(ax, width="30%", height="30%", loc=4)

            hist_r2 = np.histogram(r2s, range=(0, 1))
            xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0])
            xvals = xvals[0:len(xvals)-1]
            yvals = hist_r2[0]
            plt.plot(xvals, yvals, 'k-', linewidth=2)
            plt.axis([0, 1, 0, 1.1 * max(yvals)])

            ax.set(adjustable='box-forced', aspect='equal')
            plt.setp(axins, xticks=[], yticks=[])

            count += 1

    plt.tight_layout(pad=1.5, w_pad=0.8, h_pad=0.8)
    fig.subplots_adjust(left=0.1)
    fig.text(0.50, 0.02, 'Predicted abundance', ha='center', va='center', fontsize=14)
    fig.text(0.03, 0.5, 'Observed abundance', ha='center', va='center', rotation='vertical', fontsize=14)
    fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs)
    plt.savefig(fig_name, dpi=600, format = saveAs)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 20
0
def figS2(
    n=35289,
    figname="FigS2",
    data_dir=mydir,
    stratify=True,
    radius=2,
    remove=1,
    zipfType="mle",
    RGF=False,
    saveAs="eps",
    lognormType="pln",
):
    # TAKEN FROM THE mete_sads.py script used for White et al. (2012)
    # Used for Figure 3 Locey and White (2013)
    """Multiple obs-predicted plotter"""
    fig = plt.figure()
    count = 0
    plot_dim = 2
    fig.subplots_adjust(bottom=0.30)
    methods = ["geom", "lognorm", "mete", "zipf"]
    for i, method in enumerate(methods):
        if method == "zipf":
            obs_pred_data = importData.import_obs_pred_data(
                data_dir + "data/ObsPred/Remove_1s/Stratified/" + method + "_" + zipfType + "_obs_pred_1_stratify.txt"
            )
            INh2 = importData.import_NSR2_data(
                data_dir + "data/NSR2/Remove_1s/Stratified/" + method + "_mle" + "_NSR2_1_stratify.txt"
            )
        elif method == "lognorm":
            obs_pred_data = importData.import_obs_pred_data(
                data_dir
                + "data/ObsPred/Remove_1s/Stratified/"
                + method
                + "_"
                + lognormType
                + "_obs_pred_1_stratify.txt"
            )
            INh2 = importData.import_NSR2_data(
                data_dir + "data/NSR2/Remove_1s/Stratified/" + method + "_" + lognormType + "_NSR2_1_stratify.txt"
            )
        else:
            obs_pred_data = importData.import_obs_pred_data(
                data_dir + "data/ObsPred/Remove_1s/Stratified/" + method + "_obs_pred_1_stratify.txt"
            )
            INh2 = importData.import_NSR2_data(
                data_dir + "data/NSR2/Remove_1s/Stratified/" + method + "_NSR2_1_stratify.txt"
            )
        obs = np.asarray(list(((obs_pred_data["obs"]))))
        pred = np.asarray(list(((obs_pred_data["pred"]))))
        site = np.asarray(list(((obs_pred_data["site"]))))

        obs2 = []
        pred2 = []
        site2 = []

        obs_all = np.asarray(obs)
        pred_all = np.asarray(pred)
        site_all = np.asarray(site)

        if n == "all" or len(obs) <= n:
            obs2 = list(obs)
            pred2 = list(pred)
            site2 = list(site)

        else:
            if len(obs) > n:
                inds = np.random.choice(range(len(site)), size=n, replace=False)
                for ind in inds:
                    obs2.append(obs[ind])
                    pred2.append(pred[ind])
                    site2.append(site[ind])

        obs = np.asarray(obs2)
        pred = np.asarray(pred2)
        site = np.asarray(site2)

        if method == "zipf":
            axis_min = 0
            axis_max = 2 * max(pred)
        else:
            axis_min = 0
            axis_max = 2 * max(obs)
        ax = fig.add_subplot(plot_dim, plot_dim, count + 1)

        if method == "zipf":
            NSR2_BS = importData.import_NSR2_data(
                data_dir + "data/NSR2/Stratified_Test/Remove_1s/" + method + "_mle_NSR2_1_stratify.txt"
            )
        elif method == "lognorm":
            NSR2_BS = importData.import_NSR2_data(
                data_dir + "data/NSR2/Stratified_Test/Remove_1s/" + method + "_pln_NSR2_1_stratify.txt"
            )
        else:
            NSR2_BS = importData.import_NSR2_data(
                data_dir + "data/NSR2/Stratified_Test/Remove_1s/" + method + "_NSR2_1_stratify.txt"
            )

        if method == "geom":
            ax.set_title("Broken-stick")
        elif method == "lognorm":
            ax.set_title("Lognormal")
        elif method == "mete":
            ax.set_title("Log-series")
        elif method == "zipf":
            ax.set_title("Zipf")
        macroecotools.plot_color_by_pt_dens(
            pred, obs, radius, loglog=1, plot_obj=plt.subplot(plot_dim, plot_dim, count + 1)
        )

        plt.plot([axis_min, axis_max], [axis_min, axis_max], "k-")
        if method == "zipf":
            plt.xlim(0, axis_max)
            plt.ylim(0, axis_max)
        else:
            plt.xlim(0, axis_max)
            plt.ylim(0, axis_max)
        r2s = INh2["R2"]
        r2s = r2s.astype(float)
        # insert r2 of all data
        r2_all = np.mean(((NSR2_BS["R2"])))
        print method
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format("r", r2_all, p=2)
        if method == "geom":
            plt.text(
                0.25,
                0.90,
                r2text,
                fontsize=14,
                horizontalalignment="center",
                verticalalignment="center",
                transform=ax.transAxes,
            )
        else:
            plt.text(
                0.22,
                0.90,
                r2text,
                fontsize=14,
                horizontalalignment="center",
                verticalalignment="center",
                transform=ax.transAxes,
            )
        plt.tick_params(axis="both", which="major", labelsize=10)
        plt.subplots_adjust(wspace=0.0000000001, hspace=0.5)

        axins = inset_axes(ax, width="30%", height="30%", loc=4)

        hist_r2 = np.histogram(r2s, range=(0, 1))
        xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0])
        xvals = xvals[0 : len(xvals) - 1]
        yvals = hist_r2[0]
        plt.plot(xvals, yvals, "k-", linewidth=2)
        plt.axis([0, 1, 0, 1.1 * max(yvals)])

        ax.set(adjustable="box-forced", aspect="equal")
        plt.setp(axins, xticks=[], yticks=[])
        count += 1
    plt.tight_layout(pad=1.5, w_pad=0.8, h_pad=0.8)
    fig.text(0.50, 0.02, "Predicted abundance", ha="center", va="center", fontsize=16)
    fig.text(0.08, 0.5, "Observed abundance", ha="center", va="center", rotation="vertical", fontsize=16)
    fig_name = str(mydir + "figures/" + figname + "_RGB." + saveAs)
    plt.savefig(fig_name, dpi=600, format=saveAs)  # , bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
def fig2(n=352899, figname = 'Fig2', data_dir=mydir, \
    stratify = True, radius=2, remove = 0, zipfType = 'mle', RGF = False, \
    lognormType = 'pln', saveAs = 'eps'):
    # TAKEN FROM THE mete_sads.py script used for White et al. (2012)
    # Used for Figure 3 Locey and White (2013)
    """Multiple obs-predicted plotter"""
    fig = plt.figure()
    count = 0
    plot_dim = 2
    methods = ['geom', 'lognorm', 'mete', 'zipf']
    fig.subplots_adjust(bottom= 0.30)
    for i, method in enumerate(methods):
        if method == 'zipf':
            obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+  zipfType+'_obs_pred_stratify.txt')
            INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_mle' + '_NSR2_stratify.txt')
        elif method == 'lognorm':
            obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method + '_'+  lognormType+'_obs_pred_stratify.txt')
            INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_'+  lognormType + '_NSR2_stratify.txt')
        else:
            obs_pred_data = importData.import_obs_pred_data(data_dir + 'data/ObsPred/Stratified/'+ method +'_obs_pred_stratify.txt')
            INh2 = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified/' + method + '_NSR2_stratify.txt')
        obs = np.asarray(list(((obs_pred_data["obs"]))))
        pred = np.asarray(list(((obs_pred_data["pred"]))))
        site = np.asarray(list(((obs_pred_data["site"]))))

        obs2 = []
        pred2 = []
        site2 = []

        obs_all = np.asarray(obs)
        pred_all = np.asarray(pred)
        site_all = np.asarray(site)

        if n == 'all' or len(obs) <= n:
            obs2 = list(obs)
            pred2 = list(pred)
            site2 = list(site)

        else:
            if len(obs) > n:
                inds = np.random.choice(range(len(site)), size=n, replace=False)
                for ind in inds:
                    obs2.append(obs[ind])
                    pred2.append(pred[ind])
                    site2.append(site[ind])

        obs = np.asarray(obs2)
        pred = np.asarray(pred2)
        site =  np.asarray(site2)

        if method == 'zipf':
            axis_min = 0
            axis_max = 2  * max(pred)
        else:
            axis_min = 0
            axis_max = 2 * max(obs)
        ax = fig.add_subplot(plot_dim, plot_dim, count+1)
        if method == 'zipf':
            NSR2_BS = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/'+ method  + '_mle_NSR2_stratify.txt')
        elif method == 'lognorm':
            NSR2_BS = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/'+ method  + '_pln_NSR2_stratify.txt')
        else:
            NSR2_BS = importData.import_NSR2_data(data_dir + 'data/NSR2/Stratified_Test/'+ method  +'_NSR2_stratify.txt')

        if method == 'geom':
            ax.set_title("Broken-stick")
        elif method == 'lognorm':
            ax.set_title("Lognormal")
        elif method == 'mete':
            ax.set_title("Log-series")
        elif method == 'zipf':
            ax.set_title("Zipf")
        print len(pred), len(obs)
        macroecotools.plot_color_by_pt_dens(pred, obs, radius, loglog=1,
                        plot_obj=plt.subplot(plot_dim,plot_dim,count+1))

        plt.plot([axis_min, axis_max],[axis_min, axis_max], 'k-')
        if method == 'zipf':
            plt.xlim(0, axis_max)
            plt.ylim(0, axis_max)
        else:
            plt.xlim(0, axis_max)
            plt.ylim(0, axis_max)
        r2s = ((INh2["R2"]))
        r2s = r2s.astype(float)
        # insert r2 of all data
        r2_all = np.mean(((NSR2_BS["R2"])))
        print method + ' mean = ' + str(r2_all)
        print method + ' std dev = ' +str(np.std(r2_all))
        r2text = r"${}^{{2}}_{{m}} = {:.{p}f} $".format('r',r2_all , p=2)
        if method == 'geom':
            plt.text(0.25, 0.90, r2text,  fontsize=14,
                horizontalalignment='center',
                verticalalignment='center',transform = ax.transAxes)
        else:
            plt.text(0.22, 0.90, r2text,  fontsize=14,
                horizontalalignment='center',
                verticalalignment='center',transform = ax.transAxes)
        plt.tick_params(axis='both', which='major', labelsize=10)
        plt.subplots_adjust(wspace=0.0000000001, hspace=0.5)

        axins = inset_axes(ax, width="30%", height="30%", loc=4)

        hist_r2 = np.histogram(r2s, range=(0, 1))
        xvals = hist_r2[1] + (hist_r2[1][1] - hist_r2[1][0])
        xvals = xvals[0:len(xvals)-1]
        yvals = hist_r2[0]
        plt.plot(xvals, yvals, 'k-', linewidth=2)
        plt.axis([0, 1, 0, 1.1 * max(yvals)])

        ax.set(adjustable='box-forced', aspect='equal')
        plt.setp(axins, xticks=[], yticks=[])


        count += 1
    plt.tight_layout(pad=1.5, w_pad=0.8, h_pad=0.8)
    fig.text(0.50, 0.03, 'Predicted abundance', ha='center', va='center', fontsize=16)
    fig.text(0.08, 0.5, 'Observed abundance', ha='center', va='center', rotation='vertical', fontsize=16)
    fig_name = str(mydir + 'figures/' + figname + '_RGB.' + saveAs)
    plt.savefig(fig_name, dpi=600, format = saveAs)#, bbox_inches = 'tight')#, pad_inches=0)
    plt.close()
Esempio n. 22
0
def plot_obs_pred(sad_data, envpred_sads, dest_file='./obs_pred.png'):
    plot_color_by_pt_dens(envpred_sads['EnvPred'], sad_data['Obs'], 3, loglog=1)
    plt.loglog([min(envpred_sads['EnvPred']), max(envpred_sads['EnvPred'])], 
               [min(envpred_sads['EnvPred']), max(envpred_sads['EnvPred'])], 'k-')
    plt.savefig(dest_file, dpi = 400)