コード例 #1
0
def create_pull_2d(pull_dict):
    mc = get_mc(pull_dict)
    pulls = get_pulls(mc)
    e_pdf = EnergyPDF.create(pull_dict["e_pdf_dict"])
    weights = e_pdf.weight_mc(mc)

    x_bins = np.linspace(2., 6., n_ebins)
    ymax = pull_dict["season"]["sinDec bins"][-1]
    ymin = pull_dict["season"]["sinDec bins"][0]
    # y_bins = np.linspace(ymin, ymax, 41)
    y_bins = pull_dict["season"]["sinDec bins"]
    x_range = 0.5 * (x_bins[1:] + x_bins[:-1])
    y_range = 0.5 * (y_bins[1:] + y_bins[:-1])

    z_range = np.ones([len(x_range), len(y_range)])

    for j, lower in enumerate(x_bins[:-1]):
        upper = x_bins[j + 1]
        mask = np.logical_and(
            mc["logE"] >= lower,
            mc["logE"] < upper
        )

        cut_mc = mc[mask]

        for k, lower_dec in enumerate(y_bins[:-1]):
            upper_dec = y_bins[k + 1]
            bin_mask = np.logical_and(
                cut_mc["sinDec"] >= lower_dec,
                cut_mc["sinDec"] < upper_dec
            )

            median_pull = weighted_quantile(
                pulls[mask][bin_mask], 0.5, weights[mask][bin_mask])

            z_range[j][k] = np.log(median_pull)

    # x_range = np.array([0.] + list(x_range) + [10.])
    # y_range = np.array([y_range[0]] + list(y_range) + [y_range[-1]])

    save_path = pull_pickle(pull_dict)

    res = [x_range, y_range, z_range]

    with open(save_path, "wb") as f:
        Pickle.dump(res, f)

    print("Saved to", save_path)

    plot_path = save_path[:-3] + "pdf"

    ax = plt.subplot(111)
    X, Y = np.meshgrid(x_bins, y_bins)
    cbar = ax.pcolor(X, Y, z_range.T, cmap="seismic",
                     vmax=1.0, vmin=-1.0)
    plt.colorbar(cbar, label="Log(Median Pull)")
    plt.ylabel(r"$\sin(\delta)$")
    plt.xlabel("Log(Energy proxy)")
    plt.savefig(plot_path)
    plt.close()
コード例 #2
0
ファイル: potemkin.py プロジェクト: icecube/flarestack
 def atmo_flux():
     bkg_e_pdf_dict = {
         "energy_pdf_name": "power_law",
         "gamma": 3.7,
         "e_min_gev": 100.0,
         "e_max_gev": 10.0**7,
     }
     return EnergyPDF.create(bkg_e_pdf_dict)
コード例 #3
0
ファイル: ccsn_helpers.py プロジェクト: afedynitch/flarestack
def get_cosmology(nu_e, rate, gamma):
    energy_pdf_dict = {
        'energy_pdf_name': 'power_law',
        'gamma': gamma,
    }
    energy_pdf = EnergyPDF.create(energy_pdf_dict)
    fluence_conversion = energy_pdf.fluence_integral() * u.GeV**2
    nu_e = nu_e.to("GeV") / fluence_conversion
    functions = define_cosmology_functions(rate, nu_e, gamma)
    return functions
コード例 #4
0
def create_quantile_floor_0d(floor_dict):
    mc = get_mc(floor_dict)
    e_pdf = EnergyPDF.create(floor_dict["e_pdf_dict"])
    weights = e_pdf.weight_mc(mc)

    quantile_floor = weighted_quantile(mc["raw_sigma"],
                                       floor_dict["floor_quantile"], weights)

    save_path = floor_pickle(floor_dict)

    with open(save_path, "wb") as f:
        Pickle.dump(quantile_floor, f)

    print("Saved to", save_path)
