Пример #1
0
def saveHistogram(vector, name, title, xlabel, ylabel):
    plt.figure()
    hist(vector, bins="blocks", histtype = "step")
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.savefig(name)
Пример #2
0
def plot_size_of_vertical_isolated_lobes():
    filename = 'value_added_selection_NN'
    print(filename)
    tdata = Table(fits.open('../%s.fits' % filename)[1].data)

    size = tdata['size_thesis'] / 60.

    # Use fancy algorithms to determine the bins
    from astropy import visualization
    visualization.hist(size,
                       bins='scott',
                       histtype=u'step',
                       color='black',
                       label='All sources')

    where = np.where((tdata['position_angle'] < 100)
                     & (tdata['position_angle'] > 80))
    size_vertical = size[where]
    visualization.hist(size_vertical,
                       bins='scott',
                       histtype=u'step',
                       label='80<PA<100')

    # plt.yscale('log')
    plt.legend(fontsize=14, loc='upper left')
    plt.xlabel(r'Size (arcmin)', fontsize=14)
    plt.ylabel('Counts', fontsize=14)
    plt.tight_layout()

    plt.show()
Пример #3
0
def matched_sources_redshift():

    filename = 'value_added_selection_NN'
    print(filename)
    tdata = Table(fits.open('../%s.fits' % filename)[1].data)
    z_available = np.invert(np.isnan(tdata['z_best']))
    tdata = tdata[z_available]

    spectro_where = np.where(tdata['z_source'] == 1)
    spec = tdata[spectro_where]

    position_angle = tdata['position_angle']
    # Use fancy algorithms to determine the bins
    from astropy import visualization
    visualization.hist(position_angle,
                       bins='scott',
                       histtype=u'step',
                       color='black',
                       label='All sources')
    visualization.hist(spec['position_angle'],
                       bins='scott',
                       histtype=u'step',
                       label='Spectro')

    # plt.yscale('log')
    plt.legend(fontsize=14, loc='upper left')
    plt.xlabel(r'Position angle (degrees)', fontsize=14)
    plt.ylabel('Counts', fontsize=14)
    plt.tight_layout()
    plt.title('NN only redshift sources')

    plt.show()
Пример #4
0
def test_hist_specify_ax(rseed=0):
    rng = np.random.default_rng(rseed)
    x = rng.standard_normal(100)

    fig, ax = plt.subplots(2)
    n1, bins1, patches1 = hist(x, 10, ax=ax[0])
    assert patches1[0].axes is ax[0]

    n2, bins2, patches2 = hist(x, 10, ax=ax[1])
    assert patches2[0].axes is ax[1]
Пример #5
0
def test_hist_specify_ax(rseed=0):
    rng = np.random.RandomState(rseed)
    x = rng.randn(100)

    fig, ax = plt.subplots(2)
    n1, bins1, patches1 = hist(x, 10, ax=ax[0])
    assert patches1[0].axes is ax[0]

    n2, bins2, patches2 = hist(x, 10, ax=ax[1])
    assert patches2[0].axes is ax[1]
Пример #6
0
def test_histogram_pathological_input():
    # Regression test for https://github.com/astropy/astropy/issues/7758

    # The key feature of the data below is that one of the points is very,
    # very different than the rest. That leads to a large number of bins.
    data = [
        9.99999914e+05, -8.31312483e-03, 6.52755852e-02, 1.43104653e-03,
        -2.26311017e-02, 2.82660007e-03, 1.80307521e-02, 9.26294279e-03,
        5.06606026e-02, 2.05418011e-03
    ]

    with pytest.raises(ValueError):
        hist(data, bins='freedman', max_bins=10000)
Пример #7
0
def plot_E_PA_vs_E_position_angle():
    '''
	For the fidelity of the E_position angles:

	Compare E_PA_1 (PyBDSF PA) to  (my algorithm)
	for the multi-gaussian sources. 

	'''
    filename = 'value_added_selection_MG'
    print(filename)
    tdata = Table(fits.open('../%s.fits' % filename)[1].data)

    E_PA_1 = tdata['E_PA_1']
    E_position_angle = tdata['err_orientation']

    cutoff = 5  # degrees

    count_agree = 0
    count_disagree = 0
    for i in range(len(tdata)):
        difference = get_distance(E_PA_1[i], E_position_angle[i])
        if difference > cutoff:
            count_disagree += 1
        else:
            count_agree += 1

    # print ('Number of E_PA agreeing within %i degrees: %i'%(cutoff,count_agree))
    # print ('Number of E_PA disagreeing within %i degrees: %i'%(cutoff,count_disagree))
    # print ('Percentage agreeing: %i/%i = %f percent'%(count_agree,len(tdata),(count_agree/float(len(tdata))*100)))

    print('Number of E_PA < 5 degrees: %i' % (np.sum(E_PA_1 < 5)))
    print('Which is %i/%i = %f' % (np.sum(E_PA_1 < 5), len(tdata),
                                   np.sum(E_PA_1 < 5) / float(len(tdata))))

    # plt.hist(E_PA_1,bins=20,histtype=u'step')#,label='PyBDSF',alpha=0.5)
    # plt.hist(E_position_angle,bins=180/5,histtype=u'step',label='This study',alpha=0.5)

    # Use fancy algorithms to determine the bins
    from astropy import visualization
    visualization.hist(E_PA_1, bins='scott', histtype=u'step', color='black')

    # plt.yscale('log')
    # plt.legend(fontsize=14)
    plt.xlabel(r'1$\sigma$ error (degrees)', fontsize=14)
    plt.ylabel('Counts', fontsize=14)
    plt.tight_layout()

    plt.show()
Пример #8
0
 def _plot_scalar_state_distribution(figure, state_name, state_val,
                                     volume_target, missed_imp_penalty,
                                     ref):
     figure.set_size_inches(10, 5)
     ax1 = figure.add_subplot(2, 1, 1)
     if state_val.shape[0] > 3:  # use adaptative number of bins
         try:
             counts, bins, patches = hist(state_val[:, -1, 0],
                                          bins='knuth',
                                          ax=ax1)
         except ValueError as err:
             print("Falling back to regular hist, error was:", err)
             counts, bins, patches = ax1.hist(state_val[:, -1, 0])
     else:
         counts, bins, patches = ax1.hist(state_val[:, -1, 0])
     ax1.set_xlabel(
         f"{state_name.decode('utf-8')} (v={volume_target}, mip={missed_imp_penalty})"
     )
     ax2 = figure.add_subplot(2, 1, 2)
     ax2.hist(state_val[:, -1, 0],
              bins=bins,
              cumulative=True,
              histtype='step',
              density=True)
     if ref is not None:
         ax2.vlines(ref, 0, 1, colors='r', linewidth=2)
     ax1.set_xlabel(
         f"{state_name.decode('utf-8')} (v={volume_target}, mip={missed_imp_penalty})"
     )
     figure.tight_layout()
Пример #9
0
def test_hist_basic(rseed=0):
    rng = np.random.default_rng(rseed)
    x = rng.standard_normal(100)

    for range in [None, (-2, 2)]:
        n1, bins1, patches1 = plt.hist(x, 10, range=range)
        n2, bins2, patches2 = hist(x, 10, range=range)

        assert_allclose(n1, n2)
        assert_allclose(bins1, bins2)
Пример #10
0
def test_hist_basic(rseed=0):
    rng = np.random.RandomState(rseed)
    x = rng.randn(100)

    for range in [None, (-2, 2)]:
        n1, bins1, patches1 = plt.hist(x, 10, range=range)
        n2, bins2, patches2 = hist(x, 10, range=range)

        assert_allclose(n1, n2)
        assert_allclose(bins1, bins2)
Пример #11
0
def plot_in_cols(quant_list, col1_vals, graph_width, hmin, hmax, bin_val,
                 htype, title_str):
    """ Input:
            quant_list: list - for each item in col1_vals "i", this list
                contains a corresponding item. This item is a list containing
                a boolean index. If the item in col1 has a value of "i", and
                the corresponding item in col2 exists and is not "unknown",
                then it has a value of True, otherwise it has a value of False.
            col1_vals: a list containing the names of the items in col1 for
                which we want to make comparison graphs
            graph_width: int - the width of the graph in inches
            hmin: int - the smallest value contained in col2
            hmax: int - the largest value contained in col2
            bin_val: string - the binning algorithm to be used
            htype: the type of histogram to draw
            title_str: string - a string which will be made into the title in
                the following way:
                    title_str.format("value contained in x_vals")
                With the current setup, the unmodified x_val entry will be
                displayed as the title.
    """
    clen = len(col1_vals)
    ax_list = []
    plot_height = clen // 2 + clen % 2
    fig = plt.figure(figsize=(graph_width, plot_height * 5))
    img = get_scaled_img(fig)
    ax_im = plt.imshow(img)
    plt.axis('off')
    for v1, v2, i in zip(quant_list, col1_vals, range(clen)):
        if len(ax_list) == 0:
            ax = fig.add_subplot(plot_height, 2, i + 1, alpha=0)
        else:
            ax = fig.add_subplot(plot_height,
                                 2,
                                 i + 1,
                                 alpha=0,
                                 sharex=ax_list[0],
                                 sharey=ax_list[0])
        ax_list.append(ax)
        n, bins, patches = hist(v1,
                                bins=bin_val,
                                ax=ax,
                                histtype=htype,
                                alpha=0.7,
                                density=True)
        mu, std = norm.fit(v1)
        x = np.linspace(hmin, hmax, 100)
        y = norm.pdf(x, mu, std)
        c = match_hist_color(fig)
        l = ax.plot(x, y, color=c, linewidth=3, alpha=1)
        ax.set_title(title_str.format(col1_vals[i]))
    return fig, ax_list
