def find_nearest_neighbors(nside, pix_idx): # TODO # Determine mapping of pixel indices at highest resolutions to index # in the array pix_idx nside_max = np.max(nside) pix_idx_highres = [] for n in np.unique(nside): idx = (nside == n) pix_idx l = np.empty(pix_idx.size, dtype='f8') b = np.empty(pix_idx.size, dtype='f8') neighbor_idx = np.empty((8, pix_idx.size), dtype='i8') neighbor_dist = np.empty((8, pix_idx.size), dtype='f8') for n in np.unique(nside): idx = (nside == n) l[idx], b[idx] = hputils.pix2lb(n, pix_idx[idx], nest=True) neighbor_idx[:, idx] = 1
def get_prior_ln_Delta_EBV(nside, pix_idx, n_regions=30): # Find (l, b) for each pixel l = np.empty(pix_idx.size, dtype='f8') b = np.empty(pix_idx.size, dtype='f8') for n in np.unique(nside): idx = (nside == n) l[idx], b[idx] = hputils.pix2lb(n, pix_idx[idx], nest=True) # Determine priors in each pixel gal_model = model.TGalacticModel() ln_Delta_EBV = np.empty((pix_idx.size, n_regions+1), dtype='f8') for i,(ll,bb) in enumerate(zip(l, b)): ret = gal_model.EBV_prior(ll, bb, n_regions=n_regions) ln_Delta_EBV[i, :] = ret[1] return ln_Delta_EBV
def find_neighbors_naive(nside, pix_idx, n_neighbors): ''' Find the neighbors of each pixel using a naive algorithm. Each pixel is defined by a HEALPix nside and pixel index (in nested order). Returns two arrays: neighbor_idx (n_pix, n_neighbors) Index of each neighbor in the nside and pix_idx arrays. neighbor_dist (n_pix, n_neighbors) Distance to each neighbor. ''' # Determine (l, b) of all pixels l = np.empty(pix_idx.size, dtype='f8') b = np.empty(pix_idx.size, dtype='f8') nside_unique = np.unique(nside) for n in nside_unique: idx = (nside == n) l[idx], b[idx] = hputils.pix2lb(n, pix_idx[idx], nest=True) # Determine distances between all pixel pairs dist = gc_dist_all(l, b) # Determine closest neighbors sort_idx = np.argsort(dist, axis=1) neighbor_idx = sort_idx[:, 1:n_neighbors+1] neighbor_dist = np.sort(dist, axis=1)[:, 1:n_neighbors+1] return neighbor_idx, neighbor_dist
def load_output_file_unified(f, bounds=None, max_samples=None, load_stacked_pdfs=False): # Pixel locations dset = f['locations'] nside = dset['nside'][:] pix_idx = dset['healpix_index'][:] cloud_mask = dset['cloud_mask'][:].astype(np.bool) los_mask = dset['piecewise_mask'][:].astype(np.bool) n_stars = dset['n_stars'][:] # Cloud model tmp_samples, cloud_lnp, cloud_GR = unpack_dset(f['cloud'], max_samples=max_samples) n_clouds = tmp_samples.shape[2] / 2 cloud_mu = tmp_samples[:, :, :n_clouds] cloud_delta_EBV = tmp_samples[:, :, n_clouds:] # Piecewise-linear model los_EBV, los_lnp, los_GR = unpack_dset(f['piecewise'], max_samples=max_samples) DM_min = float(f['piecewise'].attrs['DM_min']) DM_max = float(f['piecewise'].attrs['DM_max']) EBV_min, EBV_max = 0., 5. # Stacked pdfs star_stack = None if load_stacked_pdfs: dset = f['stacked_pdfs'] star_stack = dset[:] EBV_min = float(dset.attrs['EBV_min']) EBV_max = float(dset.attrs['EBV_max']) # Filter out-of-bounds pixels if bounds != None: l = np.empty(nside.size, dtype='f8') b = np.empty(nside.size, dtype='f8') for n in np.unique(nside): idx = (nside == n) l[idx], b[idx] = hputils.pix2lb(n, pix_idx[idx], nest=True) idx = ( (l >= bounds[0]) & (l <= bounds[1]) & (b >= bounds[2]) & (b <= bounds[3]) ) nside = nside[idx] pix_idx = pix_idx[idx] cloud_mask = cloud_mask[idx] los_mask = los_mask[idx] n_stars = n_stars[idx] cloud_mu = cloud_mu[idx] cloud_delta_EBV = cloud_delta_EBV[idx] cloud_GR = cloud_GR[idx] cloud_lnp = cloud_lnp[idx] los_EBV = los_EBV[idx] los_GR = los_GR[idx] los_lnp = los_lnp[idx] if load_stacked_pdfs: star_stack = star_stack[idx] # Return if len(pix_idx) == 0: return None pix_info = (pix_idx, nside, cloud_mask, los_mask, n_stars) # Cloud information cloud_info = None if np.sum(cloud_mask) > 0: cloud_info = (cloud_mu, cloud_delta_EBV, cloud_lnp, cloud_GR) # Piecewise-linear model information los_info = None if np.sum(los_mask) > 0: los_info = (los_EBV, los_lnp, los_GR) # Limits on DM and E(B-V) (for l.o.s. fits and stacked surfaces) DM_EBV_lim = (DM_min, DM_max, EBV_min, EBV_max) return pix_info, cloud_info, los_info, star_stack, DM_EBV_lim
def dust_priors(): img_fname = pan1 + 'paper-plots/dust_priors.svg' dpi = 400 figsize = (9, 4) img_shape = (4000, 2000) nside = 16 n_regions = 30 sigma_bin = 1.4 gal_model = model.TGalacticModel() n_pix = hp.pixelfunc.nside2npix(nside) pix_idx = np.arange(n_pix) l, b = hputils.pix2lb(nside, pix_idx) log_Delta_EBV = np.empty((n_pix, n_regions+1), dtype='f8') # Determine log(Delta EBV) in each pixel and bin for i,ll,bb in zip(pix_idx, l, b): tmp = gal_model.EBV_prior(ll, bb, n_regions=n_regions, sigma_bin=1.4) log_Delta_EBV[i,:] = tmp[1][:] scatter = sigma_bin * np.random.normal(size=log_Delta_EBV.shape) EBV_rand = np.sum(np.exp(log_Delta_EBV + scatter), axis=1) EBV_smooth = np.sum(np.exp(log_Delta_EBV), axis=1) * np.exp(0.5*sigma_bin**2.) # Load SFD dust map EBV_SFD = model.get_SFD_map(nside=nside) # Rasterize maps proj = hputils.Hammer_projection() nside_arr = nside * np.ones(n_pix, dtype='i8') rasterizer = hputils.MapRasterizer(nside_arr, pix_idx, img_shape, proj=proj) bounds = rasterizer.get_lb_bounds() log_SFD = np.log10(EBV_SFD) log_rand = np.log10(EBV_rand) log_smooth = np.log10(EBV_smooth) log_SFD_diff = log_SFD - log_smooth log_rand_diff = log_rand - log_smooth print np.percentile(log_SFD_diff, [5., 10., 50., 90., 95.]) vmin_top = min([np.min(log_SFD), np.min(log_rand)]) vmax_top = max([np.max(log_SFD), np.max(log_rand)]) vmax_bot = max([np.max(np.abs(log_SFD_diff)), np.max(np.abs(log_rand_diff))]) vmin_bot = -vmax_bot img_SFD = rasterizer(log_SFD) img_rand = rasterizer(log_rand) img_SFD_diff = rasterizer(log_SFD_diff) img_rand_diff = rasterizer(log_rand_diff) # Rasterize regions to shade on maps shade_fn = lambda l, b: hputils.lb_in_bounds(l, b, [-60., 60., -5., 5.]).astype('f8') img_shade = rasterizer.rasterize_func(shade_fn) nside_hidpi = 256 n_pix_hidpi = hp.pixelfunc.nside2npix(nside_hidpi) pix_idx_hidpi = np.arange(n_pix_hidpi) nside_arr_hidpi = nside_hidpi * np.ones(n_pix_hidpi, dtype='i8') rasterizer_hidpi = hputils.MapRasterizer(nside_arr_hidpi, pix_idx_hidpi, img_shape, proj=proj) bounds_hidpi = rasterizer.get_lb_bounds() l, b = hputils.pix2lb(nside_hidpi, pix_idx_hidpi) clipped = np.empty(n_pix_hidpi, dtype='f8') # Determine log(Delta EBV) in each pixel and bin for i,(ll,bb) in enumerate(zip(l, b)): tmp = gal_model.EBV_prior(ll, bb, n_regions=n_regions, sigma_bin=1.4) clipped[i] = np.max(tmp[1][:]) >= -4. - 1.e-5 #clipped = (np.max(log_Delta_EBV, axis=1) >= -4. - 1.e-5).astype('f8') img_clipped = rasterizer_hidpi(clipped) # Plot in 2x2 grid fig = plt.figure(figsize=figsize, dpi=dpi) ax = [fig.add_subplot(2,2,i+1) for i in xrange(4)] im_11 = ax[0].imshow(img_SFD.T, extent=bounds, aspect='auto', origin='lower', interpolation='nearest', vmin=vmin_top, vmax=vmax_top, rasterized=True) im_12 = ax[1].imshow(img_rand.T, extent=bounds, aspect='auto', origin='lower', interpolation='nearest', vmin=vmin_top, vmax=vmax_top, rasterized=True) im_21 = ax[2].imshow(img_SFD_diff.T, extent=bounds, aspect='auto', origin='lower', interpolation='nearest', vmin=vmin_bot, vmax=vmax_bot, cmap='bwr', rasterized=True) im_22 = ax[3].imshow(img_rand_diff.T, extent=bounds, aspect='auto', origin='lower', interpolation='nearest', vmin=vmin_bot, vmax=vmax_bot, cmap='bwr', rasterized=True) for i in xrange(4): #ax[i].imshow(img_shade.T, extent=bounds, aspect='auto', # origin='lower', interpolation='nearest', # vmin=0., vmax=1., cmap='binary', # alpha=0.25) ax[i].imshow(img_clipped.T, extent=bounds_hidpi, aspect='auto', origin='lower', interpolation='nearest', vmin=0., vmax=1., cmap='binary', alpha=0.25, rasterized=True) fig.subplots_adjust(left=0.02, right=0.82, bottom=0.02, top=0.92, hspace=0.02, wspace=0.02) # Remove axis frames for a in ax: a.axis('off') # Color bars x0,y0,w,h = ax[1].get_position().bounds cax_top = fig.add_axes([x0 + w + 0.01, y0, 0.03, h]) x0,y0,w,h = ax[3].get_position().bounds cax_bot = fig.add_axes([x0 + w + 0.01, y0, 0.03, h]) cbar_top = fig.colorbar(im_12, cax=cax_top) cbar_bot = fig.colorbar(im_22, cax=cax_bot) # Labels x0,y0,w,h = ax[0].get_position().bounds fig.text(x0+0.5*w, y0+h+0.01, r'$\mathrm{SFD}$', fontsize=16, ha='center', va='bottom') x0,y0,w,h = ax[1].get_position().bounds fig.text(x0+0.5*w, y0+h+0.01, r'$\mathrm{Priors}$', fontsize=16, ha='center', va='bottom') cbar_top.set_label(r'$\log_{10} \mathrm{E} \left( B - V \right)$', fontsize=14) cbar_bot.set_label(r'$\log_{10} \left[ \frac{ \mathrm{E} \left( B - V \right) }{ \left< \mathrm{E} \left( B - V \right) \right> } \right]$', fontsize=14) cbar_top.locator = ticker.MaxNLocator(nbins=6) cbar_top.update_ticks() cbar_bot.locator = ticker.MaxNLocator(nbins=6) cbar_bot.update_ticks() cbar_bot.solids.set_edgecolor('face') # Fix for matplotlib svg-rendering bug cbar_top.solids.set_edgecolor('face') # Fix for matplotlib svg-rendering bug fig.savefig(img_fname, dpi=dpi, bbox_inches='tight') plt.show()
def find_neighbors(nside, pix_idx, n_neighbors): ''' Find the neighbors of each pixel. Each pixel is defined by a HEALPix nside and pixel index (in nested order). Returns two arrays: neighbor_idx (n_pix, n_neighbors) Index of each neighbor in the nside and pix_idx arrays. neighbor_dist (n_pix, n_neighbors) Distance to each neighbor. ''' # Determine (l, b) and which downsampled pixel # each (nside, pix_idx) combo belongs to nside_rough = np.min(nside) if nside_rough != 1: nside_rough /= 2 l = np.empty(pix_idx.size, dtype='f8') b = np.empty(pix_idx.size, dtype='f8') pix_idx_rough = np.empty(pix_idx.size, dtype='i8') nside_unique = np.unique(nside) for n in nside_unique: idx = (nside == n) factor = (n / nside_rough)**2 pix_idx_rough[idx] = pix_idx[idx] / factor l[idx], b[idx] = hputils.pix2lb(n, pix_idx[idx], nest=True) # For each downsampled pixel, determine nearest neighbors of all subpixels neighbor_idx = -np.ones((pix_idx.size, n_neighbors), dtype='i8') neighbor_dist = np.inf * np.ones((pix_idx.size, n_neighbors), dtype='f8') for i_rough in np.unique(pix_idx_rough): rough_neighbors = hp.get_all_neighbours(nside_rough, i_rough, nest=True) idx_centers = np.argwhere(pix_idx_rough == i_rough)[:,0] tmp = [np.argwhere(pix_idx_rough == i)[:,0] for i in rough_neighbors] tmp.append(idx_centers) idx_search = np.hstack(tmp) dist = gc_dist(l[idx_centers], b[idx_centers], l[idx_search], b[idx_search]) tmp = np.argsort(dist, axis=1)[:, 1:n_neighbors+1] fill = idx_search[tmp] neighbor_idx[idx_centers, :fill.shape[1]] = fill fill = np.sort(dist, axis=1)[:, 1:n_neighbors+1] neighbor_dist[idx_centers, :fill.shape[1]] = fill return neighbor_idx, neighbor_dist