コード例 #5
0
def create_pull_0d_e(pull_dict):
    mc = get_mc(pull_dict)
    pulls = get_pulls(mc)
    e_pdf = EnergyPDF.create(pull_dict["e_pdf_dict"])
    # gamma_precision = pull_dict.get('gamma_precision', 'flarestack')
    gamma_precision = pull_dict["gamma_precision"]

    default, bounds, name = e_pdf.return_energy_parameters()

    if name != ["gamma"]:
        raise Exception(
            "Trying to scan gamma parameter, "
            "but selected energy pdf gave the following parameters:"
            " {} {} {}".format(name, default, bounds))

    res_dict = dict()

    x_range = np.array(
        sorted(list(get_gamma_support_points(precision=gamma_precision))))

    y_range = []

    for x in x_range:
        weights = e_pdf.weight_mc(mc, x)
        median_pull = weighted_quantile(pulls, 0.5, weights)
        y_range.append(median_pull)
        res_dict[x] = np.log(median_pull)

    y_range = np.array(y_range)

    save_path = pull_pickle(pull_dict)
    plot_path = save_path[:-3] + "pdf"

    # print x_range, y_range

    plt.figure()
    plt.plot(x_range, y_range)
    plt.axhline(1.0, linestyle="--")
    plt.ylabel("Median Pull")
    plt.xlabel(name[0])
    plt.savefig(plot_path)
    plt.close()

    with open(save_path, "wb") as f:
        Pickle.dump(res_dict, f)

    print("Saved to", save_path)
コード例 #6
0
def create_pull_1d(pull_dict):
    mc = get_mc(pull_dict)
    pulls = get_pulls(mc)
    e_pdf = EnergyPDF.create(pull_dict["e_pdf_dict"])
    weights = e_pdf.weight_mc(mc)

    bins = np.linspace(2., 6., n_ebins)

    x_range = 0.5 * (bins[1:] + bins[:-1])
    y_range = []

    for j, lower in enumerate(bins[:-1]):
        upper = bins[j + 1]
        mask = np.logical_and(
            mc["logE"] >= lower,
            mc["logE"] < upper
        )
        median_pull = weighted_quantile(
            pulls[mask], 0.5, weights[mask])
        y_range.append(median_pull)

    x_range = np.array([0.] + list(x_range) + [10.])
    y_range = np.array([y_range[0]] + list(y_range) + [y_range[-1]])

    save_path = pull_pickle(pull_dict)

    res = [x_range, np.log(y_range)]

    with open(save_path, "wb") as f:
        Pickle.dump(res, f)

    print("Saved to", save_path)

    plot_path = save_path[:-3] + "pdf"

    plt.figure()
    plt.plot(x_range, y_range)
    plt.axhline(1.0, linestyle="--")
    plt.ylabel("Median Pull")
    plt.xlabel("Log(Energy Proxy/GeV)")
    plt.savefig(plot_path)
    plt.close()
コード例 #7
0
ファイル: injector.py プロジェクト: icecube/flarestack
    def __init__(self, season, sources, **kwargs):

        kwargs = read_injector_dict(kwargs)
        self.inj_kwargs = kwargs

        logger.info("Initialising Injector for {0}".format(season.season_name))
        self.injection_band_mask = dict()
        self.season = season
        self.season.load_background_model()

        self.sources = sources

        if len(sources) > 0:
            self.weight_scale = calculate_source_weight(self.sources)

        try:
            self.sig_time_pdf = TimePDF.create(
                kwargs["injection_sig_time_pdf"], season.get_time_pdf()
            )
            # self.bkg_time_pdf = TimePDF.create(kwargs["injection_bkg_time_pdf"],
            #                                    season.get_time_pdf())
            self.energy_pdf = EnergyPDF.create(kwargs["injection_energy_pdf"])
            self.spatial_pdf = SpatialPDF(kwargs["injection_spatial_pdf"], season)
        except KeyError:
            raise Exception(
                "Injection Arguments missing. \n "
                "'injection_energy_pdf', 'injection_time_pdf',"
                "and 'injection_spatial_pdf' are required. \n"
                "Found: \n {0}".format(kwargs)
            )

        if "poisson_smear_bool" in list(kwargs.keys()):
            self.poisson_smear = kwargs["poisson_smear_bool"]
        else:
            self.poisson_smear = True

        self.n_exp = np.nan

        try:
            self.fixed_n = kwargs["fixed_n"]
        except KeyError:
            self.fixed_n = np.nan