Пример #12
0
def initial_subsample_hist_AP(PT=False):
    '''histogram of the position angles of initial subsample using Astropy'''
    tdata = '../biggest_selection.fits'
    tdata = Table(fits.open(tdata)[1].data)

    if PT:
        position_angles = do_PT(tdata)
    else:
        position_angles = tdata['position_angle']

    visualization.hist(position_angles,
                       bins='scott',
                       histtype=u'step',
                       color='black')

    # plt.hist(position_angles,bins=180/5,histtype=u'step',color='black')
    plt.ylabel('Counts', fontsize=12)
    plt.xlabel('Position angle (degrees)', fontsize=12)
    plt.xlim(-8.9863954872984522, 188.91420232532226)
    # plt.ylim(0,250)
    plt.tight_layout()
    plt.show()
Пример #13
0
def plot_single_axis(quant_list,
                     col1_vals,
                     graph_width,
                     hmin,
                     hmax,
                     bin_val,
                     htype,
                     add_legend=True):
    """ Input:
            quant_list: list - for each item in col1_vals "i", this list
                contains a corresponding item. This item is a list containing
                a boolean index. If the item in col1 has a value of "i", and
                the corresponding item in col2 exists and is not "unknown",
                then it has a value of True, otherwise it has a value of False.
            col1_vals: a list containing the names of the items in col1 for
                which we want to make comparison graphs
            graph_width: int - the width of the graph in inches
            hmin: int - the smallest value contained in col2
            hmax: int - the largest value contained in col2
            bin_val: string - the binning algorithm to be used
            htype: the type of histogram to draw
            add_legend: should a legend be added?

    """
    clen = len(col1_vals)
    fig, ay = plt.subplots(figsize=(graph_width, graph_width))
    img = get_scaled_img(fig)
    ay.imshow(img)
    ax = fig.add_subplot(111)
    print(hmin, hmax)
    for v1, v2, i in zip(quant_list, col1_vals, range(clen)):
        n, bins, patches = hist(v1,
                                bins=bin_val,
                                histtype=htype,
                                alpha=0.7,
                                density=True,
                                label=col1_vals[i])
        mu, std = norm.fit(v1)
        x = np.linspace(hmin, hmax, 100)
        y = norm.pdf(x, mu, std)
        c = match_hist_color(fig)
        l = ax.plot(x, y, color=c, linewidth=2, alpha=1, zorder=15 + i)
        ax = axis_style(ax, "", alpha=0)
        ax.tick_params('both', labelsize=16)
    if add_legend:
        asset_dir = os.environ['ASSET_DIR']
        fpath = os.path.join(asset_dir, 'fonts', 'starjedi', 'Starjedi.ttf')
        prop = fm.FontProperties(fname=fpath, size=22)
        plt.legend(loc='upper right', prop=prop, bbox_to_anchor=[1, .95])
    return fig, ax, patches
Пример #14
0
    def plot_distributions(self,
                           hist_blocks='knuth',
                           color_hist='lightgrey',
                           edgecolor='black',
                           fontsize=16,
                           save_fig=True):
        """
        Plot feature distributions (i.e., where the clustering is applied).
        Only works if data has been previously scaled.
        """
        index = iter(np.arange(len(self.features)))
        figure = plt.figure(figsize=[30, 10])

        for scl_col in self.features:
            index_i = next(index)
            ax = plt.subplot(2, len(self.features), 1 + index_i)
            bin_h, bin_b, _ = hist(self.data_tb[scl_col],
                                   hist_blocks,
                                   color=color_hist,
                                   edgecolor=edgecolor)
            plt.title(scl_col, fontsize=fontsize)
            plt.xticks(fontsize=fontsize)
            plt.yticks(fontsize=fontsize)

            ax = plt.subplot(2, len(self.features),
                             1 + len(self.features) + index_i)
            bin_h, bin_b, _ = hist(self.data_scl[:, index_i],
                                   hist_blocks,
                                   color=color_hist,
                                   edgecolor=edgecolor)
            plt.xticks(fontsize=fontsize)
            plt.yticks(fontsize=fontsize)

        plt.show()
        if save_fig:
            fig_nm = f'{self.out_path}{self.label}_scl_{self.scaler}.pdf'
            self.save_fig(figure, fig_nm=fig_nm)
Пример #15
0
    def get_Rp_from_depth(
        self,
        depth=None,
        Rstar=None,
        nsamples=10000,
        percs=[50, 16, 84],
        plot=False,
        apply_contratio=False,
        return_samples=False,
    ):
        """
        estimate Rp from depth via Monte Carlo to account for Rs err
        Rs is taken from TICv8
        """
        if depth is None:
            depth = (self.toi_depth, self.toi_depth_err)
        else:
            assert isinstance(depth, tuple)
        if Rstar is None:
            # TICv8 since starhorse has no Rstar
            if self.tic_params is None:
                _ = self.query_tic_catalog(return_nearest_xmatch=True)
            # FIXME: self.tic_params.rad is different from self.toi_Rstar
            # e.g. see toi 179
            Rstar = (self.toi_Rstar, self.toi_Rstar_err)
        else:
            assert isinstance(Rstar, tuple)
        # FIXME: is TOI depth raw or undiluted?
        depth = stats.truncnorm(
            a=(0 - self.toi_depth) / self.toi_depth_err,
            b=(1 - self.toi_depth) / self.toi_depth_err,
            loc=self.toi_depth,
            scale=self.toi_depth_err,
        ).rvs(size=nsamples)

        if apply_contratio:
            # undiluted depth
            depth = depth * (1 + self.tic_params["contratio"])
        Rstar = stats.norm(loc=Rstar[0], scale=Rstar[1]).rvs(nsamples)

        Rp_samples = np.sqrt(depth) * Rstar * u.Rsun.to(u.Rearth)
        Rp, Rp_lo, Rp_hi = np.percentile(Rp_samples, percs)
        Rp, Rp_siglo, Rp_sighi = Rp, Rp - Rp_lo, Rp_hi - Rp
        if plot:
            _ = hist(Rp_samples, bins="scott")
        if return_samples:
            return (Rp, Rp_siglo, Rp_sighi, Rp_samples)
        else:
            return (Rp, Rp_siglo, Rp_sighi)
Пример #16
0
def plot_histo(data, cfg, plcfg, outd):
    """Plot histogram of data

    Using configuration in cfg, and output to outf.
    It returns outbin for further processing.
    """

    histocfg = read_histo_configuration(cfg)

    fig, ax = plt.subplots(1,1,figsize=plcfg['size'])

    if plcfg['xlog'] :
        plt.xscale('log')

    if plcfg['xlabel']:
        plt.xlabel(plcfg['xlabel'])
    if plcfg['ylabel'] :
        plt.ylabel(plcfg['ylabel'])
    if plcfg['title']:
        plt.title(plcfg['title'])

    plmin = np.min(data[0][0])
    plmax = np.max(data[0][0])

    if histocfg['meth'] == 'scott':
        w, bins = astropy.stats.scott_bin_width(data[0][0], return_bins=True)

    elif histocfg['meth'] == 'freedman' :
        w, bins = astropy.stats.freedman_bin_width(data[0][0],
                                                   return_bins=True)
    elif histocfg['meth']== 'knuth' :
        w, bins = astropy.stats.knuth_bin_width(data[0][0].ravel(),
                                                return_bins=True, quiet=False)

    for ds in range(np.shape(data)[0]):
        shdata = data[ds][0]
        outbin = hist(shdata, bins=bins, ax=ax, histtype=histocfg['httype'],
                      alpha=histocfg['alpha'], density=histocfg['dens'],
                      range=(plmin, plmax),
                      stacked=histocfg['stack'], cumulative=histocfg['cumul'],
                      log=histocfg['ylog'],
                      color=plcfg['colors'][ds])
 
    fig.savefig(outd+cfg['outfile'], dpi=plcfg['dpi'])

    return outbin
Пример #17
0
def test_hist_autobin(rseed=0):
    rng = np.random.RandomState(rseed)
    x = rng.randn(100)

    # 'knuth' bintype depends on scipy that is optional dependency
    if HAS_SCIPY:
        bintypes = [
            10,
            np.arange(-3, 3, 10), 'knuth', 'scott', 'freedman', 'blocks'
        ]
    else:
        bintypes = [10, np.arange(-3, 3, 10), 'scott', 'freedman', 'blocks']

    for bintype in bintypes:
        for range in [None, (-3, 3)]:
            n1, bins1 = histogram(x, bintype, range=range)
            n2, bins2, patches = hist(x, bintype, range=range)
            assert_allclose(n1, n2)
            assert_allclose(bins1, bins2)
