def make_h5(self, outname=None, save_noise_realizations=False): if outname is None: tools.ensure_dir_exists('spectra') outname = 'spectra/xs' + self.maps[ 0].save_string + '_' + self.maps[1].map_string + '.h5' f1 = h5py.File(outname, 'w') try: f1.create_dataset('mappath1', data=self.maps[0].mappath) f1.create_dataset('mappath2', data=self.maps[1].mappath) f1.create_dataset('xs', data=self.xs) f1.create_dataset('k', data=self.k) f1.create_dataset('k_bin_edges', data=self.k_bin_edges) f1.create_dataset('nmodes', data=self.nmodes) except: print('No power spectrum calculated.') return try: f1.create_dataset('rms_xs_mean', data=self.rms_xs_mean) f1.create_dataset('rms_xs_std', data=self.rms_xs_std) except: pass if save_noise_realizations: try: f1.create_dataset('rms_xs', data=self.rms_xs) except: pass f1.close()
def make_h5(self, outname=None): if outname is None: folder = '/mn/stornext/d16/cmbco/comap/protodir/spectra/' tools.ensure_dir_exists(folder) outname = folder + 'ps' + self.map.save_string + '.h5' f1 = h5py.File(outname, 'w') try: f1.create_dataset('mappath', data=self.map.mappath) f1.create_dataset('ps', data=self.ps) f1.create_dataset('k', data=self.k) f1.create_dataset('k_bin_edges', data=self.k_bin_edges) f1.create_dataset('nmodes', data=self.nmodes) except: print('No power spectrum calculated.') return try: f1.create_dataset('ps_2d', data=self.ps_2d) except: pass try: f1.create_dataset('rms_ps_mean', data=self.rms_ps_mean) f1.create_dataset('rms_ps_std', data=self.rms_ps_std) except: pass f1.close()
def make_h5_2d(self, outname=None): for index in range(self.how_many_combinations): i = index * 2 j = i + 1 if outname is None: tools.ensure_dir_exists('spectra_2D') outname = 'spectra_2D/xs_2D_' + self.get_information()[index][ 1] + '_and_' + self.get_information()[index][2] + '.h5' f1 = h5py.File(outname, 'w') try: f1.create_dataset('mappath1', data=self.maps[i].mappath) f1.create_dataset('mappath2', data=self.maps[j].mappath) f1.create_dataset('xs_2D', data=self.xs[index]) f1.create_dataset('k', data=self.k[index]) f1.create_dataset('k_bin_edges_perp', data=self.k_bin_edges_perp) f1.create_dataset('k_bin_edges_par', data=self.k_bin_edges_par) f1.create_dataset('nmodes', data=self.nmodes[index]) except: print('No cross-spectrum calculated.') return try: f1.create_dataset('rms_xs_mean_2D', data=self.rms_xs_mean_2D[index]) f1.create_dataset('rms_xs_std_2D', data=self.rms_xs_std_2D[index]) except: pass f1.close()
def plot_xs(self, k_array, xs_array, rms_sig_array, rms_mean_array, index, save=False): k = k_array[index] xs = xs_array[index] rms_sig = rms_sig_array[index] rms_mean = rms_mean_array[index] #lim = 200. fig = plt.figure() fig.suptitle('xs of ' + self.get_information()[index][1] + ' and ' + self.get_information()[index][2]) ax1 = fig.add_subplot(211) ax1.errorbar(k, k * xs, k * rms_sig, fmt='o', label=r'$k\tilde{C}_{data}(k)$') #added k* ax1.plot(k, 0 * rms_mean, 'k', label=r'$\tilde{C}_{noise}(k)$', alpha=0.4) ax1.plot(k, k * PS_function.PS_f(k), label='k*PS of the input signal') ax1.set_ylabel(r'$\tilde{C}(k)$ [$\mu$K${}^2$ Mpc${}^3$]') lim = np.mean(np.abs(xs[4:])) * 4 if not np.isnan(lim): ax1.set_ylim(-lim, lim) # ax1.set_ylim(0, 0.1) ax1.set_xscale('log') ax1.grid() plt.legend() ax2 = fig.add_subplot(212) ax2.errorbar(k, xs / rms_sig, rms_sig / rms_sig, fmt='o', label=r'$\tilde{C}_{data}(k)$') ax2.plot(k, 0 * rms_mean, 'k', alpha=0.4) ax2.set_ylabel(r'$\tilde{C}(k) / \sigma_\tilde{C}$') ax2.set_xlabel(r'$k$ [Mpc${}^{-1}$]') ax2.set_ylim(-12, 12) ax2.set_xscale('log') ax2.grid() plt.legend() if save == True: tools.ensure_dir_exists('figures') name_for_figure = 'figures/xs_' + self.get_information( )[index][1] + '_and_' + self.get_information()[index][2] + '.png' plt.savefig(name_for_figure, bbox_inches='tight') print('Figure saved as', name_for_figure)
def make_h5(self, index=0, outname=None): #index = -1 for i in range(len(self.maps)): for j in range( i, len(self.maps) ): #ensure that we don't take the same pairs of maps twice if i != j: #only for xs, not auto spectra #index += 1 #find a correct xs, etc. if outname is None: tools.ensure_dir_exists('spectra') outname = 'spectra/xs_' + self.get_information( )[index][1] + '_and_' + self.get_information( )[index][2] + '.h5' f1 = h5py.File(outname, 'w') try: f1.create_dataset('mappath1', data=self.maps[i].mappath) f1.create_dataset('mappath2', data=self.maps[j].mappath) f1.create_dataset('xs', data=self.xs[index]) f1.create_dataset('k', data=self.k[index]) f1.create_dataset('k_bin_edges', data=self.k_bin_edges) f1.create_dataset('nmodes', data=self.nmodes[index]) except: print('No power spectrum calculated.') return try: f1.create_dataset('rms_xs_mean', data=self.rms_xs_mean[index]) f1.create_dataset('rms_xs_std', data=self.rms_xs_std[index]) except: pass f1.close()
def xs_feed_feed_grid(map_file): went_through_first_cut = 0 went_through_sigma_cut = 0 #n_sim = 100 n_k = 14 n_feed = 19 n_sum = 0 xs_sum = np.zeros(n_k) #rms_xs_sum = np.zeros((n_k, n_sim)) xs_div = np.zeros(n_k) map_file = 'split_maps/' + map_file name_of_map = map_file.split('/')[ -1] #get rid of the path, leave only the name of the map name_of_map = name_of_map.split('.')[0] #get rid of the ".h5" part name_of_map_list = name_of_map.split('_') #co6_map_snup_elev_0_cesc_0' field = name_of_map_list[0] ff_jk = name_of_map_list[2] split_names = [] split_numbers = [] for m in range(3, len(name_of_map_list) - 1, 2): split_names.append(name_of_map_list[m]) for n in range(4, len(name_of_map_list), 2): split_numbers.append(name_of_map_list[n]) n_of_splits = read_number_of_splits(map_file, ff_jk) n_list = list(range(n_of_splits)) all_different_possibilities = list(itr.combinations( n_list, 2)) #for n_of_splits = 3, it gives [(0, 1), (0, 2), (1, 2)] how_many_combinations = len(all_different_possibilities) for u in range( how_many_combinations): #go through all the split combinations current_combo = all_different_possibilities[u] split1 = str(current_combo[0]) split2 = str(current_combo[1]) path_to_xs = 'spectra/xs_' + name_of_map + '_split' + split1 + '_feed%01i_and_' + name_of_map + '_split' + split2 + '_feed%01i.h5' xs = np.zeros((n_feed, n_feed, n_k)) rms_xs_std = np.zeros_like(xs) chi2 = np.zeros((n_feed, n_feed)) k = np.zeros(n_k) noise = np.zeros_like(chi2) for i in range(n_feed): #go through all the feed combinations for j in range(n_feed): #if i != 7 and j != 7: try: filepath = path_to_xs % (i + 1, j + 1) with h5py.File(filepath, mode="r") as my_file: #print ("finds file", i, j) xs[i, j] = np.array(my_file['xs'][:]) #print (xs[i,j]) rms_xs_std[i, j] = np.array(my_file['rms_xs_std'][:]) #print (rms_xs_std[i,j]) k[:] = np.array(my_file['k'][:]) except: xs[i, j] = np.nan rms_xs_std[i, j] = np.nan w = np.sum(1 / rms_xs_std[i, j]) noise[i, j] = 1 / np.sqrt(w) chi3 = np.sum( (xs[i, j] / rms_xs_std[i, j])**3 ) #we need chi3 to take the sign into account - positive or negative correlation chi2[i, j] = np.sign(chi3) * abs( (np.sum((xs[i, j] / rms_xs_std[i, j])**2) - n_k) / np.sqrt(2 * n_k)) #magnitude (how far from white noise) #if abs(chi2[i,j]) < 5. and not np.isnan(chi2[i,j]) and i != j: #if excess power is smaller than 5 sigma, chi2 is not nan, not on diagonal if not np.isnan(chi2[i, j]) and i != j: went_through_first_cut += 1 if abs(chi2[i, j]) < 5.: went_through_sigma_cut += 1 xs_sum += xs[i, j] / rms_xs_std[i, j]**2 #print ("if test worked") xs_div += 1 / rms_xs_std[i, j]**2 n_sum += 1 tools.ensure_dir_exists('chi2_grids') figure_name = 'chi2_grids/xs_grid_' + name_of_map + '_splits' + split1 + split2 + '.pdf' plt.figure() vmax = 15 plt.imshow(chi2, interpolation='none', vmin=-vmax, vmax=vmax, extent=(0.5, n_feed + 0.5, n_feed + 0.5, 0.5)) new_tick_locations = np.array(range(n_feed)) + 1 plt.xticks(new_tick_locations) plt.yticks(new_tick_locations) plt.xlabel('Feed of ' + ff_jk + ' ' + split1 + '-split') plt.ylabel('Feed of ' + ff_jk + ' ' + split2 + '-split') cbar = plt.colorbar() cbar.set_label(r'$|\chi^2| \times$ sign($\chi^3$)') plt.savefig(figure_name, bbox_inches='tight') #plt.show() #print ("xs_div:", xs_div) return k, xs_sum / xs_div, 1. / np.sqrt( xs_div), field, ff_jk, split_names, split_numbers
def xs_2D_plot(figure_name, k, k_bin_edges_par, k_bin_edges_perp, xs_mean, xs_sigma, titlename): #k,k_bin_edges_par, k_bin_edges_perp, xs_mean, xs_sigma = k[3:],k_bin_edges_par[3:], k_bin_edges_perp[3:], xs_mean[3:], xs_sigma[3:] fig, ax = plt.subplots(1, 3, figsize=(16, 5.6)) fig.tight_layout() fig.suptitle(titlename, fontsize=16) norm = mpl.colors.Normalize(vmin=1.3 * np.amin(xs_mean), vmax=-1.3 * np.amin(xs_mean)) img1 = ax[0].imshow(xs_mean, interpolation='none', origin='lower', extent=[0, 1, 0, 1], cmap='RdBu', norm=norm) fig.colorbar(img1, ax=ax[0], fraction=0.046, pad=0.04) img2 = ax[1].imshow(xs_mean / transfer_filt_2D(k[0], k[1]), interpolation='none', origin='lower', extent=[0, 1, 0, 1], cmap='RdBu', norm=norm) fig.colorbar(img2, ax=ax[1], fraction=0.046, pad=0.04) img3 = ax[2].imshow( xs_mean / (transfer_filt_2D(k[0], k[1]) * transfer_sim_2D(k[0], k[1])), interpolation='none', origin='lower', extent=[0, 1, 0, 1], cmap='RdBu', norm=norm) fig.colorbar(img2, ax=ax[2], fraction=0.046, pad=0.04).set_label( r'$\tilde{C}\left(k_{\bot},k_{\parallel}\right)$ [$\mu$K${}^2$ (Mpc)${}^3$]', size=16) ticks = [ 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1., 1.1, 1.2, 1.3 ] majorticks = [0.03, 0.1, 0.3, 1] majorlabels = ['0.03', '0.1', '0.3', '1'] xbins = k_bin_edges_par ticklist_x = log2lin(ticks[:-3], xbins) majorlist_x = log2lin(majorticks, xbins) ybins = k_bin_edges_perp ticklist_y = log2lin(ticks, ybins) majorlist_y = log2lin(majorticks, ybins) ax[0].set_title(r'$\tilde{C}^{\mathrm{FPXS}}$ ', fontsize=16) ax[1].set_title(r'$\tilde{C}^{\mathrm{FPXS}}/T^{pipeline}$', fontsize=16) ax[2].set_title( r'$\tilde{C}^{\mathrm{FPXS}}/\left(T^{pipeline}T^{beam}\right)$ ', fontsize=16) for i in range(3): ax[i].set_xticks(ticklist_x, minor=True) ax[i].set_xticks(majorlist_x, minor=False) ax[i].set_xticklabels(majorlabels, minor=False, fontsize=16) ax[i].set_yticks(ticklist_y, minor=True) ax[i].set_yticks(majorlist_y, minor=False) ax[i].set_yticklabels(majorlabels, minor=False, fontsize=16) ax[0].set_xlabel(r'$k_{\parallel}$ [Mpc${}^{-1}$]', fontsize=16) ax[0].set_ylabel(r'$k_{\bot}$ [Mpc${}^{-1}$]', fontsize=16) ax[1].set_xlabel(r'$k_{\parallel}$ [Mpc${}^{-1}$]', fontsize=16) ax[2].set_xlabel(r'$k_{\parallel}$ [Mpc${}^{-1}$]', fontsize=16) tools.ensure_dir_exists('xs_2D_mean_figures') plt.tight_layout() plt.savefig('xs_2D_mean_figures/' + figure_name)
def xs_with_model(figure_name, k, xs_mean, xs_sigma, titlename, scan_strategy): if scan_strategy == 'ces': plotcolor = 'indianred' if scan_strategy == 'liss': plotcolor = 'teal' lim = np.mean(np.abs(xs_mean[4:-2] * k[4:-2])) * 8 fig = plt.figure() fig.tight_layout() #fig.set_figwidth(8) ax1 = fig.add_subplot(211) ax1.errorbar(k, k * xs_mean / (transfer(k) * transfer_filt(k)), k * xs_sigma / (transfer(k) * transfer_filt(k)), fmt='o', color=plotcolor) #ax1.errorbar(k, k * xs_mean, k * xs_sigma, fmt='o', label=r'$k\tilde{C}_{data}(k)$') ax1.plot(k, 0 * xs_mean, 'k', alpha=0.4) #ax1.plot(k, k*PS_function.PS_f(k)/ transfer(k), label='k*PS of the input signal') #for simulated map #ax1.plot(k, k*PS_function.PS_f(k), label='k*PS of the input signal') #ax1.plot(k_th, k_th * ps_th_nobeam * 10, '--', label=r'$10 \times kP_{Theory}(k)$', color='dodgerblue') #ax1.plot(k_th, k_th * ps_copps_nobeam * 5, 'g--', label=r'$5 \times kP_{COPPS}$ (shot)') ax1.set_ylabel(r'$k\tilde{C}(k)$ [$\mu$K${}^2$ Mpc${}^2$]', fontsize=14) if not np.isnan(lim): if scan_strategy == 'ces': ax1.set_ylim(-lim * 3, lim * 3) # ax1.set_ylim(0, 0.1) if scan_strategy == 'liss': ax1.set_ylim(-lim * 2, lim * 2) # ax1.set_ylim(0, 0.1) ax1.set_xlim(0.04, 1.) ax1.set_xscale('log') ax1.set_title(titlename) ax1.grid() #ax1.set_xlabel(r'$k$ [Mpc${}^{-1}$]', fontsize=14) labnums = [0.05, 0.1, 0.2, 0.5, 1.] ax1.set_xticks(labnums) ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) #plt.legend(bbox_to_anchor=(0, 0.61)) #ax1.legend(ncol=3) ax2 = fig.add_subplot(212) #ax2.plot(k, diff_mean / error, fmt='o', label=r'$\tilde{C}_{diff}(k)$', color='black') ax2.errorbar(k, xs_mean / xs_sigma, xs_sigma / xs_sigma, fmt='o', color=plotcolor) #ax2.errorbar(k, sum_mean / error, error /error, fmt='o', label=r'$\tilde{C}_{sum}(k)$', color='mediumorchid') ax2.plot(k, 0 * xs_mean, 'k', alpha=0.4) #ax2.set_ylabel(r'$\tilde{C}(k) / \sigma_\tilde{C}$') ax2.set_ylabel(r'$\tilde{C}(k) / \sigma_\tilde{C}$', fontsize=14) ax2.set_xlabel(r'$k$ [Mpc${}^{-1}$]', fontsize=14) ax2.set_ylim(-5, 5) ax2.set_xlim(0.04, 1.) ax2.set_xscale('log') ax2.grid() #ax2.legend(ncol=3) ax2.set_xticks(labnums) ax2.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.tight_layout() #plt.legend() tools.ensure_dir_exists('xs_mean_figures') plt.savefig('xs_mean_figures/' + figure_name, bbox_inches='tight') plt.close(fig)
def read_map(mappath, field, control_variables, test_variables, feed_feed_variables, all_variables, feed_and_test, feed_and_control): print( 'STAGE 2/4: Splitting the map into subsets with different split combinations.' ) input_map = h5py.File(mappath, 'r') x = np.array(input_map['x'][:]) #common part for all maps y = np.array(input_map['y'][:]) #common part for all maps multisplits = input_map['multisplits'] maps_created = [] if len( feed_and_test ) != 0: #if some test variables are simultaneously feed-feed variables for test_variable in feed_and_test: map_split = np.array(multisplits['map_' + test_variable][:]) rms_split = np.array(multisplits['rms_' + test_variable][:]) shp = map_split.shape how_many_twos = len( control_variables ) + 1 #how many parts to reshape the map with respect to splits new_shape = [] for i in range(how_many_twos): new_shape.append( 2 ) #because we split in 2 - needs to be changed if more splits are implemented new_shape.append(shp[1]) #feed new_shape.append(shp[2]) #sideband new_shape.append(shp[3]) #freq new_shape.append(shp[4]) #x new_shape.append(shp[5]) #y map_split = map_split.reshape(new_shape) rms_split = rms_split.reshape(new_shape) split_names = [ ] #collect the names of the spits in the correct order for the new shape split_names.append(test_variable) for i in range(how_many_twos - 1): split_names.append(control_variables[-1 - i]) how_many_to_combine = len(split_names) - 1 #all control variables all_different_possibilities = list( itr.product(range(2), repeat=how_many_to_combine) ) #find all the combinations of 'how_many_to_combine' 0s and 1s index_of_ff_variable = 0 all_axes_to_combine = list(range(0, how_many_to_combine + 1)) all_axes_to_combine.remove( index_of_ff_variable ) #all axes for different combinations of splits, include both splits for the feed-feed variable slc = [slice(None)] * len(new_shape) #includes all elements for i in range(len(all_different_possibilities) ): #this many maps will be created for_naming = [ ] #identify which combination of splits the current map is using for j in range(how_many_to_combine): axis_index = all_axes_to_combine[j] slc[axis_index] = all_different_possibilities[i][ j] #choose 0 or 1 for this split for_naming.append(split_names[axis_index]) for_naming.append(all_different_possibilities[i][j]) my_map = map_split[tuple( slc)] #slice the map for the current combination of splits my_rms = rms_split[tuple( slc )] #slice the rms-map for the current combination of splits name = field + '_' + 'map' + '_' + test_variable for k in range(len(for_naming)): name += '_' name += str(for_naming[k]) name += '.h5' maps_created.append( name) #add the name of the current map to the list print('Creating HDF5 file for the map ' + name + '.') tools.ensure_dir_exists('split_maps') outname = 'split_maps/' + name f = h5py.File(outname, 'w') #create HDF5 file with the sliced map f.create_dataset('x', data=x) f.create_dataset('y', data=y) f.create_dataset('/jackknives/map_' + test_variable, data=my_map) f.create_dataset('/jackknives/rms_' + test_variable, data=my_rms) f.close() if len(feed_and_control ) != 0: #if some feed-feed variables are control variables for ff_variable in feed_and_control: for test_variable in test_variables: map_split = np.array(multisplits['map_' + test_variable][:]) rms_split = np.array(multisplits['rms_' + test_variable][:]) shp = map_split.shape how_many_twos = len(all_variables) - len( test_variables ) + 1 #how to reshape the map with respect to splits new_shape = [] for i in range(how_many_twos): new_shape.append(2) new_shape.append(shp[1]) #feed new_shape.append(shp[2]) #sideband new_shape.append(shp[3]) #freq new_shape.append(shp[4]) #x new_shape.append(shp[5]) #y map_split = map_split.reshape(new_shape) rms_split = rms_split.reshape(new_shape) split_names = [ ] #collect the names of the spits in the correct order for the new shape split_names.append(test_variable) for i in range(how_many_twos - 1): split_names.append(all_variables[-len(test_variables) - 1 - i]) how_many_to_combine = len( split_names ) - 1 #test variable + all control variables, except for the ff_variable all_different_possibilities = list( itr.product(range(2), repeat=how_many_to_combine) ) #find all the combinations of 'how_many_to_combine' 0s and 1s index_of_ff_variable = split_names.index(ff_variable) all_axes_to_combine = list(range(0, how_many_to_combine + 1)) all_axes_to_combine.remove( index_of_ff_variable ) #all axes for different combinations of splits, include both splits for the feed-feed variable slc = [slice(None)] * len(new_shape) #includes all elements for i in range(len(all_different_possibilities) ): #this many maps will be created for_naming = [ ] #identify which combination of splits the current map is using for j in range(how_many_to_combine): axis_index = all_axes_to_combine[j] slc[axis_index] = all_different_possibilities[i][ j] #choose 0 or 1 for this split for_naming.append(split_names[axis_index]) for_naming.append(all_different_possibilities[i][j]) my_map = map_split[tuple( slc )] #slice the map for the current combination of splits my_rms = rms_split[tuple( slc )] #slice the rms-map for the current combination of splits name = field + '_' + 'map' + '_' + ff_variable for k in range(len(for_naming)): name += '_' name += str(for_naming[k]) name += '.h5' maps_created.append( name) #add the name of the current map to the list print('Creating HDF5 file for the map ' + name + '.') tools.ensure_dir_exists('split_maps') outname = 'split_maps/' + name f = h5py.File(outname, 'w') #create HDF5 file with the sliced map f.create_dataset('x', data=x) f.create_dataset('y', data=y) f.create_dataset('/jackknives/map_' + ff_variable, data=my_map) f.create_dataset('/jackknives/rms_' + ff_variable, data=my_rms) f.close() return maps_created