def test_z_from_k3pk(self): ref_z = f212z(119.7e6) out_z, _, _, _ = load_k3pk(test_data_file, verbose=False) self.assertEqual(ref_z, out_z, msg='Expected z {0} but ' \ 'retruned z {1}'.format(ref_z,out_z) )
def test_k_from_k3pk(self): ref_k_par = .2 ref_z = f212z(119.7e6) ref_umag = 30 / (3e8 / (119.7 * 1e6)) ref_k_perp = ref_umag * pspec.dk_du(ref_z) ref_k_mag = np.sqrt(ref_k_par**2 + ref_k_perp**2) _, out_k_mag, _, _ = load_k3pk(test_data_file, verbose=False) self.assertEqual(ref_k_mag, out_k_mag, msg='Expected k_mag {0} but ' \ 'retruned k_mag {1}'.format(ref_k_mag,out_k_mag) )
def get_k3pk_from_npz(files=None, verbose=False): """ Load output from plot_pk_k3pk.npz and returns Delta^2 spectrum. Return lists of k, Delta^2, Delta^2_err ordered by decreasing redshift Return format: z, k_magnitude, Delta^2, Delta^2_err """ if files is None: # check that files are passed print 'No Files given for loading' return [], [], [], [] if len(n.shape(files)) == 0: files = [files] freqs = [] if verbose: print "parsing npz file frequencies" for filename in files: if verbose: print filename, try: if verbose: print "npz..", freqs.append(n.load(filename)['freq'] * 1e3) # load freq in MHz if verbose: print "[Success]" except(KeyError): if verbose: print "[FAIL]" try: if verbose: print "looking for path like RUNNAME/chan_chan/I/pspec.npz" dchan = (int(filename.split('/')[1].split('_')[1]) - int(filename.split('/')[1].split('_')[0])) chan = int(filename.split('/')[1].split('_')[0]) + dchan / 2 freqs.append(chan / 2. + 100) # a pretty good apprximation of chan 2 freq for 500kHz channels except(IndexError): if verbose: print "[FAIL] no freq found. Skipping..." if len(freqs) == 0: # check if any files were loaded correctly print 'No parsable frequencies found' print 'Exiting' return [], [], [], [] if verbose: print "sorting input files by frequency" files = n.array(files) files = files[n.argsort(freqs)] freqs = n.sort(freqs) if verbose: print "found freqs" freqs = n.array(freqs) if verbose: print freqs z = f212z(freqs * 1e6) if verbose: print "processing redshifts:", z k3Pk = [] k3err = [] kmags = [] for i, FILE in enumerate(files): F = n.load(FILE) if verbose: print FILE.split('/')[-1], z[i] k3Pk.append(F['k3pk']) k3err.append(F['k3err']) kmags.append(F['k']) return z, kmags, k3Pk, k3err
def get_k3pk_from_npz(files=None, verbose=False): """ Load output from plot_pk_k3pk.npz and returns Delta^2 spectrum. Return lists of k, Delta^2, Delta^2_err ordered by decreasing redshift Return format: z, k_magnitude, Delta^2, Delta^2_err """ if files is None: # check that files are passed print 'No Files given for loading' return [], [], [], [] if len(n.shape(files)) == 0: files = [files] freqs = [] if verbose: print "parsing npz file frequencies" for filename in files: if verbose: print filename, try: if verbose: print "npz..", freqs.append(n.load(filename)['freq'] * 1e3) # load freq in MHz if verbose: print "[Success]" except (KeyError): if verbose: print "[FAIL]" try: if verbose: print "looking for path like RUNNAME/chan_chan/I/pspec.npz" dchan = (int(filename.split('/')[1].split('_')[1]) - int(filename.split('/')[1].split('_')[0])) chan = int(filename.split('/')[1].split('_')[0]) + dchan / 2 freqs.append(chan / 2. + 100) # a pretty good apprximation of chan 2 freq for 500kHz channels except (IndexError): if verbose: print "[FAIL] no freq found. Skipping..." if len(freqs) == 0: # check if any files were loaded correctly print 'No parsable frequencies found' print 'Exiting' return [], [], [], [] if verbose: print "sorting input files by frequency" files = n.array(files) files = files[n.argsort(freqs)] freqs = n.sort(freqs) if verbose: print "found freqs" freqs = n.array(freqs) if verbose: print freqs z = f212z(freqs * 1e6) if verbose: print "processing redshifts:", z k3Pk = [] k3err = [] kmags = [] for i, FILE in enumerate(files): F = n.load(FILE) if verbose: print FILE.split('/')[-1], z[i] k3Pk.append(F['k3pk']) k3err.append(F['k3err']) kmags.append(F['k']) return z, kmags, k3Pk, k3err
help='Plot analytical noise approximation') parser.add_argument('--noisefiles', type=str, nargs='*', help='supply 21cmSense files to plot sensitivity') parser.add_argument('--outfile', type=str, default='pspec_k3pk', help='give custom output file name') parser.add_argument('--noC', action='store_true', help='do not plot C-weighted points') parser.add_argument('--noI', action='store_true', help='do not plot I-weighted points') args = parser.parse_args() assert args.noC != args.noI, 'gotta plot something.' zs = [] for filename in args.files: F = np.load(filename) freq = F['freq'] zs.append(f212z(freq * 1e9)) # reverse order to make high redshift of left of plot zs = np.unique(zs)[::-1] Nzs = len(zs) # define some color format sets for the plotter # colors = ['r', 'g', 'k', 'b'] markers = ['o', ',', 'd', '^', 's', 'v'] # markers = itertools.cycle(markers) marker_count = [0 for i in xrange(Nzs)] figsize = (5 * (1 + Nzs)/2., 6) # Create figure and prep subplot sizes for Delta^2 fig = plt.figure(figsize=figsize) gs = gridspec.GridSpec(1, Nzs) gs.update(hspace=0.0, wspace=0.2)
def get_k3pk_from_npz(files=None, verbose=False): ''' Loads output from plot_pk_k3pk.npz and returns Delta^2 spectrum returns lists of k, Delta^2, Delta^2_err ordered by decreasing redshift return format: z, k_magnitude, Delta^2, Delta^2_err ''' if files is None: #check that files are passed print 'No Files gives for loading' return 0,'','','' one_file_flag=False if len(n.shape(files)) ==0: files = [files]; if len(files) == 1: one_file_flag=True freqs = [] if verbose: print "parsing npz file frequencies" for filename in files: if verbose: print filename, try: if verbose: print "npz..", freqs.append(n.load(filename)['freq']*1e3) #load freq in MHz if verbose: print "[Success]" except(KeyError): if verbose: print "[FAIL]" try: if verbose: print "looking for path like RUNNAME/chan_chan/I/pspec.npz" dchan = int(filename.split('/')[1].split('_')[1])-int(filename.split('/')[1].split('_')[0]) chan = int(filename.split('/')[1].split('_')[0]) + dchan/2 freqs.append(chan/2. + 100) #a pretty good apprximation of chan 2 freq for 500kHz channels except(IndexError): if verbose: print "[FAIL] no freq found. Skipping..." if len(freqs) ==0: #check if any files were loaded correctly print 'No parsable frequencies found' print 'Exiting' return 0,'','','' if verbose: print "sorting input files" files = n.array(files) files = files[n.argsort(freqs)] freqs = n.sort(freqs) if verbose: print "found freqs" freqs = n.array(freqs) if verbose: print freqs z = f212z(freqs*1e6) if verbose: print "processing redshifts:",z #redshift_files = dict(zip(z,files)) umags = 30/(c/(freqs*1e6)) # if verbose: print "umags = ",umags kperps = umags*pspec.dk_du(z) k3Pk = [] k3err = [] kmags = [] for i,FILE in enumerate(files): F = n.load(FILE) if verbose: print FILE.split('/')[-1],z[i] # k = n.sqrt(F['kpl']**2 + kperps[i]**2) k3Pk.append(F['k3pk']) k3err.append(F['k3err']) kmags.append(F['k']) # if one_file_flag: # z = n.squeeze(z) # kmags = n.squeeze(kmags) # k3Pk = n.squeeze(k3Pk) # k3err = n.squeeze(k3err) return z, kmags, k3Pk, k3err
def get_pk_from_npz(files=None, verbose=False): ''' Loads output from plot_pk_k3pk.npz and returns P(k) spectrum returns lists of k, Pk, Pk_err, Delta^2 ordered by decreasing redshift return format: z, k_parallel, Pk, Pk_err ''' if files is None: print 'No Files gives for loading' return 0,'','','' one_file_flag=False if len(n.shape(files)) ==0: files = [files]; if len(files) == 1: one_file_flag=True freqs = [] if verbose: print "parsing npz file frequencies" for filename in files: if verbose: print filename, try: if verbose: print "npz..", freqs.append(n.load(filename)['freq']*1e3) #load freq in MHz if verbose: print "[Success]" except(KeyError): if verbose: print "[FAIL]" try: if verbose: print "looking for path like RUNNAME/chan_chan/I/pspec.npz" dchan = int(filename.split('/')[1].split('_')[1])-int(filename.split('/')[1].split('_')[0]) chan = int(filename.split('/')[1].split('_')[0]) + dchan/2 freqs.append(chan/2. + 100) #a pretty good apprximation of chan 2 freq for 500kHz channels except(IndexError): if verbose: print "[FAIL] no freq found. Skipping..." if len(freqs) ==0: #check if any files were loaded correctly print 'No parsable frequencies found' print 'Exiting' return 0,'','','' if verbose: print "sorting input files" files = n.array(files) files = files[n.argsort(freqs)] freqs = n.sort(freqs) if verbose: print "found freqs" freqs = n.array(freqs) if verbose: print freqs z = f212z(freqs*1e6) if verbose: print "processing redshifts:",z kpars = [] Pks = [] Pkerr =[] for i,FILE in enumerate(files): F = n.load(FILE) if verbose: print FILE.split('/')[-1],z[i] Pks.append(F['pk']) kpars.append(F['kpl']) Pkerr.append(F['err']) # if one_file_flag: # z = n.squeeze(z) # kpars = n.squeeze(kpars) # Pks = n.squeeze(Pks) # Pkerr = n.squeeze(Pkerr) return z, kpars, Pks, Pkerr