Пример #18
0
def FitTheHist(z, nbins=20, func=fit_dNdz, normed=1):
    from scipy.optimize import curve_fit, leastsq
    from astropy.visualization import hist

    n, bins, _ = hist(z, nbins, normed=normed, histtype='step')
    # plt.close()
    bin_centers = 0.5 * (bins[1:] + bins[:-1]
                         )  #bins[:-1] + 0.5 * (bins[1:] - bins[:-1])

    fitfunc = lambda p, x: x**p[0] * np.exp(-(x / p[2])**p[1])
    errfunc = lambda p, x, y: (y - fitfunc(p, x))

    out = leastsq(errfunc, [2.2, 1.4, 0.05], args=(bin_centers, n))
    c = out[0]

    print "Fit Coefficients:"
    print c[0], c[1], c[2]  #,c[3]

    plt.plot(bin_centers, fitfunc(c, bin_centers))
Пример #19
0
def plot_labeled_histogram(style, data, name,
                           x, pdf_true, ax=None,
                           hide_x=False,
                           hide_y=False):
    if ax is not None:
        ax = plt.axes(ax)

    counts, bins, patches = hist(data, bins=style, ax=ax,
                                 color='k', histtype='step', density=True)
    ax.text(0.95, 0.93, '%s:\n%i bins' % (name, len(counts)),
            transform=ax.transAxes,
            ha='right', va='top')

    ax.fill(x, pdf_true, '-', color='#CCCCCC', zorder=0)

    if hide_x:
        ax.xaxis.set_major_formatter(plt.NullFormatter())
    if hide_y:
        ax.yaxis.set_major_formatter(plt.NullFormatter())

    ax.set_xlim(-5, 5)

    return ax
#------------------------------------------------------------
# Plot the cloned distribution and the procedure for obtaining it
fig = plt.figure(figsize=(5, 5))
fig.subplots_adjust(hspace=0.3,
                    left=0.1,
                    right=0.95,
                    bottom=0.08,
                    top=0.92,
                    wspace=0.3)

indices = np.linspace(0, Ndata - 1, 20).astype(int)

# plot a histogram of the input
ax = fig.add_subplot(221)
hist(x, bins='knuth', ax=ax, histtype='stepfilled', ec='k', fc='#AAAAAA')
ax.set_ylim(0, 300)
ax.set_title('Input data distribution')
ax.set_xlabel('$x$')
ax.set_ylabel('$N(x)$')

# plot the cumulative distribution
ax = fig.add_subplot(222)
ax.scatter(x[indices], Px_cuml[indices], lw=0, c='k', s=9)
ax.plot(x, Px_cuml, '-k')
ax.set_xlim(-3, 3)
ax.set_ylim(-0.05, 1.05)
ax.set_title('Cumulative Distribution')
ax.set_xlabel('$x$')
ax.set_ylabel('$p(<x)$')
Пример #21
0
#------------------------------------------------------------
# plot the r vs u-r color-magnitude diagram
u = data['modelMag_u']
r = data['modelMag_r']
rPetro = data['petroMag_r']

plt.figure()
ax = plt.axes()
plt.scatter(u - r, rPetro, s=1, lw=0, c=data['z'], cmap=plt.cm.copper,
            vmin=0, vmax=0.4)
plt.colorbar(ticks=np.linspace(0, 0.4, 9)).set_label('redshift')

plt.xlim(0.5, 5.5)
plt.ylim(18, 12.5)

plt.xlabel('u-r')
plt.ylabel('rPetrosian')

#------------------------------------------------------------
# plot a histogram of the redshift

plt.figure()
hist(data['z'], bins='knuth',
     histtype='stepfilled', ec='k', fc='#F5CCB0')
plt.xlim(0, 0.4)
plt.xlabel('z (redshift)')
plt.ylabel('dN/dz(z)')

plt.show()
Пример #22
0
    plt.subplot(3, 2, 5)
    n2, bins2, patches2 = plt.hist(prlx,
                                   700,
                                   normed=1,
                                   facecolor='blue',
                                   alpha=0.75)
    plt.xlim([-10, 20])
    plt.grid(True)
    plt.rcParams["figure.figsize"] = (20, 3)

    # Chi histogram
    chil = []
    for star in data:
        chil += [
            chi(star['pmra'], 19.45) + chi(star['pmdec'], -45.35) +
            chi(star['parallax'], 7.42)
        ]

    plt.subplot(3, 2, 6)
    n2, bins2, patches2 = hist(chil,
                               bins='freedman',
                               normed=1,
                               facecolor='blue',
                               alpha=0.75)
    plt.grid(True)
    plt.rcParams["figure.figsize"] = (20, 3)

    # Save file
    plt.savefig('output.png', format='png', dpi=400)
    plt.show()
Пример #23
0
from ccdproc import ImageFileCollection
from astropy.visualization import hist
import itertools
from astropy.stats import sigma_clip, mad_std

a = CCDData.read('Master_Files/mbias_median.fits', unit='adu')
b = CCDData.read('Master_Files/mbias.fits', unit='adu')
median = np.ndarray.flatten(a.data)
sigclip = np.ndarray.flatten(b.data)

med = sigma_clip(median,
                 sigma=3,
                 cenfunc='median',
                 stdfunc=mad_std,
                 masked=False)
sig = sigma_clip(sigclip,
                 sigma=3,
                 cenfunc='median',
                 stdfunc=mad_std,
                 masked=False)

plt.figure(figsize=(10, 20))
fig, axs = plt.subplots(2, 1)

axs[0].hist(med, bins=20, label='median', histtype='stepfilled', log=False)
axs[1].hist(sig, bins=50, label='sigclip', histtype='stepfilled', log=False)
fig.show()
'''
hist(s, bins='freedman', label='average', alpha=0.5)
plt.show()
'''
		fname_kg = 'clkg_sims_dl'+str(delta_ell)+'_lmin_'+str(lmin)+'_lmax'+str(lmax)+'_KS_min_'+str(K_S_min)+'_KS_max_'+str(K_S_max)+'_nside256_zmin_'+str(zbin[0])+'_zmax'+str(zbin[1])+'_nsims'+str(nsims)+'.pkl.gz'
	clkg_sims[zbin] = pickle.load(gzip.open('spectra/sims/'+fname_kg,'rb'))
	clgg_sims[zbin] = pickle.load(gzip.open('spectra/sims/'+fname_gg,'rb'))

# assert( clkg_sims[zbins[0]]['lb'] == lb )

# To bin theoretical spectra
binner = cs.Binner(lmin=lmin, lmax=lmax, delta_ell=delta_ell)

# Initializing Cosmo class
# cosmo = Cosmo()
cosmo_nl = Cosmo(Giannantonio15Params, nonlinear=True)
# cosmo_w  = Cosmo(params={'w':-0.5})

# 2MPZ dN/dz
nz, z, _ = hist(twompz.ZPHOTO,'knuth', normed=1, histtype='step')
z = 0.5 * (z[1:]+z[:-1])
plt.close()

# Get theoretical quantities
clkg_slash = {}
clgg_slash = {}
clkg_slash_binned = {}
clgg_slash_binned = {}

for zbin in zbins:
	clkg_slash[zbin] = GetCrossSpectrum(cosmo_nl, z, nz, [zbin[0],zbin[1]], b=bias, alpha=1., lmax=500, sigma_zph=0.015, compute_at_z0=True)
	clgg_slash[zbin] = GetAutoSpectrum(cosmo_nl,  z, nz, [zbin[0],zbin[1]], b=bias, alpha=1., lmax=500, sigma_zph=0.015, compute_at_z0=True)
	clkg_slash_binned[zbin] = binner.bin_spectra(clkg_slash[zbin])
	clgg_slash_binned[zbin] = binner.bin_spectra(clgg_slash[zbin])
Пример #25
0
imX = np.empty((len(image_data), 2), dtype=np.float64)
imX[:, 0] = image_data['ra']
imX[:, 1] = image_data['dec']

# get standard stars
standards_data = fetch_sdss_S82standards()
stX = np.empty((len(standards_data), 2), dtype=np.float64)
stX[:, 0] = standards_data['RA']
stX[:, 1] = standards_data['DEC']

# crossmatch catalogs
max_radius = 1. / 3600  # 1 arcsec
dist, ind = crossmatch_angular(imX, stX, max_radius)
match = ~np.isinf(dist)

dist_match = dist[match]
dist_match *= 3600

ax = plt.axes()
hist(dist_match, bins='knuth', ax=ax,
     histtype='stepfilled', ec='k', fc='#AAAAAA')
ax.set_xlabel('radius of match (arcsec)')
ax.set_ylabel('N(r, r+dr)')
ax.text(0.95, 0.95,
        "Total objects: %i\nNumber with match: %i" % (imX.shape[0],
                                                      np.sum(match)),
        ha='right', va='top', transform=ax.transAxes)
ax.set_xlim(0, 0.2)

plt.show()
Пример #26
0
ax1.set_ylabel('P(t)')

ax2 = fig.add_subplot(122)
ax2.hist(t, bins=200, histtype='stepfilled', alpha=0.2, density=True)
ax2.set_xlabel('t')
ax2.set_ylabel('P(t)')

#------------------------------------------------------------
# Second & Third figure: Knuth bins & Bayesian Blocks
fig = plt.figure(figsize=(10, 4))
fig.subplots_adjust(left=0.1, right=0.95, bottom=0.15)

