def compute_color_prior(size=64, do_plot=False):

    # Load the gamut points location
    q_ab = np.load(os.path.join(data_dir, "pts_in_hull.npy"))

    if do_plot:
        plt.figure(figsize=(15, 15))
        gs = gridspec.GridSpec(1, 1)
        ax = plt.subplot(gs[0])
        for i in range(q_ab.shape[0]):
            ax.scatter(q_ab[:, 0], q_ab[:, 1])
            ax.annotate(str(i), (q_ab[i, 0], q_ab[i, 1]), fontsize=6)
            ax.set_xlim([-110, 110])
            ax.set_ylim([-110, 110])

    with h5py.File(os.path.join(data_dir, "CelebA_%s_data.h5" % size),
                   "a") as hf:
        # Compute the color prior over a subset of the training set
        # Otherwise it is quite long
        X_ab = hf["training_lab_data"][:100000][:, 1:, :, :]
        npts, c, h, w = X_ab.shape
        X_a = np.ravel(X_ab[:, 0, :, :])
        X_b = np.ravel(X_ab[:, 1, :, :])
        X_ab = np.vstack((X_a, X_b)).T

        if do_plot:
            plt.hist2d(X_ab[:, 0], X_ab[:, 1], bins=100, norm=LogNorm())
            plt.xlim([-110, 110])
            plt.ylim([-110, 110])
            plt.colorbar()
            plt.show()
            plt.clf()
            plt.close()

        # Create nearest neighbord instance with index = q_ab
        NN = 1
        nearest = nn.NearestNeighbors(n_neighbors=NN,
                                      algorithm='ball_tree').fit(q_ab)
        # Find index of nearest neighbor for X_ab
        dists, ind = nearest.kneighbors(X_ab)

        # We now count the number of occurrences of each color
        ind = np.ravel(ind)
        counts = np.bincount(ind)
        idxs = np.nonzero(counts)[0]
        prior_prob = np.zeros((q_ab.shape[0]))
        for i in range(q_ab.shape[0]):
            prior_prob[idxs] = counts[idxs]

        # We turn this into a color probability
        prior_prob = prior_prob / (1.0 * np.sum(prior_prob))

        # Save
        np.save(os.path.join(data_dir, "CelebA_%s_prior_prob.npy" % size),
                prior_prob)

        if do_plot:
            plt.hist(prior_prob, bins=100)
            plt.yscale("log")
            plt.show()
def compute_color_prior(size=64, do_plot=False):

    # Load the gamut points location
    q_ab = np.load(os.path.join(data_dir, "pts_in_hull.npy"))

    if do_plot:
        plt.figure(figsize=(15, 15))
        gs = gridspec.GridSpec(1, 1)
        ax = plt.subplot(gs[0])
        for i in range(q_ab.shape[0]):
            ax.scatter(q_ab[:, 0], q_ab[:, 1])
            ax.annotate(str(i), (q_ab[i, 0], q_ab[i, 1]), fontsize=6)
            ax.set_xlim([-110,110])
            ax.set_ylim([-110,110])

    with h5py.File(os.path.join(data_dir, "CelebA_%s_data.h5" % size), "a") as hf:
        # Compute the color prior over a subset of the training set
        # Otherwise it is quite long
        X_ab = hf["training_lab_data"][:100000][:, 1:, :, :]
        npts, c, h, w = X_ab.shape
        X_a = np.ravel(X_ab[:, 0, :, :])
        X_b = np.ravel(X_ab[:, 1, :, :])
        X_ab = np.vstack((X_a, X_b)).T

        if do_plot:
            plt.hist2d(X_ab[:, 0], X_ab[:, 1], bins=100, norm=LogNorm())
            plt.xlim([-110, 110])
            plt.ylim([-110, 110])
            plt.colorbar()
            plt.show()
            plt.clf()
            plt.close()

        # Create nearest neighbord instance with index = q_ab
        NN = 1
        nearest = nn.NearestNeighbors(n_neighbors=NN, algorithm='ball_tree').fit(q_ab)
        # Find index of nearest neighbor for X_ab
        dists, ind = nearest.kneighbors(X_ab)

        # We now count the number of occurrences of each color
        ind = np.ravel(ind)
        counts = np.bincount(ind)
        idxs = np.nonzero(counts)[0]
        prior_prob = np.zeros((q_ab.shape[0]))
        for i in range(q_ab.shape[0]):
            prior_prob[idxs] = counts[idxs]

        # We turn this into a color probability
        prior_prob = prior_prob / (1.0 * np.sum(prior_prob))

        # Save
        np.save(os.path.join(data_dir, "CelebA_%s_prior_prob.npy" % size), prior_prob)

        if do_plot:
            plt.hist(prior_prob, bins=100)
            plt.yscale("log")
            plt.show()
Exemplo n.º 3
0
def compute_color_prior(X_ab, show_grid=False):
    """
    计算先验色
    :param X_ab:
    :param show_grid:
    :return:
    """
    q_ab = np.load("../data/params/pts_in_hull.npy")

    if show_grid:
        plt.figure(figsize=(15, 15))
        gs = gridspec.GridSpec(1, 1)
        ax = plt.subplot(gs[0])
        for i in range(q_ab.shape[0]):
            ax.scatter(q_ab[:, 0], q_ab[:, 1])
            ax.annotate(str(i), (q_ab[i, 0], q_ab[i, 1]), fontsize=6)
            ax.set_xlim([-110, 110])
            ax.set_ylim([-110, 110])

    X_a = np.ravel(X_ab[:, :, :, 0])
    X_b = np.ravel(X_ab[:, :, :, 1])
    X_ab = np.vstack((X_a, X_b)).T

    if show_grid:
        plt.hist2d(X_ab[:, 0],
                   X_ab[:, 1],
                   bins=100,
                   normed=True,
                   norm=LogNorm())
        plt.xlim([-110, 110])
        plt.ylim([-110, 110])
        plt.colorbar()
        plt.show()
        plt.clf()
        plt.close()

    nearest = nn.NearestNeighbors(n_neighbors=1,
                                  algorithm='ball_tree').fit(q_ab)
    distances, index = nearest.kneighbors(X_ab)

    index = np.ravel(index)
    counts = np.bincount(index)
    idxs = np.nonzero(counts)[0]
    prior_prob = np.zeros((q_ab.shape[0]))
    for i in range(q_ab.shape[0]):
        prior_prob[idxs] = counts[idxs]

    # 返回先验概率
    prior_prob = prior_prob / (1.0 * np.sum(prior_prob))

    # Save
    np.save("../data/params/prior_prob.npy", prior_prob)

    if show_grid:
        plt.hist(prior_prob, bins=100)
        plt.yscale("log")
        plt.show()
Exemplo n.º 4
0
def eur_vs_imputed_aaf(m):
    # Color proportional to #variants
    P.figure(3)
    P.clf()
    in_both = (m[:, 0] > 0) & (m[:, 2] > 0)
    P.hist2d(m[in_both, 2], m[in_both, 0], bins=40, norm=LogNorm())
    P.xlabel(r'Hutterite Imputed AAF')
    P.ylabel('EUR AAF')
    P.title('EUR vs. Hutterite Imputed AAF (#variants = %d)' % (m.shape[0],))
    P.colorbar()
Exemplo n.º 5
0
def eur_vs_imputed_aaf(m):
    # Color proportional to #variants
    P.figure(3)
    P.clf()
    in_both = (m[:, 0] > 0) & (m[:, 2] > 0)
    P.hist2d(m[in_both, 2], m[in_both, 0], bins=40, norm=LogNorm())
    P.xlabel(r'Hutterite Imputed AAF')
    P.ylabel('EUR AAF')
    P.title('EUR vs. Hutterite Imputed AAF (#variants = %d)' % (m.shape[0], ))
    P.colorbar()
Exemplo n.º 6
0
def make_movie(list):
	os.system("mkdir movie_plots")
	for k in range(len(list)):
		if k < 10:
			file_name = "img-000"+str(k)+".png"
		elif k < 100:
			file_name = "img-00" +str(k)+".png"
		elif k < 1000:
			file_name = "img-0" + str(k) +".png"
		else:
			file_name = "img-"+str(k) + ".png"
		plt.hist2d(list[k][0],list[k][1], bins=50)
		plt.title("Histogram of Generated Distribution")
		plt.grid(True)
		plt.savefig(file_name)
		os.system("mv "+ file_name + " movie_plots")
		plt.clf()
