Esempio n. 1
0
def bias_test():
    #Making new trials...
    n_sigs = np.r_[:201:10]
    trials = [tr.get_many_fits(1000, n_sig=n_sig, logging=False, seed=n_sig, poisson=False) for n_sig in n_sigs]
        
    for (n_sig, t) in zip(n_sigs, trials):
        t['ntrue'] = np.repeat(n_sig, len(t))

    allt = cy.utils.Arrays.concatenate(trials)

    fig, axs = plt.subplots(1, 2, figsize=(6,3))

    dns = np.mean(np.diff(n_sigs))
    ns_bins = np.r_[n_sigs - 0.5*dns, n_sigs[-1] + 0.5*dns]
    expect_kw = dict(color='C0', ls='--', lw=1, zorder=-10)
    expect_gamma = tr.sig_injs[0].flux[0].gamma
    
    ax = axs[0]
    h = hl.hist((allt.ntrue, allt.ns), bins=(ns_bins, 100))
    hl.plot1d(ax, h.contain_project(1),errorbands=True, drawstyle='default')

    lim = ns_bins[[0, -1]]
    ax.set_xlim(ax.set_ylim(lim))
    ax.plot(lim, lim, **expect_kw)
    ax.set_aspect('equal')


    ax = axs[1]
    h = hl.hist((allt.ntrue, allt.gamma), bins=(ns_bins, 100))
    hl.plot1d(ax, h.contain_project(1),errorbands=True, drawstyle='default')
    ax.axhline(expect_gamma, **expect_kw)
    ax.set_xlim(axs[0].get_xlim())

    for ax in axs:
        ax.set_xlabel('n_inj')
        ax.grid()
    axs[0].set_ylabel('n_s')
    axs[1].set_ylabel('gamma')

    plt.title('Gamma={}, {} Weighted'.format(g,w.capitalize()))
    plt.tight_layout()
    plt.savefig('/data/user/mcampana/analysis/Blazar_1FLE/plots/BiasTest_{}weighting_gamma{}_{}hemi_{}_{}_u1FLEblazars_{}.png'.format(w, g, hemisphere, pDirName, dSetName, today_date))
    plt.close()
    
    return
Esempio n. 2
0
def TSchi2():
    fig, ax = plt.subplots()

    b = cy.bk.get_best(bg_chi2, 'hemisphere', h, 'weight', w, 'gamma', g)
    hst = b.get_hist(bins=30)
    hl.plot1d(ax, hst, crosses=True, label='{} bg trials'.format(b.n_total))

    x = hst.centers[0]
    norm = hst.integrate().values
    ax.semilogy(x, norm * b.pdf(x), lw=1, ls='--',
                label='Chi2 fit: {} dof, eta={}'.format(np.round(b.ndof,3), np.round(b.eta,3)))

    ax.set_xlabel('TS')
    ax.set_ylabel('number of trials')
    ax.legend()
    ax.set_title('{} Weighted, {} Hemisphere(s)'.format(w.capitalize(), h.capitalize()))

    plt.tight_layout()
    plt.savefig('/data/user/mcampana/analysis/Blazar_1FLE/plots/Chi2TS_{}trials_{}weighting_{}hemi_{}_{}_u1FLEblazars_{}.png'.format(b.n_total, w, h, pDirName, dSetName, today_date))
    plt.close()
    
    return
Esempio n. 3
0
def multi_TSchi2():
    nrow, ncol = 3, 3
    fig, aaxs = plt.subplots(nrow, ncol, figsize=(12,12), sharex=True, sharey=True, dpi=200)
    axs = np.ravel(aaxs)
    # keep track of which ax's we already used
    used_axs = []
    for (i, gam) in enumerate(gamma):
        ax = axs[i]
        # plot histogram
        b = cy.bk.get_best(bg_chi2, 'weight', w, 'gamma', gam)
        h = b.get_hist(bins=30, range=(0, 10))
        hl.plot1d(ax, h, crosses=True, label='{} bg trials'.format(b.n_total))
        # plot chi2 fit to nonzero values
        norm = h.integrate().values
        ts = np.linspace(.1, h.range[0][1], 100)
        ax.plot(ts, norm * b.pdf(ts), label='Chi2 fit: {} dof, eta={}'.format(np.round(b.ndof,2), np.round(b.eta,3)))
        # set limits and label dec
        ax.semilogy(nonposy='clip')
        ax.set_ylim(.3, 3e3)
        ax.text(10, 5e2, 'gamma={}'.format(gam), ha='right', va='center')
        ax.legend(loc='best')
        used_axs.append(ax)
    # hide unused ax's
    for ax in axs:
        if ax not in used_axs:
            ax.set_visible(False)
    # add x and y labels
    for ax in aaxs[-1]:
        if ax in used_axs:
            ax.set_xlabel('TS')
    for ax in aaxs[:,0]:
        ax.set_ylabel('trials per bin')
    plt.tight_layout()
    plt.savefig('/data/user/mcampana/analysis/Blazar_1FLE/plots/Chi2TS_{}weighting_{}hemi_{}_{}_u1FLEblazars_{}.png'.format(w,h, pDirName, dSetName, today_date))
    plt.close()
    
    return
trials = [tr.get_many_fits(100, n_sig=n_sig, logging=False, seed=n_sig*np.random.randint(1,1000)) for n_sig in n_sig_arr]
for (n_sig, t) in zip(n_sig_arr, trials):
    t['ntrue'] = np.repeat(n_sig, len(t))