for bins, title, subplot in zip(['knuth', 'blocks'],
                                ["Knuth's rule", 'Bayesian blocks'],
                                [121, 122]):
    ax = fig.add_subplot(subplot)

    # plot a standard histogram in the background, with alpha transparency
    hist(t, bins=200, histtype='stepfilled',
         alpha=0.2, density=True, label='standard histogram')

    # plot an adaptive-width histogram on top
    hist(t, bins=bins, ax=ax, color='black',
         histtype='step', density=True, label=title)

    ax.legend(prop=dict(size=12))
    ax.set_xlabel('t')
    ax.set_ylabel('P(t)')

plt.show()
Пример #27
0
def bayes_linear(x, y, x_err, y_err, nWalkers=10, nBurn=100, nSample=1000,
                 conf_interval=[15.9, 84.1], verbose=False,
                 return_samples=False):
    '''
    Fit a line with errors in both variables using MCMC.

    Original version of this function is Erik Rosolowsky's:
    https://github.com/low-sky/py-low-sky/blob/master/BayesLinear.py

    Parameters
    ----------
    x : `~numpy.ndarray`
        x data.
    y : `~numpy.ndarray`
        y data.
    x_err : `~numpy.ndarray`
        x errors.
    y_err : `~numpy.ndarray`
        y errors.
    nWalkers : int, optional
        Number of walkers in the sampler (>2 required). Defaults to 10.
    nBurn : int, optional
        Number of steps to burn chain in for. Default is 100.
    nSample : int, optional
        Number of steps to sample chain with. Default is 1000.
    conf_interval : list, optional
        Upper and lower percentiles to estimate the bounds on the parameters.
        Defaults to the 1-sigma intervals (34.1% about the median).
    verbose : bool, optional
        Plot the resulting fit.
    return_samples : bool, optional
        Returns the entire chain of samples, when enabled.

    Returns
    -------
    params : `~numpy.ndarray`
        Fit parameters (slope, intercept)
    errors : `~numpy.ndarray`
        Confidence interval defined by values given in `conf_interval`
        (slope, intercept).
    samples : `~numpy.ndarray`
        Samples from the chain. Returned only when `return_samples` is enabled.

    '''

    try:
        import emcee
    except ImportError:
        raise ImportError("emcee must be installed to use Bayesian fitting.")

    def _logprob(p, x, y, x_err, y_err):
        theta, b = p[0], p[1]
        if np.abs(theta - np.pi / 4) > np.pi / 4:
            return -np.inf
        Delta = (np.cos(theta) * y - np.sin(theta) * x - b * np.cos(theta))**2
        Sigma = (np.sin(theta))**2 * x_err**2 + (np.cos(theta))**2 * y_err**2
        lp = -0.5 * np.nansum(Delta / Sigma) - 0.5 * np.nansum(np.log(Sigma))

        return lp

    ndim = 2
    p0 = np.zeros((nWalkers, ndim))
    p0[:, 0] = np.pi / 4 + np.random.randn(nWalkers) * 0.1
    p0[:, 1] = np.random.randn(nWalkers) * y.std() + y.mean()

    sampler = emcee.EnsembleSampler(nWalkers, ndim, _logprob,
                                    args=[x, y, x_err, y_err])
    pos, prob, state = sampler.run_mcmc(p0, nBurn)
    sampler.reset()
    sampler.run_mcmc(pos, nSample)

    slopes = np.tan(sampler.flatchain[:, 0])
    intercepts = sampler.flatchain[:, 1]

    slope = np.median(slopes)
    intercept = np.median(intercepts)

    params = np.array([slope, intercept])

    # Use the percentiles given in conf_interval
    error_intervals = np.empty((2, 2))
    error_intervals[0] = np.percentile(slopes, conf_interval)
    error_intervals[1] = np.percentile(intercepts, conf_interval)

    if verbose:
        # Make some trace plots, PDFs and a plot of the range of solutions

        import matplotlib.pyplot as plt
        from astropy.visualization import hist

        fig = plt.figure(figsize=(9.9, 4.8))

        ax = plt.subplot2grid((4, 4), (0, 0), colspan=1, rowspan=2)
        ax.plot(slopes, 'k', linewidth=0.5)
        ax.set_ylabel("Slope")
        # ax.set_xlabel("Iteration")
        ax.get_xaxis().set_ticklabels([])

        ax2 = plt.subplot2grid((4, 4), (0, 1), colspan=1, rowspan=2)
        ax2.plot(intercepts, 'k', linewidth=0.5)
        ax2.set_ylabel("Intercept")
        # ax2.set_xlabel("Iteration")
        ax2.get_xaxis().set_ticklabels([])

        ax3 = plt.subplot2grid((4, 4), (2, 0), colspan=1, rowspan=2)
        hist(slopes, bins='knuth', color='k', alpha=0.6, ax=ax3)
        ax3.axvline(slope, color='r', linestyle='-')
        ax3.axvline(error_intervals[0][0], color='r', linestyle='--')
        ax3.axvline(error_intervals[0][1], color='r', linestyle='--')
        ax3.set_xlabel("Slope")

        ax4 = plt.subplot2grid((4, 4), (2, 1), colspan=1, rowspan=2)
        hist(intercepts, bins='knuth', color='k', alpha=0.6, ax=ax4)
        ax4.axvline(intercept, color='r', linestyle='-')
        ax4.axvline(error_intervals[1][0], color='r', linestyle='--')
        ax4.axvline(error_intervals[1][1], color='r', linestyle='--')
        ax4.set_xlabel("Intercept")

        ax5 = plt.subplot2grid((4, 4), (0, 2), colspan=2, rowspan=3)
        ax_r = plt.subplot2grid((4, 4), (3, 2), colspan=2,
                                rowspan=1,
                                sharex=ax5)

        ax5.errorbar(x, y, xerr=x_err, yerr=y_err, fmt='o', color='k')
        ax5.set_ylabel("log Spectral Length")
        xvals = np.linspace(x.min(), x.max(), x.size * 10)
        ax5.plot(xvals, slope * xvals + intercept, 'r-')
        ax5.fill_between(xvals,
                         error_intervals[0, 0] * xvals + error_intervals[1, 0],
                         error_intervals[0, 1] * xvals + error_intervals[1, 1],
                         facecolor='red', interpolate=True, alpha=0.4)
        # ax5.get_xaxis().set_ticklabels([])

        # Some very large error bars makes it difficult to see the model
        y_range = np.ptp(y)
        x_range = np.ptp(x)
        ax5.set_ylim([y.min() - y_range / 4, y.max() + y_range / 4])
        ax5.set_xlim([x.min() - x_range / 4, x.max() + x_range / 4])

        ax_r.errorbar(x, y - (slope * x + intercept),
                      xerr=x_err, yerr=y_err, fmt='o', color='k')

        ax_r.axhline(0., color='red', linestyle='--', alpha=0.7)
        ax_r.set_ylabel("Residuals")
        ax_r.set_xlabel("log Spatial Length")

        print("Slope: {0} ({1}, {2})".format(slope, error_intervals[0, 0],
                                             error_intervals[0, 1]))
        print("Intercept: {0} ({1}, {2})".format(intercept,
                                                 error_intervals[1, 0],
                                                 error_intervals[1, 1]))

        plt.tight_layout()

        fig.subplots_adjust(hspace=0.1)

        plt.show()

    if return_samples:
        return params, error_intervals, np.vstack([slopes, intercepts])

    return params, error_intervals