Exemplo n.º 7
0
    def bethe_bloch_plot(self):


        fig, ax = plt.subplots(figsize=(9, 6))


        my_cmap = ListedColormap(sns.color_palette("Blues", 50))
        fig.set_facecolor('white')

        h = plt.hist2d(np.log10(self.data_list['Energy (GeV)']), np.log10(self.data_list['Charge per unit length']),
                       bins=[np.linspace(-1., 5., 14), np.linspace(1.5, 3.5, 35)], cmin=1., cmap=my_cmap)
        plt.colorbar(label="Number of Events")
        plt.title("Muon Losses")
        #plt.plot(np.log10(E_array), np.log10(muon_BB), c='r', label="Bethe-Bloch", lw=3)
        plt.ylabel(r'$\log (\frac{dQ}{dx} \times \frac{0.9 \mu m}{q_{e}})$', fontsize=26)
        plt.xlabel('$\log$( $E_{\mu}$ / MeV) ')
        plt.xlim(0, 4.2)
        plt.ylim(1.5, 3.25)
        plt.show()
Exemplo n.º 8
0
    def p2d(x, y, x_bins, y_bins, mode='hist'):

        if mode == "scatter":
            plt.scatter(x, y, s=5)
        elif mode == 'hist':
            return plt.hist2d(x, y, bins=(
                x_bins,
                y_bins,
            ))
        elif mode == 'contour':
            h2, a, b = np.histogram2d(x, y, bins=(
                x_bins,
                y_bins,
            ))

            plt.contourf(
                a[:-1],
                b[:-1],
                np.transpose(h2),
                #levels=np.logspace(np.log10(h2.max()/1000.), np.log10(h2.max()), 100)
                levels=np.linspace(0, h2.max(), 100))
Exemplo n.º 9
0
    def bethe_bloch_plot(self):

        me = .511
        E_array = np.logspace(-2., 4., 100)
        BB = []

        for E in E_array:

            if self.pid == 'mu+' or self.pid == 'mu-':
                BB.append(self.get_dEdx(206.7 * me, E, 1., self.pid))
            elif self.pid == 'e+' or self.pid == 'e-':
                BB.append(self.get_dEdx(me, E, 1., self.pid))



        BB = np.array(BB)

        #todo: may change this /2 if counting hole charge is possible
        BB = BB * 1e6 / 2 * (1. / 3.62) * 1e-4  # convert from MeV / cm to electron charge per um


        fig, ax = plt.subplots(figsize=(9, 6))


        my_cmap = ListedColormap(sns.color_palette("Blues", 50))
        fig.set_facecolor('white')

        h = plt.hist2d(np.log10(self.data_list['Energy (GeV)']), np.log10(self.data_list['Charge per unit length']),
                       bins=[np.linspace(-1., 5., 14), np.linspace(1.5, 3.5, 35)], cmin=1., cmap=my_cmap)
        plt.colorbar(label="Number of Events")
        plt.title(str(self.pid) + " Losses")
        plt.plot(np.log10(E_array), np.log10(BB), c='r', label="Bethe-Bloch", lw=3)
        plt.ylabel(r'$\log (\frac{dQ}{dx} \times \frac{0.9 \mu m}{q_{e}})$', fontsize=26)
        plt.xlabel('$\log$( $E_{' + str(self.pid) + '}$ / MeV) ')
        #plt.xlim(-2, 4.2)
        #plt.ylim(1.5, 3.25)
        plt.show()
Exemplo n.º 10
0
drlv_bins = np.linspace(0, tol, nbins)
ng = int(d.labels.max()) + 1
drlv = np.sqrt(d.drlv2)
dp5 = [drlv[d.labels == i] for i in range(ng)]
hl = [np.histogram(dpi, drlv_bins)[0] for dpi in dp5]
print("hl0:", hl[0].shape, drlv_bins.shape)
if drlv_bins.shape[0] != hl[0].shape[0]:
    plotbins = (drlv_bins[1:] + drlv_bins[:-1]) / 2

pl.subplot(211)
for i in range(ng):
    pl.plot(plotbins, hl[i], label=str(i))

if 1:
    print(" " * 10, end=' ')
    for j in range(ng):
        print("%5d" % (j), end=' ')
    print()
    for i in range(len(drlv_bins) - 1):
        print("%10.6f" % (drlv_bins[i]), end=' ')
        for j in range(ng):
            print("%5d" % (hl[j][i]), end=' ')
        print()
pl.subplot(212)
pl.hist2d(drlv, d.labels, (drlv_bins, np.arange(-0.5, ng, 1.)), vmin=0.5)
pl.colorbar()
pl.ylabel("Grain")
pl.xlabel("drlv")
pl.show()
#     model_snowline.append(np.nanmean(dem_y[np.logical_and(model_scd_y > 110, model_scd_y < 130)]))
#     modis_snowline.append(np.nanmean(dem_y[np.logical_and(modis_scd_y > 110, modis_scd_y < 130)]))

# plt.plot(modis_snowline, np.arange(4.7e6, 6.2e6, 5.e4) + 2.5e4)
# plt.plot(model_snowline, np.arange(4.7e6, 6.2e6, 5.e4) + 2.5e4)
# plt.grid()

# plt.legend()
# plt.ylabel('NZTM northing (m)')
# plt.xlabel('Elevation (m)')
# plt.tight_layout()
# plt.savefig(r'C:\Users\conwayjp\OneDrive - NIWA\projects\CARH2101\snow reanalysis\snowline elevation with northing.png',dpi=300)

fig1 = plt.figure(figsize=[4, 4])
h2d = plt.hist2d(modis_scd.ravel()[~np.isnan(modis_scd.ravel())],
                 nztm_dem.ravel()[~np.isnan(modis_scd.ravel())],
                 bins=(np.arange(0, 420, 30), np.arange(0, 4000, 200)),
                 norm=LogNorm())
plt.colorbar()
plt.xticks(h2d[1][:-1])
plt.yticks(h2d[2][:-1])
plt.ylabel('Elevation (m)')
plt.xlabel('SCD (days)')
plt.title('(b) MODIS SCD by elevation', fontweight='bold', fontsize=10)
mode_modis = np.argmax(h2d[0], axis=0)
scd_x = h2d[1][:-1]
mode_modis_scd = np.asarray([scd_x[m] for m in mode_modis])
mode_modis_scd += 15
# plt.scatter(mode_modis_scd,h2d[2][:-1]+100,18,'k')
plt.scatter(mode_modis_scd, h2d[2][:-1] + 100, 18, 'k', label='mode (MODIS)')
plt.legend(loc='upper left')
plt.tight_layout()

print "=================="
print "best fit slope:"
pstats(mh)
print "best fit normalization (pivoted) to ", ay
pstats(ch)
print "best fit scatter in y direction:"
pstats(sh)
print "=================="

#
# plot results:
#

plt.rc('font', family='sans-serif', size=16)
fig = plt.figure(figsize=(6, 10))
plt.subplot(211)
plt.title('Metropolis')
plt.plot(chain)

ax = plt.subplot(212)
plt.hist2d(mh, ch, bins=60, norm=LogNorm(), normed=1)
plt.colorbar()

plt.legend(loc='upper center')
plt.ylabel('c')
plt.xlabel('m')

plt.show()
Exemplo n.º 13
0
def comparar_gas_ideal(T, r="0", zoom=False):
    n = len(pairs)
    dq = np.zeros(Nreps * n, dtype=np.float32)
    dp = np.zeros(Nreps * n, dtype=np.float32)
    for i in range(Nreps):
        sim_q = np.random.random(3 * 1000) * L
        sim_p = np.random.normal(0, 1, 3 * 1000) * np.sqrt(m * T) * factor_p
        sim_q = np.array(sim_q, dtype=np.float32)
        sim_p = np.array(sim_p, dtype=np.float32)
        dq[n * i:n * (i + 1)], dp[n * i:n * (i + 1)] = deltas(sim_q, sim_p)
    plt.figure()
    counts, xbins, ybins, image = plt.hist2d(dq,
                                             dp,
                                             bins=200,
                                             norm=LogNorm(),
                                             cmap=plt.cm.rainbow)
    new_counts = scipy.ndimage.filters.gaussian_filter(counts, 1)
    plt.colorbar()
    CS = plt.contour(new_counts.transpose(),
                     extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
                     linewidths=3,
                     colors="black",
                     levels=np.logspace(1, 2, 2))
    plt.clabel(CS, colors="black", inline=True, fmt="%d", fontsize=20)
    plt.xlabel(r'$\Delta q$ [fm]', fontsize=14)
    plt.ylabel(r'$\Delta p$ [MeV/c]', fontsize=14)
    plt.title(r'Gas ideal - $\rho^*=%1.3f$ - $T=%1.4fMeV$' %
              (qo**3 * N / V, T),
              fontsize=16)
    plt.axis([0, np.sqrt(3) * L / 2, 0, 4000])
    plt.grid()
    if (zoom):
        plt.axis([0, 4 * qo, 0, 4 * po * factor_p])
    # DORSO
    rho = "rho" + r
    data = np.loadtxt(rho + "/histograma_2D_%f.txt" % T)
    xbins = data[0, 1:]
    ybins = data[1:, 0]
    counts = data[1:-1, 1:-1]
    Nbins_2D = len(xbins) - 1
    dq = []
    dp = []
    for x in range(len(xbins) - 1):
        for y in range(len(ybins) - 1):
            for i in range(int(counts[x, y])):
                dq.append((xbins[x + 1] + xbins[x]) * 0.5)
                if (rho[0] != "M"):
                    dp.append((ybins[y + 1] + ybins[y]) * 0.5 * factor_p)
                else:
                    dp.append((ybins[y + 1] + ybins[y]) * 0.5)
    plt.figure()
    counts, xbins, ybins, image = plt.hist2d(dq,
                                             dp,
                                             bins=Nbins_2D,
                                             norm=LogNorm(),
                                             cmap=plt.cm.rainbow)
    new_counts = scipy.ndimage.filters.gaussian_filter(counts, 1)
    plt.colorbar()
    CS = plt.contour(new_counts.transpose(),
                     extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
                     linewidths=3,
                     colors="black",
                     levels=np.logspace(1, 2, 2))
    plt.clabel(CS, colors="black", inline=True, fmt="%d", fontsize=20)
    plt.xlabel(r'$\Delta q$ [fm]', fontsize=14)
    plt.ylabel(r'$\Delta p$ [MeV/c]', fontsize=14)
    plt.title(r'Dorso - $\rho^*=%1.3f$ - $T=%1.4fMeV$' % (qo**3 * N / V, T),
              fontsize=16)
    plt.axis([0, np.sqrt(3) * L / 2, 0, 4000])
    plt.grid()
    if (zoom):
        plt.axis([0, 4 * qo, 0, 4 * po * factor_p])
    plt.show()
    return dq, dp
