Ejemplo n.º 1
0
def test_ys_from_p4s(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)
    ys = 0.5 * np.log(
        (p4s[..., 0] + p4s[..., 3]) / (p4s[..., 0] - p4s[..., 3]))

    assert epsilon_diff(ys, ef.ys_from_p4s(p4s))
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def test_etas_from_p4s(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)

    p3tots = np.linalg.norm(p4s[..., 1:], axis=-1)
    etas = 0.5 * np.log((p3tots + p4s[..., 3]) / (p3tots - p4s[..., 3]))

    assert epsilon_diff(etas, ef.etas_from_p4s(p4s), 1e-13)
Ejemplo n.º 4
0
def test_efm_batch_compute(v, M, measure, normed):
    events = ef.gen_random_events(2, M)
    e = ef.EFM(v, measure=measure, normed=normed, coords='epxpypz')

    r1 = [e.compute(event) for event in events]
    r2 = e.batch_compute(events)

    assert epsilon_percent(r1, r2, 10**-1)
Ejemplo n.º 5
0
def test_gen_random_events(nevents, nparticles, dim, mass):
    if mass == 'array':
        mass = np.random.rand(nevents, nparticles)

    events = ef.gen_random_events(nevents, nparticles, dim=dim, mass=mass)

    assert events.shape == (nevents, nparticles, dim)
    if mass != 'random':
        assert epsilon_diff(ef.ms_from_ps(events)**2, mass**2, 10**-13)
Ejemplo n.º 6
0
def test_ms_from_ps(dim, nevents, nparticles):
    masses = np.random.rand(nevents, nparticles)
    events = ef.gen_random_events(nevents, nparticles, mass=masses, dim=dim)
    masses = masses.reshape(events.shape[:-1])

    results = ef.ms_from_ps(events)
    assert epsilon_diff(results, masses, 1e-10)

    if dim == 4:
        assert epsilon_diff(results, ef.ms_from_p4s(events), 1e-10)
Ejemplo n.º 7
0
def test_gen_random_events(nevents, nparticles, dim, mass):
    if mass == 'array':
        mass = np.random.rand(nevents, nparticles)

    events = ef.gen_random_events(nevents, nparticles, dim=dim, mass=mass)

    assert events.shape == (nevents, nparticles, dim)

    if not (isinstance(mass, six.string_types) and mass == 'random'):
        assert epsilon_diff(ef.ms_from_ps(events)**2, mass**2, 10**-13)
Ejemplo n.º 8
0
def test_batch_compute_vs_compute(measure, beta, kappa, normed):
    if measure == 'hadr' and kappa == 'pf':
        pytest.skip('hadr does not do pf')
    if 'efm' in measure and beta != 2:
        pytest.skip('only test efm when beta=2')
    events = ef.gen_random_events(10, 15)
    s = ef.EFPSet('d<=6', measure=measure, beta=beta, kappa=kappa, normed=normed)
    r_batch = s.batch_compute(events)
    r = np.asarray([s.compute(event) for event in events])
    assert epsilon_percent(r_batch, r, 10**-14)
Ejemplo n.º 9
0
def test_shapes_from_p4s(method, nevents, nparticles):
    events = ef.gen_random_events(nevents, nparticles, dim=4,
                                  mass='random').reshape(
                                      nevents, nparticles, 4)
    event, particle = events[0], events[0, 0]

    func = getattr(ef, method)
    results = func(events)

    assert epsilon_diff(results[0], func(event))
    assert epsilon_diff(results[0, 0], func(particle))
Ejemplo n.º 10
0
def test_batch_compute_vs_compute(measure, beta, kappa, normed):
    if measure == 'hadr' and kappa == 'pf':
        pytest.skip('hadr does not do pf')
    if kappa == 'pf' and normed:
        pytest.skip('normed not supported with kappa=pf')
    if ('efm' in measure) and (beta != 2):
        pytest.skip('only beta=2 can use efm measure')
    events = ef.gen_random_events(10, 15)
    s = ef.EFPSet('d<=6', measure=measure, beta=beta, kappa=kappa, normed=normed)
    r_batch = s.batch_compute(events, n_jobs=1)
    r = np.asarray([s.compute(event) for event in events])
    assert epsilon_percent(r_batch, r, 10**-14)
