def _match_one_by_z(self, binnum): """ match redshifts in this bin, but apply no selection to the randoms """ import weighting z=self.z rz=self.rz nrand=rz.size w=self.binner.select_bin(self.data, binnum) rand_weights = weighting.hist_match(rz, z[w], self['binsize']) return w,rand_weights
def _match_one_by_selection_and_z(self, binnum): """ match by applying the same selection to real and random data, as well as redshift matching """ import weighting z=self.z rz=self.rz nrand=rz.size w=self.binner.select_bin(self.data, binnum) wr=self.binner.select_bin(self.rdata, binnum) rand_weights = weighting.hist_match(rz[wr], z[w], self['binsize']) return w,wr,rand_weights
def compare_hist_match(binsize, binnum=9, l=None, r=None): """ Compare hist_match with weights and with remove method. """ import weighting import lensing if l is None or r is None: l,r = load_test_data() binner=lensing.binning.LambdaBinner(12) print("selecting ",binner.bin_label(binnum)) w=binner.select_bin(l, binnum) print(" kept %d/%d" % (w.size,l.size)) print("using remove method") keep = weighting.hist_match_remove(r['z'], l['z'][w], binsize) perc = keep.size/(1.*r.size) print(" used number: %d/%d = %0.2f" % (keep.size,r.size, perc)) print(" combining") comb_rm = average_lensums(r[keep]) for i in xrange(comb_rm['r'].size): print("%0.3f %15.12f %15.12f" % \ (comb_rm['r'][0,i], comb_rm['dsig'][0,i], comb_rm['wsum_mean'][0,i])) print("using weights method") weights = weighting.hist_match(r['z'], l['z'][w], binsize) effnum = weights.sum() effperc = effnum/r.size print(" effective number: %d/%d = %0.2f" % (effnum,r.size, effperc)) print(" combining") comb = average_lensums(r, weights=weights) for i in xrange(comb_rm['r'].size): print("%0.3f %15.12f %15.12f %15.12f %15.12f" % \ (comb['r'][0,i], comb['dsig'][0,i], comb_rm['dsig'][0,i], comb['wsum_mean'][0,i], comb_rm['wsum_mean'][0,i]))
def main(): options,args = parser.parse_args(sys.argv[1:]) if len(args) < 2: parser.print_help() sys.exit(1) lensrun=args[0] randrun=args[1] bintype=options.bintype binsize=float(options.binsize) show=options.show if bintype is None: raise ValueError("currently demand some kind of binning") conf=lensing.files.cascade_config(lensrun) z_field=conf['lens_config']['z_field'] print('z_field:',z_field) b = lensing.binning.instantiate_binner(bintype) nbin=b.get_nbin() # read collated lens catalog and select lenses with the # bin criteria. Then match the randoms redshift histogram # this is where z is, may be a different name in the collated data data = lensing.files.collated_read(sample=lensrun) rand = lensing.files.collated_read(sample=randrun) z=data[z_field] output = lensing.binning.lensbin_struct(data['rsum'][0].size, n=nbin) outextra = 'randmatch-%s' % randrun weights_file=lensing.files.sample_file(type='weights', sample=lensrun, name=b.get_name(), extra=outextra) print("opening weights file for writing:",weights_file) wfits=fitsio.FITS(weights_file,'rw',clobber=True) html_name=lensing.files.sample_file(type='binned-plots', sample=lensrun, name=b.get_name(), extra=outextra, ext='html') makedir_fromfile(html_name) html_file=open(html_name,'w') html_file.write('<html>\n<body bgcolor=white>\n') for binnum in xrange(nbin): print("-"*70) print("%s/%s: " % (binnum+1,nbin), b.bin_label(binnum)) png_extra='%02d-%s-%s' % (binnum,outextra,randrun) pngfile=lensing.files.sample_file(type='binned-plots', sample=lensrun, name=b.get_name(), extra=png_extra, ext='png') tit=b.bin_label(binnum) tit+=' rand: '+randrun w = b.select_bin(data, binnum) print("matching hist with weights") extra_weights=None if options.erf_mean_field is not None: print("erf weights from:",options.erf_mean_field) if options.erf_sigma_field is None: raise ValueError("send both --erf-mean-field and --erf-sigma-field") x = rand[options.erf_mean_field] sigma=rand[options.erf_sigma_field] weights=b.get_bin_erf_weights_1d(binnum, x, sigma) else: if options.extra_weights is not None: extra_weights=rand[extra_weights] weights = weighting.hist_match(rand['z'], z[w], binsize, extra_weights1=extra_weights) weights *= ( 1.0/weights.max() ) effnum = weights.sum() effperc = effnum/rand.size print("effective number: %d/%d = %0.2f" % (effnum,rand.size, effperc)) print("combining randoms with weights") comb = lensing.outputs.average_lensums(rand, weights=weights) weighting.plot_results1d(rand['z'], z[w], weights, binsize, pngfile=pngfile, title=tit, show=show) wstruct=zeros(weights.size, dtype=[('weights','f8')]) wstruct['weights'] = weights # a new extension for each bin wfits.write(wstruct) html_file.write(' <img src="%s"><p>\n' % os.path.basename(pngfile)) # copy all common tags for n in comb.dtype.names: output[n][binnum] = comb[n][0] html_file.write('</body>\n</html>\n') html_file.close() wfits.close() lensing.files.sample_write(data=output,type='binned', sample=lensrun,name=b.get_name(),extra=outextra)