Exemplo n.º 14
0
            diff = abs(t - ht)
            xh = float(line[1])
            yh = float(line[2])
            zh = float(line[3])
            
    f.close()

    x = x - xh
    y = y - yh
    z = z - zh

    #plt.figure(figsize=(20,6))

    plt.subplot(131)
    #plt.scatter(x,y,c=colors[j], marker=shapes[j])
    plt.hist2d(x,y,bins=(xbins,xbins),norm=LogNorm())
    plt.xlabel(r'$x$ (kpc/$h$)',fontsize=18)
    plt.ylabel(r'$y$ (kpc/$h$)',fontsize=18)
    plt.xlim(-200,200)
    plt.ylim(-200,200)
    
    plt.subplot(132)
    #plt.scatter(x,z,c=colors[j], marker=shapes[j])
    plt.hist2d(x,z,bins=(xbins,xbins),norm=LogNorm())
    plt.xlabel(r'$x$ (kpc/$h$)',fontsize=18)
    plt.ylabel(r'$z$ (kpc/$h$)',fontsize=18)
    plt.xlim(-200,200)
    plt.ylim(-200,200)
    
    
    plt.subplot(133)
    def compute_color_priority(self):
        """
        Compute the color priority
        :return: 
        """
        print(" -- Compute color priority ...")
        # Load the gamut points location
        quantized_ab = np.load(os.path.join(self._data_dir, "pts_in_hull.npy"))

        if self._make_plot:
            plt.figure(figsize=(15, 15))
            grid_spec = gridspec.GridSpec(1, 1)
            ax = plt.subplot(grid_spec[0])

            for i in range(quantized_ab.shape[0]):
                ax.scatter(quantized_ab[:, 0], quantized_ab[:, 1])
                ax.annotate(str(i), (quantized_ab[i, 0], quantized_ab[i, 1]), fontsize=6)
                ax.set_xlim([-110, 110])
                ax.set_ylim([-110, 110])

        # Compute the color priority over a subset of the training set
        with h5py.File(os.path.join(self._data_dir, "HDF5_data_{}.h5".format(self._img_size)), "a") as hf_handler:

            x_ab = hf_handler["training_lab_data"][:100000][:, 1:, :, :]

            x_a = np.ravel(x_ab[:, 0, :, :])
            x_b = np.ravel(x_ab[:, 1, :, :])
            x_ab = np.vstack((x_a, x_b)).T

            if self._make_plot:
                plt.hist2d(x_ab[:, 0], x_ab[:, 1], bins=100, norm=LogNorm())
                plt.xlim([-110, 110])
                plt.ylim([-110, 110])
                plt.colorbar()
                plt.show()
                plt.clf()
                plt.close()

            # Create nearest neighbor instance with index = quantized_ab
            nearest_neighbor = 1
            nearest = nn.NearestNeighbors(n_neighbors=nearest_neighbor, algorithm="ball_tree").fit(quantized_ab)

            # Find index of nearest neighbor for x_ab
            distance, index = nearest.kneighbors(x_ab)

            # We now count the number of occurrences of each color
            index = np.ravel(index)
            counts = np.bincount(index)
            indexes = np.nonzero(counts)[0]
            priority_probability = np.zeros((quantized_ab.shape[0]))

            for i in range(quantized_ab.shape[0]):
                priority_probability[indexes] = counts[indexes]

            # We turn this into a color probability
            priority_probability = priority_probability / (1.0 * np.sum(priority_probability))

            # Save the probability
            np.save(os.path.join(self._data_dir, "DataSet_prior_probability_{}.npy".format(self._img_size)), priority_probability)

            if self._make_plot:
                plt.hist(priority_probability, bins=100)
                plt.yscale("log")
                plt.show()
Exemplo n.º 16
0
def pairgrid_heatmap(x, y, **kws):
    cmap = sns.light_palette(kws.pop("color"), as_cmap=True)
    plt.hist2d(x, y, cmap=cmap, cmin=1, **kws)
Exemplo n.º 17
0
def compute_prior_prob(image_files, width, height, do_plot, pts_in_hull_path,
                       prior_prob_path):
    """
    Compute color prior probabilities for pts in hull
    Reference: https://github.com/foamliu/Colorful-Image-Colorization/blob/master/class_rebal.py
    Usage:
        df_data        = pd.read_hdf(os.path.join(data_dir, "preprocessing", "DIV2K", "div2k.hdf5"), "data")
        list_types     = ["'train'"]
        df_select_data = df_data.query("type in [" + ",".join(list_types) + "]")
        image_dir      = os.path.join(dataset_dir, "DIV2K").replace("\\", "/")

        image_files    = image_dir + "/" + df_select_data["path"].values
        image_files[0:3], len(image_files)

        info = dict(
            image_files      = image_files,
            pts_in_hull_path = os.path.join(data_dir, "colorization_richard_zhang", "pts_in_hull.npy"),
            prior_prob_path  = os.path.join(data_dir, "preprocessing", "DIV2K", "prior_prob_train_div2k.npy"),
            width   = 112,
            height  = 112,
            do_plot = True
        )
        locals().update(**info)
        prior_prob = compute_prior_prob(**info)
    """
    # Load ab image
    X_ab = []
    for image_path in tqdm.tqdm(image_files):
        result = read_image(image_path,
                            is_resize=True,
                            width=width,
                            height=height)
        X_ab.append(result["res_img_Lab"][:, :, 1:])
    # for
    X_ab = np.array(X_ab)
    X_ab = X_ab - 128.0

    # Load the gamut points location
    q_ab = np.load(pts_in_hull_path)

    if do_plot:
        plt.figure(figsize=(8, 8))
        plt.title("ab quantize")
        gs = gridspec.GridSpec(1, 1)
        ax = plt.subplot(gs[0])
        for i in range(q_ab.shape[0]):
            ax.scatter(q_ab[:, 0], q_ab[:, 1])
            ax.annotate(str(i), (q_ab[i, 0], q_ab[i, 1]), fontsize=6)
            ax.set_xlim([-110, 110])
            ax.set_ylim([-110, 110])
        # for
    # if

    npts, c, h, w = X_ab.shape
    X_a_ravel = np.ravel(X_ab[:, :, :, 0])
    X_b_ravel = np.ravel(X_ab[:, :, :, 1])
    X_ab_ravel = np.vstack((X_a_ravel, X_b_ravel)).T

    if do_plot:
        plt.title("Prior Distribution in ab space\n", fontsize=16)
        plt.hist2d(X_ab_ravel[:, 0],
                   X_ab_ravel[:, 1],
                   bins=100,
                   density=True,
                   norm=LogNorm(),
                   cmap=plt.cm.jet)
        plt.xlim([-120, 120])
        plt.ylim([-120, 120])
        plt.xticks(fontsize=12)
        plt.yticks(fontsize=12)
        plt.xlabel("b channel", fontsize=14)
        plt.ylabel("a channel", fontsize=14)
        plt.colorbar()
        plt.show()
        plt.clf()
        plt.close()
    # if

    # Create nearest neighbord instance with index = q_ab
    NN = 1
    nearest = nn.NearestNeighbors(n_neighbors=NN,
                                  algorithm='ball_tree').fit(q_ab)
    # Find index of nearest neighbor for X_ab
    dists, ind = nearest.kneighbors(X_ab_ravel)

    # We now count the number of occurrences of each color
    ind = np.ravel(ind)
    counts = np.bincount(ind)
    idxs = np.nonzero(counts)[0]
    prior_prob = np.zeros((q_ab.shape[0]))

    prior_prob[idxs] = counts[idxs]

    # We turn this into a color probability
    prior_prob = prior_prob / (1.0 * np.sum(prior_prob))

    # Save
    if prior_prob_path is not None:
        save_dir = os.path.dirname(prior_prob_path)
        if save_dir != "" and os.path.exists(save_dir) == False:
            os.makedirs(save_dir)
        pts_in_hull_name = os.path.basename(pts_in_hull_path)
        safe_copy(pts_in_hull_path, os.path.join(save_dir, pts_in_hull_name))
        np.save(prior_prob_path, prior_prob)
    # if

    if do_plot:
        plt.hist(prior_prob, bins=100)
        plt.xlabel("Prior probability")
        plt.ylabel("Frequency")
        plt.yscale("log")
        plt.show()
    # if

    return prior_prob
    pass
