def _plot_zhist(self, rz, z, rand_weights, binnum): """ plot the z hist for data, randoms, and weighted randoms """ import weighting pngfile=get_match_weights_file(self['lens_run'], self['rand_run'], self['bin_scheme'], binnum=binnum, ext='png') tit=self.binner.get_label(binnum) tit+=' rand: '+self['rand_run'] print(" writing:",pngfile) if rand_weights is None: rand_weights=numpy.ones(rz.size) weighting.plot_results1d(rz, z, rand_weights, self['binsize'], pngfile=pngfile, title=tit, xlabel='z', label1='rand', label2='lenses', show=self['show'])
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)