Generate simulated data
n = 20
sf = _Sampler(lambda x: np.exp(-(x**2)/2), limits=(-4, 4) )
data = sf(10000)
tests = np.array([np.abs(data.mean()), np.abs(data.std()-1) ])
assert np.all(tests<5e-2 ), f'Failed Tests: mean {data.mean()}, std {data.std()}'
func = lambda x: x**2
wfun = _Sampler(func)
test2 = wfun.mean, np.mean(wfun(1000))
assert np.abs( test2[0]-test2[1] ) < 1e-1, f'Not almost equal: {test2}'
Test with a function peaked at both ends, generate equal signal and background
rng = np.random.default_rng(42)
nn = np.array( [ len(generate_times(0,1,10, rng=rng )) for i in range(100000) ])
plt.hist(nn, np.linspace(0,25,26), label=f'Mean: {nn.mean():.2f},\n std: {nn.std():.2f}');
plt.legend()
plt.title(f'Check that generated number is Poisson', fontsize=12);
# test weight function
s, lam = 1,0.1
def wfuntest(s, b=1, lam=0.1, Nb=10000, rng=42):
wfun = WeightFunction(s=s,b=b,wt_signif=lam, rng=rng)
n = int(Nb*(1+s/b))
samp = wfun.sample(s, n )
wts = wfun.weights(s, n)
sumwts = np.sum(wts) #wfun.weights(s,b, 10000)
fig, (ax1, ax3) = plt.subplots(1,2, figsize=(7,3), sharex=True)
plt.subplots_adjust(top=0.80, wspace=0.3)
r = np.linspace(0,1, 1000)
ax1.plot(r, wfun(r)/(s+b), '-',color='orange', lw=2,label='PSF+bkg')
ax1.set(ylim=(0,None), xlim=(0,1), xlabel='$r^2$', title='position')
ax1.hist(samp, np.linspace(0,1,51), density=True, color='cornflowerblue',
label=f'generated:\n mean:{np.mean(samp):.2f}\n cnt: {len(samp)}');
ax1.legend(fontsize=10)
#ax1.set(title='sampled', xlabel='$r^2$'); ax1.legend()
ax3.hist(wts, bins=np.linspace(0,1,51), label=f'sum:{sumwts:.1f}')
ax3.set(title='weights', label='weight'); ax3.legend(fontsize=10)
fig.suptitle(f'WeightFunction test: s={s}')
wfuntest(s)
def src_flare(t, tzero=5, width=0.25, amp=2):
return 1e-6*(1 + amp*np.exp(-(t-tzero)**2/2/width))
sim = Simulation('test_sim', src_flux=src_flare, tstart=0, tstop=10, rng=42 , debug=10)
sim.run()
sim.photons.hist(bins=50, color='cornflowerblue');
sim.exposure.describe()
%time self.setup()#alpha = lambda n: 0)