Exemplo n.º 18
0
    #A = F_star/F_prev

    U = np.random.uniform()

    if U < A:
        y[i] = x2_star
        i = i + 1
        x2_prev = x2_star
    else:
        y[i] = x2_prev
        x2_prev = y[i]
        i = i + 1

# PLOTTING
plt.figure(figsize=[12, 12])
counts, xd, yd, fig = plt.hist2d(x, y, 31, cmap="Purples")
#or try plt.hexbin(x,y, cmap="Purples")

xt, yt = np.meshgrid(np.linspace(-3, 3, 100), np.linspace(-3, 3, 100))

plt.contour(xt, yt, np.exp(-xt**2 / 2) * np.exp(-yt**2 / 2))

plt.scatter(x, y, 0.1, alpha=0.2, cmap="BuGn")

# Yeah, I just can't figure out how to do these multivariate problems - I had the same problem with last week's homework. I think that I'm not setting up my proposal choices of x_star and y_star correctly, especially when the proposal function is not a simple normal or uniform. I also don't really know how the 2D contour plot should be set-up. I just saw the solution from HW3 was posted and tried adapting that but ran out of time.

# In[126]:

x_mean = np.mean(x)
y_mean = np.mean(y)
np.std(x)
Exemplo n.º 19
0
    #if (modelnames[m] != 'Persist'):
    y_pred = (model.predict(X_test)).ravel()
    #else:
    #    y_pred = np.roll(y_test.ravel(),1)
        
    y_preds[m,:] = y_pred.ravel()
    rms[m] = np.mean((y_pred-y_test)*(y_pred-y_test))
    residuals = y_pred-y_test
    gherkin = {"y_test":y_test, "y_pred":y_pred.ravel(), "residuals":residuals}
    # Bala, insert code here to calculate p values for residual
    pickle.dump( gherkin , open( "{0}/{1}_fit_residuals.pkl".format(dir,modelnames[m]), "wb" ) )
    
    print(modelnames[m], rms[m])
    f.write("{0}: {1:02d} {2:7.4f} {3:7.4f}\n".format(modelnames[m], lookforward, rms[m], np.mean((y_pred[27000:45000]-y_test[27000:45000])*(y_pred[27000:45000]-y_test[27000:45000]))))
    
    plt.hist2d(kp_unscale(y_test), kp_unscale(y_preds[m,:]), range=[[0,9],[0,9]], bins=[10,10], norm=LogNorm())
    plt.title(modelnames[m])
    plt.savefig('{0}/kp_regress_model_{1}.jpg'.format(dir,modelnames[m]))

f.close()

# In[167]:

#for m in range(len(models)):
#    plt.hist2d(kp_unscale(y_test), kp_unscale(y_preds[m,:]))#,norm=LogNorm())
#    plt.xlim(0,9)
#    plt.ylim(0,9)
#    plt.title(modelnames[m])
#    plt.colorbar()
    #plt.show()
#    plt.savefig('{0}/kp_regress_hist2d_{1}.jpg'.format(dir,modelnames[m]))
Exemplo n.º 20
0
def plot_chain2d(xh,
                 yh,
                 x0,
                 y0,
                 Rxval,
                 Ryval,
                 nRval=None,
                 modelpdf=None,
                 params=[]):
    from matplotlib import pylab as plt
    from matplotlib.colors import LogNorm
    import scipy.optimize as opt

    x = np.arange(np.min(xh) - 1.0,
                  np.max(xh) + 1, 0.05)
    y = np.arange(np.min(yh) - 1.0, np.max(yh) + 1, 0.05)

    X, Y = np.meshgrid(x, y)
    xs = np.array(zip(X, Y))
    Z = modelpdf(xs, params=params)

    plt.rc('font', family='sans-serif', size=16)
    fig = plt.figure(figsize=(15, 15))
    plt.subplot(221)
    plt.title('Gelman-Rubin indicator')
    #plt.plot(chain)
    plt.yscale('log')
    iterd = np.linspace(0, len(xh) - 1)
    xp = nRval * np.arange(np.size(Rxval))
    #print "x, Rxval:", np.size(xp), np.size(Rxval)
    plt.plot(xp, Rxval)
    plt.plot(xp, Ryval)

    ax = plt.subplot(222)
    plt.hist2d(xh, yh, bins=100, norm=LogNorm(), normed=1)
    plt.colorbar()

    dlnL2 = np.array(
        [2.30,
         9.21])  # contours enclosing 68.27 and 99% of the probability density
    xc = np.zeros([1, 2])
    Lmax = modelpdf(xc, params=params)
    lvls = Lmax / np.exp(0.5 * dlnL2)
    cs = plt.contour(X,
                     Y,
                     Z,
                     linewidths=(1.0, 2.0),
                     colors='black',
                     norm=LogNorm(),
                     levels=lvls,
                     legend='target pdf')

    xmin = -4.0
    xmax = 4.0
    nbins = 80
    dx = (xmax - xmin) / nbins
    dx2 = dx * dx
    H, xbins, ybins = np.histogram2d(xh,
                                     yh,
                                     bins=(np.linspace(xmin, xmax, nbins),
                                           np.linspace(xmin, xmax, nbins)))

    H = np.rot90(H)
    H = np.flipud(H)
    Hmask = np.ma.masked_where(H == 0, H)
    H = H / np.sum(H)

    clevs = [0.99, 0.6827]

    lvls = []
    for cld in clevs:
        sig = opt.brentq(conf_interval, 0., 1., args=(H, cld))
        lvls.append(sig)

    plt.contour(H,
                linewidths=(3.0, 2.0),
                colors='magenta',
                levels=lvls,
                norm=LogNorm(),
                extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]])

    plt.title('MCMC samples vs target distribution')

    labels = ['68.27%', '99%']
    for i in range(len(labels)):
        cs.collections[i].set_label(labels[i])

    plt.legend(loc='upper left')
    plt.ylabel('y')
    plt.xlabel('x')

    plt.show()
Exemplo n.º 21
0
print(sigma_hat)
print(np.linalg.det(sigma_hat))

a = np.zeros([len(label), len(f_x[0])])
for i in range(len(label)):
    a[i] += f_x[i]

print(np.cov(a))
print(np.linalg.det(np.cov(a)))

# print('{:.10f}'.format(np.linalg.det(sigma_hat)))

# P(f(x)| y=c) = N(f(x)|mu-hat[c], sigma-hat)
N = multivariate_normal(mean=mu_hat[0], cov=(sigma_hat + e))
bins = np.arange(0, 1000, 10)
plt.hist2d(N, bins)
plt.show()

# print(distance[i][int(len(data)*(95.0/100.0))-1])    # 95퍼센트 위치에 해당하는 인덱스의 값(근사)
# print(np.percentile(data, 95))                       # 95% 백분위수 출력(데이터의 95%가 발견되는 기댓값)
a = [None] * 10
for i in range(10):
    a[i] = list()
    a[i].append(np.percentile(distance[i], 95))

