Beispiel #1
0
def get_fake(**kwargs):
    fake = '/astro/net/angst2/philrose/PHAT/paper/referree/test.st'
    d = np.loadtxt(fake, unpack=True)

    x = d[2]
    y = d[3]
    Chi = d[4]
    SNR = d[5]
    Sharp = d[6]
    Round = d[7]
    Crowd = d[9]
    Type = d[10]
    mag_F275W = d[15]
    chi_F275W = d[18]
    snr_F275W = d[19]
    sharp_F275W = d[20]
    round_F275W = d[21]
    crowd_F275W = d[22]
    flag_F275W = d[23]
    mag_F336W = d[28]
    chi_F336W = d[31]
    snr_F336W = d[32]
    sharp_F336W = d[33]
    round_F336W = d[34]
    crowd_F336W = d[35]
    flag_F336W = d[36]

    cut_F336W = data.quality_cut(sharp_F336W, snr_F336W, crowd_F336W,
                                 round_F336W, **kwargs)
    cut_F275W = data.quality_cut(sharp_F275W, snr_F275W, crowd_F275W,
                                 round_F275W, **kwargs)

    cut_both = list(set(cut_F336W) & set(cut_F275W))
    return mag_F336W[cut_both]
Beispiel #2
0
def new_mag_cut(**kwargs):
    '''
    this can run by a call to ASTs, not just by running main
    set the magnitude cutoff to be the faintest recovered magnitude of a 
    star whose input magnitude was at the 50% completeness limit
    '''
    ast_file = kwargs.get('ast_file')
    comp_data = kwargs.get('comp_data')

    ast = load_ast_file(ast_file)
    comp = file_io.load_comp_data(comp_data)
    pm = 0.05

    # within dmag range
    rec1 = recovered_asts(ast['mag1in'], ast['mag1out'])
    rec2 = recovered_asts(ast['mag2in'], ast['mag2out'])

    # quality cut on all asts
    qc1 = quality_cut(ast['sharp1'],
                      ast['snr1'],
                      ast['crowd1'],
                      ast['round1'],
                      loud=True,
                      **kwargs)

    qc2 = quality_cut(ast['sharp2'],
                      ast['snr2'],
                      ast['crowd2'],
                      ast['round2'],
                      loud=True,
                      **kwargs)
    # each radial bin
    from params import PHAT_DIR_EXTRA3
    contour = PHAT_DIR_EXTRA3 + 'contours_v3.reg'
    inds = points_inside_ds9_polygon(contour, ast['ra'], ast['dec'])
    filters = ('F275W', 'F336W')
    ax1s = graphics.setup_five_panel_plot(fig=1)
    [ax.set_xlabel('$%s$ Recovered' % filters[0]) for ax in ax1s]
    ax1s[0].set_ylabel('$%s$ Recovered-Input' % filters[0])
    cols = kwargs.get('cols')
    axs = graphics.two_panel_plot_vert_bsquat()
    axs[0].set_xlabel('F275W Recovered')
    axs[1].set_ylabel('standard deviation')
    axs[0].set_ylabel('residual')
    for i in range(len(inds)):
        Rinds1 = list(set(rec1) & set(qc1) & set(inds[i]))
        #Rinds1 = list(set(rec1) & set(inds[i]))
        #Rinds1 = list(set(rec1) & set(qc1) & set(qc2) & set(inds[i]))
        within1 = np.nonzero(
            (ast['mag1in'][Rinds1] <= comp['comp50mag1'][i] + pm)
            & (ast['mag1in'][Rinds1] >= comp['comp50mag1'][i] - pm))[0]
        mag1_cut = np.max(ast['mag1out'][Rinds1][within1])
        print mag1_cut
        ast_diff_test(ast['mag1out'][Rinds1],
                      ast['mag1in'][Rinds1],
                      axs=axs,
                      color=cols[i],
                      spacing=0.4)
        axs[0].set_ylim(-0.01, 0.1)
        ax1s[i].plot(ast['mag1out'][inds[i]],
                     ast['mag1out'][inds[i]] - ast['mag1in'][inds[i]],
                     '.',
                     color='black')
        ax1s[i].plot(ast['mag1out'][Rinds1],
                     ast['mag1out'][Rinds1] - ast['mag1in'][Rinds1],
                     '.',
                     color='blue')
        #ax1s[i].plot(ast['mag1in'][inds[i]],ast['mag1out'][inds[i]]-ast['mag1in'][inds[i]],'.',color='black')
        #ax1s[i].plot(ast['mag1in'][Rinds1],ast['mag1out'][Rinds1]-ast['mag1in'][Rinds1],'.',color='blue')
        ax1s[i].plot(np.zeros(100) + mag1_cut,
                     np.linspace(-2, 10, 100),
                     color='green')
        ax1s[i].plot(np.zeros(100) + comp['comp50mag1'][i],
                     np.linspace(-2, 10, 100),
                     color='red')
        ax1s[i].xaxis.set_major_locator(MultipleLocator(2))
        ax1s[i].set_xlim(19, 27)
        ax1s[i].set_ylim(-2, 2)
    plt.savefig('ast_diff_mag1%s' % EXT)

    ax2s = graphics.setup_five_panel_plot(fig=2)
    [ax.set_xlabel('$%s$ Recovered' % filters[1]) for ax in ax2s]
    ax2s[0].set_ylabel('$%s$ Recovered-Input' % filters[1])
    axs = graphics.two_panel_plot_vert_bsquat()
    axs[0].set_xlabel('F336W Recovered')
    axs[1].set_ylabel('standard deviation')
    axs[0].set_ylabel('residual')
    for i in range(len(inds)):
        Rinds2 = list(set(rec2) & set(qc2) & set(inds[i]))
        #Rinds2 = list(set(rec2) & set(qc1) & set(qc2) & set(inds[i]))
        within2 = np.nonzero(
            (ast['mag2in'][Rinds2] <= comp['comp50mag2'][i] + pm)
            & (ast['mag2in'][Rinds2] >= comp['comp50mag2'][i] - pm))[0]
        mag2_cut = np.max(ast['mag2out'][Rinds2][within2])
        print mag2_cut
        ast_diff_test(ast['mag2out'][Rinds2],
                      ast['mag2in'][Rinds2],
                      axs=axs,
                      color=cols[i],
                      spacing=0.4)
        axs[0].set_ylim(-0.01, 0.1)
        ax2s[i].plot(ast['mag2out'][inds[i]],
                     ast['mag2out'][inds[i]] - ast['mag2in'][inds[i]],
                     '.',
                     color='black')
        ax2s[i].plot(ast['mag2out'][Rinds2],
                     ast['mag2out'][Rinds2] - ast['mag2in'][Rinds2],
                     '.',
                     color='blue')
        ax2s[i].plot(np.zeros(100) + mag2_cut,
                     np.linspace(-2, 10, 100),
                     color='green')
        ax2s[i].plot(np.zeros(100) + comp['comp50mag2'][i],
                     np.linspace(-2, 10, 100),
                     color='red')
        ax2s[i].xaxis.set_major_locator(MultipleLocator(2))
        ax2s[i].set_xlim(19, 27)
        ax2s[i].set_ylim(-2, 2)
    plt.savefig('ast_diff_mag2%s' % EXT)
    return
