def test_call(obj, scale_func): scale_func.ret = 48.0 jet = Object([('pt', 40.0), ('eta', 1.1), ('phi', 0.1)]) jet_corr = obj(jet) assert scale_func.args == (40.0, 1.1) assert jet_corr == Object([('pt', 48.0), ('eta', 1.1), ('phi', 0.1)])
def test_call(obj): o1 = Object((('eta', 0), ('phi', 0))) o2 = Object((('eta', 0), ('phi', 0))) assert obj(o1, o2) == 0.0 o1 = Object((('eta', 0.5), ('phi', 0.5))) o2 = Object((('eta', 0), ('phi', 0))) assert obj(o1, o2) == math.sqrt(0.5**2 + 0.5**2)
def test_event(obj, event): obj.begin(event) event.Jet[:] = [ Object([('Pt', 40.0), ('Eta', 1.1), ('Phi', 0.1)]), Object([('Pt', 30.0), ('Eta', 2.1), ('Phi', 0.2)]), Object([('Pt', 20.0), ('Eta', 3.1), ('Phi', 0.3)]), ] obj.event(event) assert event.nJet == [3]
def test_event(obj, event): obj.begin(event) event.Jet[:] = [ Object([('Pt', 40.0), ('Eta', 1.1), ('Phi', 0.1)]), Object([('Pt', 30.0), ('Eta', 2.1), ('Phi', 0.2)]), Object([('Pt', 20.0), ('Eta', 3.1), ('Phi', 0.3)]), ] obj.event(event) assert event.jet_pt == [40.0, 30.0, 20.0] assert event.jet_eta == [1.1, 2.1, 3.1] assert event.jet_phi == [0.1, 0.2, 0.3]
def test_match_empty_B(mock_distance_func): o1 = Object((('x', 0), ('y', 0))) o2 = Object((('x', 1), ('y', 0))) A = [o1, o2] B = [] Amatched, BmatchedSorted, Aunmatched, Bunmatched = split_matched_unmatched( A, B, mock_distance_func, 2) assert Amatched == [] assert BmatchedSorted == [] assert Aunmatched == [o1, o2] assert Bunmatched == []
def test_match_2A_within_distance(mock_distance_func): a1 = Object((('x', 0), ('y', 0))) a2 = Object((('x', 1.5), ('y', 0))) b1 = Object((('x', 1), ('y', 0))) b2 = Object((('x', 5), ('y', 0))) A = [a1, a2] B = [b1, b2] Amatched, BmatchedSorted, Aunmatched, Bunmatched = split_matched_unmatched( A, B, mock_distance_func, 2) assert Amatched == [a2] assert BmatchedSorted == [b1] assert Aunmatched == [a1] assert Bunmatched == [b2]
def test_event(obj, event): obj.begin(event) in_obj = [ Object((('pt', 50), )), Object((('pt', 45), )), Object((('pt', 20), )) ] event.Jet[:] = in_obj obj.event(event) assert event.JetSelected == [ Object((('pt', 50), )), Object((('pt', 45), )) ] assert event.JetSelected[0] is in_obj[0] # not a copy assert event.JetSelected[1] is in_obj[1] # not a copy
def test_event_default_out_obj_attr_names(event): obj = ArraysIntoObjectZip( in_array_prefix='jet', in_array_names=['pt', 'eta', 'phi'], ) obj.begin(event) event.jet_pt[:] = [40.0, 30.0, 20.0] event.jet_eta[:] = [1.1, 2.1, 3.1] event.jet_phi[:] = [0.1, 0.2, 0.3] obj.event(event) expected = [ Object([('pt', 40.0), ('eta', 1.1), ('phi', 0.1)]), Object([('pt', 30.0), ('eta', 2.1), ('phi', 0.2)]), Object([('pt', 20.0), ('eta', 3.1), ('phi', 0.3)]), ] assert event.jet == expected
def test_init_copy_extra_kwargs(obj, caplog): with caplog.at_level(logging.INFO, logger='scribblers.obj'): obj_copy = Object(obj, A=10) assert len(caplog.records) == 1 assert caplog.records[0].levelname == 'WARNING' assert caplog.records[0].name == 'scribblers.obj' assert 'extra arguments' in caplog.records[0].msg assert obj == obj_copy assert obj is not obj_copy assert obj._attrdict is not obj_copy._attrdict
def test_call_exactly_multiple_2pi(obj): o1 = Object((('eta', 0), ('phi', 0))) o2 = Object((('eta', 0), ('phi', 2*math.pi))) assert obj(o1, o2) == 0.0 o1 = Object((('eta', 0), ('phi', 0))) o2 = Object((('eta', 0), ('phi', 4*math.pi))) assert obj(o1, o2) == 0.0 o1 = Object((('eta', 0), ('phi', 2*math.pi))) o2 = Object((('eta', 0), ('phi', 0))) assert obj(o1, o2) == 0.0 o1 = Object((('eta', 0), ('phi', 4*math.pi))) o2 = Object((('eta', 0), ('phi', 0))) assert obj(o1, o2) == 0.0
def test_event_simple(obj, event): obj.begin(event) a1 = Object((('x', 0), ('y', 0))) a2 = Object((('x', 3), ('y', 0))) a3 = Object((('x', 6), ('y', 0))) a4 = Object((('x', 9), ('y', 0))) b1 = Object((('x', 13), ('y', 0))) b2 = Object((('x', 6.5), ('y', 0))) b3 = Object((('x', 5), ('y', 0))) b4 = Object((('x', 2), ('y', 0))) b5 = Object((('x', 1), ('y', 0))) event.A[:] = [a1, a2, a3, a4] event.B[:] = [b1, b2, b3, b4, b5] obj.event(event) assert event.Amatched == [a1, a2, a3] assert event.BmatchedSorted == [b5, b4, b2] assert event.Aunmatched == [a4] assert event.Bunmatched == [b1, b3]
def test_event(obj, correction, event): obj.begin(event) in_obj = [ Object((('pt', 50), ('eta', 1.2))), Object((('pt', 45), ('eta', 1.5))), Object((('pt', 20), ('eta', -0.2))) ] event.Jet[:] = in_obj obj.event(event) assert event.JetCorrected == [ Object((('pt', 100), ('eta', 1.2))), Object((('pt', 90), ('eta', 1.5))), Object((('pt', 40), ('eta', -0.2))) ] assert event.Jet == [ Object((('pt', 50), ('eta', 1.2))), Object((('pt', 45), ('eta', 1.5))), Object((('pt', 20), ('eta', -0.2))) ]
max_distance=0.4) ret.begin(mockevent) yield ret ret.end() ##__________________________________________________________________|| def test_repr(obj): repr(obj) @pytest.mark.parametrize('in_obj1, in_obj2, expected', [ pytest.param([], [], [], id='empty'), pytest.param([ Object([('PT', 888.443), ('Eta', 0.659), ('Phi', 0.359), ('Mass', 79.528)]), Object([('PT', 808.685), ('Eta', 0.801), ('Phi', -2.717), ('Mass', 102.789)]), Object([('PT', 186.931), ('Eta', 0.725), ('Phi', 3.003), ('Mass', 19.412)]), Object([('PT', 68.112), ('Eta', 0.985), ('Phi', -0.167), ('Mass', 9.606)]), ], [ Object([('PT', 26.454), ('Eta', 0.550), ('Phi', 0.424), ('Mass', 0.000)]), Object([('PT', 12.695), ('Eta', 0.782), ('Phi', 0.269), ('Mass', 0.000)]), Object([('PT', 10.472), ('Eta', 0.982), ('Phi', -0.222), ('Mass', 0.000)]), Object([('PT', 32.270), ('Eta', 0.783), ('Phi', -2.794), ('Mass', 0.000)]),
def test_setattr_newattr(obj): obj.mass = 15.0 assert obj.mass == 15.0 assert obj == Object([('pt', 40.0), ('eta', 1.1), ('phi', 0.1), ('mass', 15.0)])
def test_setattr_modify(obj): obj.pt = 50.0 assert obj.pt == 50.0 assert obj == Object([('pt', 50.0), ('eta', 1.1), ('phi', 0.1)])
def test_init_copy(obj): obj_copy = Object(obj) assert obj == obj_copy assert obj is not obj_copy assert obj._attrdict is not obj_copy._attrdict
def test_init_no_args(): Object()
def obj(): return Object([('pt', 40.0), ('eta', 1.1), ('phi', 0.1)])
# Tai Sakuma <*****@*****.**> import pytest from scribblers.obj import Object from .compare_obj import cmp_obj_list_almost_equal from .compare_obj import cmp_obj_almost_equal ##__________________________________________________________________|| @pytest.mark.parametrize('list1, list2, expected', [ pytest.param([], [], True, id='empty'), pytest.param([Object([('i', 3)]), Object([('i', 5)])], [Object([('i', 3)]), Object([('i', 5)])], True, id='same-1'), pytest.param([Object([('i', 3)]), Object([('i', 5)])], [Object([('i', 3)]), Object([('i', 8)])], False, id='different-content'), pytest.param([Object([('i', 3)]), Object([('i', 5)])], [Object([('i', 3)])], False, id='list1-longer'), pytest.param([Object([('i', 3)])], [Object([('i', 3)]), Object([('i', 5)])], False, id='list2-longer'), pytest.param( [Object([('i', 3), ('x', 10.1)]), Object([('i', 5), ('x', 32.5)])], [Object([('i', 3), ('x', 10.1)]),