Пример #28
0
def showSkyHist(skypix,
                skypix2=None,
                skypix3=None,
                sbExpt=None,
                pngName='skyhist.png',
                skyAvg=None,
                skyStd=None,
                skyMed=None,
                skySkw=None,
                savePng=True):
    """
    Plot the distribution of sky pixels.

    Parameters:
    """
    fig = plt.figure(figsize=(6, 4))
    ax = fig.add_subplot(111)
    fig.subplots_adjust(hspace=0.07, wspace=0.0,
                        left=0.06, bottom=0.15, top=0.99, right=0.95)
    fontsize = 12
    ax.minorticks_on()

    for tick in ax.xaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)
    for tick in ax.yaxis.get_major_ticks():
        tick.label1.set_fontsize(fontsize)

    if astroHist:
        _ = hist(skypix, bins='knuth', ax=ax, alpha=0.4, color='cyan',
                 histtype='stepfilled', normed=True)
    else:
        _ = plt.hist(skypix, bins=100, ax=ax, alpha=0.4, color='cyan',
                     histtype='stepfilled', normed=True)

    if skypix2 is not None:
        if astroHist:
            _ = hist(skypix2, bins='knuth', ax=ax, alpha=0.9, color='k',
                     histtype='step', normed=True, linewidth=2)
        else:
            _ = plt.hist(skypix2, bins=100, ax=ax, alpha=0.9, color='k',
                         histtype='step', normed=True, linewidth=2)

    if skypix3 is not None:
        if astroHist:
            _ = hist(skypix3, bins='knuth', ax=ax, alpha=0.8, color='k',
                     histtype='step', normed=True, linewidth=2,
                     linestyle='dashed')
        else:
            _ = plt.hist(skypix3, bins=100, ax=ax, alpha=0.8, color='k',
                         histtype='step', normed=True, linewidth=2,
                         linestyle='dashed')
    # Horizontal line
    ax.axvline(0.0, linestyle='-', color='k', linewidth=1.5)

    # Basic properties of the sky pixels
    skyMin = np.nanmin(skypix)
    skyMax = np.nanmax(skypix)
    if skyAvg is None:
        skyAvg = np.nanmean(skypix)
    if skyStd is None:
        skyStd = np.nanstd(skypix)
    if skyMed is None:
        skyMed = np.nanmedian(skypix)
        if not np.isfinite(skyMed):
            skyMed = np.median(skypix)
    if skySkw is None:
        skySkw = scipy.stats.skew(skypix)
    # Highligh the mode of sky pixel distribution
    ax.axvline(skyMed, linestyle='--', color='b', linewidth=1.5)

    ax.set_xlabel('Pixel Value', fontsize=12)
    ax.set_xlim(skyAvg - 4.0 * skyStd, skyAvg + 5.0 * skyStd)
    # Show a few information
    ax.text(
        0.7, 0.9, "Min : %8.4f" % skyMin, fontsize=12, transform=ax.transAxes)
    ax.text(
        0.7, 0.8, "Max : %8.4f" % skyMax, fontsize=12, transform=ax.transAxes)
    ax.text(
        0.7, 0.7, "Avg : %8.4f" % skyAvg, fontsize=12, transform=ax.transAxes)
    ax.text(
        0.7, 0.6, "Std : %8.4f" % skyStd, fontsize=12, transform=ax.transAxes)
    ax.text(
        0.7, 0.5, "Med : %8.4f" % skyMed, fontsize=12, transform=ax.transAxes)
    ax.text(
        0.7, 0.4, "Skew: %8.4f" % skySkw, fontsize=12, transform=ax.transAxes)
    if sbExpt is not None:
        ax.text(0.7, 0.3, "S.B : %8.5f" % sbExpt, fontsize=12,
                transform=ax.transAxes)

    if savePng:
        fig.savefig(pngName, dpi=70)
        plt.close(fig)
     epar_hdu = fits.open(epar_file)
     epar_data = epar_hdu[0].data[epar_ext[par_i],:,:]
     epar_hdu.close()
     par_hdu = fits.open(par_file)
     par_data = par_hdu[0].data
     par_hdu.close()
     pmin_list = plot_param['pmin_list']
     pmax_list = plot_param['pmax_list']
     pmin = np.max([pmin_list[par_i],np.nanmin(par_data)])
     pmax = np.min([pmax_list[par_i],np.nanmax(par_data)])
     par_masked = mask_hist(par_data,epar_data,epar_limits[par_i],pmax,par_min=pmin)
     par_masked = par_masked[np.isfinite(par_masked)]
     if par == 'Vlsr':
         bin_width = 0.3
         nbins = np.int((np.max(par_masked) - np.min(par_masked !=0))/bin_width)
         hist(par_masked[par_masked !=0],bins=nbins,ax=ax,histtype='stepfilled',alpha=0.3,
              color=plot_colours[region_i],label=region,normed=True)
     else:
         hist(par_masked[par_masked !=0],bins='knuth',ax=ax,histtype='stepfilled',alpha=0.3,
              color=plot_colours[region_i],label=region,normed=True)
     if (i+1) != len(region_list):
         ax.set_xticklabels([])
     ax.set_xlim(hist_minx_list[par_i],hist_maxx_list[par_i])
     #ax.set_ylim(0,hist_maxy_list[par_i])
     if par == 'Tkin':
         if region == 'OrionA' or region == 'L1688' or region == 'NGC1333':
             ax.set_ylim(0,0.25)
     ax.yaxis.set_major_locator(ticker.MultipleLocator(ytick_int_maj[par_i]))
     ax.yaxis.set_minor_locator(ticker.MultipleLocator(ytick_int_min[par_i]))
     ax.annotate('{0}'.format(region),xy=(0.97,0.7),xycoords='axes fraction',horizontalalignment='right')
 #ax.legend(frameon=False)
 ax.set_xlabel(label)
Пример #30
0
def plot_histogram(data,
                   max_data=None,
                   range=None,
                   bins='knuth',
                   figsize=(6,6),
                   xlabel=None, ylabel=None,
                   label=None,
                   suptitle=None,
                   plotfile_prefix=None,
                   plotfile_suffix=None,
                   plotfile=None,
                   outpath=None,
                   showplot=None,
                   saveplot=True):
    """
    plot a histogram using astropy.visualization.hist
    UPDATE: was ported from astroML.plotting.hist (http://astroML.org/)

    *astropy.visualization.hist*

    https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html

    astropy.visualization.hist(x, bins=10, ax=None, **kwargs)

    http://docs.astropy.org/en/stable/api/astropy.visualization.hist.html
    https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html

    Enhanced histogram function

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins. Aside from the bins argument allowing a
    string specified how bins are computed, the parameters are the same as
    pylab.hist().

    matplotlib.pyplot.hist(x, bins=None,
                           range=None, density=None, weights=None,
                           cumulative=False, bottom=None,
                           histtype='bar', align='mid',
                           orientation='vertical', rwidth=None,
                           log=False, color=None, label=None,
                           stacked=False, normed=None,
                           hold=None, data=None,
                           **kwargs)



    http://www.astroml.org/modules/generated/astroML.plotting.hist.html

    astroML.plotting.hist(x, bins=10, range=None, ax=None, **kwargs)

    Enhanced histogram

    bins : int or list or str (optional)

    If bins is a string, then it must be one of:
    'blocks' : use bayesian blocks for dynamic bin widths
    'knuth' : use Knuth's rule to determine bins
    'scott' : use Scott's rule to determine bins
    'freedman' : use the Freedman-diaconis rule to determine bins

    This is a histogram function that enables the use of more sophisticated
    algorithms for determining bins. Aside from the bins argument allowing
    a string specified how bins are computed, the parameters are the same
    as pylab.hist().



    """
    # from astroML.plotting import hist
    from astropy.visualization import hist

    if max_data != None:
        max_data = np.max(dist)
    xmax = max_data

    fig = plt.figure(figsize=figsize)
    # fig = plt.gcf()
    # fig.set_size_inches(10,10)

    ax = plt.axes()
    ndata = len(data)
    if range is None:
        range = [np.min(data), np.max(data)]
    if range is not None:
        print('range:', range, np.min(data), np.max(data))
        print(range, type(data), type(range), type(range[0]))
        print(data.dtype)
        idata = (data > range[0])
        # & data < range[1])
        print(len(data), len(data[idata]))
        data = data[idata]
    ndata = len(data)
    print('bins:', bins)
    hist(data, bins=bins, range=range,
         ax=ax, label=str(ndata),
         histtype='stepfilled',
         ec='k', fc='#AAAAAA')

    if xlabel is not None: ax.set_xlabel(xlabel)
    if ylabel is None: ax.set_ylabel('counts')
    if ylabel is not None: ax.set_ylabel(ylabel)

    if suptitle is not None:
        plt.suptitle(suptitle, fontsize='medium')

    #plt.show()

    # get basename without file extension
    #basename=os.path.splitext(os.path.basename(filename))[0]
    #figname = basename + '_' + 'scheme3_image_sep_NN_360arcsec_arcmin.png'

    plt.legend()
    plotid()

    if saveplot:
        if plotfile is None and plotfile_suffix is None:
            plotfile = 'histogram.png'
        if plotfile is None and plotfile_suffix is not None:
            plotfile = 'histogram_' + plotfile_suffix + '.png'

        if outpath is None:
            outpath = './'
        print('plotfile:', plotfile)
        print('outpath:', outpath)
        plotfile = outpath + plotfile
        print('Saving:', plotfile)
        plt.savefig(plotfile)

    if showplot:
        plt.show()

    plt.close()

    return
