def _compare_dist(self, dist): import biggles import esutil as eu from biggles import plot_hist r=dist.rvs(self.deep_pars.shape[0]) names=['c1','c2','M1','M2','T','I'] grid=eu.plotting.Grid(6) tab=biggles.Table(grid.nrow, grid.ncol) for i in xrange(6): row,col=grid(i) plt=plot_hist(r[:,i], nbin=100, xlabel=names[i], visible=False) plt=plot_hist(self.deep_pars[:,i], nbin=100, color='red', plt=plt, visible=False) tab[row,col] = plt pngfile=files.get_psample_summed_url(self['run'], self['is2n'], itrial=self['itrial'], ext='png') pngfile=pngfile.replace('.png','-hist.png') print(pngfile) tab.write_img(1200,800,pngfile) key=raw_input('hit a key: ') if key=='q': stop
def make_r50_plot(r50): print("r50:",r50.mean(), r50.std()) nbin=50 plt=plot_hist(r50, nbin=nbin, visible=False, norm=1, min=0, max=6, aspect_ratio=1, xlabel=r'$r_{50} [pixels]$') # the r50 from the parametric sims p=ngmix.priors.LogNormal(1.54, 1.0) r=p.sample(r50.size*10) plot_hist(r, nbin=nbin, visible=False, norm=1, min=0, max=6, plt=plt, color='red', smooth=True, type='dashed') plt.y1.draw_ticklabels=False return plt
def plot_resid_hist(self): means=self.means if means.size == 1: return else: import biggles fits=self.fits args=self.args #Q=calc_q(fits) diff = means['shear'] - means['shear_true'] plt = biggles.plot_hist(diff, nbin=20, visible=False, xlabel=r'$\gamma - \gamma_{True}$') dmax=numpy.abs(diff).max() plt.xrange=[-1.3*dmax, 1.3*dmax] fname=self._get_resid_hist_file() eu.ostools.makedirs_fromfile(fname) print("writing:",fname) plt.write_eps(fname) if args.show: plt.show(width=1000, height=1000)
def plot_resid_hist(self): means = self.means if means.size == 1: return else: import biggles fits = self.fits args = self.args #Q=calc_q(fits) diff = means['shear'] - means['shear_true'] plt = biggles.plot_hist(diff, nbin=20, visible=False, xlabel=r'$\gamma - \gamma_{True}$') dmax = np.abs(diff).max() plt.xrange = [-1.3 * dmax, 1.3 * dmax] fname = self._get_resid_hist_file() eu.ostools.makedirs_fromfile(fname) print("writing:", fname) plt.write_eps(fname) if args.show: plt.show(width=1000, height=1000)
def make_s2n_plot(s2n): log_s2n=numpy.log10( s2n.clip(min=0.001,max=10000.0) ) plt=plot_hist(log_s2n, nbin=100, visible=False, min=0.5, max=2.5, xlabel=r'$log_{10}(S/N)$', aspect_ratio=1, norm=1) plt.y1.draw_ticklabels=False return plt
def test_1gauss_T_recovery(noise, T=8.0, counts=1.0, ntrial=100, show=True, png=None): import biggles T_true=T T_meas=numpy.zeros(ntrial) for i in xrange(ntrial): while True: try: gm=test_1gauss( noise=noise, T=T_true, counts=counts, verbose=False, ) T=gm.get_T() T_meas[i]=T break except GMixRangeError: pass except GMixMaxIterEM: pass mean=T_meas.mean() std=T_meas.std() print("<T>:",mean,"sigma(T):",std) binsize=0.2*std plt=biggles.plot_hist(T_meas, binsize=binsize, visible=False) p=biggles.Point(T_true, 0.0, type='filled circle', size=2, color='red') plt.add(p) plt.title='Flux: %g T: %g noise: %g' % (counts, T_true, noise) xmin=mean-4.0*std xmax=mean+4.0*std plt.xrange=[xmin, xmax] if show: plt.show() if png is not None: print(png) plt.write_img(800, 800, png)
def plothist_weights(weights, show=False): import biggles normweights = weights/weights.max() w,=numpy.where(normweights > 0) print("plothist_weights: greater than zero: %d/%d" % (w.size,normweights.size)) lw = numpy.log10(normweights[w]) #binsize = 0.1*lw.std() plt=biggles.plot_hist(lw, nbin=50, #binsize=binsize, xlabel=r'$log_{10}(relative weight)$', visible=False) if show: plt.show() return plt
def compare_images(self, type, nrand=None): """ compare images of the specified type parameters ---------- type: string Type of images to compare, e.g. 'image','weight' etc. nrand: integer, optional Compare at most this many images. A random subset will be drawn """ if nrand is not None: nrand = min(nrand, self.ind1.size) ids = self._get_random_subset(nrand) else: ids = numpy.arange(self.ind1.size) means = [] ntot = ids.size for icount, index in enumerate(ids): print(" %d/%d %d" % (icount + 1, ntot, index)) i1 = self.ind1[index] i2 = self.ind2[index] ncut = self._check_ncutout(i1, i2) for icut in range(ncut): im2 = self.m2.get_cutout(i2, icut, type=type) im1 = self.m1.get_cutout(i1, icut, type=type) #if type=='image': # wt=self.m2.get_cutout(i2, icut, type='weight') # err=numpy.sqrt(1.0/wt.max()) # print(" compare to noise:",err) if type == 'image': ok, mean, err, std = self._compare_images(im1, im2) means.append(mean) elif type == 'weight': self._compare_weights(im1, im2) else: self._compare_images_exact(im1, im2) if type == 'image' and self.png_prefix is not None: import images fname = self.png_prefix + '-imdiff-%06d-%03d.png' % (index, icut) print("writing:", fname) if ok: oks = 'OK' else: oks = 'Not OK' images.compare_images( im1, im2, dims=(1500, 1500), #width=1500, #height=1500, title='rms: %.3g mean: %.3g +/- %.3g %s' % (std, mean, err, oks), file=fname, ) #if 'q'==raw_input("hit a key: (q to quit)"): # stop if type == 'image': import esutil as eu print(len(means)) means = numpy.array(means) mdiff = means.mean() mdiff_err = means.std() / numpy.sqrt(means.size) print("average mean diff: %g +/- %g" % (mdiff, mdiff_err)) mdiff, mdiff_std, mdiff_err = eu.stat.sigma_clip(means, get_err=True) print("clipped average mean diff: %g +/- %g" % (mdiff, mdiff_err)) if self.png_prefix is not None: import biggles binsize = mdiff_std * 0.1 fname = self.png_prefix + '-imdiff-means.png' biggles.plot_hist( means, binsize=binsize, visible=False, file=fname, )
def fit_prior(run, is2n=0, field='pars_noshear',show=False): import biggles import esutil as eu from sklearn.mixture import GMM import fitsio gm=ngmix.gmix.GMixND() alldata=files.read_output(run, is2n) data=alldata[field] # fit |g| g=sqrt(data[:,2]**2 + data[:,3]**2) gp=ngmix.priors.GPriorBA() bs=eu.stat.Binner(g) bs.dohist(nbin=100) bs.calc_stats() xvals=bs['center'] yvals=bs['hist'].astype('f8') gp.dofit(xvals, yvals) rg=gp.sample1d(1000000) #eta1,eta2,good=ngmix.shape.g1g2_to_eta1eta2_array(data[:,2], # data[:,3]) #eta = 2*numpy.arctanh(g) #logg=log(g) outfile=files.get_fitprior_url(run, is2n) epsfile=files.get_fitprior_url(run, is2n, ext='eps') # fit TF with n-dimensional gaussian ngauss=20 gm.fit(data[:, 4:], ngauss, min_covar=1.0e-4) ''' print("fitting log(g)") g_gmm=GMM(n_components=10, n_iter=5000, min_covar=1.0e-4, covariance_type='full') g_gmm.fit(eta[:,newaxis]) if not g_gmm.converged_: print("DID NOT CONVERGE") rg=g_gmm.sample(1000000) gplt=biggles.plot_hist(eta,nbin=100,xlabel='|g|', norm=1, visible=False) ''' gplt=biggles.plot_hist(g,nbin=100,xlabel='|g|', norm=1, visible=False) #gplt=biggles.plot_hist(rg[:,0],nbin=100, gplt=biggles.plot_hist(rg,nbin=100, plt=gplt, norm=1, color='red', visible=False) print("saving mixture to:",outfile) gm.save_mixture(outfile) with fitsio.FITS(outfile,'rw') as fits: gout=numpy.zeros(1, dtype=[('sigma','f8')]) gout['sigma'] = gp.fit_pars[1] fits.write(gout, extname='gfit') # # plots # r=gm.sample(1000000) tab=biggles.Table(2,2) Tplt=biggles.plot_hist(data[:,4], nbin=100, xlabel='log(T)', norm=1, visible=False) biggles.plot_hist(r[:,0], nbin=100, color='red', norm=1, plt=Tplt, visible=False) Fplt=biggles.plot_hist(data[:,5], nbin=100, xlabel='log(F)', norm=1, visible=False) biggles.plot_hist(r[:,1], nbin=100, color='red', norm=1, plt=Fplt, visible=False) ''' eta1plt=biggles.plot_hist(eta1, nbin=100, xlabel='eta1', norm=1, visible=False) eta2plt=biggles.plot_hist(eta2, nbin=100, xlabel='eta2', norm=1, visible=False) ''' tab[0,0] = Tplt tab[0,1] = Fplt #tab[1,0] = eta1plt #tab[1,1] = eta2plt tab[1,0] = gplt tab[1,1] = gplt tab.aspect_ratio=0.5 print("writing:",epsfile) tab.write_eps(epsfile) if show: biggles.configure('screen','width',1200) biggles.configure('screen','height',700) tab.show()
def compare_images(self, type, nrand=None): """ compare images of the specified type parameters ---------- type: string Type of images to compare, e.g. 'image','weight' etc. nrand: integer, optional Compare at most this many images. A random subset will be drawn """ if nrand is not None: nrand = min(nrand, self.ind1.size) ids = self._get_random_subset(nrand) else: ids = numpy.arange(self.ind1.size) means=[] ntot = ids.size for icount, index in enumerate(ids): print(" %d/%d %d" % (icount+1,ntot,index)) i1 = self.ind1[index] i2 = self.ind2[index] ncut=self._check_ncutout(i1, i2) for icut in range(ncut): im2 = self.m2.get_cutout(i2, icut, type=type) im1 = self.m1.get_cutout(i1, icut, type=type) #if type=='image': # wt=self.m2.get_cutout(i2, icut, type='weight') # err=numpy.sqrt(1.0/wt.max()) # print(" compare to noise:",err) if type=='image': ok, mean, err, std = self._compare_images(im1, im2) means.append(mean) elif type == 'weight': self._compare_weights(im1, im2) else: self._compare_images_exact(im1, im2) if type=='image' and self.png_prefix is not None: import images fname=self.png_prefix + '-imdiff-%06d-%03d.png' % (index, icut) print("writing:",fname) if ok: oks='OK' else: oks='Not OK' images.compare_images( im1, im2, dims=(1500,1500), #width=1500, #height=1500, title='rms: %.3g mean: %.3g +/- %.3g %s' % (std, mean, err, oks), file=fname, ) #if 'q'==raw_input("hit a key: (q to quit)"): # stop if type=='image': import esutil as eu print(len(means)) means = numpy.array(means) mdiff = means.mean() mdiff_err = means.std()/numpy.sqrt(means.size) print("average mean diff: %g +/- %g" % (mdiff, mdiff_err)) mdiff, mdiff_std, mdiff_err = eu.stat.sigma_clip(means, get_err=True) print("clipped average mean diff: %g +/- %g" % (mdiff, mdiff_err)) if self.png_prefix is not None: import biggles binsize=mdiff_std*0.1 fname=self.png_prefix + '-imdiff-means.png' biggles.plot_hist( means, binsize=binsize, visible=False, file=fname, )