コード例 #8
0
def create_quantile_floor_0d_e(floor_dict):
    mc = get_mc(floor_dict)
    e_pdf = EnergyPDF.create(floor_dict["e_pdf_dict"])

    default, bounds, name = e_pdf.return_energy_parameters()

    if len(name) != 1:
        raise Exception(
            "Trying to scan just one energy parameter, "
            "but selected energy pdf gave the following parameters:"
            " {} {} {}".format(name, default, bounds))

    x_range = np.linspace(bounds[0][0], bounds[0][1], n_step)
    y_range = []

    for x in x_range:
        weights = e_pdf.weight_mc(mc, x)
        quantile_floor = weighted_quantile(mc["raw_sigma"],
                                           floor_dict["floor_quantile"],
                                           weights)
        y_range.append(quantile_floor)

    y_range = np.array(y_range)

    save_path = floor_pickle(floor_dict)

    res = [x_range, np.log(y_range)]

    with open(save_path, "wb") as f:
        Pickle.dump(res, f)

    print("Saved to", save_path)

    plot_path = floor_pickle(floor_dict)[:-3] + "pdf"

    plt.figure()
    plt.plot(x_range, np.degrees(y_range))
    plt.savefig(plot_path)
    plt.close()
コード例 #9
0
def create_quantile_floor_1d(floor_dict):

    mc = get_mc(floor_dict)
    e_pdf = EnergyPDF.create(floor_dict["e_pdf_dict"])
    weights = e_pdf.weight_mc(mc)

    bins = np.linspace(2., 6., 30)

    x_range = 0.5 * (bins[1:] + bins[:-1])
    y_range = []

    for j, lower in enumerate(bins[:-1]):
        upper = bins[j + 1]
        mask = np.logical_and(
            mc["logE"] >= lower,
            mc["logE"] < upper
        )
        quantile_floor = weighted_quantile(
            mc["raw_sigma"][mask], floor_dict["floor_quantile"], weights[mask])

        y_range.append(quantile_floor)

    x_range = np.array([0.] + list(x_range) + [10.])
    y_range = np.array([y_range[0]] + list(y_range) + [y_range[-1]])

    save_path = floor_pickle(floor_dict)
    res = [x_range, np.log(y_range)]

    with open(save_path, "wb") as f:
        Pickle.dump(res, f)
    print("Saved to", save_path)

    plot_path = floor_pickle(floor_dict)[:-3] + "pdf"

    plt.figure()
    plt.plot(x_range, np.degrees(y_range))
    plt.savefig(plot_path)
    plt.close()
コード例 #10
0
import unittest
import pickle
import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline
from flarestack.data.public import icecube_ps_3_year
from flarestack.core.unblinding import create_unblinder
from flarestack.core.energy_pdf import EnergyPDF
from flarestack.utils.prepare_catalogue import ps_catalogue_name
from flarestack.shared import energy_spline_dir

logger = logging.getLogger()
logger.setLevel("ERROR")

base_energy_pdf = {"energy_pdf_name": "power_law", "gamma": 3.0}

g = EnergyPDF.create(base_energy_pdf)

n_steps = 1e3

e_range = np.logspace(0, 7, int(n_steps))

f = InterpolatedUnivariateSpline(e_range, np.log(g.f(e_range)))

spline_save_path = "{0}e_2_power_law_{1}.npy".format(energy_spline_dir,
                                                     n_steps)

logging.info("Saving to {0}".format(spline_save_path))

with open(spline_save_path, "wb") as h:
    pickle.dump(f, h)
コード例 #11
0
ファイル: simcube.py プロジェクト: afedynitch/flarestack
 def muon_bundle_flux():
     e_pdf_dict = {
         "energy_pdf_name": "power_law",
         "gamma": 3.0,
     }
     return EnergyPDF.create(e_pdf_dict)
コード例 #12
0
ファイル: simcube.py プロジェクト: afedynitch/flarestack
 def atmo_flux():
     e_pdf_dict = {
         "energy_pdf_name": "power_law",
         "gamma": 3.7,
     }
     return EnergyPDF.create(e_pdf_dict)