Ejemplo n.º 11
0
def test_C2D2C3(measure, beta, normed):

    # skip the efm measures for beta other than 2
    if ('efm' in measure) and (beta != 2):
        pytest.skip('only beta=2 can use efm measure')

    # generate a random event with 10 particles
    event = ef.gen_random_events(1, 10, dim=4)

    # specify the relevant graphs and EFPs to compute C1, D2, C3
    line = ef.EFP([(0, 1)],
                  measure=measure,
                  coords='epxpypz',
                  beta=beta,
                  normed=True)(event)
    triangle = ef.EFP([(0, 1), (0, 2), (1, 2)],
                      measure=measure,
                      coords='epxpypz',
                      beta=beta,
                      normed=True)(event)
    kite = ef.EFP([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)],
                  measure=measure,
                  coords='epxpypz',
                  beta=beta,
                  normed=True)(event)

    # determine the observables
    C2val = triangle / line**2
    D2val = triangle / line**3
    C3val = kite * line / triangle**2

    for strassen in [True, False]:

        # skip strassen for EFM measures and hadr
        if ('efm' in measure or measure == 'hadr') and strassen:
            continue

        D2 = ef.obs.D2(measure=measure,
                       beta=beta,
                       strassen=strassen,
                       normed=normed,
                       coords='epxpypz')
        assert epsilon_diff(D2(event), D2val, 10**-10)

        C2 = ef.obs.C2(measure=measure,
                       beta=beta,
                       strassen=strassen,
                       normed=normed,
                       coords='epxpypz')
        assert epsilon_diff(C2(event), C2val, 10**-10)

    C3 = ef.obs.C3(measure=measure, beta=beta, coords='epxpypz')
    assert epsilon_diff(C3(event), C3val, 10**-10)
Ejemplo n.º 12
0
def test_efm_vs_efmset_compute(sigs, M, measure, normed):

    efmset = ef.EFMSet(sigs, measure=measure, normed=normed, coords='epxpypz')
    efms = [
        ef.EFM(*sig, measure=measure, normed=normed, coords='epxpypz')
        for sig in sigs
    ]

    for event in ef.gen_random_events(2, M):
        efm_dict = efmset.compute(event)
        for sig, efm in zip(sigs, efms):
            print(sig, np.max(np.abs(efm_dict[sig] - efm.compute(event))))
            assert epsilon_percent(efm_dict[sig], efm.compute(event), 10**-10)
Ejemplo n.º 13
0
def test_pt2s_from_p4s(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)

    pt2s = ef.pt2s_from_p4s(p4s)
    slow_pt2s = []
    for i in range(nevents):
        event_pt2s = []
        for j in range(nparticles):
            event_pt2s.append(p4s[i, j, 1]**2 + p4s[i, j, 2]**2)
        slow_pt2s.append(event_pt2s)

    slow_pt2s = np.asarray(slow_pt2s)
    assert epsilon_diff(slow_pt2s, pt2s)
Ejemplo n.º 14
0
def test_efm_vs_efmset_batch_compute(sigs, M, measure, normed):

    efmset = ef.EFMSet(sigs, measure=measure, normed=normed, coords='epxpypz')
    efms = [
        ef.EFM(*sig, measure=measure, normed=normed, coords='epxpypz')
        for sig in sigs
    ]

    events = ef.gen_random_events(2, M)
    efm_dict = efmset.batch_compute(events)

    for sig, efm in zip(sigs, efms):
        results = efm.batch_compute(events)
        for i in range(len(events)):
            assert epsilon_percent(efm_dict[i][sig], results[i], 10**-10)
Ejemplo n.º 15
0
def test_ms_from_p4s(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)

    ms = ef.ms_from_p4s(p4s)
    slow_ms = []
    for i in range(nevents):
        event_ms = []
        for j in range(nparticles):
            event_ms.append(
                np.sqrt(p4s[i, j, 0]**2 - p4s[i, j, 1]**2 - p4s[i, j, 2]**2 -
                        p4s[i, j, 3]**2))
        slow_ms.append(event_ms)

    slow_ms = np.asarray(slow_ms)
    assert epsilon_diff(slow_ms, ms)
