def test_copy_from(self): keyvalcomposer1 = MockKeyValueComposer() summarizer1 = MockSummarizer() obj1 = Summary.Reader(keyvalcomposer1, summarizer1) keyvalcomposer2 = MockKeyValueComposer() summarizer2 = MockSummarizer() obj2 = Summary.Reader(keyvalcomposer2, summarizer2) obj1.copy_from(obj2) self.assertIs(summarizer2, summarizer1.copy_from_called_with[0])
def test_event_nevents(self): keyvalcomposer = MockKeyValueComposer() summarizer = MockSummarizer() weightCalculator = MockWeightCalculator() obj = Summary.Reader( keyvalcomposer, summarizer, weightCalculator=weightCalculator, nevents=2 # read only first 2 events ) key1 = MockKey('key1') val1 = MockVal('val1') event1 = MockEvent(event='event1', keys=(key1, ), vals=(val1, )) event2 = MockEvent(event='event2', keys=(key1, ), vals=(val1, )) event3 = MockEvent(event='event3', keys=(key1, ), vals=(val1, )) obj.event(event1) obj.event(event2) obj.event(event3) self.assertEqual([ (key1, val1, MockWeight(event1)), (key1, val1, MockWeight(event2)), ], summarizer.add_called_with)
def test_call_inactive(self): obj = Summary.KeyValueComposer() obj.ArrayReader = MockArrayReader obj.active = False event = MockEvent() self.assertEqual((), obj(event))
def test_3_elements(self): arrays = [[], [], []] idxs_conf = (1, 0, 2) obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) arrays[0][:] = [12, 13, 14] arrays[1][:] = [105] arrays[2][:] = [33, 35, 37] self.assertEqual(((13, 105, 37), ), obj.read()) arrays[0][:] = [12] # <- out of range arrays[1][:] = [105] arrays[2][:] = [33, 35, 37] self.assertEqual((), obj.read()) arrays[0][:] = [12, 13, 14] arrays[1][:] = [] # <- out of range arrays[2][:] = [33, 35, 37] self.assertEqual((), obj.read()) arrays[0][:] = [12, 13, 14] arrays[1][:] = [105] arrays[2][:] = [33, 35] # <- out of range self.assertEqual((), obj.read())
def test_empty(self): arrays = [] idxs_conf = () obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) self.assertEqual(((), ), obj.read())
def test_event(self): keyvalcomposer = MockKeyValueComposer() summarizer = MockSummarizer() weightCalculator = MockWeightCalculator() obj = Summary.Reader(keyvalcomposer, summarizer, weightCalculator=weightCalculator) # two key-val pairs key1 = MockKey('key1') key2 = MockKey('key2') val1 = MockVal('val1') val2 = MockVal('val2') event = MockEvent(event='event1', keys=(key1, key2), vals=(val1, val2)) obj.event(event) self.assertEqual([ (key1, val1, MockWeight(event)), (key2, val2, MockWeight(event)), ], summarizer.add_called_with) # no key-val pairs summarizer.add_called_with[:] = [] event = MockEvent(event='event1', keys=(), vals=()) obj.event(event) self.assertEqual([], summarizer.add_called_with)
def test_1_index_1_wildcard(self): arrays = [[], []] idxs_conf = (1, '*') obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) arrays[0][:] = [] arrays[1][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [203, 204, 205] arrays[1][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [203, 204, 205] arrays[1][:] = [12] self.assertEqual(((204, 12), ), obj.read()) arrays[0][:] = [203, 204, 205] arrays[1][:] = [12, 13, 14] self.assertEqual(((204, 12), (204, 13), (204, 14)), obj.read()) arrays[0][:] = [] arrays[1][:] = [12, 13, 14] self.assertEqual((), obj.read())
def test_begin(self): keyvalcomposer = MockKeyValueComposer() summarizer = MockSummarizer() obj = Summary.Reader(keyvalcomposer, summarizer) self.assertIsNone(keyvalcomposer.began_with) event = MockEvent(event='event1', keys=(), vals=()) obj.begin(event) self.assertEqual(event, keyvalcomposer.began_with)
def test_1_element(self): arrays = [[]] idxs_conf = (0, ) obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) arrays[0][:] = [12, 13, 14] self.assertEqual(((12, ), ), obj.read()) arrays[0][:] = [] self.assertEqual((), obj.read())
def test_collect_arrays(self): obj = Summary.KeyValueComposer() event = MockEvent() event.var1 = [] event.var2 = [] event.var3 = [] attr_names = ('var1', 'var2', 'var3') arrays = obj._collect_arrays(event, attr_names) self.assertIs(event.var1, arrays[0]) self.assertIs(event.var2, arrays[1]) self.assertIs(event.var3, arrays[2])
def test_call_NoneKey_1Val(self): obj = Summary.KeyValueComposer() obj.active = True obj._lenkey = 0 obj.binnings = None array_reader = MockArrayReader() obj._array_reader = array_reader event = MockEvent() array_reader.ret = ((12.8, ), ) self.assertEqual((((), (12.8, )), ), obj(event))
def test_end_None_nextKeyComposer(self): key1 = MockKey('key1') key2 = MockKey('key2') key3 = MockKey('key3') keyvalcomposer = MockKeyValueComposer() summarizer = MockSummarizer() summarizer.keys_return[:] = [key1, key2, key3] nextKeyComposer = None obj = Summary.Reader(keyvalcomposer, summarizer, nextKeyComposer=nextKeyComposer) obj.end() self.assertEqual(set([]), set(summarizer.add_key_called_with))
def test_call_2Keys_1Val_NoneBinnings(self): obj = Summary.KeyValueComposer() obj.active = True obj._lenkey = 2 obj.binnings = None array_reader = MockArrayReader() obj._array_reader = array_reader event = MockEvent() array_reader.ret = ((15.3, 22.8, 101.1), ) self.assertEqual((((15.3, 22.8), (101.1, )), ), obj(event))
def test_3_wildcards(self): arrays = [[], [], []] idxs_conf = ('*', '*', '*') obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) arrays[0][:] = [] arrays[1][:] = [] arrays[2][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [12, 13] arrays[1][:] = [] arrays[2][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [] arrays[1][:] = [104] arrays[2][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [] arrays[1][:] = [104] arrays[2][:] = [1001, 1002, 1003] self.assertEqual((), obj.read()) arrays[0][:] = [12, 13, 14] arrays[1][:] = [104, 105] arrays[2][:] = [1001, 1002, 1003] self.assertEqual(( (12, 104, 1001), (12, 104, 1002), (12, 104, 1003), (12, 105, 1001), (12, 105, 1002), (12, 105, 1003), (13, 104, 1001), (13, 104, 1002), (13, 104, 1003), (13, 105, 1001), (13, 105, 1002), (13, 105, 1003), (14, 104, 1001), (14, 104, 1002), (14, 104, 1003), (14, 105, 1001), (14, 105, 1002), (14, 105, 1003), ), obj.read())
def test_logging_nonexistent_var(self): obj = Summary.KeyValueComposer( keyAttrNames=('var1', ), binnings=(MockBinningFloor(max=30), ), valAttrNames=('var2', ), ) event = MockEvent() event.var2 = [] obj.begin(event) event.var2[:] = [ 20.3, ] self.assertEqual((), obj(event))
def test_1_wildcard_1_backref(self): arrays = [[], []] idxs_conf = ('*', None) backref_idxs = [None, 0] obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf, backref_idxs=backref_idxs) arrays[0][:] = [] arrays[1][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [12] arrays[1][:] = [104] self.assertEqual(((12, 104), ), obj.read()) arrays[0][:] = [12] arrays[1][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [] arrays[1][:] = [104] self.assertEqual((), obj.read()) arrays[0][:] = [12, 13] arrays[1][:] = [104, 105] self.assertEqual(((12, 104), (13, 105)), obj.read()) arrays[0][:] = [12, 13] arrays[1][:] = [104] self.assertEqual(((12, 104), ), obj.read()) arrays[0][:] = [12] arrays[1][:] = [104, 105] self.assertEqual(((12, 104), ), obj.read()) arrays[0][:] = [12, 13, 14] arrays[1][:] = [104, 105, 106] self.assertEqual(((12, 104), (13, 105), (14, 106)), obj.read()) arrays[0][:] = [] arrays[1][:] = [104, 105, 106] self.assertEqual((), obj.read()) arrays[0][:] = [12, 13, 14] arrays[1][:] = [104, 105] self.assertEqual(((12, 104), (13, 105)), obj.read())
def test_1_wildcard(self): arrays = [[]] idxs_conf = ('*', ) obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) arrays[0][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [12] self.assertEqual(((12, ), ), obj.read()) arrays[0][:] = [12, 13] self.assertEqual(((12, ), (13, )), obj.read()) arrays[0][:] = [12, 13, 14] self.assertEqual(((12, ), (13, ), (14, )), obj.read())
def test_call_1Key_1Val(self): obj = Summary.KeyValueComposer() obj.active = True obj._lenkey = 1 obj.binnings = (MockBinningFloor(max=30), ) array_reader = MockArrayReader() obj._array_reader = array_reader event = MockEvent() array_reader.ret = ((12.5, 20.3), ) self.assertEqual((((12, ), (20.3, )), ), obj(event)) array_reader.ret = ((32.5, 20.3), ) self.assertEqual((), obj(event))
def test_example_back_reference_twice(self): obj = Summary.KeyValueComposer( keyAttrNames=('ev', 'jet_pt', 'jet_eta', 'mu_pt', 'mu_eta', 'jet_phi'), binnings=( MockBinningFloor(), MockBinningFloor(), MockBinningFloor(max=3), # <- use max for jet_eta MockBinningFloor(), MockBinningFloor(max=2), # <- use max for mu_eta MockBinningEcho(), ), keyIndices=(None, '(*)', '\\1', '(*)', '\\2', '\\1'), valAttrNames=('jet_energy', 'muon_energy'), valIndices=('\\1', '\\2'), ) event = MockEvent() event.ev = [] event.jet_pt = [] event.jet_eta = [] event.jet_phi = [] event.jet_energy = [] event.mu_pt = [] event.mu_eta = [] event.muon_energy = [] obj.begin(event) event.ev[:] = [1001] event.jet_pt[:] = [15.3, 12.9, 9.2, 10.5] event.jet_eta[:] = [-1.2, 5.2, 2.2, 0.5] # <- 2nd value is greater than max event.jet_phi[:] = [0.1, 0.6, 1.2] # <- the last value is missing event.jet_energy[:] = [16.2, 13.1, 10.1, 11.8] event.mu_pt[:] = [20.2, 11.9, 13.3, 5.2] event.mu_eta[:] = [2.2, 1.2, -1.5, -0.5] # <- 1st value is greater than max event.muon_energy[:] = [22.1, 15.2, 16.3] # <- the last value is missing self.assertEqual(( ((1001, 15, -2, 11, 1, 0.1), (16.2, 15.2)), ((1001, 15, -2, 13, -2, 0.1), (16.2, 16.3)), ((1001, 9, 2, 11, 1, 1.2), (10.1, 15.2)), ((1001, 9, 2, 13, -2, 1.2), (10.1, 16.3)), ), obj(event))
def test_2_indices_wildcards(self): arrays = [[], [], [], [], []] idxs_conf = (1, '*', '*', 2, '*') obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf) arrays[0][:] = [] arrays[1][:] = [] arrays[2][:] = [] arrays[3][:] = [] arrays[4][:] = [] self.assertEqual((), obj.read()) arrays[0][:] = [55, 66, 77] arrays[1][:] = [12, 13, 14] arrays[2][:] = [104, 105] arrays[3][:] = [222, 333, 444, 555] arrays[4][:] = [1001, 1002, 1003] self.assertEqual(( (66, 12, 104, 444, 1001), (66, 12, 104, 444, 1002), (66, 12, 104, 444, 1003), (66, 12, 105, 444, 1001), (66, 12, 105, 444, 1002), (66, 12, 105, 444, 1003), (66, 13, 104, 444, 1001), (66, 13, 104, 444, 1002), (66, 13, 104, 444, 1003), (66, 13, 105, 444, 1001), (66, 13, 105, 444, 1002), (66, 13, 105, 444, 1003), (66, 14, 104, 444, 1001), (66, 14, 104, 444, 1002), (66, 14, 104, 444, 1003), (66, 14, 105, 444, 1001), (66, 14, 105, 444, 1002), (66, 14, 105, 444, 1003), ), obj.read())
def test_end(self): key1 = MockKey('key1') key11 = MockKey('key11') key2 = MockKey('key2') key21 = MockKey('key21') key22 = MockKey('key22') key3 = MockKey('key3') nextdic = { key1: (key11, ), key2: (key21, key22), key3: (), } keyvalcomposer = MockKeyValueComposer() summarizer = MockSummarizer() summarizer.keys_return[:] = [key1, key2, key3] nextKeyComposer = MockNextKeyComposer(nextdic) obj = Summary.Reader(keyvalcomposer, summarizer, nextKeyComposer=nextKeyComposer) obj.end() self.assertEqual(set([key11, key21, key22]), set(summarizer.add_key_called_with))
def test_call_2Keys_1Val(self): obj = Summary.KeyValueComposer() obj.active = True obj._lenkey = 2 obj.binnings = (MockBinningFloor(max=30), MockBinningFloor(max=50)) array_reader = MockArrayReader() obj._array_reader = array_reader event = MockEvent() array_reader.ret = ((15.3, 22.8, 101.1), ) self.assertEqual((((15, 22), (101.1, )), ), obj(event)) array_reader.ret = ((45.3, 22.8, 101.1), ) # 1st element out of range self.assertEqual((), obj(event)) array_reader.ret = ((15.3, 52.8, 101.1), ) # 2nd element out of range self.assertEqual((), obj(event)) array_reader.ret = ((45.3, 52.8, 101.1), ) # both out of range self.assertEqual((), obj(event))
def test_1_index_2_wildcards_4_backrefs(self): arrays = [[], [], [], [], [], [], [], []] idxs_conf = (0, '*', None, '*', None, None, None, None) backref_idxs = [None, None, 1, None, 3, 1, 1, 3] obj = Summary.BackrefMultipleArrayReader(arrays=arrays, idxs_conf=idxs_conf, backref_idxs=backref_idxs) arrays[0][:] = [1001] arrays[1][:] = [12, 13, 14, 15] arrays[2][:] = [104, 105, 106, 107] arrays[5][:] = [403, 404, 405] # <- shorter arrays[6][:] = [207, 208, 209, 210] arrays[3][:] = [51, 52] # <- shorter arrays[4][:] = [84, 85, 86] arrays[7][:] = [91, 92, 93] self.assertEqual(((1001, 12, 104, 51, 84, 403, 207, 91), (1001, 12, 104, 52, 85, 403, 207, 92), (1001, 13, 105, 51, 84, 404, 208, 91), (1001, 13, 105, 52, 85, 404, 208, 92), (1001, 14, 106, 51, 84, 405, 209, 91), (1001, 14, 106, 52, 85, 405, 209, 92)), obj.read())
def test_call_one_same(self): binnings = (MockBinningPlusOneNext(), MockBinningSameNext(), MockBinningPlusOneNext()) keyComposer = Summary.NextKeyComposer(binnings) self.assertEqual(((12, 8, 20), (11, 8, 21)), keyComposer((11, 8, 20)))
def test_call_no_binning(self): binnings = () keyComposer = Summary.NextKeyComposer(binnings) self.assertEqual((), keyComposer(()))
def test_call_all_none(self): binnings = (MockBinningNoneNext(), MockBinningNoneNext(), MockBinningNoneNext()) keyComposer = Summary.NextKeyComposer(binnings) self.assertEqual((), keyComposer((11, 8, 20)))
def test_argument_length_2(self): Summary.BackrefMultipleArrayReader(arrays=[[], []], idxs_conf=(1, 2), backref_idxs=None)
def test_results(self): keyvalcomposer = MockKeyValueComposer() summarizer = MockSummarizer() obj = Summary.Reader(keyvalcomposer, summarizer) self.assertIs(summarizer, obj.results())
def test_1(self): actual = Summary.parse_indices_config( (None, None, '(*)', '(*)', '\\1', '\\2')) expected = ([None, None, None, None, 2, 3], [0, 0, '*', '*', '\\1', '\\2']) self.assertEqual(expected, actual)
def test_events(self): weight = Summary.WeightCalculatorOne() self.assertEqual(1.0, weight(MockEvent()))