コード例 #13
0
def calculate_transient_cosmology(e_pdf_dict,
                                  rate,
                                  name,
                                  zmax=8.,
                                  nu_bright_fraction=1.0,
                                  diffuse_fraction=None,
                                  diffuse_fit="joint_15"):

    e_pdf_dict = read_e_pdf_dict(e_pdf_dict)

    diffuse_flux, diffuse_gamma = get_diffuse_flux_at_1GeV(diffuse_fit)

    logger.info("Using the {0} best fit values of the diffuse flux.".format(
        diffuse_fit))
    # print "Raw Diffuse Flux at 1 GeV:", diffuse_flux / (4 * np.pi * u.sr)
    logger.info("Diffuse Flux at 1 GeV: {0}".format(diffuse_flux))
    logger.info("Diffuse Spectral Index is {0}".format(diffuse_gamma))

    if "gamma" not in e_pdf_dict:
        logger.warning(
            "No spectral index has been specified. "
            "Assuming source has spectral index matching diffuse flux")
        e_pdf_dict["gamma"] = diffuse_gamma

    savedir = plots_dir + "cosmology/" + name + "/"

    try:
        os.makedirs(savedir)
    except OSError:
        pass

    energy_pdf = EnergyPDF.create(e_pdf_dict)
    fluence_conversion = energy_pdf.fluence_integral() * u.GeV**2

    if "nu_flux_at_1_gev" in e_pdf_dict.keys():
        nu_flux_at_1_gev = e_pdf_dict["nu_flux_at_1_gev"].to("GeV-1")
        nu_e = (nu_flux_at_1_gev * fluence_conversion).to("erg")

    else:
        nu_e = e_pdf_dict["source_energy_erg"]
        nu_flux_at_1_gev = nu_e.to("GeV") / fluence_conversion

    gamma = e_pdf_dict["gamma"]

    logger.info(f"Neutrino Energy is {nu_e:.2g}")
    logger.info(f"Neutrino Flux at 1 GeV is {nu_flux_at_1_gev:.2g}")
    logger.info(f"Local rate is {rate(0.0):.2g}")

    zrange, step = np.linspace(0.0, zmax, int(1 + 1e3), retstep=True)

    rate_per_z, nu_flux_per_z, nu_flux_per_source, cumulative_nu_flux = \
        define_cosmology_functions(rate, nu_flux_at_1_gev, gamma, nu_bright_fraction)

    logger.info(
        f"Cumulative sources at z=8.0: {cumulative_z(rate_per_z, 8.0)[-1].value:.2g}"
    )

    nu_at_horizon = cumulative_nu_flux(8)[-1]

    logger.info(f"Cumulative flux at z=8.0 (1 GeV): {nu_at_horizon:.2g}")
    logger.info(
        f"Cumulative annual flux at z=8.0 (1 GeV): {(nu_at_horizon * u.yr).to('GeV-1 cm-2 sr-1'):.2g}"
    )

    ratio = nu_at_horizon.value / diffuse_flux.value
    logger.info(f"Fraction of diffuse flux at 1GeV: {ratio:.2g}")
    logger.info(f"Cumulative neutrino flux {nu_at_horizon:.2g}")
    logger.debug(f"Diffuse neutrino flux {diffuse_flux:.2g}")

    if diffuse_fraction is not None:
        logger.info(
            f"Scaling flux so that, at z=8, the contribution is equal to {diffuse_fraction:.2g}"
        )
        nu_flux_at_1_gev *= diffuse_fraction / ratio
        logger.info(
            f"Neutrino Energy rescaled to {(nu_flux_at_1_gev * fluence_conversion).to('erg'):.2g}"
        )

    plt.figure()
    plt.plot(zrange, rate(zrange))
    plt.yscale("log")
    plt.xlabel("Redshift")
    plt.ylabel(r"Rate [Mpc$^{-3}$ year$^{-1}$]")
    plt.tight_layout()
    plt.savefig(savedir + 'rate.pdf')
    plt.close()

    plt.figure()
    plt.plot(zrange, rate_per_z(zrange) / rate(zrange))
    plt.yscale("log")
    plt.xlabel("Redshift")
    plt.ylabel(r"Differential Comoving Volume [Mpc$^{3}$ dz]")
    plt.tight_layout()
    plt.savefig(savedir + 'comoving_volume.pdf')
    plt.close()

    logger.debug("Sanity Check:")
    logger.debug("Integrated Source Counts \n")

    for z in [0.01, 0.08, 0.1, 0.2, 0.3, 0.7, 8]:
        logger.debug("{0}, {1}, {2}".format(z,
                                            Distance(z=z).to("Mpc"),
                                            cumulative_z(rate_per_z, z)[-1]))

    for nearby in [0.1, 0.3]:

        logger.info(
            f"Fraction of flux from nearby (z<{nearby}) sources: {cumulative_nu_flux(nearby)[-1] / nu_at_horizon:.2g}"
        )

    plt.figure()
    plt.plot(zrange, rate_per_z(zrange))
    plt.yscale("log")
    plt.ylabel("Differential Source Rate [year$^{-1}$ dz]")
    plt.xlabel("Redshift")
    plt.tight_layout()
    plt.savefig(savedir + 'diff_source_count.pdf')
    plt.close()

    plt.figure()
    plt.plot(zrange[1:-1], [x.value for x in cumulative_z(rate_per_z, zrange)])
    plt.yscale("log")
    plt.ylabel("Cumulative Sources")
    plt.xlabel("Redshift")
    plt.tight_layout()
    plt.savefig(savedir + 'integrated_source_count.pdf')
    plt.close()

    plt.figure()
    plt.plot(zrange[1:-1], nu_flux_per_z(zrange[1:-1]))
    plt.yscale("log")
    plt.xlabel("Redshift")
    plt.tight_layout()
    plt.savefig(savedir + 'diff_vol_contribution.pdf')
    plt.close()

    cum_nu = [x.value for x in cumulative_nu_flux(zrange)]

    plt.figure()
    plt.plot(zrange[1:-1], cum_nu)
    plt.yscale("log")
    plt.xlabel("Redshift")
    plt.ylabel(r"Cumulative Neutrino Flux [ GeV$^{-1}$ cm$^{-2}$ s$^{-1}$ ]")
    plt.axhline(y=diffuse_flux.value, color="red", linestyle="--")
    plt.tight_layout()
    plt.savefig(savedir + 'int_nu_flux_contribution.pdf')
    plt.close()

    plt.figure()
    plt.plot(zrange[1:-1], [nu_flux_per_z(z).value for z in zrange[1:-1]])
    plt.yscale("log")
    plt.xlabel("Redshift")
    plt.ylabel(
        r"Differential Neutrino Flux [ GeV$^{-1}$ cm$^{-2}$ s$^{-1}$ dz]")
    plt.axhline(y=diffuse_flux.value, color="red", linestyle="--")
    plt.tight_layout()
    plt.savefig(savedir + 'diff_nu_flux_contribution.pdf')
    plt.close()

    plt.figure()
    plt.plot(zrange[1:-1],
             [(nu_flux_per_source(z)).value for z in zrange[1:-1]])
    plt.yscale("log")
    plt.xlabel("Redshift")
    plt.ylabel(r"Time-Integrated Flux per Source [ GeV$^{-1}$ cm$^{-2}$]")
    plt.tight_layout()
    plt.savefig(savedir + 'nu_flux_per_source_contribution.pdf')
    plt.close()

    return nu_at_horizon