Beispiel #3
0
def main(**kwargs):
    ast_file = kwargs.get('ast_file')
    ast = load_ast_file(ast_file)
    from params import PHAT_DIR_EXTRA3
    contour = PHAT_DIR_EXTRA3 + 'contours_v3.reg'
    cras, cdecs = file_io.read_reg(contour)
    outverts = np.column_stack((cras[-1], cdecs[-1]))

    rec1 = recovered_asts(ast['mag1in'], ast['mag1out'])
    rec2 = recovered_asts(ast['mag2in'], ast['mag2out'])

    # quality cut on all asts
    qc1 = quality_cut(ast['sharp1'], ast['snr1'], ast['crowd1'], ast['round1'],
                      **kwargs)

    qc2 = quality_cut(ast['sharp2'], ast['snr2'], ast['crowd2'], ast['round2'],
                      **kwargs)

    qcd = list(set(qc1) & set(qc2))
    inds = points_inside_ds9_polygon(contour, ast['ra'], ast['dec'])
    comp_corr_file = open(kwargs.get('comp_file'), 'w')
    comp_corr_file.write('# region (0=in) mags comp_frac \n')
    comp_data_file = open(kwargs.get('comp_data'), 'w')
    print 'comp correction file:', kwargs.get('comp_file')
    print 'comp data file:', comp_data_file
    single = kwargs.get('single')
    kwargs['ast_file_ext'] = '_' + kwargs.get('comp_file').replace(
        '.dat', '').split('_')[-1]
    if single == False:
        qc1 = qcd
        qc2 = qcd
        kwargs['ast_file_ext'] += '_dd'
        color_range = (-0.7, 0.5)
        ast_color = ast['mag1in'] - ast['mag2in']
        color_inds = np.nonzero((ast_color > color_range[0])
                                & (ast_color < color_range[1]))[0]

    Rinds1s, Rinds2s, Bins, Frac1s, Frac2s = [], [], [], [], []
    Rhist1s, Rhist2s, Qhist1s, Qhist2s = [], [], [], []
    comp_data_file.write(
        '# Region comp50mag1 comp50mag2 comp90mag1 comp90mag2\n')
    print '# Region comp50mag1 comp50mag2 comp90mag1 comp90mag2'
    for i in range(len(inds)):
        # in each annulus, passed qc, recovered
        if single == False:
            Rinds1 = list(
                set(rec1) & set(qc1) & set(inds[i]) & set(color_inds))
            Rinds2 = list(
                set(rec2) & set(qc2) & set(inds[i]) & set(color_inds))
            # REWRITING INDS OF I FOR THIS COLOR RANGE.
            inds[i] = list(set(inds[i]) & set(color_inds))
        else:
            Rinds1 = list(set(rec1) & set(qc1) & set(inds[i]))
            Rinds2 = list(set(rec2) & set(qc2) & set(inds[i]))

        hist_range = (18.34, 28.)
        # use hist_bin_optimization to get this value
        #spacing = 0.04
        spacing = 0.2
        Rhist1, Rbins1 = np.histogram(ast['mag1in'][Rinds1],
                                      bins=np.arange(hist_range[0],
                                                     hist_range[1], spacing))
        Rhist2, Rbins2 = np.histogram(ast['mag2in'][Rinds2],
                                      bins=np.arange(hist_range[0],
                                                     hist_range[1], spacing))
        Qhist1, Qbins1 = np.histogram(ast['mag1in'][inds[i]],
                                      bins=np.arange(hist_range[0],
                                                     hist_range[1], spacing))
        Qhist2, Qbins2 = np.histogram(ast['mag2in'][inds[i]],
                                      bins=np.arange(hist_range[0],
                                                     hist_range[1], spacing))

        # need to be float...
        frac1 = np.array(map(float, Qhist1)) / np.array(map(float, Rhist1))
        frac2 = np.array(map(float, Qhist2)) / np.array(map(float, Rhist2))

        c_lim1 = GenUtils.closest_match(0.5, 1 / frac1)
        c_lim2 = GenUtils.closest_match(0.5, 1 / frac2)
        c_lim1a = GenUtils.closest_match(0.90, 1 / frac1)
        c_lim2a = GenUtils.closest_match(0.90, 1 / frac2)

        if i == 0:
            inmag1 = c_lim1
            inmag2 = c_lim2
            [comp_corr_file.write('%.2f ' % bin) for bin in Rbins1]
            comp_corr_file.write('\n')
        else:
            print 1. / frac1[inmag1], 1. / frac2[inmag2]
            comp_data_file.write(
                '# inner mag limit corresponds to 50 percent completeness \n# 275: %.3f \n# 336: %.3f \n'
                % (1. / frac1[inmag1], 1. / frac2[inmag2]))

        comp_corr_file.write('F336W %i ' % i)
        [comp_corr_file.write('%.8f ' % frac) for frac in frac1]
        comp_corr_file.write('\nF275W %i ' % i)
        [comp_corr_file.write('%.8f ' % frac) for frac in frac2]
        comp_corr_file.write('\n')

        # for Table1 need comp50,98 index 0 to be inner ring.
        comp50mag1 = Rbins1[c_lim1]
        comp50mag2 = Rbins1[c_lim2]
        comp92mag1 = Rbins1[c_lim1a]
        comp92mag2 = Rbins1[c_lim2a]

        comp_data_file.write(
            '%i %.1f %.1f %.1f %.1f\n' %
            (i, comp50mag1, comp50mag2, comp92mag1, comp92mag2))
        print '%i %.1f %.1f %.1f %.1f' % (i, comp50mag1, comp50mag2,
                                          comp92mag1, comp92mag2)

        Rinds1s.append(Rinds1)
        Rinds2s.append(Rinds2)
        Bins.append(Rbins1)
        Frac1s.append(frac1)
        Frac2s.append(frac2)
        Rhist1s.append(Rhist1)
        Rhist2s.append(Rhist2)
        Qhist1s.append(Qhist1)
        Qhist2s.append(Qhist2)

    ax1, ax2 = ast_completeness_plot(Bins, Frac1s, Frac2s, **kwargs)
    ast_diff_plot(ast, Rinds1s, Rinds2s, inds, **kwargs)
    ast_hist_plot(Bins, Rhist1s, Qhist1s, Rhist2s, Qhist2s, **kwargs)

    comp_corr_file.close()
    comp_data_file.close()