ood_distance = np.array(a)
print(
    "<ODD threshold of Euclidian distance of penultimate logits in each label>"
)
print(ood_distance)
print()
Exemplo n.º 22
0
    # regr = linear_model.BayesianRidge()
    regr = tree.DecisionTreeRegressor()
    regr.fit(cat_train_X, cat_train_y)

    cat_target_prediction = regr.predict(cat_target_X)

    diff = np.array(cat_target_true) - cat_target_prediction
    np_cat_target_true = np.array(cat_target_true)

    print "Normalized Median Absolute Deviation :" + str(nmad(diff))
    p_outliers = outliers(diff, np_cat_target_true, nmad(diff))
    print "Percentage of outliers = " + str(p_outliers) + " %"

    plt.figure()
    # plt.hist2d(np.array(cat_target_true), cat_target_prediction, bins=1000)
    plt.hist2d(np_cat_target_true, diff, bins=1000)
    plt.title(str(regr)[0:15])
    plt.xlabel('z')
    plt.ylabel('dz')
    plt.xlim([0., 0.6])
    plt.ylim([-0.08, 0.08])
    # plt.show()
    plt.savefig("plots/" + str(regr)[0:13] + ".png")

    """
    model = machine.logistic()
    model.train(cat_train_X, cat_train_y)
    cat_target_prediction = model.use(cat_target_X)
    """

    """
plt.ylabel('contas')
plt.xlabel(r'$masa \; (\frac{MeV}{c^2})$')
plt.grid()
plt.figure(4)
plt.hist(mA, bins=100, range=(1080, 1140))
plt.title(r'masa $\bar{\Lambda}^0$ datos LHCb')
plt.ylabel('contas')
plt.xlabel(r'$masa \; (\frac{MeV}{c^2})$')
plt.grid()

#plot
plt.figure(1)
plt.plot(x,
         y,
         linestyle='None',
         marker='o',
         markersize='0.1',
         color='g',
         label='datos LHCb')
plt.title('Armenteros-Podolanski plot')
plt.xlabel(r'$\alpha$')
plt.ylabel(r'$P_T \; (\frac{MeV}{c})$')
plt.legend(loc='best', markerscale=18)
plt.grid()
plt.figure(5)
plt.hist2d(x, y, bins=100, norm=LogNorm())
plt.title('Armenteros-Podolanski density plot')
plt.xlabel(r'$\alpha$')
plt.ylabel(r'$P_T \; (\frac{MeV}{c})$')
plt.colorbar()
Exemplo n.º 24
0
def analyze_activity(activity, lut_merged=None, onlycache=False):
    #if 'Snow' not in activity['name']:
    #    return

    cache_key = (activity["id"], "v0")
    if cache_key in cache:
        activity['analysis'] = cache[cache_key]
        print("\033[32min cache!\033[0m")
        return True
    if onlycache:
        return False

    d_N = 10

    if 'streams' not in activity:
        print("\033[31mno streams!\033[0m")
        return

    streams = activity['streams']

    try:
        distance = np.array(streams['distance']['data'])
        altitude = np.array(streams['altitude']['data'])
        ttime = np.array(streams['time']['data'])
        latlng = np.array(streams['latlng']['data'])
    except Exception as e:
        print("\033[31mno good data!\033[0m", e)
        traceback.print_exc()
        return

    cadence = None
    heartrate = None
    try:
        cadence = np.array(streams['cadence']['data']) * 2.
        heartrate = np.array(streams['heartrate']['data'])
    except Exception as e:
        print("\033[31mno optional data!\033[0m", e)

    d_distance = d_(distance, d_N)
    d_altitude = d_(altitude, d_N)
    d_d3 = (d_distance**2 + d_altitude**2)**0.5
    d_time = d_(ttime, d_N)

    speed_kph = d_d3 / d_time * 3600. / 1000.
    vam = d_altitude / d_time
    grade = d_altitude / d_d3 * 100.

    def dealias(x):
        mn = np.round(np.abs(x[x > 0]).min(), 5)
        print("found step", mn)
        x += np.random.rand(altitude.shape[0]) * mn - mn / 2.

    if cadence is not None:
        dealias(cadence)
    dealias(d_altitude)
    dealias(d_distance)

    speed_kph = d_d3 / d_time * 3600. / 1000.
    vam = d_altitude / d_time * 1000
    grade = d_altitude / d_d3 * 100.

    m = np.abs(d_altitude) < 1000
    m &= np.abs(vam) < 2000

    def p2d(x, y, x_bins, y_bins, mode='hist'):

        if mode == "scatter":
            plt.scatter(x, y, s=5)
        elif mode == 'hist':
            return plt.hist2d(x, y, bins=(
                x_bins,
                y_bins,
            ))
        elif mode == 'contour':
            h2, a, b = np.histogram2d(x, y, bins=(
                x_bins,
                y_bins,
            ))

            plt.contourf(
                a[:-1],
                b[:-1],
                np.transpose(h2),
                #levels=np.logspace(np.log10(h2.max()/1000.), np.log10(h2.max()), 100)
                levels=np.linspace(0, h2.max(), 100))

    for mode in 'hist', :  #'contour':
        f, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
        lut_speed_grade = p2d(
            speed_kph[m],
            #d_altitude[m]/d_time[m]*3600.,
            #60./(d_distance[m]/d_time[m]*3600./1000.),
            #d_distance[m]/d_time[m]*3600./1000.,
            #d_altitude[m]/d_time[m]*3600.,
            #d_altitude[m]/d_time[m]*3600,
            grade[m],
            mode=mode,
            x_bins=np.linspace(0, 15, 70),
            y_bins=np.linspace(-60, 60, 70),
            #y_bins = np.linspace(-2000, 2000, 70),
        )

        #luts.append(lut_speed_grade)

        plt.subplots_adjust(wspace=0)

        ax1.scatter(
            latlng[:, 1],
            latlng[:, 0],
            #c=cm.jet(np.array(streams['velocity_smooth']['data'])/6.),
            c=cm.jet(
                (
                    np.array(d_altitude * 3600) / (1000.)
                    #np.array(streams['altitude']['data'])-320)/(600-320)
                ), ))

        plt.title(activity['name'])

    print("total time", (ttime[-1] - ttime[0]) / 3600., "hr")

    x_grade = np.linspace(-50, 50)
    ax2.plot(
        [speed_estim_for_grade(x, lut_speed_grade, (0, 1.)) for x in x_grade],
        x_grade,
        c="k",
        lw=3,
    )

    if lut_merged is not None:
        ax2.plot([
            speed_estim_for_grade(x, lut_merged, (0.3, 0.7)) for x in x_grade
        ],
                 x_grade,
                 c="r",
                 lw=3,
                 ls=":")

    if heartrate is not None:
        plt.figure()
        plt.hist2d(
            grade[m],
            heartrate[m],
            bins=(np.linspace(-60, 60, 100), np.linspace(0, 200, 100)),
        )
        plt.axvline(8.)

    _ = estimate_track(d_d3,
                       d_altitude,
                       d_time,
                       ttime,
                       lut_speed_grade,
                       d_N=d_N)
    total_time_estim = _['total_time_estim']
    sum_time = _['sum_time']

    _ = estimate_track(d_d3, d_altitude, d_time, ttime, lut_merged, d_N=d_N)
    total_time_model_estim = _['total_time_estim']

    print("total time estimate", total_time_estim / 3600., "hr", "summed time",
          sum_time / 3600., "hr")
    print("total time model estimate", total_time_model_estim / 3600., "hr",
          "summed time", sum_time / 3600., "hr")

    S = {}
    activity['analysis'] = S

    activity['analysis']['modes'] = extract_modes(grade, d_time / (d_N - 1),
                                                  d_d3 / (d_N - 1), altitude)
    activity['analysis']['modes_string'] = ",".join([
        "%.5lg" % m['time_fraction_pc']
        for n, m in sorted(activity['analysis']['modes'].items(),
                           key=lambda x: x[1]['order']) if n is not "all"
    ])

    S['total_time_estimate_s'] = total_time_estim
    S['total_time_model_estimate_s'] = total_time_model_estim
    S['summed_time_s'] = sum_time
    S['lut'] = lut_speed_grade

    buf = io.BytesIO()
    plt.savefig(buf, format='png')

    S['lut_png_b64'] = base64.b64encode(buf.getvalue())

    #print("total run fraction", np.sum(m_run)/m_run.shape[0])

    #break

    #ax=plt.gca()
    #ax2=plt.twiny()
    #ax2.set_xlim(60./np.array(ax.get_xlim()))

    #plt.xlabel("pace")
    #plt.ylabel("grade")
    #plt.ylabel("VAM")

    cache[cache_key] = activity['analysis']
Exemplo n.º 25
0
            diff = abs(t - ht)
            xh = float(line[1])
            yh = float(line[2])
            zh = float(line[3])

    f.close()

    x = x - xh
    y = y - yh
    z = z - zh

    #plt.figure(figsize=(20,6))

    plt.subplot(131)
    #plt.scatter(x,y,c=colors[j], marker=shapes[j])
    plt.hist2d(x, y, bins=(xbins, xbins), norm=LogNorm())
    plt.xlabel(r'$x$ (kpc/$h$)', fontsize=18)
    plt.ylabel(r'$y$ (kpc/$h$)', fontsize=18)
    plt.xlim(-200, 200)
    plt.ylim(-200, 200)

    plt.subplot(132)
    #plt.scatter(x,z,c=colors[j], marker=shapes[j])
    plt.hist2d(x, z, bins=(xbins, xbins), norm=LogNorm())
    plt.xlabel(r'$x$ (kpc/$h$)', fontsize=18)
    plt.ylabel(r'$z$ (kpc/$h$)', fontsize=18)
    plt.xlim(-200, 200)
    plt.ylim(-200, 200)

    plt.subplot(133)
    #plt.scatter(y,z,c=colors[j], marker=shapes[j])
Exemplo n.º 26
0
import matplotlib as mpl
import matplotlib.pylab as plt
import numpy as np
#
#import math

end = int(0.1 * fluka1.shape[0])

fig = plt.figure()

plt.subplot(121)
x = fluka1[0:end, 1]
y = fluka1[0:end, 2]

plt.hist2d(x, y, bins=100, norm=mpl.colors.LogNorm(), cmap="viridis")

#H, xedges, yedges = np.histogram2d(x, y, bins=50, normed = True)
#
#plt.imshow(H)

#H2, _, _ = np.histogram2d(x,y, bins=50)
#extent = [0,1, xedges[-1], xedges[0]]
#plt.imshow(H/H2)
#plt.colorbar()

plt.xlabel('x [cm]', fontsize=12)
plt.ylabel('y [cm]', fontsize=12)
plt.title('Before TPST')
cbar = plt.colorbar()
#cbar.ax.set_xticklabels(0.10,100)
Exemplo n.º 27
0
	N = 0
	for target in proteins:
		target = 'T0760'
		for decoy in tqdm(decoys[target]):
			decoy_name = decoy[0].split('/')[-1]
			p_corr_x, p_corr_y = get_correlation(target, decoy_name)
			corr_x = corr_x + p_corr_x
			corr_y = corr_y + p_corr_y
			pearson_prot = stat.pearsonr(p_corr_x, p_corr_y)[0]

			# for x, y in zip(corr_x, corr_y):
			# 	if y<0:
			# 		print corr_x
			# 		print corr_y
			# 		print decoy_name
			# 		print target
			# 		raise(Exception("RMSD<0"))

			if not np.isnan(pearson_prot):
				av_pearson += pearson_prot
				N+=1
			
		break
	from matplotlib.colors import LogNorm
	# pearson_prot = stat.pearsonr(corr_x, corr_y)[0]	
	print av_pearson/N
	plt.hist2d(corr_x, corr_y, bins=100, range=np.array([(0.1, 5), (0, 10)]), norm=LogNorm())
	plt.colorbar()
	plt.savefig("corr_T0760_lr10A.png")
	
Exemplo n.º 28
0
 for k in seleccion:
     data_aux = np.loadtxt(caso + "distribucion_%f.txt" % (rhos[k]),
                           dtype=np.float32)
     data_q = data_aux[:, 0]
     data_p = data_aux[:, 1]
     plt.figure()
     for i in range(g):
         q_temp = np.array(data_q[i * 3 * n:(i + 1) * 3 * n],
                           dtype=np.float32)
         p_temp = np.array(data_p[i * 3 * n:(i + 1) * 3 * n],
                           dtype=np.float32)
         dq[i * p:(i + 1) * p], dp[i * p:(i + 1) * p] = deltas(
             q_temp, p_temp)
     counts, xbins, ybins, image = plt.hist2d(dq,
                                              dp,
                                              bins=100,
                                              norm=LogNorm(),
                                              cmap=plt.cm.rainbow)
     plt.colorbar()
     new_counts = scipy.ndimage.filters.gaussian_filter(counts, 1)
     CS = plt.contour(new_counts.transpose(),
                      extent=[xbins[0], xbins[-1], ybins[0], ybins[-1]],
                      linewidths=3,
                      colors="black",
                      levels=np.logspace(0, 2, 3))
     plt.clabel(CS, colors="black", inline=True, fmt="%d", fontsize=20)
     plt.xlabel(r'$\Delta q$')
     plt.ylabel(r'$\Delta p$')
     plt.axis([0, xbins[-1], 0, ybins[-1]])
     plt.title(r'$\rho=%1.4f fm^{-3}$' % (rhos[k]))
 plt.show()
Exemplo n.º 29
0
def loadfiles(task):
#Load an individual simulation
    tbegin = time.clock()

    dirs,files,grp,finalstep = task
    tfile = dirs + files + '.' + finalstep + '/' + files + '.' + finalstep
    print(tfile + '.' + grp)
    s = pynbody.load(tfile)
    s.physical_units()
    print("simulation loaded")
    hs = s.halos()       
    h = hs[int(grp)]
    print("halos loaded")
    pynbody.analysis.halo.center(h,mode='com',vel= False) #not as accurate as hyb but saves memory and time
    print("halo center of mass")
    #pynbody.analysis.angmom.sideon(h)
    #print("halo aligned")
    hrvir = np.max(h.gas['r'])   
   
    print("Read in .fits files")
    """
    #Read in iords of all particle that have been in the disk
    disk_iord_file = pyfits.open(dirs + 'grp' + grp + '.reaccrdiskall_iord.fits')
    #Include those particles that start off in the disk
    disk_early_iord_file = pyfits.open(dirs + 'grp' + grp + '.earlydisk_iord.fits')
    """

    #Read in iords of all particles that have been in the halo
    disk_iord_file = fits.open(dirs + 'grp' + grp + '.reaccr_iord.fits')
    #Include those particles that start off in the halo
    disk_early_iord_file = fits.open(dirs + 'grp' + grp + '.earlyhalo_iord.fits')

    disk_iord = disk_iord_file[0].data
    disk_early_iord = disk_early_iord_file[0].data
    disk_iord = np.concatenate((disk_iord,disk_early_iord))

    indicies = np.in1d(s.star['igasorder'],disk_iord) #Select for star particles in the main simulation that formed out of gas particles once in the disk/halo.
    disk_parts_star = s.star[np.nonzero(indicies)] #Note that multiple star particles can form from the same gas particle
    #Merge list of star formed from disk gas with list of all stars in halo at z = 0
    iord_disk_parts_star = disk_parts_star['iord'] #stars formed out of gas that was in the halo
    iord_halo_star = h.star['iord'] #stars in halo at z = 0
    iord_stars = np.concatenate((iord_disk_parts_star,iord_halo_star)) #concatenate arrays of iord
    temp, indices = np.unique(iord_stars, return_index=True) #remove duplicate stars
    iord_stars = iord_stars[indices]

    #Array of gas iords and star iords
    iord_all = np.concatenate((disk_iord,iord_stars)) #concatenate arrays of iord
    indicies = np.in1d(s['iord'],iord_all)
    disk_parts = s[np.nonzero(indicies)] #Note, many of the gas particles in disk_iord will have been deleted by z = 0 because of star formation

    #Calculate metallicity of gas and stars from O and Fe and the radius
    disk_parts.gas['metalMassFrac'] = 2.09*disk_parts.gas['OxMassFrac'] + 1.06*disk_parts.gas['FeMassFrac']
    disk_parts.star['metalMassFrac'] = 2.09*disk_parts.star['OxMassFrac'] + 1.06*disk_parts.star['FeMassFrac']
    disk_parts.dark['metalMassFrac'] = 0
    disk_parts['rad'] = np.sqrt(disk_parts['x']**2 + disk_parts['y']**2 + disk_parts['z']**2)
 
    # Goal: for any particle that is out of the halo at z = 0, use its metallicity at the time it left the halo, rather than its current metallicity
    reoutflow_iord_file = fits.open(dirs + 'grp' + grp + '.reoutflow_iord.fits') #All gas that exited the disk after having been in it
    reoutflow_iord = reoutflow_iord_file[0].data
    reoutflow_iord_rev = reoutflow_iord[::-1]
    temp, indices = np.unique(reoutflow_iord_rev, return_index=True) #indicies corresponding to unique outflow particles
    reoutflow_iord_rev_uniq = reoutflow_iord_rev[indices]

    reoutflow_met_file = fits.open(dirs + 'grp' + grp + '.reoutflow_history.fits')
    reoutflow_met = reoutflow_met_file[1].data['metallicity']
    reoutflow_met_rev = reoutflow_met[::-1]
    reoutflow_met_rev_uniq = reoutflow_met_rev[indices]

    # Select for only those outflowing particles that are gasous and outside of the halo at z = 0
    indicies = np.in1d(reoutflow_iord_rev_uniq,s['iord'])
    reoutflow_iord_rev_uniq = reoutflow_iord_rev_uniq[np.nonzero(indicies)] #Only those particles that are gasous at z = 0
    reoutflow_met_rev_uniq = reoutflow_met_rev_uniq[np.nonzero(indicies)]
    #Choice of whether adjusting the metallicities of those particles not in the disk or only those particles not in the halo.
    if 0:
        indicies = np.in1d(reoutflow_iord_rev_uniq,h['iord'],invert = 1) #only those particles that are not in the halo    
        reoutflow_iord_rev_uniq_out = reoutflow_iord_rev_uniq[np.nonzero(indicies)]
        reoutflow_met_rev_uniq_out = reoutflow_met_rev_uniq[np.nonzero(indicies)]
    else:
        reoutflow_iord_rev_uniq_out = reoutflow_iord_rev_uniq
        reoutflow_met_rev_uniq_out = reoutflow_met_rev_uniq
        
    #Select all particles that were in the halo but then leave it
    #to make sure that all gas particles in outflow were once accreted (debugging)
    disk_iord_uniq = np.unique(disk_iord)
    indicies = np.in1d(disk_iord_uniq,reoutflow_iord_rev_uniq)
    print(len(reoutflow_iord_rev_uniq))
    print(len(disk_iord_uniq[np.nonzero(indicies)]))
    indicies = np.in1d(reoutflow_iord_rev_uniq,disk_iord_uniq)
    print(len(reoutflow_iord_rev_uniq[np.nonzero(indicies)])) 
    #Why do these arrays not have the same length?!
    #Why are there particles in reoutflow that aren't in reaccr or early_halo?!

    # Replace their metallicity 
    # Matching particles in the outflow to those in disk_iord
    iord_intersect = np.intersect1d(disk_parts['iord'],reoutflow_iord_rev_uniq_out) #Find the iords of the elements in common
    a = disk_parts['iord']
    b = reoutflow_iord_rev_uniq_out
    intersect = np.intersect1d(a,b)
    asort = np.argsort(a)
    a_ind = np.searchsorted(a,intersect,sorter = asort)
    bsort = np.argsort(b)
    b_ind = np.searchsorted(b,intersect,sorter = bsort)
    inda = asort[a_ind]
    indb = bsort[b_ind]

    # Debugging plot to make sure that z = 0 and exiting metallicities are reasonable similar
    fig = plt.figure(7)
    temp = plt.hist2d(np.log10(np.array(disk_parts[inda]['metalMassFrac'])),np.log10(reoutflow_met_rev_uniq_out[indb]),bins = 400)
    plt.xlabel('log z = 0 Metallicity')
    plt.ylabel('log Metallicity when expelled')
    plt.plot([-9,0],[-9,0],color = 'k')

    disk_parts[inda]['metalMassFrac'] = reoutflow_met_rev_uniq_out[indb]

    #Make images
    outfilebase = dirs + files + '.grp' + grp
    print('Disk particles selected, now write to ' + outfilebase)
    outflowImages(s,disk_parts,hrvir,outfilebase)
    pltZvsR(disk_parts,hrvir,outfilebase)
    print 'Memory usage: %s (kb)' % resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    tend = time.clock()
    print 'Time elapsed: %f ' % (float(tend) - float(tbegin))
Exemplo n.º 30
0
f00 = plt.figure()
N, bins, patches = plt.hist(z, bins=61)
"""
for i in range(0,20):
    patches[i].set_facecolor('b')