Пример #31
0
def main(args):
    SetPlotStyle()

    # Params 'n' stuff
    fits_planck_mask = '../Data/mask_convergence_planck_2015_512.fits'
    fits_planck_map = '../Data/convergence_planck_2015_512.fits'
    twoMPZ_mask = 'fits/2MPZ_mask_tot_256.fits'  # N_side = 256
    twoMPZ_cat = 'fits/results16_23_12_14_73.fits'

    nside = args.nside
    do_plots = False
    delta_ell = args.delta_ell
    lmin = args.lmin
    lmax = args.lmax
    K_S_min = args.K_S_min
    K_S_max = args.K_S_max

    # Load Cat and Mask
    print("...loading 2MPZ catalogue...")
    twompz = Load2MPZ(twoMPZ_cat, K_S_min=K_S_min, K_S_max=K_S_max)
    print("...done...")

    # Load Planck CMB lensing map 'n' mask
    print("...loading Planck map/mask...")
    planck_map, planck_mask = LoadPlanck(fits_planck_map,
                                         fits_planck_mask,
                                         nside,
                                         do_plots=0,
                                         filt_plank_lmin=0)
    print("...done...")

    print("...reading 2MPZ mask...")
    mask = hp.read_map(twoMPZ_mask, verbose=False)
    nside_mask = hp.npix2nside(mask.size)
    if nside_mask != nside:
        mask = hp.ud_grade(mask, nside)
    print("...done...")

    # Common Planck & WISExSCOS mask
    mask *= planck_mask

    # Counts 'n' delta map
    cat_tmp = twompz[(twompz.ZSPEC >= args.zmin) & (twompz.ZSPEC < args.zmax)]
    counts = hpy.GetCountsMap(cat_tmp.RA,
                              cat_tmp.DEC,
                              nside,
                              coord='G',
                              rad=True)
    delta = hpy.Counts2Delta(counts, mask=mask)

    est = cs.Master(mask,
                    lmin=lmin,
                    lmax=lmax,
                    delta_ell=delta_ell,
                    MASTER=1,
                    pixwin=True)

    print('...extracting kg spectra...')
    kg, err_kg = est.get_spectra(planck_map, map2=delta, analytic_errors=True)
    print("\t=> Detection at rougly %3.2f sigma ") % (np.sum(
        (kg[0:] / err_kg[0:])**2)**.5)

    # Initializing Cosmo class
    cosmo = Cosmo()
    cosmo_nl = Cosmo(nonlinear=True)

    nz, z, _ = hist(twompz.ZSPEC[twompz.ZSPEC > 0.],
                    'knuth',
                    normed=1,
                    histtype='step')
    z = 0.5 * (z[1:] + z[:-1])
    plt.close()

    fig = plt.figure()  #figsize=(10, 8))

    ax = fig.add_subplot(1, 1, 1)
    plt.title(r'$%.2f < z < %.2f$' % (args.zmin, args.zmax))

    clkg = GetCrossSpectrum(cosmo,
                            z,
                            nz, [args.zmin, args.zmax],
                            b=1,
                            alpha=1.,
                            lmax=500,
                            sigma_zph=0.)
    clkg_nl = GetCrossSpectrum(cosmo_nl,
                               z,
                               nz, [args.zmin, args.zmax],
                               b=1,
                               alpha=1.,
                               lmax=500,
                               sigma_zph=0.)

    # z, n_z = GetdNdzNorm(nz=1000)
    # clkg = GetCrossSpectrum(cosmo, z, n_z, [bins_edges[zbin][0],0.3], b=1.24, alpha=1., lmax=1000)
    # # clkg = GetCrossSpectrum(cosmo, z, n_z, [bins_edges[zbin][0],bins_edges[zbin][1]], b=1, alpha=1.24, lmax=1000)
    # clkg_nl = GetCrossSpectrum(cosmo_nl, z, n_z, [bins_edges[zbin][0],0.5], b=1.24, alpha=1., lmax=1000)
    # # clkg_nl = GetCrossSpectrum(cosmo_nl, z, n_z, [bins_edges[zbin][0],bins_edges[zbin][1]], b=1.24, alpha=1., lmax=1000)
    # # clgg_pherrz = GetAutoSpectrum(cosmo, z, dndz, [bins_edges[zbin][0],bins_edges[zbin][1]], b=1.24, alpha=1., lmax=1000, sigma_zph=0.03)

    lab = r'2MPZ  $%.1f < K_S < %.1f$' % (K_S_min, K_S_max)
    ax.plot(clkg, color='grey', label=r'$b=1$')
    ax.plot(clkg_nl, color='darkgrey', ls='--', label=r'NL $b=1$')
    ax.errorbar(est.lb, kg, yerr=err_kg, fmt='o', capsize=0, ms=5, label=lab)
    ax.set_ylabel(r'$C_{\ell}^{\kappa g}$')
    ax.set_xlabel(r'Multipole $\ell$')
    ax.legend(loc='best')
    ax.axhline(ls='--', color='grey')
    ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
    ax.set_xlim([2., 200])
    ax.set_yscale('log')
    ax.set_ylim([1e-8, 1e-5])

    plt.tight_layout()
    plt.savefig('plots/2MPZ_Planck_clkg_dl' + str(args.delta_ell) + '_lmin_' +
                str(args.lmin) + '_lmax' + str(args.lmax) + '_KS_min_' +
                str(args.K_S_min) + '_KS_max_' + str(args.K_S_max) + '_nside' +
                str(args.nside) + '_zmin_' + str(args.zmin) + '_zmax' +
                str(args.zmax) + '_split_spec.pdf',
                bbox_inches='tight')
    # plt.show()
    # plt.close()

    # embed()

    np.savetxt(
        'spectra/2MPZ_Planck2015_clkg_dl' + str(args.delta_ell) + '_lmin_' +
        str(args.lmin) + '_lmax' + str(args.lmax) + '_KS_min_' +
        str(args.K_S_min) + '_KS_max_' + str(args.K_S_max) + '_nside' +
        str(args.nside) + '_zmin_' + str(args.zmin) + '_zmax' +
        str(args.zmax) + '_split_spec.dat', np.c_[est.lb, kg, err_kg])
Пример #32
0
min_distances = []

for s1 in GSO_satellites:
    p1 = s1.at(t)
    min_dist = Distance(au=100).km
    for s2 in GSO_satellites:
        if s2 == s1:
            continue
        p2 = s2.at(t)
        dist = (p2 - p1).distance().km
        if dist < min_dist:
            min_dist = dist
    min_distances.append(min_dist)

fig, ax = plt.subplots()
hist(min_distances, bins='scott', density=True, alpha=0.5, ax=ax)
ax.set_title(u'Распределение КА по расстоянию между «ближайшими соседями»')
ax.set_xlabel(u'Расстояние, км')
ax.set_ylabel(u'Частота')
plt.savefig('Распределение КА по расстоянию.png')

orbit_heights = []

eccos = []

LEO_sat_count = 0
SSO_sat_count = 0

for satellite in satellites:
    height = satellite.at(t).subpoint().elevation.km
    if height <= 2000:
Пример #33
0
ax2.set_ylabel('P(t)')

#------------------------------------------------------------
# Second & Third figure: Knuth bins & Bayesian Blocks
fig = plt.figure(figsize=(10, 4))
fig.subplots_adjust(left=0.1, right=0.95, bottom=0.15)

for bins, title, subplot in zip(['knuth', 'blocks'],
                                ["Knuth's rule", 'Bayesian blocks'],
                                [121, 122]):
    ax = fig.add_subplot(subplot)

    # plot a standard histogram in the background, with alpha transparency
    hist(t,
         bins=200,
         histtype='stepfilled',
         alpha=0.2,
         density=True,
         label='standard histogram')

    # plot an adaptive-width histogram on top
    hist(t,
         bins=bins,
         ax=ax,
         color='black',
         histtype='step',
         density=True,
         label=title)

    ax.legend(prop=dict(size=12))
    ax.set_xlabel('t')
    ax.set_ylabel('P(t)')