コード例 #14
0
ファイル: potemkin.py プロジェクト: icecube/flarestack
    def simulate_dec_range(self, fluence, lower_sin_dec, upper_sin_dec):
        mean_sin_dec = 0.5 * (lower_sin_dec + upper_sin_dec)

        solid_angle = 2 * np.pi * (upper_sin_dec - lower_sin_dec)
        sim_fluence = fluence * solid_angle  # GeV^-1 cm^-2

        effective_area_f = self.load_effective_area()

        def source_eff_area(e):
            return effective_area_f(
                np.log10(e), mean_sin_dec) * self.bkg_flux_model.flux_model_f(
                    e, mean_sin_dec)

        lower, upper = self.bkg_flux_model.flux_range()

        logger.debug(f"Simulating between {lower:.2g} GeV and {upper:.2g} GeV")

        int_eff_a = EnergyPDF.integrate(source_eff_area, lower, upper)

        # Effective areas are given in m2, but flux is in per cm2

        int_eff_a *= 10**4

        n_exp = sim_fluence * int_eff_a

        n_sim = np.random.poisson(n_exp)

        new_events = np.empty((n_sim, ), dtype=self.event_dtype)

        new_events["ra"] = [random.random() * 2 * np.pi for _ in range(n_sim)]

        new_events["sinDec"] = [
            lower_sin_dec + (random.random() * (upper_sin_dec - lower_sin_dec))
            for _ in range(n_sim)
        ]
        new_events["dec"] = np.arcsin(new_events["sinDec"])
        new_events["time"] = self.get_time_pdf().simulate_times([], n_sim)

        fluence_ints, log_e_range = EnergyPDF.piecewise_integrate(
            source_eff_area, lower, upper)

        fluence_ints = fluence_ints

        fluence_ints = np.array(fluence_ints)
        fluence_ints /= np.sum(fluence_ints)

        fluence_cumulative = [
            np.sum(fluence_ints[:i]) for i, _ in enumerate(fluence_ints)
        ]

        fluence_cumulative = [0.0] + fluence_cumulative + [1.0]

        log_e_range = list(log_e_range) + [log_e_range[-1]]

        sim_true_e = interp1d(fluence_cumulative, log_e_range)

        true_e_vals = np.array(
            [10.0**sim_true_e(random.random()) for _ in range(n_sim)])

        new_events["logE"] = self.energy_proxy_map(true_e_vals)

        angular_res_f = self.load_angular_resolution()

        new_events["sigma"] = angular_res_f(new_events["logE"]).copy()
        new_events["raw_sigma"] = new_events["sigma"].copy()

        return new_events