Ejemplo n.º 16
0
def test_ys_from_pts_etas_ms(nevents, nparticles):
    p4s = ef.gen_random_events(nevents, nparticles, dim=4,
                               mass='random').reshape(nevents, nparticles, 4)

    ys = ef.ys_from_p4s(p4s)
    y_primes = ef.ys_from_pts_etas_ms(ef.pts_from_p4s(p4s),
                                      ef.etas_from_p4s(p4s),
                                      ef.ms_from_p4s(p4s))

    assert epsilon_diff(ys, y_primes, 1e-12)

    # test cutoff
    pts, ms = 1000 * np.random.rand(25), 10 * np.random.rand(25)
    etas = 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)):
        ys_c1 = ef.ys_from_pts_etas_ms(pts, etas, ms, _cutoff=c1)
        ys_c2 = ef.ys_from_pts_etas_ms(pts, etas, ms, _cutoff=c2)

        assert epsilon_diff(ys_c1, ys_c2, 1e-12)
Ejemplo n.º 17
0
def test_efms(v, measure, normed, M):

    events = ef.gen_random_events(2, M)
    e = ef.EFM(v, measure=measure, normed=normed, coords='epxpypz')

    for event in events:
        if measure == 'hadrefm':
            zs = np.atleast_1d(ef.pts_from_p4s(event))
        elif measure == 'eeefm':
            zs = event[:, 0]

        nhats = event / zs[:, np.newaxis]
        if normed:
            zs = zs / zs.sum()

        e_ans = e.compute(event)
        if v == 0:
            assert epsilon_percent(e_ans, zs.sum(), 10**-13)
        else:
            s_ans = slow_efm(zs, nhats, v)
            assert epsilon_percent(s_ans, e_ans, 10**-13)
