Beispiel #1
0
def test_coordinate_transforms(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)
    ptyphims = ef.ptyphims_from_p4s(p4s)
    new_p4s = ef.p4s_from_ptyphims(ptyphims)

    assert epsilon_diff(p4s, new_p4s, 1e-11)
Beispiel #2
0
def test_efp_kappa_hadr(event, beta, kappa, normed, kappa_normed_behavior):

    asymbox_graph = [(0,1),(0,1),(1,2),(1,2),(1,2),(2,3),(2,3),(2,3),(2,3),(3,0),(0,3),(3,0)]
    efp_result = ef.EFP(asymbox_graph, measure='hadr', beta=beta, kappa=kappa,
                                       normed=normed, coords='epxpypz',
                                       kappa_normed_behavior=kappa_normed_behavior).compute(event)

    pts, ys, phis, ms = ef.ptyphims_from_p4s(event).T
    thetas = np.asarray([[(ys[i]-ys[j])**2 + min(abs(phis[i]-phis[j]), 2*np.pi-abs(phis[i]-phis[j]))**2
                          for i in range(len(event))] for j in range(len(event))])**(beta/2)

    zs = pts**kappa
    if normed and kappa_normed_behavior == 'orig':
        zs /= np.sum(zs)
    if normed and kappa_normed_behavior == 'new':
        zs /= np.sum(pts)**kappa

    asymbox = 0
    for i1 in range(len(zs)):
        for i2 in range(len(zs)):
            for i3 in range(len(zs)):
                for i4 in range(len(zs)):
                    asymbox += (zs[i1]*zs[i2]*zs[i3]*zs[i4]*thetas[i1,i2]**2*
                                thetas[i2,i3]**3*thetas[i3,i4]**4*thetas[i1,i4]**3)

    assert epsilon_percent(asymbox, efp_result, epsilon=10**-13)
Beispiel #3
0
def test_etas_from_pts_ys_ms(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)
    ptyphims = ef.ptyphims_from_p4s(p4s)

    etas = ef.etas_from_p4s(p4s)
    etas_primes = ef.etas_from_pts_ys_ms(ptyphims[..., 0], ptyphims[..., 1],
                                         ptyphims[..., 3])

    assert epsilon_diff(etas, etas_primes, 1e-12)

    # test cutoff
    pts, ms = 1000 * np.random.rand(25), 10 * np.random.rand(25)
    ys = np.random.choice([-1., 1.], size=25) * 100 * np.random.rand(25)

    for c1, c2 in zip(np.linspace(20, 100, 5), 20 + 80 * np.random.rand(5)):
        etas_c1 = ef.etas_from_pts_ys_ms(pts, ys, ms, _cutoff=c1)
        etas_c2 = ef.etas_from_pts_ys_ms(pts, ys, ms, _cutoff=c2)

        assert epsilon_diff(etas_c1, etas_c2, 1e-12)
Beispiel #4
0
def test_shapes_from_ptyphis(method, nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)
    ptyphims = ef.ptyphims_from_p4s(p4s)

    func = getattr(ef, method)

    if 'ms' in method:
        for end in [3, 4]:
            results = func(ptyphims[..., :end])

            assert epsilon_diff(results[0], func(ptyphims[0, ..., :end]))

    elif 'pids' in method:
        ptyphims[..., 3] = (
            np.random.choice([-1., 1.], size=(nevents, nparticles)) *
            np.random.choice(list(
                ef.utils.particle_utils.PARTICLE_MASSES.keys()),
                             size=(nevents, nparticles)))
        results = func(ptyphims)

        assert epsilon_diff(results[0], func(ptyphims[0]))
