def bin_points(x, y, yerr): bins = np.geomspace(0.1, x.max(), 100) x_b = bs(x, x, 'mean', bins=bins)[0] y_b = bs(x, y, 'mean', bins=bins)[0] yerr_b = bs(x, yerr, sum_errors, bins=bins)[0] valid = ~np.isnan(x_b) x_b = x_b[valid] y_b = y_b[valid] yerr_b = yerr_b[valid] return x_b, y_b, yerr_b
def imbin(im, binning_c=1, binning_r=1): """ Bins image by binning_c in the column direction and binning_r in the row direction """ shape = np.shape(im) from functools import reduce def factors(n): return np.array(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) fac_r = factors(shape[0]) fac_c = factors(shape[1]) bin_r = fac_r[np.abs(fac_r-binning_r).argmin()] #print('row binning set to {}, closest factor of row length to {}'.format(bin_r, binning_r)) bin_c = fac_c[np.abs(fac_c-binning_c).argmin()] #print('col binning set to {}, closest factor of col length to {}'.format(bin_c, binning_c)) rows = np.arange(shape[0]) if binning_r == 1: ROWS = rows else: ROWS = bs(rows, rows, statistic='mean', bins=len(rows)/bin_r) cols = np.arange(shape[1]) if binning_c == 1: COLS = cols else: COLS = bs(cols, cols, statistic='mean', bins=len(cols)/bin_c) def rebin(arr, new_shape): if len(new_shape) == 2: shape = (int(new_shape[0]), int(arr.shape[0] // new_shape[0]), int(new_shape[1]), int(arr.shape[1] // new_shape[1])) elif len(new_shape) == 3: shape = (int(new_shape[0]), int(arr.shape[0] // new_shape[0]), int(new_shape[1]), int(arr.shape[1] // new_shape[1]), int(new_shape[2]), int(arr.shape[2] // new_shape[2])) return arr.reshape(shape).mean(-1).mean(1) if len(shape) == 2: binning = np.array([bin_r, bin_c]) elif len(shape) == 3: binning = np.array([bin_r, bin_c, 1]) IM = rebin(im, (shape / binning)) return IM, ROWS[0], COLS[0], binning
def norm_mass(file1): ''' Checking that core abundance increases with host halo mass as a power law with index unity ''' infall_mass = gio.gio_read(file1, 'infall_mass').flatten() infall_step = gio.gio_read(file1, 'infall_step').flatten() host_tag = gio.gio_read(file1, 'fof_halo_tag').flatten() host_mass = infall_mass infall_mass = infall_mass[host_mass >= 1.e12] host_tag = host_tag[host_mass >= 1.e12] infall_step = infall_step[host_mass >= 1.e12] host_tag_u = host_tag[infall_step == 499] host_mass = host_mass[host_mass >= 1.e12][infall_step == 499] dict_values = dict(zip(host_tag_u, host_mass)) def apply_dict(i): return dict_values[i] infall_mass = gio.gio_read(file1, 'infall_mass').flatten() infall_step = gio.gio_read(file1, 'infall_step').flatten() host_tag = gio.gio_read(file1, 'fof_halo_tag').flatten() mask = (infall_mass > 1.e12) & (infall_step != 499) tags, counts = np.unique(host_tag[mask], return_counts=True) mass_tot = np.array(map(apply_dict, tags)) mean_sats, edges, nums = bs(np.log10(mass_tot), counts - 1., statistic='mean', bins=100) bin_mean = (edges[1:] - edges[:-1]) / 2. + edges[:-1] idx_13 = np.where( np.abs(bin_mean - 13.5) == np.min(np.abs(bin_mean - 13.5))) idx_15 = np.where( np.abs(bin_mean - 14.5) == np.min(np.abs(bin_mean - 14.5))) diff_val = (np.log10(mean_sats[idx_15]) - np.log10(mean_sats[idx_13])) criteria = (diff_val < 1.2) & (diff_val > 0.8) print((np.log10(mean_sats[idx_15]) - np.log10(mean_sats[idx_13]))) if vis: plt.figure() plt.plot(bin_mean, mean_sats) plt.ylabel('N_cores (>1.e12 mass)') plt.yscale('log') plt.xlabel('log10(host mass)') plt.xlim([12.5, 15]) plt.show() if criteria: print( "power law index of core abundance as a function of host halo mass has an index of nearly unity" ) return 0 else: print("core abundance as a function of host halo mass needs checking") return 1
def cauchy(x0, *x): m, Circstd = x0 data_x, data_y = Bedges, hist xtemp = np.linspace(-0.5 * np.pi, 0.5 * np.pi, num=1000) model = (np.sinh(2.0 * Circstd**2)) / ( np.pi * (np.cosh(2.0 * Circstd**2) - np.cos(2.0 * (xtemp - m)))) model = bs(xtemp, model, bins=data_x)[0] model = model / (np.sum(model) * (data_x[1] - data_x[0])) chisq = (((data_y - model)**2)).sum() return chisq
def binned_stats(): data = Table.read('data_v4.txt', format='ascii.commented_header', guess=False) band_names = data.colnames for i in range(11, len(data.colnames), 4): band = band_names[i] band_mag = data[band] band_unc = data[band+'_unc'] trim = np.logical_and(band_mag != -99, band_unc != -99) band_mag_trim = band_mag[trim] band_unc_trim = band_unc[trim] stats = bs(band_mag_trim, band_unc_trim, statistic='median', bins=10) print '__________{}_________'.format(band) print 'unc_bins: {}'.format(stats[0]) print 'mag_bins: {} \n'.format(stats[1])
def binned_stats(): data = Table.read('data_v4.txt', format='ascii.commented_header', guess=False) band_names = data.colnames for i in range(11, len(data.colnames), 4): band = band_names[i] band_mag = data[band] band_unc = data[band + '_unc'] trim = np.logical_and(band_mag != -99, band_unc != -99) band_mag_trim = band_mag[trim] band_unc_trim = band_unc[trim] stats = bs(band_mag_trim, band_unc_trim, statistic='median', bins=10) print '__________{}_________'.format(band) print 'unc_bins: {}'.format(stats[0]) print 'mag_bins: {} \n'.format(stats[1])
def get_lf(zrange, bins, old=True): if old: z, m, p = np.loadtxt('Data/glikman11qso.dat', usecols=(1, 2, 3), unpack=True) else: z, m, p = np.loadtxt('Data/glikman11debug.dat', usecols=(1, 2, 3), unpack=True) select = ((z >= zrange[0]) & (z < zrange[1])) m = m[select] p = p[select] area = 1.71 # deg^2 dz = 0.02 dm = 0.05 if old: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_ndwfs_old.dat', usecols=(1, 2, 3), unpack=True) else: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_ndwfs.dat', usecols=(1, 2, 3), unpack=True) vol = volume(zsel, area) * dz psel[(zsel < zrange[0]) | (zsel >= zrange[1])] = 0.0 # m[:12] because only those qsos are from NDWFS v1 = np.array( [binvol(x, zrange, bins, msel, psel, vol, zsel) for x in m[:12]]) area = 2.05 # deg^2 if old: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_dls_old.dat', usecols=(1, 2, 3), unpack=True) else: zsel, msel, psel = np.loadtxt('Data/glikman11_selfunc_dls.dat', usecols=(1, 2, 3), unpack=True) vol = volume(zsel, area) * dz psel[(zsel < zrange[0]) | (zsel >= zrange[1])] = 0.0 # m[12:] because only those qsos are from DLS v2 = np.array( [binvol(x, zrange, bins, msel, psel, vol, zsel) for x in m[12:]]) v = np.concatenate((v1, v2)) print v.size v_nonzero = v[np.where(v > 0.0)] print v_nonzero.size m = m[np.where(v > 0.0)] h = np.histogram(m, bins=bins, weights=1.0 / (v_nonzero)) nums = h[0] mags = (h[1][:-1] + h[1][1:]) * 0.5 dmags = np.diff(h[1]) * 0.5 left = mags - h[1][:-1] right = h[1][1:] - mags phi = nums logphi = np.log10(phi) # cMpc^-3 mag^-1 n = np.histogram(m, bins=bins)[0] print n nlims = pci(n, interval='frequentist-confidence') nlims *= phi / n uperr = np.log10(nlims[1]) - logphi downerr = logphi - np.log10(nlims[0]) mags, b, c = bs(m, m, bins=bins) left = mags - b[:-1] right = b[1:] - mags return mags, left, right, logphi, uperr, downerr
def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir): plot_num = 0 if self.truncate_cat_name: catalog_name = re.split('_', catalog_name)[0] for plot_param in self.plot_list: plot_num += 1 if plot_param["frame"] == "rest": mag_frame = "Mag_true" mag_end = "_z0" else: mag_frame = "mag" mag_end = "" mag1_str = "{}_{}_{}{}".format(mag_frame, plot_param["mag1"], plot_param["filter"], mag_end) mag2_str = "{}_{}_{}{}".format(mag_frame, plot_param["mag2"], plot_param["filter"], mag_end) mag1_val = self._get_quantity( catalog_instance, mag1_str, redshift_block_limit=plot_param['redshift_block_limit'], redshift_limit=plot_param['redshift_limit'], ) mag2_val = self._get_quantity( catalog_instance, mag2_str, redshift_block_limit=plot_param['redshift_block_limit'], redshift_limit=plot_param['redshift_limit'], ) redshift = self._get_quantity( catalog_instance, 'redshift', redshift_block_limit=plot_param['redshift_block_limit'], redshift_limit=plot_param['redshift_limit'], ) clr_val = mag1_val - mag2_val title = "" slct, title = self._get_selection_and_title( catalog_instance, title, plot_param, redshift_limit=plot_param['redshift_limit'], redshift_block_limit=plot_param['redshift_block_limit']) fig, ax = plt.subplots() # for ax_this in (ax, self.summary_ax): if plot_param['redshift_limit'] is not None: redshift_bins = np.linspace( 0, 1.05 * plot_param['redshift_limit'], 256) elif plot_param['redshift_block_limit'] is not None: redshift_bins = np.linspace( 0, 1.05 * (plot_param['redshift_block_limit']), 256) else: redshift_bins = np.linspace(0, 1.05, 256) h, xbins, ybins = np.histogram2d(redshift[slct], clr_val[slct], bins=(redshift_bins, np.linspace(-0.4, 2.2, 256))) if plot_param["log_scale"]: pc = ax.pcolor(xbins, ybins, h.T + 1.0, norm=clr.LogNorm()) fig.colorbar(pc, ax=ax).set_label("Population Density + 1") else: pc = ax.pcolor(xbins, ybins, h.T) fig.colorbar(pc, ax=ax).set_label("Population Density") mag1 = re.split('_', mag1_str)[1] #get filter mag2 = re.split('_', mag2_str)[1] #get filter # plot observations for v in self.validation_data.values(): color = mag1 + '-' + mag2 if v['format'] == 'fit': coeffs = v[color] zmask = (redshift_bins >= v['zmin']) & (redshift_bins <= v['zmax']) obs = np.zeros(len(redshift_bins[zmask])) for n, coeff in enumerate(coeffs): obs += coeff * redshift_bins[zmask]**(len(coeffs) - 1 - n) ax.plot(redshift_bins[zmask], obs, color='r', label=v['label']) elif v['format'] == 'data': if color in v.keys(): zbins = np.asarray(v['bins']) mean, _, num = bs(v['z'], v[color], bins=zbins) std, _, num = bs(v['z'], v[color], bins=zbins, statistic='std') fmask = np.isfinite(mean) z_cen = 0.5 * (zbins[1:] + zbins[:-1]) ax.errorbar(z_cen[fmask], mean[fmask], ls='', marker='o', yerr=np.sqrt(std[fmask]), c='orange', label=v['label']) counts = [ np.sum(num == i + 1) for i in range(len(zbins)) ] print(z_cen[fmask], mean[fmask], std[fmask], counts) legend = ax.legend(loc='lower right', fontsize=self.legend_size) plt.setp(legend.get_texts(), color='w') ax.set_ylabel('{} - {}'.format(mag1, mag2), size=self.font_size) ax.set_xlabel('Redshift $z$', size=self.font_size) if self.title_in_legend: title = '{}\n{}'.format(catalog_name, title) else: ax.set_title(catalog_name) ax.text(0.05, 0.95, title, transform=ax.transAxes, verticalalignment='top', color='white', fontsize=self.text_size) fig.savefig( os.path.join(output_dir, 'plot_{}.png'.format(plot_num))) plt.close(fig) return TestResult(0, inspect_only=True)
def bin_errors(array): return bs(x, array, sum_errors, bins=self.binning)
def bin_errors(array): return bs(x, array, sum_errors, bins=bins)
def binning(array): return bs(x, array, 'mean', bins=bins)
print('Getting data in specific percentile') mcmc_table_pctl, bf_params, bf_chi2 = \ get_paramvals_percentile(mcmc_table, 68, chi2) model_init = halocat_init(halo_catalog, z_median) gals_df = populate_mock(bf_params[:5], model_init) #mstar cut at 8.9 in h=1 (8.6) gals_df['cs_flag'] = np.where(gals_df['halo_hostid'] == \ gals_df['halo_id'], 1, 0) cen_halos_bf, sat_halos_bf = get_host_halo_mock(gals_df, 'vishnu') cen_gals_bf, sat_gals_bf = get_stellar_mock(gals_df, 'vishnu') bins = np.linspace(10, 15, 7) shmr = bs(cen_halos_bf, cen_gals_bf, statistic='mean', bins=bins) centers = 0.5 * (shmr[1][1:] + shmr[1][:-1]) x_bf = centers y_bf = shmr[0] H0 = 100 cz_inner = 3000 cz_outer = 12000 dist_inner = kms_to_Mpc(H0, cz_inner) #Mpc/h dist_outer = kms_to_Mpc(H0, cz_outer) #Mpc/h v_inner = vol_sphere(dist_inner) v_outer = vol_sphere(dist_outer) v_sphere = v_outer - v_inner v_sim = v_sphere / 8
def binning(array): return bs(x, array, 'mean', bins=self.binning)
sigma = np.std(deltav[deltav != 0]) # blue_cen_mstar_data.append(cen_mstar) sigma_blue_data.append(sigma) deltav_red_data = np.asarray(deltav_red_data) deltav_blue_data = np.asarray(deltav_blue_data) red_cen_mstar_data = np.asarray(red_cen_mstar_data) blue_cen_mstar_data = np.asarray(blue_cen_mstar_data) sigma_red_data = np.asarray(sigma_red_data) sigma_blue_data = np.asarray(sigma_blue_data) # deltav_red_data = np.log10(deltav_red_data) # deltav_blue_data = np.log10(deltav_blue_data) std_stats_red_data = bs(red_cen_mstar_data, deltav_red_data, statistic='std', bins=np.linspace(8.9, 11.5, 5)) std_stats_blue_data = bs(blue_cen_mstar_data, deltav_blue_data, statistic='std', bins=np.linspace(8.9, 11.5, 5)) count_stats_red_data = bs(red_cen_mstar_data, deltav_red_data, statistic='count', bins=np.linspace(8.9, 11.5, 5)) count_stats_blue_data = bs(blue_cen_mstar_data, deltav_blue_data, statistic='count', bins=np.linspace(8.9, 11.5, 5))
plt.xlabel('RA (pixels)') plt.ylabel('dec (pixels)') plt.savefig('%s%d%sbeta.png' % (PATH, NGC, band), bbox_inches='tight') plt.close('all') ''' USE FMIN TO FIT WRAPPED CAUCHY DISTRIBUTION TO HISTOGRAM AND SAVE PLOT ''' data_x, data_y = Bedges, hist guess = [mean_abc, ABCircstdC] best_parameters = fmin(cauchy, guess, (data_x, data_y)) m, Circstd = best_parameters[0], best_parameters[1] xtemp = np.linspace(-0.5 * np.pi, 0.5 * np.pi, num=1000) model = (np.sinh(2.0 * Circstd**2)) / ( np.pi * (np.cosh(2.0 * Circstd**2) - np.cos(2.0 * (xtemp - m)))) fit = bs(xtemp, model, bins=data_x)[0] fit = fit / (np.sum(fit) * (data_x[1] - data_x[0])) Bedges = np.array([ 0.5 * (Bedges[i] + Bedges[i + 1]) for i in np.arange(len(Bedges) - 1) ]) plt.hist(abc_flat, range=[-0.5 * np.pi, 0.5 * np.pi], bins=nbin, edgecolor='black', linewidth=1.1, histtype='step', normed=True) plt.xlabel(r' $\alpha - \beta$' ' (degs)') plt.ylabel(r' $P(\alpha - \beta)$') plt.plot(Bedges,