Ejemplo n.º 18
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]))
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
def test_linear_relations(measure):
    
    graphs ={# d=0
        'dot': [],

        # d=1
        'line': [(0,1)],

        # d=2
        'dumbbell': [(0,1), (0,1)],
        'wedge': [(0,1),(1,2)],
        'linesqd' : [(0,1),(2,3)],

        # d = 3
        'tribell' : [(0,1),(0,1),(0,1)],
        'triangle' : [(0,1),(1,2),(2,0)],
        'asymwedge' : [(0,1),(0,1),(1,2)],
        'birdfoot' : [(0,1),(0,2),(0,3)],
        'chain' : [(0,1),(1,2),(2,3)],
        'linedumbbell' : [(0,1),(2,3),(2,3)],
        'linewedge' : [(0,1),(2,3),(3,4)],
        'linecbd'  : [(0,1),(2,3),(4,5)],

        # d = 4
        'quadbell' : [(0,1),(0,1),(0,1),(0,1)],
        'doublewedge' : [(0,1),(0,1),(1,2),(1,2)],
        'icecreamcone' : [(0,1),(0,1),(1,2),(2,0)],
        'asymwedge2' : [(0,1),(0,1),(0,1),(1,2)],
        'square' : [(0,1),(1,2),(2,3),(3,0)],
        'flyswatter' : [(0,1),(1,2),(2,3),(3,1)],
        'chain2mid' : [(0,1),(1,2),(1,2),(2,3)],
        'chain2end' : [(0,1),(1,2),(2,3),(2,3)],
        'asymbirdfoot' : [(0,1),(0,1),(1,2),(1,3)],
        'bigbirdfoot' : [(0,1),(0,2),(0,3),(0,4)],
        'dog' : [(0,1),(1,2),(2,3),(2,4)],
        'bigchain' : [(0,1),(1,2),(2,3),(3,4)],

        'dumbbellwedge' : [(0,1),(0,1),(2,3),(3,4)],
        'triangleline' : [(0,1),(1,2),(2,0),(3,4)],
        'dumbbellsqd' : [(0,1),(0,1),(2,3),(2,3)],

        # d = 5
        'pentagon' : [(0,1),(1,2),(2,3),(3,4),(4,0)],
        'triangledumbbell': [(0,1),(0,1),(2,3),(3,4),(4,2)]
        }
    
    # pick a random event with 2 particles
    event = ef.gen_random_events(1, 2, dim=4)

    # compute the value of all of the EFPs on this event
    d = {name: ef.EFP(graph, measure=measure, coords='epxpypz')(event) for name,graph in graphs.items()}

    eps = 10**-8
    
    # check that the identities in the EFM paper are valid (i.e. = 0)
    assert epsilon_diff(2 * d['wedge'] - d['dumbbell'], 0, eps)
    assert epsilon_diff(2 * d['triangle'], 0, eps)
    assert epsilon_diff(d['tribell'] - 2 * d['asymwedge'], 0, eps)
    assert epsilon_diff(2 * d['chain'] - d['linedumbbell'] - d['triangle'], 0, eps)
    assert epsilon_diff(d['birdfoot'] + d['chain'] - d['asymwedge'], 0, eps)
    
    # Four Dimensions
    # pick a random event in 4 dimensions
    event = ef.gen_random_events(1, 25, dim=4)

    # compute the value of all of the EFPs on this event
    d = {name: ef.EFP(graph, measure=measure, coords='epxpypz')(event) for name,graph in graphs.items()}

    # check that the identity in the paper is valid (i.e. = 0)
    assert epsilon_percent(6*d['pentagon'], 5*d['triangledumbbell'], 10**-11)
    
    # count the number of leafless multigraphs (all or just connected) with degree d
    ds = np.arange(11)
    counts_all, counts_con = [], []

    # for each degree, get the graphs with edges<=d and check whether they are leafless
    for d in ds:
        counts_all.append(np.sum([leafless(graph) for graph in ef.EFPSet(('d<=',d)).graphs()]))
        counts_con.append(np.sum([leafless(graph) for graph in ef.EFPSet(('d<=',d), ('p==',1)).graphs()]))

    # note: computed counts are cumulative, must take the difference to get individual d    
    counts_all = np.asarray(counts_all[1:]) - np.asarray(counts_all[:-1])
    counts_con = np.asarray(counts_con[1:]) - np.asarray(counts_con[:-1])
    
    # ensure agreement with the table in the paper
    assert epsilon_diff(counts_all, [0,1,2,5,11,34,87,279,897,3129], eps)
    assert epsilon_diff(counts_con, [0,1,2,4,9,26,68,217,718,2553], eps)
Ejemplo n.º 21
0
    zs = (pts**kappa) / norm
    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(M)
    ] for j in range(M)])**(beta / 2)

    assert epsilon_diff(hzs, zs, 10**-13)
    assert epsilon_diff(hthetas, thetas, 10**-13)


@pytest.mark.measure
@pytest.mark.parametrize('normed', [True, False])
@pytest.mark.parametrize('kappa', [0, .5, 1])
@pytest.mark.parametrize('beta', [.2, 1, 2])
@pytest.mark.parametrize('event', ef.gen_random_events(3, 15))
def test_measure_hadr_p4s(event, beta, kappa, normed):
    M = len(event)
    pTs = np.sqrt(event[:, 1]**2 + event[:, 2]**2)
    ys = 0.5 * np.log(
        (event[:, 0] + event[:, 3]) / (event[:, 0] - event[:, 3]))
    phis = np.arctan2(event[:, 2], event[:, 1])

    # compute using the energyflow package
    hmeas = ef.Measure('hadr', beta, kappa, normed, 'epxpypz', True)
    hzs, hthetas = hmeas.evaluate(event)

    # compute naively
    norm = 1 if not normed else np.sum(pTs**kappa)
    zs = (pTs**kappa) / norm
    thetas = np.asarray([[
Ejemplo n.º 22
0
def test_gen_random_events(nevents, nparticles, dim, mass):
    events = ef.gen_random_events(nevents, nparticles, dim=dim, mass=mass)
    assert events.shape == (nevents, nparticles, dim)
    assert epsilon_diff(ef.ms_from_p4s(events)**2, mass**2, 10**-13)