def load_output_file_raw(f, bounds=None, max_samples=None, load_stacked_pdfs=False): # Define output pix_idx = [] nside = [] cloud_mu = [] cloud_delta_EBV = [] cloud_lnp = [] cloud_GR = [] cloud_mask = [] los_EBV = [] los_lnp = [] los_GR = [] los_mask = [] star_stack = [] n_stars = [] DM_min, DM_max = None, None EBV_min, EBV_max = 0., 5. for name,item in f.iteritems(): # Load pixel position try: pix_idx_tmp = int(item.attrs['healpix_index'][0]) nside_tmp = int(item.attrs['nside'][0]) except: continue # Check if pixel is in bounds if bounds != None: l, b = hputils.pix2lb_scalar(nside_tmp, pix_idx_tmp, nest=True) if ( (l < bounds[0]) or (l > bounds[1]) or (b < bounds[2]) or (b > bounds[3]) ): continue # Pixel location pix_idx.append(pix_idx_tmp) nside.append(nside_tmp) # Cloud model if 'clouds' in item: samples_tmp, lnp_tmp, GR_tmp = unpack_dset(item['clouds'], max_samples=max_samples) n_clouds = samples_tmp.shape[2] / 2 cloud_mu.append(samples_tmp[:, :, :n_clouds]) cloud_delta_EBV.append(samples_tmp[:, :, n_clouds:]) cloud_lnp.append(lnp_tmp) cloud_GR.append(GR_tmp) del samples_tmp, lnp_tmp, GR_tmp cloud_mask.append(True) else: cloud_mask.append(False) # Piecewise-linear model if 'los' in item: samples_tmp, lnp_tmp, GR_tmp = unpack_dset(item['los'], max_samples=max_samples) DM_min = float(item['los'].attrs['DM_min']) DM_max = float(item['los'].attrs['DM_max']) los_EBV.append(samples_tmp) los_lnp.append(lnp_tmp) los_GR.append(GR_tmp) del samples_tmp, lnp_tmp, GR_tmp los_mask.append(True) else: los_mask.append(False) # Load stellar chains and create stacked image if load_stacked_pdfs: dset = item['stellar chains'] star_samples = dset[:, 1:, 1:3] conv = dset.attrs['converged'].astype(np.bool) lnZ = dset.attrs['ln(Z)'] n_stars.append(star_samples.shape[0]) idx = conv & (np.percentile(lnZ, 98.) - lnZ < 5.) star_samples = star_samples[idx] n_stars_tmp, n_star_samples, n_star_dim = star_samples.shape star_samples.shape = (n_stars_tmp * n_star_samples, n_star_dim) res = (501, 121) E_range = np.linspace(EBV_min, EBV_max, res[0]*2+1) DM_range = np.linspace(DM_min, DM_max, res[1]*2+1) stack_tmp, tmp1, tmp2 = np.histogram2d(star_samples[:,0], star_samples[:,1], bins=[E_range, DM_range]) stack_tmp = gaussian_filter(stack_tmp.astype('f8'), sigma=(4, 2), mode='reflect') stack_tmp = stack_tmp.reshape([res[0], 2, res[1], 2]).mean(3).mean(1) stack_tmp *= 100. / np.max(stack_tmp) stack_tmp = stack_tmp.astype('f2') stack_tmp.shape = (1, stack_tmp.shape[0], stack_tmp.shape[1]) star_stack.append(stack_tmp) else: n_stars.append(item['stellar chains'].shape[0]) # Concatenate output if len(pix_idx) == 0: return None # Pixel information pix_idx = np.array(pix_idx) nside = np.array(nside) cloud_mask = np.array(cloud_mask) los_mask = np.array(los_mask) n_stars = np.array(n_stars) pix_info = (pix_idx, nside, cloud_mask, los_mask, n_stars) # Cloud information cloud_info = None if np.sum(cloud_mask) > 0: cloud_mu = np.cumsum( np.concatenate(cloud_mu), axis=2) cloud_delta_EBV = np.exp( np.concatenate(cloud_delta_EBV) ) cloud_lnp = np.concatenate(cloud_lnp) cloud_GR = np.concatenate(cloud_GR) 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_EBV = np.cumsum(np.exp( np.concatenate(los_EBV) ), axis=2) los_lnp = np.concatenate(los_lnp) los_GR = np.concatenate(los_GR) los_info = (los_EBV, los_lnp, los_GR) # Stacked stellar surfaces if load_stacked_pdfs: star_stack = np.concatenate(star_stack) else: star_stack = None # 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 plot_pixel(self, map_idx): if map_idx == -1: return # Load and stretch stacked stellar pdfs star_stack = self.data.star_stack[0][map_idx].astype('f8') #dE = self.los_coll.star_E_range[1] - self.los_coll.star_E_range[0] #dDM = self.los_coll.star_DM_range[1] - self.los_coll.star_DM_range[0] #sigma = [0.015/dE, 0.15/dDM] #star_stack = gaussian_filter(star_stack, sigma, mode='mirror') star_stack /= np.max(star_stack) norm1 = 1. / np.power(np.max(star_stack, axis=0), 0.8) norm2 = 1. / np.power(np.sum(star_stack, axis=0), 0.8) norm = np.sqrt(norm1 * norm2) norm[np.isinf(norm)] = 0. star_stack = np.einsum('ij,j->ij', star_stack, norm) # Determine maximum E(B-V) w_y = np.mean(star_stack, axis=1) y_max = np.max(np.where(w_y > 1.e-2)[0]) EBV_stack_max = y_max * (5. / star_stack.shape[0]) # Load piecewise-linear profiles mu = self.data.get_los_DM_range() EBV_all = self.data.los_EBV[0][map_idx, :, :] nside = self.data.nside[0][map_idx] pix_idx = self.data.pix_idx[0][map_idx] l, b = hputils.pix2lb_scalar(nside, pix_idx, nest=True, use_negative_l=True) EBV_los_max = 1.5 * np.percentile(EBV_all[:, -1], 95.) EBV_max = min([EBV_los_max, EBV_stack_max]) # Load ln(p), if available lnp = None lnp_txt = None if self.data.los_lnp != []: lnp = self.data.los_lnp[0][map_idx, :] / self.data.n_stars[0][map_idx] GR = self.data.los_GR[0][map_idx, :] lnp_min, lnp_max = np.percentile(lnp[1:], [10., 90.]) GR_max = np.max(GR) lnp_txt = '$\ln \, p_{\mathrm{best}} = %.2f$\n' % lnp[0] lnp_txt += '$\ln \, p_{90\%%} = %.2f$\n' % lnp_max lnp_txt += '$\ln \, p_{10\%%} = %.2f$\n' % lnp_min lnp_txt += '$\mathrm{GR}_{\mathrm{max}} = %.3f$' % GR_max lnp = (lnp - lnp_min) / (lnp_max - lnp_min) lnp[lnp > 1.] = 1. lnp[lnp < 0.] = 0. else: lnp = [0. for EBV in EBV_all] # Set up figure fig = plt.figure(figsize=(8,5), dpi=150) ax = fig.add_subplot(1,1,1) fig.subplots_adjust(left=0.12, bottom=0.12, top=0.85) # Plot stacked stellar pdfs bounds = self.data.DM_EBV_lim ax.imshow(np.sqrt(star_stack), extent=bounds, origin='lower', aspect='auto', cmap='Blues', interpolation='nearest') # Plot samples alpha = 1. / np.power(EBV_all.shape[0], 0.55) for i,EBV in enumerate(EBV_all[1:]): c = (1.-lnp[i+1], 0., lnp[i+1]) ax.plot(mu, EBV, c=c, alpha=alpha) # Plot best fit ax.plot(mu, EBV_all[0, :], 'g', lw=2, alpha=0.5) ax.set_xlim(mu[0], mu[-1]) ax.set_ylim(0., EBV_max) # Add labels ax.set_xlabel(r'$\mu$', fontsize=16) ax.set_ylabel(r'$\mathrm{E} \left( B - V \right)$', fontsize=16) title_txt = '$\mathrm{nside} = %d, \ \mathrm{i} = %d$\n' % (nside, pix_idx) title_txt += '$\ell = %.2f, \ b = %.2f$' % (l, b) ax.set_title(title_txt, fontsize=16, multialignment='center') if lnp_txt != None: ylim = ax.get_ylim() y_txt = ylim[0] + 0.95 * (ylim[1] - ylim[0]) x_txt = mu[0] + 0.05 * (mu[-1] - mu[0]) ax.text(x_txt, y_txt, lnp_txt, fontsize=16, multialignment='left', va='top') plt.show()