Пример #34
0
def main(args):
    SetPlotStyle()

    # Params 'n' stuff
    fits_planck_mask = '../Data/mask_convergence_planck_2015_512.fits'
    fits_planck_map = '../Data/convergence_planck_2015_512.fits'
    twoMPZ_mask = 'fits/2MPZ_mask_tot_256.fits'  # N_side = 256
    twoMPZ_cat = 'fits/results16_23_12_14_73.fits'
    twoMPZ_cat2 = 'fits/results2_14_20_37_1053.fits.gz'
    fits_ebv = 'fits/lambda_sfd_ebv.fits'
    nsidelow = 64
    nside = args.nside
    do_plots = False
    delta_ell = args.delta_ell
    lmin = args.lmin
    lmax = args.lmax
    K_S_min = args.K_S_min
    K_S_max = args.K_S_max
    # lbmin    = 2
    # lbmax    = 10

    # Load Cat and Mask
    print("...loading 2MPZ catalogue...")
    twompz = Load2MPZ(twoMPZ_cat, K_S_min=K_S_min, K_S_max=K_S_max)
    twompz2 = Load2MPZ(twoMPZ_cat2, K_S_min=K_S_min, K_S_max=K_S_max)
    print("...done...")

    # E(B-V) reddening map by Schlegel+ in gal coord at nside=512
    ebv = hp.read_map(fits_ebv, verbose=False)
    ebv = hp.ud_grade(ebv, nsidelow, pess=True)
    A_K = ebv * 0.367  # K-band correction for Galactic extinction
    print("...Reddening map loaded...")

    # Get nstar map
    nstar = GetNstarMap(twompz['RA'],
                        twompz['DEC'],
                        twompz['STARDENSITY'],
                        nsidelow,
                        rad=True)

    # Get 2MPZ mask (A_K + nstar + counts)
    # !!! "torque rod gashes" still need to be removed !!!
    mask2mpz = Get2MPZMask(A_K,
                           nstar,
                           nside=nside,
                           maskcnt=hpy.GuessMask(twompz['RA'],
                                                 twompz['DEC'],
                                                 nsidelow,
                                                 rad=True))
    print("Mask f_sky is %3.2f" % np.mean(mask2mpz))

    # Load Planck CMB lensing map 'n' mask
    print("...loading Planck map/mask...")
    planck_map, planck_mask = LoadPlanck(fits_planck_map,
                                         fits_planck_mask,
                                         nside,
                                         do_plots=0,
                                         filt_plank_lmin=0)
    print("...done...")

    print("...reading 2MPZ mask...")
    mask = hp.read_map(twoMPZ_mask, verbose=False)
    nside_mask = hp.npix2nside(mask.size)
    if nside_mask != nside:
        mask = hp.ud_grade(mask, nside)
    print("...done...")

    # Common Planck & WISExSCOS mask
    mask *= planck_mask

    # embed()

    # Counts n overdesnity map
    cat_tmp = twompz[(twompz.ZPHOTO >= args.zmin)
                     & (twompz.ZPHOTO < args.zmax)]
    a, b = train_test_split(cat_tmp, test_size=0.5)

    counts_a = hpy.GetCountsMap(a.RA, a.DEC, nside, coord='G', rad=True)
    counts_b = hpy.GetCountsMap(b.RA, b.DEC, nside, coord='G', rad=True)
    # hp.write_map('fits/counts_'+zbin+'_sum.fits', counts_sum[zbin])
    # hp.write_map('fits/counts_'+zbin+'_diff.fits', counts_diff[zbin])
    print("\tNumber of sources in bin [%.2f,%.2f) is %d" %
          (args.zmin, args.zmax, np.sum((counts_a + counts_b) * mask)))

    print("...converting to overdensity maps...")

    delta_a = hpy.Counts2Delta(counts_a, mask=mask)
    delta_b = hpy.Counts2Delta(counts_b, mask=mask)

    delta_sum = 0.5 * (delta_a + delta_b)
    delta_diff = 0.5 * (delta_a - delta_b)
    # hp.write_map('fits/delta_'+zbin+'_sum.fits', delta_sum[zbin])
    # hp.write_map('fits/delta_'+zbin+'_diff.fits', delta_diff[zbin])

    cat_tmp2 = twompz2[(twompz2.ZPHOTO >= args.zmin)
                       & (twompz2.ZPHOTO < args.zmax)]
    a, b = train_test_split(cat_tmp2, test_size=0.5)

    counts_a2 = hpy.GetCountsMap(a.RA, a.DEC, nside, coord='G', rad=True)
    counts_b2 = hpy.GetCountsMap(b.RA, b.DEC, nside, coord='G', rad=True)

    delta_a2 = hpy.Counts2Delta(counts_a2, mask=mask)
    delta_b2 = hpy.Counts2Delta(counts_b2, mask=mask)

    delta_sum2 = 0.5 * (delta_a2 + delta_b2)
    delta_diff2 = 0.5 * (delta_a2 - delta_b2)

    print("...done...")

    # embed()

    # exit()

    # XC estimator ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    est_mask = cs.Master(mask,
                         lmin=lmin,
                         lmax=lmax,
                         delta_ell=delta_ell,
                         MASTER=0,
                         pixwin=True)
    # est_mask2mpz = cs.Master(mask2mpz, lmin=lmin, lmax=lmax, delta_ell=delta_ell, MASTER=0, pixwin=True)
    print('...extracting gg spectra...')

    clGS, err_gg = est_mask.get_spectra(delta_sum, analytic_errors=True)
    clGD = est_mask.get_spectra(delta_diff)
    gg = clGS - clGD

    clGS_2mpz, err_gg_2mpz = est_mask.get_spectra(delta_sum2,
                                                  analytic_errors=True)
    clGD_2mpz = est_mask.get_spectra(delta_diff2)
    gg_2mpz = clGS_2mpz - clGD_2mpz

    # clGS_2mpz, err_gg_2mpz = est_mask2mpz.get_spectra(delta_sum, analytic_errors=True)
    # clGD_2mpz = est_mask2mpz.get_spectra(delta_diff)
    # gg_2mpz = clGS_2mpz - clGD_2mpz

    print('...done...')

    # Initializing Cosmo class
    cosmo = Cosmo()
    cosmo_nl = Cosmo(nonlinear=True)

    nz, z, _ = hist(twompz.ZPHOTO, 'knuth', normed=1)
    z = 0.5 * (z[1:] + z[:-1])
    plt.close()

    fig = plt.figure()  #figsize=(10, 8))

    ax = fig.add_subplot(1, 1, 1)
    plt.title(r'$%.2f < z < %.2f$' % (args.zmin, args.zmax))

    # z, n_z  = GetdNdzNorm(nz=1000)
    # clgg    = GetAutoSpectrumMagLim( cosmo,    2.21, 0.053, 1.43, bias=1.24, alpha=1., lmax=1000)
    # clgg_ph = GetAutoSpectrumMagLim( cosmo_nl, 2.21, 0.053, 1.43, bias=1.24, alpha=1., lmax=1000, sigma_zph=0.03)
    # clgg_nl = GetAutoSpectrumMagLim( cosmo_nl, 2.21, 0.053, 1.43, bias=1.24, alpha=1., lmax=1000)
    clgg = GetAutoSpectrum(cosmo,
                           z,
                           nz, [args.zmin, args.zmax],
                           b=1.,
                           alpha=1.,
                           lmax=500,
                           sigma_zph=0.015,
                           i=0)
    clgg_nl = GetAutoSpectrum(cosmo_nl,
                              z,
                              nz, [args.zmin, args.zmax],
                              b=1.,
                              alpha=1.,
                              lmax=500,
                              sigma_zph=0.015,
                              i=0)
    # clgg    = GetAutoSpectrum(cosmo,   z,  dndz, [0., 0.08, 0.5], b=1.24, alpha=1., lmax=500, sigma_zph=0.015, i=i)
    # clgg_nl = GetAutoSpectrum(cosmo_nl, z, dndz, [0., 0.08, 0.5], b=1.24, alpha=1., lmax=500, sigma_zph=0.015, i=i)
    # ax = fig.add_subplot(1,1,i+1)
    lab = r'2MPZ  $%.1f < K_S < %.1f$' % (K_S_min, K_S_max)
    ax.plot(clgg, color='grey', label=r'$b=1$ $\sigma_z=0.015$')
    ax.plot(clgg_nl,
            color='darkgrey',
            ls='--',
            label=r'NL $b=1$ $\sigma_z=0.015$')
    ax.errorbar(est_mask.lb,
                gg,
                yerr=err_gg,
                fmt='o',
                capsize=0,
                ms=5,
                label=lab)
    ax.errorbar(est_mask.lb + 1,
                gg_2mpz,
                yerr=err_gg_2mpz,
                fmt='x',
                capsize=0,
                ms=5)  #, label=lab)
    ax.set_ylabel(r'$C_{\ell}^{gg}$')
    ax.set_xlabel(r'Multipole $\ell$')
    ax.legend(loc='best')
    ax.axhline(ls='--', color='grey')
    ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
    ax.set_xlim([2., 200])
    ax.set_yscale('log')
    ax.set_ylim([1e-5, 1e-3])

    plt.tight_layout()
    # plt.savefig('plots/2MPZ_clgg_dl'+str(args.delta_ell)+'_lmin_'+str(args.lmin)+'_lmax'+str(args.lmax)+'_KS_min_'+str(args.K_S_min)+'_KS_max_'+str(args.K_S_max)+'_nside'+str(args.nside)+'_zmin_'+str(args.zmin)+'_zmax'+str(args.zmax)+'_split.pdf', bbox_inches='tight')
    plt.show()
    # plt.close()

    embed()
dls_10deg = GetDls(spectra_folder_10deg)
dls_15deg = GetDls(spectra_folder_15deg)

fig, ax = subplots(2,3, figsize=(20,12))

# Different lmax plot ~~~~~~~~~~~~~~~~~~~~~~~~~~~
for iLM, LM in enumerate([1200,1900]):
	ax[iLM,0].set_title(r'Radius 5  deg - $\ell_{\rm max}=%d - N=%d$'%(LM,dls_5deg.shape[0]), size=15)
	ax[iLM,1].set_title(r'Radius 10 deg - $\ell_{\rm max}=%d - N=%d$'%(LM,dls_10deg.shape[0]), size=15)
	ax[iLM,2].set_title(r'Radius 15 deg - $\ell_{\rm max}=%d - N=%d$'%(LM,dls_15deg.shape[0]), size=15)
	for lm in [300, 650, 1000]:
		taus_15_CR = GetManyTaus(dls_15deg, dltt_ref, lm, LM, method='CR')
		taus_10_CR = GetManyTaus(dls_10deg, dltt_ref, lm, LM, method='CR')
		taus_5_CR  = GetManyTaus(dls_5deg, dltt_ref, lm, LM, method='CR')

		hist(taus_5_CR,  'knuth', histtype='step', ax=ax[iLM,0], label=r'$\ell_{\rm min} = %d\, \gamma_1=%.2f\pm%.2f$'%(lm,stats.skew(taus_5_CR),err_skew(taus_5_CR)))
		hist(taus_10_CR, 'knuth', histtype='step', ax=ax[iLM,1], label=r'$\ell_{\rm min} = %d\, \gamma_1=%.2f\pm%.2f$'%(lm,stats.skew(taus_10_CR),err_skew(taus_10_CR)))
		hist(taus_15_CR, 'knuth', histtype='step', ax=ax[iLM,2], label=r'$\ell_{\rm min} = %d\, \gamma_1=%.2f\pm%.2f$'%(lm,stats.skew(taus_15_CR),err_skew(taus_15_CR)))
		# hist(taus_5_CR,  'knuth', histtype='step', ax=ax[iLM,0], label=r'$(\ell_{\rm min},\ell_{\rm max}) = (%d,%d)\, \gamma_1=%.2f\pm%.2f$'%(lm,LM,stats.skew(taus_5_CR),err_skew(taus_5_CR)))
		# hist(taus_10_CR, 'knuth', histtype='step', ax=ax[iLM,1], label=r'$(\ell_{\rm min},\ell_{\rm max}) = (%d,%d)\, \gamma_1=%.2f\pm%.2f$'%(lm,LM,stats.skew(taus_10_CR),err_skew(taus_10_CR)))
		# hist(taus_15_CR, 'knuth', histtype='step', ax=ax[iLM,2], label=r'$(\ell_{\rm min},\ell_{\rm max}) = (%d,%d)\, \gamma_1=%.2f\pm%.2f$'%(lm,LM,stats.skew(taus_15_CR),err_skew(taus_15_CR)))