コード例 #15
0
def calculate_astronomy(flux, e_pdf_dict, catalogue):

    flux /= (u.GeV * u.cm**2 * u.s)

    energy_PDF = EnergyPDF.create(e_pdf_dict)

    astro_res = dict()

    phi_integral = energy_PDF.flux_integral() * u.GeV

    e_integral = energy_PDF.fluence_integral() * u.GeV**2

    # Calculate fluence

    tot_fluence = (flux * e_integral)

    astro_res["Energy Flux (GeV cm^{-2} s^{-1})"] = tot_fluence.value

    logger.debug("Energy Flux:{0}".format(tot_fluence))

    src_1 = np.sort(catalogue, order="distance_mpc")[0]

    frac = calculate_source_weight(src_1) / calculate_source_weight(catalogue)

    si = flux * frac

    astro_res["Flux from nearest source"] = si

    logger.debug("Total flux: {0}".format(flux))
    logger.debug("Fraction from nearest source: {0}".format(frac))
    logger.debug("Flux from nearest source: {0}".format(flux * frac))

    lumdist = src_1["distance_mpc"] * u.Mpc

    area = (4 * math.pi * (lumdist.to(u.cm))**2)

    dNdA = (si * phi_integral).to(u.s**-1 * u.cm**-2)

    # int_dNdA += dNdA

    N = dNdA * area

    logger.debug("There would be {:.3g} neutrinos emitted.".format(N))
    logger.debug(
        "The energy range was assumed to be between {0} and {1}".format(
            energy_PDF.integral_e_min, energy_PDF.integral_e_max))
    # Energy requires a 1/(1+z) factor

    zfactor = find_zfactor(lumdist)
    etot = (si * area * e_integral).to(u.erg / u.s) * zfactor

    astro_res["Mean Luminosity (erg/s)"] = etot.value

    logger.debug("The required neutrino luminosity was {0}".format(etot))

    cr_e = etot / f_cr_to_nu

    logger.debug(
        "Assuming {0:.3g}% was transferred from CR to neutrinos, we would require a total CR luminosity of {1}"
        .format(100 * f_cr_to_nu, cr_e))

    return astro_res
コード例 #16
0
    },
    "IIP": {
        "Fixed": 389.757926076,
        "Fit": 436.534453846
    }
}

# Limits are quoted assuming a reference distance of 1 Mpc for a source

ref_dist = 1 * u.Mpc

# Limits are calculated assuming the following Energy PDF

e_pdf_dict = {"energy_pdf_name": "power_law", "gamma": 2.5}

energy_PDF = EnergyPDF.create(e_pdf_dict)
e_integral = energy_PDF.fluence_integral() * u.GeV**2

for (sn, sn_dict) in limits.items():
    for llh_type in ["Fixed", "Fit"]:
        area = (ref_dist.to("cm"))**2
        energy = (sn_dict[llh_type] * 4 * np.pi * u.sr * limit_units * area *
                  e_integral).to("erg")
        sn_dict[llh_type + " Energy (erg)"] = energy

dt = {'names': ['t', 'E'], 'formats': ['<f8', '<f8']}
dt_pvals = {'names': ['t', 'pval'], 'formats': ['<f8', '<f8']}