Beispiel #5
0
def test_sum_ptyphims(nparticles, scheme):
    p4s = ef.gen_random_events(10, nparticles, dim=4, mass='random')
    ptyphims = ef.ptyphims_from_p4s(p4s)

    if scheme == 'escheme':

        for ev_p4s, ev_ptyphims in zip(p4s, ptyphims):
            tot = ef.p4s_from_ptyphims(
                ef.sum_ptyphims(ev_ptyphims, scheme=scheme))
            tot_p4 = ev_p4s.sum(axis=0)

            assert epsilon_diff(tot, tot_p4, 10**-12)

    elif scheme == 'ptscheme':

        for ev_ptyphims in ptyphims:
            tot = ef.sum_ptyphims(ev_ptyphims, scheme=scheme)

            pt = ev_ptyphims[:, 0].sum()
            y = np.sum(ev_ptyphims[:, 0] * ev_ptyphims[:, 1]) / pt
            phi = np.sum(ev_ptyphims[:, 0] * ev_ptyphims[:, 2]) / pt

            assert epsilon_diff(tot, np.array([pt, y, phi]), 10**-12)
Beispiel #6
0
    def fit_pfn(self, model_settings):

        # convert labels to categorical
        Y_PFN = energyflow.utils.to_categorical(self.y, num_classes=2)

        # preprocess by centering jets and normalizing pts
        X_PFN = self.X_particles
        for x_PFN in X_PFN:
            mask = x_PFN[:, 0] > 0
            yphi_avg = np.average(x_PFN[mask, 1:3],
                                  weights=x_PFN[mask, 0],
                                  axis=0)
            x_PFN[mask, 1:3] -= yphi_avg
            x_PFN[mask, 0] /= x_PFN[:, 0].sum()

        # Convert (E,px,py,pz) to (pT,y,phi,m)
        X_PFN = energyflow.ptyphims_from_p4s(
            self.X_particles
        )[:, :, :]  # Note: 4th entry is m, not PID .. could rewrite routine

        # handle particle id channel !! Note: If changed to pT,y,phi,m the 4th component is not PID but m .. fix later
        #if model_settings['use_pids']:
        #    self.my_remap_pids(X_PFN)
        #else:
        #    X_PFN = X_PFN[:,:,:3]

        # Split data into train, val and test sets
        (X_PFN_train, X_PFN_val, X_PFN_test, Y_PFN_train, Y_PFN_val,
         Y_PFN_test) = energyflow.utils.data_split(X_PFN,
                                                   Y_PFN,
                                                   val=self.n_val,
                                                   test=self.n_test)
        # build architecture
        pfn = energyflow.archs.PFN(input_dim=X_PFN.shape[-1],
                                   Phi_sizes=model_settings['Phi_sizes'],
                                   F_sizes=model_settings['F_sizes'])

        # train model
        pfn.fit(X_PFN_train,
                Y_PFN_train,
                epochs=model_settings['epochs'],
                batch_size=model_settings['batch_size'],
                validation_data=(X_PFN_val, Y_PFN_val),
                verbose=1)

        # get predictions on test data
        preds_PFN = pfn.predict(X_PFN_test, batch_size=1000)

        # Get AUC and ROC curve + make plot
        auc_PFN = sklearn.metrics.roc_auc_score(Y_PFN_test[:, 1], preds_PFN[:,
                                                                            1])
        print('Particle Flow Networks/Deep Sets: AUC = {} (test set)'.format(
            auc_PFN))

        self.roc_curve_dict['PFN'] = sklearn.metrics.roc_curve(
            Y_PFN_test[:, 1], preds_PFN[:, 1])

        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

        # Now we compare the PFN ROC curve to single observables

        # 1. Jet mass (Note: This takes in (pt,y,phi) and converts it to 4-vectors and computes jet mass)
        #             (Note: X_PFN_train is centered and normalized .. should be ok)
        masses = np.asarray([
            energyflow.ms_from_p4s(
                energyflow.p4s_from_ptyphims(x).sum(axis=0))
            for x in X_PFN_train
        ])
        self.roc_curve_dict['Jet_mass'] = sklearn.metrics.roc_curve(
            Y_PFN_train[:, 1], -masses)

        # 2. Multiplicity (Is this a useful observable for pp vs AA?)
        mults = np.asarray([np.count_nonzero(x[:, 0]) for x in X_PFN_train])
        self.roc_curve_dict['Multiplicity'] = sklearn.metrics.roc_curve(
            Y_PFN_train[:, 1], -mults)