time = timer.time
allt = cy.utils.Arrays.concatenate(trials)
#allt
np.savez(wrkdir+"trials_with_sig_inj_"+str(index)+".npz",allt.as_array)

fig, axs = plt.subplots(1, 2, figsize=(6,3))
dns = np.mean(np.diff(n_sig_arr))
ns_bins = np.r_[n_sig_arr - 0.5*dns, n_sig_arr[-1] + 0.5*dns]
expect_kw = dict(color='C0', ls='--', lw=1, zorder=-10)
expect_gamma = tr.sig_injs[0].flux[0].gamma
ax = axs[0]
h = hl.hist((allt.ntrue, allt.ns), bins=(ns_bins, 100))
hl.plot1d(ax, h.contain_project(1),errorbands=True, drawstyle='default')
lim = ns_bins[[0, -1]]
ax.set_xlim(ax.set_ylim(lim))
ax.plot(lim, lim, **expect_kw)
ax.set_aspect('equal')

ax = axs[1]
h = hl.hist((allt.ntrue, allt.gamma), bins=(ns_bins, 100))
hl.plot1d(ax, h.contain_project(1),errorbands=True, drawstyle='default')
ax.axhline(expect_gamma, **expect_kw)
ax.set_xlim(axs[0].get_xlim())
for ax in axs:
    ax.set_xlabel(r'$n_{inj}$')
    ax.grid()
axs[0].set_ylabel(r'$n_s$')
axs[1].set_ylabel(r'$\gamma$')
    bg = cy.dists.Chi2TSD(cy.utils.Arrays(init=bg_arr))
else:
    print("computing background:")
    bg_arr = tr.get_many_fits(nscramble, seed=random.randint(1, 1000))
    bg = cy.dists.Chi2TSD(bg_arr)
    np.save(bkgfile_comb,bg_arr.as_array)
    
print(bg.description)
                              
################################################################################
# BACKGROUND PLOTTING (OPTIONAL)
################################################################################
if make_background_plot:
    fig, ax = plt.subplots()
    h = bg.get_hist(bins=30)
    hl.plot1d(ax, h, crosses=True, label='{} bg trials'.format(bg.n_total))
    x = h.centers[0]
    norm = h.integrate().values
    ax.semilogy(x, norm * bg.pdf(x), lw=1, ls='--',label=r'$\chi^2[{:.2f}\ dof,\ \eta={:.3f}]$'.format(bg.ndof, bg.eta))

    ax.set_xlabel(r'TS')
    ax.set_ylabel(r'number of trials')
    ax.legend()
    plt.tight_layout()
    plt.savefig(wrkdir+"background_plot_for_index_%s.png"%(index))

################################################################################
# SENSITIVITY
################################################################################
if compute_sig_trials:
    sens = tr.find_n_sig(
Esempio n. 6
0
    ax.grid(alpha=.5, ls='-')
    cb.set_label(clabel)
    ax.set_title(title)
    plt.tight_layout()
    return fig, ax


src_5 = cy.utils.Sources(ra=0, dec=0, extension=np.radians(5))

tr_5 = get_tr(src_5, ana7, sigsub=True)

bg_5 = cy.dists.Chi2TSD(tr_5.get_many_fits(10000))

fig, ax = plt.subplots()
h = bg_5.get_hist(bins=50)
hl.plot1d(ax, h.normalize(), label=r'$N_{s}=0$')  #, crosses=True)
x = np.linspace(.1, h.range[0][-1], 100)
ax.semilogy(x,
            bg_5.eta * bg_5.chi2.pdf(x),
            'k--',
            lw=1,
            label=r'$\chi_{2}^{2}$')
plt.xlabel(r'TS', fontsize=14)
plt.ylabel('Trials', fontsize=14)
#plt.ylim(0,30)
plt.title(r'$5^{\circ}$ source extension')
plt.legend()
plt.tight_layout()
ax.tick_params(axis='both', which='major', length=8, labelsize=10)
ax.tick_params(axis='both', which='minor', length=4)
plt.savefig('/data/user/drysewyk/llh_sandbox/Plots/Back_TS_Dist_5deg_SS.png')
Esempio n. 7
0
        sens = tr.find_n_sig(0,
                             .9,
                             batch_size=500,
                             max_batch_size=500,
                             seed=random.randint(0, 1e3),
                             mp_cpus=n_cpus)
        # N.B. must set E0 to a value within the energy_range
        # of the flux for this trial runner
        sens['E2dNdE'] = tr.to_E2dNdE(sens, E0=Emin)
        senss.append(sens)

np.save('/data/user/mkovacevich/FRB_analysis/DiffSens_gamma_' + str(gamma) +
        '_dt_' + str(dt) + '.npy',
        senss,
        allow_pickle=True)
'''
fluxs = [s['E2dNdE']/1e3 for s in senss] # convert GeV->TeV
diffsens = hl.Hist(Ebins, fluxs)
#diffsens

fig, ax = plt.subplots()
hl.plot1d(ax, diffsens)
ax.loglog()
ax.set(
    ylim=1e-3,
    xlabel=r'$E$ [GeV]',
    ylabel=r'$E^2\cdot dN/dE~~[\text{TeV}\,\text{cm}^2\,\text{}]$',
    title=r'Differential Sensitivity (dt = ' + str(dt) +' s and gamma = ' + str(gamma) +')'
)
ax.grid()
fig.savefig('/data/user/mkovacevich/FRB_analysis/DifferentialSensitivity_gamma_'+str(gamma)+'_dt_'+str(dt)+'.png')