p_vals = {
    'box': {
        'Ibc':
コード例 #17
0
def create_pull_1d_e(floor_dict):

    mc = get_mc(floor_dict)
    pulls = get_pulls(mc)
    e_pdf = EnergyPDF.create(floor_dict["e_pdf_dict"])
    gamma_precision = floor_dict.get("gamma_precision", "flarestack")

    default, bounds, name = e_pdf.return_energy_parameters()

    if name != ["gamma"]:
        raise Exception(
            "Trying to scan gamma parameter, "
            "but selected energy pdf gave the following parameters:"
            " {} {} {}".format(name, default, bounds))

    e_range = np.array(
        sorted(list(get_gamma_support_points(precision=gamma_precision))))

    bins = np.linspace(2.0, 6.0, 5)
    x_range = 0.5 * (bins[1:] + bins[:-1])
    x_range = np.array([0.0] + list(x_range) + [10.0])

    res_dict = dict()

    z_range = []

    for e in sorted(e_range):
        weights = e_pdf.weight_mc(mc, e)

        vals = []

        for j, lower in enumerate(bins[:-1]):
            upper = bins[j + 1]
            mask = np.logical_and(mc["logE"] >= lower, mc["logE"] < upper)

            median_pull = weighted_quantile(pulls[mask], 0.5, weights[mask])

            vals.append(median_pull)

        vals = [vals[0]] + vals + [vals[-1]]

        z_range.append(vals)
        res_dict[e] = [x_range, np.log(vals)]

    z_range = np.array(z_range)

    # z_range = np.vstack((z_range.T[0], z_range.T)).T
    #
    # z_range = np.vstack((z_range.T, z_range.T[-1])).T

    save_path = pull_pickle(floor_dict)

    # res = [x_range, e_range, z_range]

    with open(save_path, "wb") as f:
        Pickle.dump(res_dict, f)
    print("Saved to", save_path)

    plot_path = save_path[:-3] + "pdf"

    ax = plt.subplot(111)
    X, Y = np.meshgrid(x_range, e_range)
    # cbar = ax.pcolor(X, Y, np.log(np.degrees(z_range.T)), cmap="viridis", )
    cbar = ax.pcolor(X,
                     Y,
                     np.log(z_range),
                     cmap="seismic",
                     vmax=1.0,
                     vmin=-1.0)
    plt.colorbar(cbar, label="Log(Median Pull)")
    plt.ylabel(name[0])
    plt.xlabel("Log(Energy proxy)")
    plt.savefig(plot_path)
    plt.close()
コード例 #18
0
def create_quantile_floor_1d_e(floor_dict):

    mc = get_mc(floor_dict)
    e_pdf = EnergyPDF.create(floor_dict["e_pdf_dict"])

    default, bounds, name = e_pdf.return_energy_parameters()

    if name != ["gamma"]:
        raise Exception(
            "Trying to scan gamma parameter, "
            "but selected energy pdf gave the following parameters:"
            " {} {} {}".format(name, default, bounds))

    e_range = np.linspace(bounds[0][0], bounds[0][1], n_step)

    bins = np.linspace(2.0, 6.0, 20)

    z_range = []

    for j, lower in enumerate(bins[:-1]):
        upper = bins[j + 1]
        mask = np.logical_and(mc["logE"] >= lower, mc["logE"] < upper)

        cut_mc = mc[mask]

        vals = []

        for e in e_range:
            weights = e_pdf.weight_mc(cut_mc, e)

            quantile_floor = weighted_quantile(cut_mc["raw_sigma"],
                                               floor_dict["floor_quantile"],
                                               weights)

            vals.append(quantile_floor)

        z_range.append(vals)

    x_range = 0.5 * (bins[1:] + bins[:-1])
    x_range = np.array([0.0] + list(x_range) + [10.0])

    z_range = np.array([z_range[0]] + z_range + [z_range[-1]])

    save_path = floor_pickle(floor_dict)
    res = [x_range, e_range, np.log(z_range)]

    with open(save_path, "wb") as f:
        Pickle.dump(res, f)
    print("Saved to", save_path)

    from scipy.interpolate import RectBivariateSpline

    spline = RectBivariateSpline(x_range,
                                 e_range,
                                 np.log(np.degrees(z_range)),
                                 kx=1,
                                 ky=1,
                                 s=0)

    Z = []
    for x in x_range:
        Z.append(spline(x, e_range)[0])
    Z = np.array(Z)

    plot_path = floor_pickle(floor_dict)[:-3] + "pdf"

    ax = plt.subplot(111)
    X, Y = np.meshgrid(x_range, e_range)
    # cbar = ax.pcolor(X, Y, np.log(np.degrees(z_range.T)), cmap="viridis", )
    cbar = ax.pcolor(
        X,
        Y,
        Z.T,
        cmap="viridis",
    )
    plt.colorbar(cbar, label="Log(Angular Error Floor/deg)")
    plt.ylabel(name[0])
    plt.xlabel("Log(Energy proxy)")
    plt.savefig(plot_path)
    plt.close()
コード例 #19
0
    print(norm.cdf(1.0))

    def symmetric_gauss(sigma):
        return 1 - 2 * norm.sf(sigma)

    def gauss_2d(sigma):
        # return symmetric_gauss(sigma) ** 2
        return symmetric_gauss(sigma)

    print(symmetric_gauss(1.0))
    print(gauss_2d(1.0))
    print(gauss_2d(1.177))

    e_pdf_dict = {"Name": "Power Law", "Gamma": 3.0}

    e_pdf = EnergyPDF.create(e_pdf_dict)

    pc = BaseAngularErrorModifier.create(IC86_1_dict, e_pdf_dict, "no_floor",
                                         "median_2d")
    mc, x, y = get_data(IC86_1_dict)[:10]

    pulls = x / y

    weights = e_pdf.weight_mc(mc)

    median_pull = weighted_quantile(pulls, 0.5, weights)

    def med_pull(data):
        y = np.degrees(data["sigma"])
        pulls = x / y
        med = weighted_quantile(pulls, 0.5, weights)
コード例 #20
0
def create_pull_2d_e(pull_dict):
    save_path = pull_pickle(pull_dict)
    base_dir = save_path[:-3] + "/"

    try:
        os.makedirs(base_dir)
    except OSError:
        pass

    mc = get_mc(pull_dict)
    pulls = get_pulls(mc)
    e_pdf = EnergyPDF.create(pull_dict["e_pdf_dict"])
    gamma_precision = pull_dict.get("gamma_precision", "flarestack")

    x_bins = np.linspace(2.0, 6.0, n_ebins)
    ymax = pull_dict["season"]["sinDec bins"][-1]
    ymin = pull_dict["season"]["sinDec bins"][0]
    y_bins = np.linspace(ymin, ymax, 11)

    x_range = 0.5 * (x_bins[1:] + x_bins[:-1])
    y_range = 0.5 * (y_bins[1:] + y_bins[:-1])

    res_dict = dict()

    e_range = np.array(
        sorted(list(get_gamma_support_points(precision=gamma_precision))))

    for e in e_range:

        z_range = np.ones([len(x_range), len(y_range)])

        weights = e_pdf.weight_mc(mc, e)

        for j, lower in enumerate(x_bins[:-1]):
            upper = x_bins[j + 1]
            mask = np.logical_and(mc["logE"] >= lower, mc["logE"] < upper)

            cut_mc = mc[mask]

            for k, lower_dec in enumerate(y_bins[:-1]):
                upper_dec = y_bins[k + 1]
                bin_mask = np.logical_and(cut_mc["sinDec"] >= lower_dec,
                                          cut_mc["sinDec"] < upper_dec)

                median_pull = weighted_quantile(pulls[mask][bin_mask], 0.5,
                                                weights[mask][bin_mask])

                z_range[j][k] = np.log(median_pull)
        res_dict[e] = [x_range, y_range, z_range]

        plot_path = base_dir + str(e) + ".pdf"

        ax = plt.subplot(111)
        X, Y = np.meshgrid(x_bins, y_bins)
        cbar = ax.pcolor(X, Y, z_range.T, cmap="seismic", vmax=1.0, vmin=-1.0)
        plt.colorbar(cbar, label="Log(Median Pull)")
        plt.ylabel(r"$\sin(\delta)$")
        plt.xlabel("Log(Energy proxy)")
        plt.savefig(plot_path)
        plt.close()

    save_path = pull_pickle(pull_dict)

    with open(save_path, "wb") as f:
        Pickle.dump(res_dict, f)

    print("Saved to", save_path)