for i in range(20,40):
    patches[i].set_facecolor('r')
for i in range(40, len(patches)):
    patches[i].set_facecolor('b')
"""
plt.xlabel("z in meter")


f0 = plt.figure()
plt.hist2d(z, dE, bins=[50, 50])
plt.xlabel("z in meter")
plt.ylabel("dE/E in sigmadE/E")
c = plt.colorbar()
c.set_label('number of particles', rotation=270)


f01 = plt.figure()
plt.hist(x, bins=200)
plt.xlabel("x in sig_x")

f02 = plt.figure()
plt.hist(y, bins=200)
plt.xlabel("y in sig_y")

f1 = plt.figure()
Exemplo n.º 31
0
                                           snr=20,
                                           dgd=10e-12,
                                           freq_off=00e6,
                                           lwdth=000e3,
                                           roll_frame_sync=True,
                                           modal_delay=[2000, 3000])
mysig3.sync2frame()
print(mysig3.shiftfctrs)
mysig3.corr_foe()
wxy, eq_sig = equalisation.pilot_equaliser(mysig3, (1e-3, 1e-3),
                                           45,
                                           foe_comp=False,
                                           methods=("cma", "sbd_data"))
cpe_sig, ph = phaserec.pilot_cpe(eq_sig, N=5, use_seq=False)
#cpe_sig = eq_sig
print(cpe_sig.cal_gmi())
plt.figure()
plt.subplot(121)
plt.hist2d(cpe_sig[0].real, cpe_sig[0].imag, bins=200)
#plt.plot(wxy[0][0].real, '-k')
#plt.plot(wxy[0][1].real, '--k')
#plt.plot(wxy[0][0].imag, '-c')
#plt.plot(wxy[0][1].imag, '--c')
plt.subplot(122)
plt.hist2d(cpe_sig[1].real, cpe_sig[1].imag, bins=200)
#plt.plot(wxy[1][0].real, '-k')
#plt.plot(wxy[1][1].real, '--k')
#plt.plot(wxy[1][0].imag, '-c')
#plt.plot(wxy[1][1].imag, '--c')
plt.show()
Exemplo n.º 32
0
import numpy as np
import astropy
from astropy import wcs
from astropy.io import fits
from astropy.table import Table
import matplotlib.pylab as plt

field = np.sys.argv[1]
x = fits.open('w'+field+'_rand_xy_mask.fits')
index = x[1].data['flag']==1
bins = 500
r1,r2 = np.count_nonzero(index),x[1].data['flag'].shape[0]
print(r1*1./r2)
plt.hist2d(x[1].data['ra'][index],x[1].data['dec'][index],bins=(bins,bins))
Exemplo n.º 33
0
def func_plot_cfhist(dat_string,
                     goal_var,
                     input_var,
                     train_fraction=0.6,
                     rel_flag=0,
                     max_depth_in=None):
    # TODO:rename train_fraction, its actually eval fraction
    # Applies a decisiontreeregressor returns a 2d hist plots
    # rel_flag: Since relative humidity is not part of the douze data it is calculated here and added according to this flat
    # rel_flag = 1, adds rel_hum
    # rel_flag = 2, adds qvlm_sqsm (qt-sat)
    # TODO: get rid of default rel_flag=0!! it will cause an error

    # Loading data
    data = Dataset(dat_string)
    # .ravel returns a masked flattened array
    # but all values are actually unmasked at this point

    # FLATTEN INPUT 2D ARRAY TO 1D MASKED ARRAY
    # TODO: add user-defined function flatten , flatten(variable)
    # y - e.g. LES cloud fraction,'True' values to compare with
    y = data.variables[goal_var][:].ravel()

    # FOR ALL INPUT VARIABLES' NAMES
    for input in input_var:
        # FLATTEN ARRAY 2D -> 1D
        x_temp = data.variables[input][:].ravel()
        # if input is the first in a list input fields
        # TODO: ask Philipp whats the point of this conditional?, use enumerate instead?
        if input == input_var[0]:
            X = x_temp
        else:
            #vstack concacenates row array into 2d array, by putting one (1,N) on top of the other
            # APPEND  a new 1xN row on every step
            X = np.vstack([X, x_temp])

    input_var_tmp = input_var.copy()
    # print(input_var_tmp)

    # Add relative humidity
    if rel_flag == 1:
        # qvlm - mean liquid water + vapor
        # qsm  - mean saturation mixing ratio?
        rel_sat = data.variables['qvlm'][:].ravel(
        ) / data.variables['qsm'][:].ravel()
        X = np.vstack([X, rel_sat])
        input_var_tmp.append('rel_sat')
    if rel_flag == 2:
        # TODO: qvlm, qsm are hardcoded here! Make them an explicit function input!
        # qvlm - liquid and gas water content, [kg/kg]?
        # qvl_qs the saturation deficit when qvlm is smaller than qsm
        qvl_qs = data.variables['qvlm'][:].ravel(
        ) - data.variables['qsm'][:].ravel()
        X = np.vstack([X, qvl_qs])
        input_var_tmp.append('qvl_qs')

    # print(input_var_tmp)
    # TODO: train_fraction is a fraction of true cloud fraction LES values to train on?
    n_e = int(len(y) * train_fraction)
    # or return a permuted range len=
    p = np.random.permutation(len(y))

    # TODO: variables below declared, but not called. Delete?
    y_shuf = y[p]
    X_shuf = X[:, p]

    #TODO: pick the train part randomly(not succesively) from y vector?

    y_train = y[n_e:]
    y_eval = y[:n_e]

    X_train = X[:, n_e:]
    X_eval = X[:, :n_e]

    X_train = X_train.transpose()
    X_eval = X_eval.transpose()

    # Now we set up and train the decision tree regressor
    # max_depth – The maximum depth of the tree. If None, then nodes are expanded
    # until all leaves are pure or until all leaves contain less than min_samples_split samples.
    regr_1 = tree.DecisionTreeRegressor(max_depth=max_depth_in)
    # Build a decision tree regressor from the training set (X, y)
    regr_1.fit(X_train, y_train)

    # Now the prediction magic happens
    y_pred = regr_1.predict(X_eval)
    # TODO: actually use array masking to increase performance. y_pred is a masked array, but has no masked entries!
    fig = plt.figure(figsize=(10, 10))
    bla = plt.hist2d(y_eval, y_pred, bins=100, norm=LogNorm())
    plt.colorbar()
    # bla = plt.hist2d(rel_sat,cl_l,bins=100, norm=LogNorm())
    # bla = plt.plot(sat_x,sat_ave,color='w',linewidth=2)
    # plt.vlines(1,0,1,'k')

    return fig
Exemplo n.º 34
0
    def HIST2D(self, BinsHist, gaus_peak, SDOM_num):
        #plotting 2D histogram of time residuals vs timestamps
        plt.figure(figsize=(12, 12))
        keep = self.rising_a_elim_3 >= 0
        self.abs_elim = self.abs_elim_3[keep] + self.rising_a_elim_3[keep]
        self.weights_keep = self.weights[keep]
        x = self.abs_elim
        y = self.abs_elim % gaus_peak
        #Log-scale
        h, self.xedges, self.yedges, img = plt.hist2d(
            x,
            y,
            BinsHist,
            #[np.linspace(0.0e10,0.2e10, 150), np.linspace(97620, 102000, 150)],
            #[np.linspace(2.68e10, 3e10, 150), np.linspace(13410, 13500, 150)],
            #cmin = 4 ,
            norm=LogNorm())
        cb = plt.colorbar()
        plt.xlabel('timestamps')
        plt.ylabel('time residuals')

        #No log-scale
        plt.figure(figsize=(12, 12))
        h, self.xedges, self.yedges, img = plt.hist2d(
            x,
            y,
            BinsHist,
            #[np.linspace(0.0e10,0.2e10, 150), np.linspace(97620, 102000, 150)],
            #[np.linspace(2.68e10, 3e10, 150), np.linspace(13410, 13500, 150)],
            #cmin = 4 ,
            #norm = LogNorm()
        )
        cb = plt.colorbar()
        plt.xlabel('timestamps')
        plt.ylabel('residuals')

        #the loop scans the graph to find the brightest bins - the brightest bins (ie bins with more events) generally correspond to the POCAM events
        self.POCAM_bins = ([])  #stores the bins that contain POCAM events
        for j in range(0, BinsHist):
            #print(j, j+1)
            bins = h[j:j + 1, 0:].flatten()
            max_ind = np.argmax(bins)  #selecting the brightest bin
            #moving along x-axis the POCAM_bins correspond to the bin number along y_axis
            self.POCAM_bins = np.append(self.POCAM_bins, max_ind)

        POCAM_diff = abs(
            self.POCAM_bins[1:] - self.POCAM_bins[:-1]
        )  #Subtracting the brightest bin number to observe where the jumps in time residuals have occurred

        #Conditions for selecting runs according to each sDOM
        if SDOM_num == ['SDOM5']:
            #Mode = mode(POCAM_diff[POCAM_diff > 1]) #in case this doesn't work use Mode = 1
            Mode = 1
            jump_index = np.where(
                (POCAM_diff > Mode - 25) * (POCAM_diff < Mode + 25))
        if SDOM_num == ['SDOM1']:
            Mode = 1
            jump_index = np.where(
                (POCAM_diff > Mode - 25) * (POCAM_diff < Mode + 25))
        if SDOM_num == ['SDOM2']:
            Mode = 1
            jump_index = np.where(
                (POCAM_diff > Mode - 25) * (POCAM_diff < Mode + 25))
        if SDOM_num == ['SDOM3']:
            Mode = 1
            jump_index = np.where(
                (POCAM_diff > Mode - 25) * (POCAM_diff < Mode + 25))

        self.JumpIndex = (np.array(jump_index).flatten()) + 1
        #print(self.JumpIndex)

        plt.plot(
            self.POCAM_bins, '.'
        )  #plotting POCAM_bins as a scatter plot to check if the right bins are bein picked

        self.gaus_peak = gaus_peak  #storing gaus_peak value as self.gaus_peak to use for calc_res function
        return self.abs_elim, BinsHist, self.JumpIndex, self.xedges, self.yedges, self.POCAM_bins, POCAM_diff
Exemplo n.º 35
0
import matplotlib.pylab as plt
import numpy as np
import pandas as pd
from matplotlib import cm
from scipy.stats import norm, chi2, binom, gamma
import seaborn as sns
from math import cos, sin, log, tan, pi, exp, sqrt, cosh, sinh, tanh, atan, atan2

for i in range(510):
    p = plt.figure(figsize=(13, 13), facecolor='black', dpi=100)
    p = plt.axis('off')
    x, y = np.random.normal(0, 1, i**2 + 20), np.random.normal(0, 1, i**2 + 20)
    p = plt.hist2d(x, y, bins=100, cmap='afmhot')
    p = plt.savefig(
        f'C:/Users/Alejandro/Pictures/RandomPlots/20042020/plot{i}.PNG',
        facecolor='black')
Exemplo n.º 36
0
    def showL11(self, sKey='vPhiObs'):

        """Plots loebman et al. 2011"""

        plt.figure(1, figsize=(8,4))
        plt.clf()

        plt.scatter(self.tSim['FeH'][self.bSel], \
                        self.tSim[sKey][self.bSel], \
                        alpha=0.25, s=4, edgecolor='None')

        # set the title
        sTitle = '(%.2f <= R < %.2f) ; (%.2f <= |Z| < %.2f); (%.1f <= Age < %.1f)' \
            % (self.RMin, self.RMax, self.ZMin, self.ZMax, self.AgeMin, self.AgeMax)

        if self.useFlatUncertainty:
            sTitle = '%s - PM error %.2f mas/yr' % (sTitle, self.uncPM)

        plt.suptitle(sTitle, fontsize=12)
        plt.xlabel(r"$\left[ \frac{Fe}{H} \right]$", fontsize=14)
        plt.ylabel(r"$v_{\phi}$, km s$^{-1}$", fontsize=14)

        bStrips = (self.stripCounts > 10)
        plt.errorbar(self.stripFeHs[bStrips], \
                         self.stripMeans[bStrips], \
                         yerr=self.stripStdds[bStrips], \
                         ecolor='k', fmt='yo', zorder=5)

        plt.scatter(self.stripFeHs[bStrips], \
                         self.stripMeans[bStrips], \
                        color=self.stripCounts[bStrips], \
                        s=36, edgecolor='y', \
                        cmap=plt.cm.get_cmap('cubehelix'), zorder=15)
        plt.colorbar()

        if sKey.find('Phi') > -1:
            dum = plt.hist2d(self.tSim['FeH'][self.bSel], \
                                 self.tSim[sKey][self.bSel], \
                                 bins=(75, 75), \
                                 range=[[-1.5,0.5], [-350., -50.]], \
                                 alpha=0.5, zorder=2)
            plt.colorbar()

        plt.xlim(-1.5,0.5)

        if sKey.find('Phi') > -1:
            plt.ylim(-50., -350.)

        if sKey.find('mu') > -1:
            plt.ylim(-100., 100.)

        # Couple of useful pieces to just see what the various lines
        # look like over the data
        xFine = np.linspace(-1.2, 0.3)
        showByHand = False
        if showByHand:
            pTry = [-0.4, -40., -240., 40.]
            plt.plot(xFine, twoStraight(xFine, pTry), 'k-', lw=2, zorder=25)

        showFit=True
        if showFit:
            plt.plot(xFine, oneStraight(xFine, self.parsStraight), \
                                            'k--', lw=2, zorder=25)

            plt.plot(xFine, twoStraight(xFine, self.parsBroken), \
                                            'k-', lw=2, zorder=25)
Exemplo n.º 37
0
print "=================="
print "best fit slope:"
pstats(mh)
print "best fit normalization (pivoted) to ",ay
pstats(ch)
print "best fit scatter in y direction:"
pstats(sh)
print "=================="

#            
# plot results:
#
    

plt.rc('font', family='sans-serif', size=16)
fig=plt.figure(figsize=(6,10))
plt.subplot(211)
plt.title('Metropolis')
plt.plot(chain)

ax = plt.subplot(212)
plt.hist2d(mh,ch, bins=60, norm=LogNorm(), normed=1)
plt.colorbar()

plt.legend(loc='upper center')
plt.ylabel('c')
plt.xlabel('m')

plt.show()