for i in xrange(3):
	ax[0,i].legend(loc='best')
	ax[1,i].legend(loc='best')
	ax[1,i].set_xlabel(r'$\hat{\tau}$', size=15)
	ax[0,i].set_xlim(-0.1,0.1)
	ax[1,i].set_xlim(-0.1,0.1)
	ax[0,i].axvline(0, ls='--', color='grey')
	ax[1,i].axvline(0, ls='--', color='grey')
Пример #36
0
plt.figure(3)
plt.hexbin(samples[:, 0],
           samples[:, 1],
           gridsize=100,
           mincnt=1,
           cmap='Greys',
           extent=[2, 2.6, -40, 90])
plt.xlabel(r'$m$')
plt.ylabel(r'$b$')

print('4. Showing the fully marginalised distributions')
plt.figure(4, figsize=(8, 5))
plt.subplot2grid((1, 2), (0, 0))
values, bins, patches = hist(samples[:, 0],
                             bins='knuth',
                             histtype='step',
                             color='k',
                             density=True,
                             lw=1.5)
plt.xlabel(r'$m$')
plt.ylabel(r'$P(m)$')
plt.subplot2grid((1, 2), (0, 1))
values, bins, patches = hist(samples[:, 1],
                             bins='knuth',
                             histtype='step',
                             color='k',
                             density=True,
                             lw=1.5)
plt.xlabel(r'$b$')
plt.ylabel(r'$P(b)$')
plt.tight_layout()
plt.show()
Пример #37
0
    def _repr_html_(self):
        """
        Produces an HTML summary of the timeseries data with plots for use in
        Jupyter notebooks.
        """
        # Call _text_summary and reformat as an HTML table
        partial_html = (self._text_summary()[34:].replace(
            "\n", "</td></tr><tr><th>").replace(":\t", "</th><td>"))
        text_to_table = (f"""\
            <table style='text-align:left'>
                <tr><th>{partial_html}</td></tr>
            </table>""").replace("\n", "")

        # Create the timeseries plots for each channel as a panel in one
        # figure. The color list below is passed to both timeseries and
        # histogram plotting methods for consistency.
        cols = [
            'b', 'g', 'r', 'c', 'm', 'y', 'tab:blue', 'tab:orange', 'tab:red',
            'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:green',
            'tab:olive', 'tab:cyan', 'palegreen', 'pink'
        ]
        dat = self.to_dataframe()
        fig, axs = plt.subplots(
            nrows=len(self.columns),
            ncols=1,
            sharex=True,
            constrained_layout=True,
            figsize=(6, 10),
        )
        # If all channels have the same unit, then one shared y-axis
        # label is set. Otherwise, each subplot has its own yaxis label.
        for i in range(len(self.columns)):
            if len(self.columns) == 1:
                axs.plot(
                    dat.index,
                    dat[self.columns[i]].values,
                    color=cols[i],
                    label=self.columns[i],
                )
                if (dat[self.columns[i]].values < 0).any() is np.bool_(False):
                    axs.set_yscale("log")
                axs.legend(frameon=False, handlelength=0)
                axs.set_ylabel(self.units[self.columns[i]])
            else:
                axs[i].plot(
                    dat.index,
                    dat[self.columns[i]].values,
                    color=cols[i],
                    label=self.columns[i],
                )
                if (dat[self.columns[i]].values < 0).any() is np.bool_(False):
                    axs[i].set_yscale("log")
                axs[i].legend(frameon=False, handlelength=0)
                axs[i].set_ylabel(self.units[self.columns[i]])
        plt.xticks(rotation=30)
        spc = _figure_to_base64(fig)
        plt.close(fig)
        # Make histograms for each column of data. The histograms are
        # created using the Astropy hist method that uses Scott's rule
        # for bin sizes.
        hlist = []
        for i in range(len(dat.columns)):
            if set(np.isnan(dat[self.columns[i]].values)) != {True}:
                fig = plt.figure(figsize=(5, 3), constrained_layout=True)
                hist(
                    dat[self.columns[i]].
                    values[~np.isnan(dat[self.columns[i]].values)],
                    log=True,
                    bins="scott",
                    color=cols[i],
                )
                plt.title(self.columns[i] + " [click for other channels]")
                plt.xlabel(self.units[self.columns[i]])
                plt.ylabel("# of occurences")
                hlist.append(_figure_to_base64(fig))
                plt.close(fig)

        # This loop creates a formatted list of base64 images that is passed
        # directly into the JS script below, so all images are written into
        # the html page when it is created (allows for an arbitrary number of
        # histograms to be rotated through onclick).
        hlist2 = []
        for i in range(len(hlist)):
            hlist2.append(f"data:image/png;base64,{hlist[i]}")

        # The code below creates unique names to be passed to the JS script
        # in the html code. Otherwise, multiple timeseries summaries will
        # conflict in a single notebook.
        source = str(self.source) + str(time.perf_counter_ns())

        return textwrap.dedent(f"""\
            <pre>{html.escape(object.__repr__(self))}</pre>
            <script type="text/javascript">
            function ImageChange(images) {{
                this.images = images;
                this.i = 0;
                this.next = function(img) {{
                    this.i++;
                    if (this.i == images.length)
                    this.i = 0;
                    img.src = images[this.i];
                }}
            }}
            var {source} = new ImageChange({hlist2});
            </script>
            <table>
                <tr>
                    <td style='width:40%'>{text_to_table}</td>
                    <td rowspan=3>
                        <img src='data:image/png;base64,{spc}'/>
                    </td>
                </tr>
                <tr>
                </tr>
                <tr>
                    <td>
                    <img src="{hlist2[0]}" alt="Click here for histograms"
                         onclick="{source}.next(this)"/>
                    </td>
                </tr>
            </table>""")
Пример #38
0
Файл: mcz.py Проект: zpace/pyMCZ
    def make_corner(self, galnum, scheme, lines=False):
        # make DFM-/emcee-style triangle plot

        res = self.res_d[galnum]

        colnames = res.colnames

        if self.nsample == 1:
            raise ValueError("need a sampled metallicity, not estimated! " + "Use self.sample(), with n fairly large")

        other_gcs = ["logR23", "E(B-V)"]
        if lines == True:
            other_gcs = [
                "[OII]3727",
                "Hb",
                "[OIII]4959",
                "[OIII]5007",
                "[OI]6300",
                "Ha",
                "[NII]6584",
                "[SII]6717",
                "[SII]6731",
                "[SIII]9069",
                "[SIII]9532",
            ] + other_gcs

        if scheme == "all":
            scheme = ["P10", "M08", "D02", "M91", "KD02", "M13", "PP04", "Z94", "KK04", "P10"]
            raise RuntimeWarning("Caution using all Z indicators, " + "corner plots take a long time to generate")

            scheme_cols = [n for n in colnames if np.array(True if s in n else False for s in scheme).any()]

        elif scheme not in ["P10", "M08", "D02", "M91", "KD02", "M13", "PP04", "Z94", "KK04", "P10"]:
            raise ValueError("bad metallicity scheme")

        else:
            scheme_cols = [n for n in colnames if scheme in n]

        good_cols = scheme_cols + other_gcs

        data = np.array([res[c] for c in good_cols]).T
        # get rid of pesky nans
        data = data[~np.isnan(data).any(axis=1)]

        labels = good_cols

        figure = corner.corner(data, labels=labels, quantiles=[0.16, 0.5, 0.84])

        # if we have multiple metallicity indicators, hist them all together
        if len(scheme_cols) > 1:
            scheme_colis = np.array([True if l in scheme_cols else False for l in labels]).astype(bool)
            Ztot_ax = figure.add_axes([0.575, 0.675, 0.4, 0.3])
            Ztot_data = np.array([res[c] for c in scheme_cols]).flatten()
            Ztot_data = Ztot_data[~np.isnan(Ztot_data)]
            hist(Ztot_data, ax=Ztot_ax, bins="knuth", histtype="step", color="k")
            plt.xticks(rotation=45.0)
            plt.setp(Ztot_ax.get_yticklabels(), visible=False)
            Ztot_ax.set_xlabel(r"$Z = 12 + \log{\frac{O}{H}}$")
            Ztot_ax.tick_params(axis="x", labelsize="x-small")

            pcs = np.percentile(Ztot_data, [14, 50, 86])
            for p in pcs:
                Ztot_ax.axvline(p, color="k", linestyle="--")

        figure.savefig("corner_{}_{}.png".format(self.name, galnum))