Beispiel #4
0
def ast_LF(ast, **kwargs):
    cont_file = kwargs.get('cont_file')
    one_plot = kwargs.get('one_plot', True)
    powerlaw = lambda x, amp, index: amp * (x**index)
    import CrowdTest
    cols = kwargs.get('cols')
    double_plot = kwargs.get('double_plot', False)
    two_panel = kwargs.get('two_panel', True)
    lf_o = CrowdTest.load_saved_lf(
        filename=
        '/Users/phil/research/PHAT/code/UVBrights/diy_final/lf_data.dat')
    comp_data = kwargs['comp_data']
    mag1_cut = file_io.get_comp90mags(comp_data)[0][0]
    mag2_cut = file_io.get_comp90mags(comp_data)[1][0]
    print '# mag1_cut, mag2_cut', mag1_cut, mag2_cut
    if cont_file != None:
        cf = open(cont_file, 'w')
        cf.write('# mag1_cut, mag2_cut %.1f %.1f\n' % (mag1_cut, mag2_cut))
    from params import PHAT_DIR_EXTRA3
    contour = PHAT_DIR_EXTRA3 + 'contours_v3.reg'
    inds = calcs.points_inside_ds9_polygon(contour, ast['ra'], ast['dec'])
    filters = ('F275W', 'F336W')

    rec1 = recovered_asts(ast['mag1in'], ast['mag1out'])
    rec2 = recovered_asts(ast['mag2in'], ast['mag2out'])

    # quality cut on all asts
    qc1 = quality_cut(ast['sharp1'],
                      ast['snr1'],
                      ast['crowd1'],
                      ast['round1'],
                      loud=True,
                      **kwargs)

    qc2 = quality_cut(ast['sharp2'],
                      ast['snr2'],
                      ast['crowd2'],
                      ast['round2'],
                      loud=True,
                      **kwargs)
    spacing = .05
    if two_panel == True:
        ax1, ax2 = graphics.two_panel_plot(11, 5, 'x', 'x', '$\#$')
        axs = [ax1, ax2, ax1, ax2, ax1]
    for j, filter in enumerate(filters):
        fcolor = 'blue'
        if double_plot == True:
            axs, baxs = graphics.setup_ten_panel_plot(fig=j + 1, noy=True)
        elif two_panel == False:
            fig = j + 1
            if one_plot == True: fig = 1
            axs = graphics.setup_five_panel_plot(fig=fig, noy=True)
            if j != 0: fcolor = 'green'
        sortkeys = np.sort(lf_o[filter].keys())
        for k, region in enumerate(sortkeys):
            if filter == 'F336W':
                qc = qc2
                ast_mag_in = ast['mag2in']
                ast_mag_out = ast['mag2out']
                mag = mag2_cut
                if two_panel == True: axs[k] = ax2
                axs[k].set_xlim(26.1, 16)
                index = 16.943523372280001
                amp = 1e-17
            if filter == 'F275W':
                qc = qc1
                ast_mag_in = ast['mag1in']
                ast_mag_out = ast['mag1out']
                mag = mag1_cut
                if two_panel == True: axs[k] = ax1
                axs[k].set_xlim(25.9, 16)
                index = 19.246755161139998
                amp = 1e-20

            Rinds = list(set(rec1) & set(qc) & set(inds[k]))
            bins = np.arange(np.min(ast_mag_in[Rinds]),
                             np.max(ast_mag_in[Rinds]), spacing)
            hist = powerlaw(bins, 1e-17, 15)
            #hist = powerlaw(bins,amp,index)
            # paste tests here
            axs[k].plot(bins, hist, ls='steps', color='black')
            hist_o, bins_o = lf_o[filter][region].values()
            #axs[k].plot(bins_o,np.array(hist_o)*1e2,ls='steps',color=cols[k])
            # bin up input mag
            in1s = np.digitize(ast_mag_in[Rinds], bins)
            new_mags = np.array([])
            for i in range(len(bins)):
                if hist[i] == 0: continue
                if len(np.array(in1s == i).nonzero()[0]) <= 1: continue
                # output mag corresponding to the input mag bin
                ast_mag = ast_mag_out[Rinds][np.array(in1s == i).nonzero()[0]]
                # make cumulative distribution
                sort_ast = np.sort(ast_mag)
                yvals = np.arange(len(sort_ast)) / float(len(sort_ast))
                # interpolate it from 0,1
                f = scipy.interpolate.interp1d(yvals,
                                               sort_ast,
                                               bounds_error=False)
                # pick randomly from distribution the same number as in the current histogram
                new_mags = np.append(new_mags, f(np.random.random(hist[i])))
            # find new LF
            new_hist, lixo = np.histogram(new_mags, bins=bins)
            #print sum(new_hist), sum(hist)
            axs[k].plot(bins[1:], new_hist, ls='steps', color=fcolor, lw=2)
            ofst = 0.1
            #mag = closest_match(bins[1:],(new_hist-hist[1:])/hist[1:],.2)
            g = scipy.interpolate.interp1d(bins[1:],
                                           (new_hist - hist[1:]) / hist[1:])
            print filter, k, g(mag)
            if cont_file != None:
                cf.write('%s %i %f\n' % (filter, k, g(mag)))
            if double_plot == True:
                baxs[k].plot(np.zeros(10) + mag,
                             np.linspace(baxs[k].get_ylim()[0],
                                         baxs[k].get_ylim()[1], 10),
                             '--',
                             color='black',
                             label='%.2f' % g(mag))
                baxs[k].set_xlim(
                    np.max(ast_mag_in) - ofst,
                    np.min(ast_mag_in) + ofst)
                baxs[k].legend(loc=0, frameon=False)
                baxs[k].set_xlabel('$%s$' % filter)
                baxs[k].plot(bins[1:], (new_hist - hist[1:]) / hist[1:],
                             color='black',
                             lw=2,
                             ls='steps')
            else:
                if one_plot == True:
                    axs[k].set_xlabel(r'$\rm{mag}$')
                    if k != 0: axs[k].yaxis.set_major_formatter(nullfmt)
                else:
                    axs[k].set_xlabel('$%s$' % filter)
            axs[k].xaxis.set_major_locator(MultipleLocator(2))
            axs[k].xaxis.set_minor_locator(MultipleLocator(1))
            axs[k].set_ylim(1e1, 1e5)
            axs[k].plot(np.zeros(3) + mag,
                        np.logspace(1, 5, 3),
                        '--',
                        lw=2,
                        color=fcolor,
                        label='%.2f' % g(mag))
            axs[k].set_yscale('log')
        axs[0].set_ylabel('$\#$')
        axs[k].annotate(r'$F275W$', xy=(22, 5e4), fontsize=20, color='blue')
        axs[k].annotate(r'$F336W$', xy=(22, 3e4), fontsize=20, color='green')
        [ax.set_ylim(1e2, 1e5) for ax in axs]
        [ax.set_xlim(26, 19) for ax in axs]
        if double_plot == True: baxs[0].set_ylabel('(input-obs)/input')
        if one_plot != True:
            plt.figure(j + 1)
            plt.savefig('ast_lf_test_%s%s' % EXT % filter)
        else:
            plt.savefig('ast_lf_test%s' % EXT)
    if cont_file != None: cf.close()
    return axs