def get_spectrum_measurement( self, angle_vals, kernels, output_name, angle_units='arcmin', windows="SAMPLE", angle_lims=None): # The fits format stores all the measurements # as one long vector. So we build that up here from the various # bins that we will load in. These are the different columns bin1 = [] bin2 = [] value = [] angular_bin = [] angle = [] if angle_lims is not None: angle_min = [] angle_max = [] else: angle_min, angle_max = None, None n_angle_sample = len(angle_vals) #If real-space, get angle_vals in radians for interpolation if angle_units is not None: angle_vals_with_units = angle_vals * twopoint.ANGULAR_UNITS[angle_units] angle_vals_interp = (angle_vals_with_units.to(twopoint.ANGULAR_UNITS["rad"])).value else: angle_vals_interp = angle_vals # Bin pairs. Varies depending on auto-correlation for (i,j) in self.bin_pairs: spec_interp = self.spec_interps[(i,j)] # Convert arcmin to radians for the interpolation spec_sample = spec_interp( angle_vals_interp ) #cl_sample = interp1d(theory_angle, cl)(angle_sample_radians) # Build up on the various vectors that we need bin1.append(np.repeat(i, n_angle_sample)) bin2.append(np.repeat(j, n_angle_sample)) value.append(spec_sample) angular_bin.append(np.arange(n_angle_sample)) angle.append(angle_vals) if angle_lims is not None: angle_min.append( angle_lims[:-1] ) angle_max.append( angle_lims[1:] ) # Convert all the lists of vectors into long single vectors bin1 = np.concatenate(bin1) bin2 = np.concatenate(bin2) angular_bin = np.concatenate(angular_bin) value = np.concatenate(value) angle = np.concatenate(angle) if angle_lims is not None: angle_min = np.concatenate(angle_min) angle_max = np.concatenate(angle_max) bins = (bin1, bin2) # Build the output object type reqired. s = twopoint.SpectrumMeasurement(output_name, bins, self.types, kernels, windows, angular_bin, value, angle=angle, angle_unit=angle_units, angle_min=angle_min, angle_max=angle_max ) return s
def makeSpectrum(self, name, types, angle_unit, kernels=(None, None)): self.tIL1 = np.concatenate(self.tIL1) self.tIL2 = np.concatenate(self.tIL2) self.aIL = np.concatenate(self.aIL) self.angList = np.concatenate(self.angList) self.valList = np.concatenate(self.valList) spec = twopoint.SpectrumMeasurement(name, (self.tIL1, self.tIL2), types, kernels, 'SAMPLE', self.aIL, self.valList, angle=self.angList, angle_unit=angle_unit) return spec
def init_specs(self): dtype1 = [ twopoint.Types.galaxy_shear_plus_real, twopoint.Types.galaxy_shear_minus_real, twopoint.Types.galaxy_position_real, twopoint.Types.galaxy_position_real ] dtype2 = [ twopoint.Types.galaxy_shear_plus_real, twopoint.Types.galaxy_shear_minus_real, twopoint.Types.galaxy_shear_plus_real, twopoint.Types.galaxy_position_real ] nznameindex1 = [0, 0, 1, 1] nznameindex2 = [0, 0, 0, 1] # Setup xi extensions self.exts = [] for i, name in enumerate(TWO_POINT_NAMES): self.exts.append( twopoint.SpectrumMeasurement( name, # hdu name ([], []), # tomographic bins (dtype1[i], dtype2[i]), # type of 2pt statistic (NOFZ_NAMES[nznameindex1[i]], NOFZ_NAMES[nznameindex2[i]]), # associated nofz "SAMPLE", # window function None, # id None, # value npairs=None, # pair counts angle=None, angle_unit='arcmin')) # units if self.params['lensfile'] == 'None': self.exts = self.exts[:2] return
def spectrum_measurement_from_block(block, section_name, output_name, types, kernels, angle_sample, real_space): # The dictionary type_table stores the codes used in the FITS files for # the types of spectrum type_codes = (types[0].name, types[1].name) _, _, bin_format = type_table[type_codes] # for cross correlations we must save bin_ji as well as bin_ij. # but not for auto-correlations. Also the numbers of bins can be different is_auto = (types[0] == types[1]) if block.has_value(section_name, "nbin"): nbin_a = block[section_name, "nbin"] else: nbin_a = block[section_name, "nbin_a"] nbin_b = block[section_name, "nbin_b"] # This is the ell values that have been calculated by cosmosis, not to # be confused with the ell values at which we want to save the results #(which is ell_sample) if real_space: # This is in radians theory_angle = block[section_name, "theta"] angle_sample_radians = np.radians(angle_sample / 60.) else: theory_angle = block[section_name, "ell"] angle_sample_radians = angle_sample # This is the length of the sample values n_angle_sample = len(angle_sample) # The fits format stores all the measurements # as one long vector. So we build that up here from the various # bins that we will load in. These are the different columns bin1 = [] bin2 = [] value = [] angular_bin = [] angle = [] # Bin pairs. Varies depending on auto-correlation for i in range(nbin_a): if is_auto: jmax = i + 1 else: jmax = nbin_b for j in range(jmax): # Load and interpolate from the block cl = block[section_name, bin_format.format(i + 1, j + 1)] # Convert arcmin to radians for the interpolation cl_interp = SpectrumInterp(theory_angle, cl) cl_sample = cl_interp(angle_sample_radians) #cl_sample = interp1d(theory_angle, cl)(angle_sample_radians) # Build up on the various vectors that we need bin1.append(np.repeat(i + 1, n_angle_sample)) bin2.append(np.repeat(j + 1, n_angle_sample)) value.append(cl_sample) angular_bin.append(np.arange(n_angle_sample)) angle.append(angle_sample) # Convert all the lists of vectors into long single vectors bin1 = np.concatenate(bin1) bin2 = np.concatenate(bin2) angular_bin = np.concatenate(angular_bin) value = np.concatenate(value) angle = np.concatenate(angle) bins = (bin1, bin2) # At the moment we only support this window function windows = "SAMPLE" if real_space: angle_unit = "arcmin" else: angle_unit = None # Build the output object type reqired. s = twopoint.SpectrumMeasurement(output_name, bins, types, kernels, windows, angular_bin, value, angle=angle, angle_unit=angle_unit) return s
def run_treecorr(config_file, tomo_file, cat_file, output_file): config = treecorr.read_config(config_file) tomo_data = fitsio.read(tomo_file) shear_data = fitsio.read(cat_file) tomo_header = fitsio.read_header(tomo_file, 1) nbin = tomo_header.get("NBIN") gg = treecorr.GGCorrelation(config) theta = [] xip = [] xim = [] bin1 = [] bin2 = [] angbin = [] print("Not doing the proper ngmix weighting in this test!") for b1 in range(nbin): w1 = np.where(tomo_data['BIN'] == b1 + 1) d1 = shear_data[w1] cat1 = treecorr.Catalog(g1=d1['E_1'], g2=d1['E_1'], w=d1['W'], ra=d1['RA'], dec=d1['DEC'], ra_units='deg', dec_units='deg') for b2 in range(nbin): if b2 < b1: continue w2 = np.where(tomo_data['BIN'] == b2 + 1) d2 = shear_data[w2] cat2 = treecorr.Catalog(g1=d2['E_1'], g2=d2['E_1'], w=d2['W'], ra=d2['RA'], dec=d2['DEC'], ra_units='deg', dec_units='deg') gg.process(cat1, cat2) ntheta = len(gg.meanr) theta.append(gg.meanr) xip.append(gg.xip) xim.append(gg.xim) bin1.append(np.repeat(b1, ntheta)) bin2.append(np.repeat(b2, ntheta)) angbin.append(np.arange(ntheta, dtype=int)) theta = np.concatenate(theta) xip = np.concatenate(xip) xim = np.concatenate(xim) bin1 = np.concatenate(bin1) bin2 = np.concatenate(bin2) angbin = np.concatenate(angbin) tp = twopoint.Types.galaxy_shear_plus_real tm = twopoint.Types.galaxy_shear_minus_real XIP = twopoint.SpectrumMeasurement("xip", (bin1, bin2), (tp, tp), ("source", "source"), "SAMPLE", angbin, xip, angle=theta, angle_unit="arcmin") XIM = twopoint.SpectrumMeasurement("xim", (bin1, bin2), (tm, tm), ("source", "source"), "SAMPLE", angbin, xim, angle=theta, angle_unit="arcmin") output = twopoint.TwoPointFile([XIP, XIM], [], [], None) output.to_fits(output_file, clobber=True)