def test(self, relerr=0.2,dowrite=False, epsfile=None): omega_m=0.25 z = 0.3 nfw = lensing.nfw.NFW(omega_m, z) lin = lensing.linear.Linear(omega_m=omega_m) r200=0.5 c=5.0 B=1.0 print 'generating fake data with %i%% errors' % (relerr*100,) nr = 21 logrmin=-2.0 logrmax=1.5 r = logspace(logrmin,logrmax,nr) ds = nfw.dsig(r,r200,c) + B*lin.dsig(r) dserr = ds*relerr ds += dserr*numpy.random.standard_normal(nr) if dowrite: f='~/tmp/fake-dsig.dat' print 'Writing fake data to:',f colprint(r,ds,dserr,format='%0.8f', file=f) print 'generating fine grid truth values' rtrue = logspace(logrmin,logrmax,nr*5) dstrue = nfw.dsig(rtrue,r200,c) + B*lin.dsig(rtrue) drhotrue = nfw.rho(rtrue,r200,c) + B*lin.drho(rtrue) # add linear term mnfw = nfw.m(rtrue, r200,c) mlin = lin.m(rtrue, B) mtrue = mnfw + mlin print 'Doing inversion' odict = self.invert(r,ds,dserr=dserr) tab = Table(2,2) od = lensing.plotting.plot_dsig(odict, noshow=True) # show some log-interpolated points of dsig nper = 10 for i in xrange(nr-1): amp = odict['ds_amp'][i] slope = odict['ds_slope'][i] off = odict['ds_offset'][i] x = logspace(log10(r[i]),log10(r[i+1]),nper) y = amp*x**(-slope) - off od['plt'].add(Curve(x,y,color='blue')) od['plt'].add(Curve(rtrue,dstrue,color='red')) tab[0,0] = od['plt'] # plot uncorrected with no error bar rod = lensing.plotting.plot_drho(r=odict['rdrho'], drho=odict['drho_noendc'], drhoerr=odict['drhoerr']*0, noshow=True) rod2 = lensing.plotting.add_to_log_plot(rod['plt'], odict['rdrho'], odict['drho'], odict['drhoerr'], color='blue') pnocorr = rod['p'] p = rod2['p'] pnocorr.label = 'no end correction' p.label = 'end correction' plist = [pnocorr,p] drhotrue_curve = Curve(rtrue,drhotrue,color='red') drhotrue_curve.label = 'Truth' rod['plt'].add(drhotrue_curve) plist.append(drhotrue_curve) rod['plt'].add(PlotKey(0.1,0.2,plist)) tab[0,1] = rod['plt'] # for the mass stuff # first show log-interpolated points of drho to see how # it looks rod_interp = lensing.plotting.plot_drho(odict, noshow=True) nper = 10 for i in xrange(odict['drho'].size-1): amp = odict['drho_amp'][i] slope = odict['drho_slope'][i] off = odict['drho_offset'][i] x = logspace(log10(r[i]),log10(r[i+1]),nper) y = amp*x**(-slope) - off y = where(y < 1.e-5, 1.e-5, y) rod_interp['plt'].add(Curve(x,y,color='blue')) tab[1,0] = rod_interp['plt'] mod = lensing.plotting.plot_mass(odict, noshow=True) mp = mod['p'] mp.label = 'Recovered Mass' nfw_curve = Curve(rtrue,mnfw,color='green') nfw_curve.label = 'NFW' mod['plt'].add(nfw_curve) lin_curve = Curve(rtrue,mlin,color='orange') lin_curve.label = 'linear' mod['plt'].add(lin_curve) true_curve = Curve(rtrue,mtrue,color='magenta') true_curve.label = 'True' mod['plt'].add(true_curve) #m200 = nfw.m200(r200) #m200p = Points([r200],[m200],type='cross',color='blue') #m200p.label = r'$M_{200} true' #mod['plt'].add(m200p) # now find r200 from the mass profile #mvf = MvirFinder(omega_m, z, 200) #rvir,rvir_err,mvir,mvir_err = mvf.find_mvir(odict['rmass'], # odict['mass'], # odict['masserr']) #mvp = Points([rvir],[mvir],type='filled square',color='red') #mvexp = SymErrX([rvir],[mvir],[rvir_err],color='red') #mveyp = SymErrY([rvir],[mvir],[mvir_err],color='red') #mvp.label = r'$M_{200}' #mod['plt'].add(mvp,mvexp,mveyp) #mod['plt'].add(PlotKey(0.1,0.9,[mp,true_curve,nfw_curve,lin_curve, # m200p,mvp])) mod['plt'].add(PlotKey(0.1,0.9,[mp,true_curve,nfw_curve,lin_curve])) tab[1,1] = mod['plt'] if dowrite: f='~/tmp/fake-dsig-invert.dat' print 'Writing inverted fake data to:',f colprint(odict['rdrho'], odict['drho'],odict['mass'],odict['masserr'], format='%20.8e',file=f) if epsfile is not None: print 'Writing eps file:',epsfile tab.write_eps(epsfile) else: tab.show()
def test_range(): # here we test range queries, including # equality checks, even though these are more simply # done using the match() method. with tempfile.TemporaryDirectory() as tmpdir: fdbfile = os.path.join(tmpdir, 'f8file.db') idbfile = os.path.join(tmpdir, 'i4file.db') # create the databases n = 10 f = numpy.arange(n, dtype='f8') i = numpy.arange(n, dtype='i4') dbf8 = numpydb.create(fdbfile, "f8", "i4") dbi4 = numpydb.create(idbfile, "i4", "i4") dbf8.put(f, i) dbi4.put(i, i) dbs = [dbf8, dbi4] types = ['float', 'int'] for i in range(2): db = dbs[i] type = types[i] print('\nTesting %s data' % type) print('-'*70) val = 9999 print('\nfirst printing all') print('-'*70) keys, data = db.range(None, val, '<', select='both') colprint(keys, data, format='%15s') val = 5 print('\ntesting == %s on %s' % (val, type)) print('-'*70) keys, data = db.range(val, None, '=', select='both') colprint(keys, data, format='%15s') print('\ntesting >= %s on %s' % (val, type)) print('-'*70) keys, data = db.range(val, None, '>=', select='both') colprint(keys, data, format='%15s') print('\ntesting > %s on %s' % (val, type)) print('-'*70) keys, data = db.range(val, None, '>', select='both') colprint(keys, data, format='%15s') print('\ntesting <= %s on %s' % (val, type)) print('-'*70) keys, data = db.range(None, val, '<=', select='both') colprint(keys, data, format='%15s') print('\ntesting < %s on %s' % (val, type)) print('-'*70) keys, data = db.range(None, val, '<', select='both') colprint(keys, data, format='%15s') low = 5 high = 8 print('\ntesting %s <= key <= %s %s' % (low, high, type)) print('-'*70) keys, data = db.range(low, high, '[]', select='both') colprint(keys, data, format='%15s') print('\ntesting %s <= key < %s %s' % (low, high, type)) print('-'*70) keys, data = db.range(low, high, '[)', select='both') colprint(keys, data, format='%15s') print('\ntesting %s < key <= %s %s' % (low, high, type)) print('-'*70) keys, data = db.range(low, high, '(]', select='both') colprint(keys, data, format='%15s') print('\ntesting %s < key < %s %s' % (low, high, type)) print('-'*70) keys, data = db.range(low, high, '()', select='both') colprint(keys, data, format='%15s') dbf8.close() dbi4.close()
def test_range(): # here we test range queries, including # equality checks, even though these are more simply # done using the match() method. # temp file names fdbfile=tempfile.mktemp(prefix='f8rand-i4index-', suffix='.db') idbfile=tempfile.mktemp(prefix='i4data-i4index-', suffix='.db') # create the databases n=10 f = numpy.arange(n,dtype='f8') i = numpy.arange(n,dtype='i4') numpydb.create(fdbfile, "f8", "i4") numpydb.create(idbfile, "i4", "i4") db = numpydb.Open(fdbfile, "r+") db.put(f, i) db.close() del db db = numpydb.Open(idbfile, "r+") db.put(i, i) db.close() del db # now open for reading and tests fdb = numpydb.Open(fdbfile) idb = numpydb.Open(idbfile) dbs = [fdb, idb] types = ['float','int'] for i in xrange(2): db = dbs[i] type = types[i] print '\nTesting %s data' % type print '-'*70 val=9999 print '\nfirst printing all' print '-'*70 keys,data = db.range(None, val, '<', select='both') colprint(keys,data, format='%15s') val=5 print '\ntesting == %s on %s' % (val,type) print '-'*70 keys,data = db.range(val, None, '=', select='both') colprint(keys,data, format='%15s') print '\ntesting >= %s on %s' % (val,type) print '-'*70 keys,data = db.range(val, None, '>=', select='both') colprint(keys,data, format='%15s') print '\ntesting > %s on %s' % (val,type) print '-'*70 keys,data = db.range(val, None, '>', select='both') colprint(keys,data, format='%15s') print '\ntesting <= %s on %s' % (val,type) print '-'*70 keys,data = db.range(None, val, '<=', select='both') colprint(keys,data, format='%15s') print '\ntesting < %s on %s' % (val,type) print '-'*70 keys,data = db.range(None, val, '<', select='both') colprint(keys,data, format='%15s') low=5 high=8 print '\ntesting %s <= key <= %s %s' % (low,high,type) print '-'*70 keys,data = db.range(low,high, '[]', select='both') colprint(keys,data, format='%15s') print '\ntesting %s <= key < %s %s' % (low,high,type) print '-'*70 keys,data = db.range(low,high, '[)', select='both') colprint(keys,data, format='%15s') print '\ntesting %s < key <= %s %s' % (low,high,type) print '-'*70 keys,data = db.range(low,high, '(]', select='both') colprint(keys,data, format='%15s') print '\ntesting %s < key < %s %s' % (low,high,type) print '-'*70 keys,data = db.range(low,high, '()', select='both') colprint(keys,data, format='%15s') os.remove(fdbfile) os.remove(idbfile)
def test_range1(): # here we test range queries, including # equality checks, even though these are more simply # done using the match() method. with tempfile.TemporaryDirectory() as tmpdir: fdbfile = os.path.join(tmpdir, 'f8file.db') idbfile = os.path.join(tmpdir, 'i4file.db') # create the databases n = 10 f = numpy.arange(n, dtype='f8') i = numpy.arange(n, dtype='i4') numpydb.create(fdbfile, "f8", "i4") numpydb.create(idbfile, "i4", "i4") db = numpydb.Open(fdbfile, "r+") db.put(f, i) db.close() del db db = numpydb.Open(idbfile, "r+") db.put(i, i) db.close() del db # now open for reading and tests fdb = numpydb.Open(fdbfile) idb = numpydb.Open(idbfile) dbs = [fdb, idb] types = ['float', 'int'] for i in range(2): db = dbs[i] type = types[i] print('\nTesting %s data' % type) print('-'*70) val = 9999 print('\nfirst printing all') print('-'*70) keys, data = db.range1(val, '<', select='both') colprint(keys, data, format='%15s') val = 5 print('\ntesting == %s on %s' % (val, type)) print('-'*70) keys, data = db.range1(val, '=', select='both') colprint(keys, data, format='%15s') print('\ntesting >= %s on %s' % (val, type)) print('-'*70) keys, data = db.range1(val, '>=', select='both') colprint(keys, data, format='%15s') print('\ntesting > %s on %s' % (val, type)) print('-'*70) keys, data = db.range1(val, '>', select='both') colprint(keys, data, format='%15s') print('\ntesting <= %s on %s' % (val, type)) print('-'*70) keys, data = db.range1(val, '<=', select='both') colprint(keys, data, format='%15s') print('\ntesting < %s on %s' % (val, type)) print('-'*70) keys, data = db.range1(val, '<', select='both') colprint(keys, data, format='%15s')