def test_visible_properties(self): EC_width, EC_height = 16, 32 width, height, num_frames = 48, 64, 128 num_EC = (width * height) / (EC_width * EC_height) template = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames) self.assertEqual(EC_width, template.EC_width) self.assertEqual(EC_height, template.EC_height) self.assertEqual(width, template.frame_width) self.assertEqual(height, template.frame_height) self.assertEqual(int(width / EC_width), template.num_cols) self.assertEqual(int(height / EC_height), template.num_rows) self.assertEqual(num_EC, template.num_EC) self.assertEqual(num_frames, template.num_frames) self.assertEqual((num_frames, height, width), template.packet_shape) # test creating a template from incosistent properties ## first: negative values self.assertRaises(ValueError, templates.PacketTemplate, -1, EC_height, width, height, num_frames) self.assertRaises(ValueError, templates.PacketTemplate, EC_width, -1, width, height, num_frames) self.assertRaises(ValueError, templates.PacketTemplate, EC_width, EC_height, -1, height, num_frames) self.assertRaises(ValueError, templates.PacketTemplate, EC_width, EC_height, width, -1, num_frames) self.assertRaises(ValueError, templates.PacketTemplate, EC_width, EC_height, width, height, -1) # second: width and height that are not evenly divisible by EC_width or EC_height respectively self.assertRaises(ValueError, templates.PacketTemplate, EC_height + 1, EC_width, width, height, num_frames) self.assertRaises(ValueError, templates.PacketTemplate, EC_height, EC_width + 1, width, height, num_frames)
def test_equality_check(self): EC_width, EC_height = 16, 32 width, height, num_frames = 48, 64, 128 template = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames) template2 = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames) self.assertEqual(template, template2) template2 = templates.PacketTemplate(EC_width, EC_height, width + EC_width, height, num_frames) self.assertNotEqual(template, template2) template2 = templates.PacketTemplate(EC_width, EC_height, width, height + EC_height, num_frames) self.assertNotEqual(template, template2) template2 = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames + 1) self.assertNotEqual(template, template2)
def test_property_checking(self): gtu, w, h, ec_w, ec_h = 128, 64, 48, 32, 16 template = templates.PacketTemplate(ec_w, ec_h, w, h, gtu) start_x, start_y, start_gtu = (3, 5), (1, 10), (2, 4) duration, shower_max = (2, 10), (7, 15) shower_template = templates.SimulatedShowerTemplate( template, duration, shower_max) # start_x lower bound is less than 0, upper bound is less than lower bound or upper bound is larger than frame width self._assert_set_prop_raises(shower_template, 'start_x', (-start_x[0], start_x[1]), ValueError) self._assert_set_prop_raises(shower_template, 'start_x', (start_x[1], start_x[0]), ValueError) self._assert_set_prop_raises(shower_template, 'start_x', (start_x[0], w + 1), ValueError) self._assert_set_prop_not_raises(shower_template, 'start_x', start_x) # start_y lower bound is less than 0, upper bound is less than lower bound or upper bound is larger than frame height self._assert_set_prop_raises(shower_template, 'start_y', (-start_y[0], start_y[1]), ValueError) self._assert_set_prop_raises(shower_template, 'start_y', (start_y[1], start_y[0]), ValueError) self._assert_set_prop_raises(shower_template, 'start_y', (start_y[0], h + 1), ValueError) self._assert_set_prop_not_raises(shower_template, 'start_y', start_y) # start_gtu lower bound is less than 0, upper bound is less than lower bound or upper bound is larger than number of frames self._assert_set_prop_raises(shower_template, 'start_gtu', (-start_gtu[0], start_gtu[1]), ValueError) self._assert_set_prop_raises(shower_template, 'start_gtu', (start_gtu[1], start_gtu[0]), ValueError) self._assert_set_prop_raises(shower_template, 'start_gtu', (start_gtu[0], gtu + 1), ValueError) self._assert_set_prop_not_raises(shower_template, 'start_gtu', start_gtu) # duration lower bound is less than 1, upper bound is less than lower bound or upper bound is larger than the number of frames self._assert_set_prop_raises(shower_template, 'shower_duration', (0, duration[1]), ValueError) self._assert_set_prop_raises(shower_template, 'shower_duration', (duration[1], duration[0]), ValueError) self._assert_set_prop_raises(shower_template, 'shower_duration', (duration[0], gtu + 1), ValueError) self._assert_set_prop_not_raises(shower_template, 'duration', duration) # bg_diff lower bound is less than 1 or upper bound is less than lower bound self._assert_set_prop_raises(shower_template, 'shower_max', (0, shower_max[1]), ValueError) self._assert_set_prop_raises(shower_template, 'shower_max', (shower_max[1], shower_max[0]), ValueError) self._assert_set_prop_not_raises(shower_template, 'shower_max', shower_max)
def test_equality_check(self): gtu, w, h, ec_w, ec_h = 128, 64, 48, 32, 16 t = templates.PacketTemplate(ec_w, ec_h, w, h, gtu) lam, bec = (0.1, 0.6), (1, 2) bg_temp = templates.SyntheticBackgroundTemplate(t, bg_lambda=lam, bad_ECs_range=bec) bg_temp2 = templates.SyntheticBackgroundTemplate(t, bg_lambda=lam, bad_ECs_range=bec) self.assertEqual(bg_temp, bg_temp2) bg_temp2.bg_lambda_range = (0.2, 0.6) self.assertNotEqual(bg_temp, bg_temp2) bg_temp2.bg_lambda_range = lam bg_temp2.bad_ECs_range = (3, 4) self.assertNotEqual(bg_temp, bg_temp2)
def test_simu_shower(self): EC_width, EC_height = 16, 32 width, height, num_frames = 48, 64, 20 template = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames) generator = gen.FlatValsGenerator(20, 10) angles = [45, 135, 225, 315, 45, 135, 225, 315] start_xs = [0, width-1, width-1, 0, width-3, 3, 3, width-3] start_ys = [0, 0, height-1, height-1, height-3, height-3, 3, 3] start_gtus = [3, 6, 2, 0, 10, 2, 1, 6] durations = [10, 7, 12, 12, 5, 6, 16, 11] num_iterations = [10, 7, 12, 12, 5, 5, 5, 5] maximums = [20, 10, 30, 15, 7, 10, 2, 16] num_data = len(start_xs) for data_idx in range(num_data): packet = np.ones((num_frames, height, width)) reference_packet = np.ones((num_frames, height, width)) start_x, start_y, start_gtu = start_xs[data_idx], start_ys[data_idx], start_gtus[data_idx] start = (start_gtu, start_y, start_x) angle, duration, shower_max = angles[data_idx], durations[data_idx], maximums[data_idx] iterations = num_iterations[data_idx] generator.reset(shower_max, duration) GTU, Y, X, vals = sdutils.create_simu_shower_line(angle, start, template, generator) packet[GTU, Y, X] += vals # create reference data to compare the method call results to ang_rad = math.radians(angle) delta_x, delta_y = math.cos(ang_rad), math.sin(ang_rad) gtu_idx = start_gtu ref_X, ref_Y, ref_GTU = [], [], [] ref_vals = tuple([shower_max,] * iterations) for idx in range(iterations): y, x = int(start_y+delta_y*idx), int(start_x+delta_x*idx) reference_packet[gtu_idx, y, x] += shower_max ref_X.append(x); ref_Y.append(y) ref_GTU.append(gtu_idx) gtu_idx += 1 self.assertTupleEqual(tuple(ref_X), X) self.assertTupleEqual(tuple(ref_Y), Y) self.assertTupleEqual(tuple(ref_GTU), GTU) self.assertTupleEqual(tuple(ref_vals), vals) self.assertTrue(np.array_equal(packet, reference_packet), msg="Packets at iteration {} are not equal".format(data_idx))
def setUpClass(cls): # mock filenames cls.srcfile, cls.triggerfile = "srcfile", None # packet template for the functions ec_width, ec_height = 16, 32 width, height, num_frames = 48, 64, 20 cls.template = templates.PacketTemplate(ec_width, ec_height, width, height, num_frames) # the packets as used in the functions n_packets = 2 cls.packets = np.ones((n_packets * num_frames, height, width), dtype=np.uint8) cls.packets[0, 0, 0] = 10 cls.packets[num_frames, 0, 0] = 2 cls.expected_packets_shape = (n_packets, num_frames, height, width) cls.expected_packets = cls.packets.reshape(cls.expected_packets_shape) # the extractor object cls.extractor = io_utils.PacketExtractor(packet_template=cls.template)
def test_unit_conversions(self): EC_width, EC_height = 16, 32 width, height, num_frames = 48, 64, 128 template = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames) xs = [0, 10, 21, 30, 21, 40, 0] ys = [10, 0, 1, 42, 10, 50, 0] ec_ids = [0, 0, 1, 4, 1, 5, 0] ec_xys = [[0, 0], [0, 0], [1, 0], [1, 1], [1, 0], [2, 1], [0, 0]] for idx in range(len(xs)): x, y, ec_x, ec_y, ec_idx = xs[idx], ys[idx], ec_xys[idx][ 0], ec_xys[idx][1], ec_ids[idx] self.assertEqual(template.x_to_ec_x(x), ec_x) self.assertEqual(template.y_to_ec_y(y), ec_y) self.assertEqual(template.ec_idx_to_ec_xy(ec_idx), (ec_x, ec_y)) self.assertEqual(template.xy_to_ec_idx(x, y), ec_idx) self.assertEqual(template.ec_xy_to_ec_idx(ec_x, ec_y), ec_idx) self.assertTupleEqual(template.ec_idx_to_xy_slice(1), (slice(16, 32), slice(0, 32)))
def test_EC_error(self): EC_width, EC_height = 16, 32 width, height, num_frames = 48, 64, 20 num_EC = int((width * height) / (EC_width * EC_height)) shower_ec_indexes = [2, 5] template = templates.PacketTemplate(EC_width, EC_height, width, height, num_frames) packet = np.ones((num_frames, height, width)) # case 1: method should terminate without selecting any ECs at all X, Y, ECs = sdutils.select_random_ECs(template, 0, excluded_ECs=shower_ec_indexes) self._fill_EC_with_zeros(packet, X, Y) self.assertTrue(np.array_equal( packet, np.ones((num_frames, height, width)) )) X, Y, ECs = sdutils.select_random_ECs(template, 0, excluded_ECs=[]) self._fill_EC_with_zeros(packet, X, Y) self.assertTrue(np.array_equal( packet, np.ones((num_frames, height, width)) )) # case 2: method should select all ECs except the rightmost 2 EC cells X, Y, ECs = sdutils.select_random_ECs(template, num_EC - len(shower_ec_indexes), excluded_ECs=shower_ec_indexes) self._fill_EC_with_zeros(packet, X, Y) for frame in packet: self.assertEqual(np.count_nonzero(frame), EC_width*EC_height*(len(shower_ec_indexes))) self.assertTrue(np.array_equal( frame[0:2*EC_height, 0:2*EC_width], np.zeros((height, width - EC_width)) )) # case 3: if more malfunctioned ECs are requested than possible without selecting ECs in excluded_ECs, then settle for num_EC - len(excluded_ECs) packet = np.ones((num_frames, width, height)) ## only one possible EC can malfunction shower_ec_indexes = range(1, num_EC) X, Y, ECs = sdutils.select_random_ECs(template, num_EC, excluded_ECs=shower_ec_indexes) self._fill_EC_with_zeros(packet, X, Y) for frame in packet: self.assertEqual(np.count_nonzero(frame), width*height - EC_width*EC_height) self.assertTrue(np.array_equal( frame[0:EC_height, 0:EC_width], np.zeros((EC_height, EC_width)) ))
def test_property_generators(self): gtu, w, h, ec_w, ec_h = 128, 64, 48, 32, 16 template = templates.PacketTemplate(ec_w, ec_h, w, h, gtu) sx, sy, sg = 3, 10, 2 d, m = 7, 15 shower_template = templates.SimulatedShowerTemplate(template, (d, d), (m, m), start_x=(sx, sx), start_y=(sy, sy), start_gtu=(sg, sg)) # if MIN == MAX, returned value should never change for idx in range(10): self.assertTupleEqual(shower_template.get_new_start_coordinate(), (sg, sy, sx)) self.assertEqual(shower_template.get_new_shower_max(), m) self.assertEqual(shower_template.get_new_shower_duration(), d) sx, sy, sg = (3, 5), (1, 10), (2, 4) d, m = (2, 10), (7, 15) shower_template.start_x, shower_template.start_y = sx, sy shower_template.start_gtu = sg shower_template.shower_duration, shower_template.shower_max = d, m # if MIN != MAX, returned values should be within a certain range for idx in range(10): start_gtu, start_y, start_x = shower_template.get_new_start_coordinate( )[0:3] self.assertGreaterEqual(start_gtu, sg[0]) self.assertGreaterEqual(start_y, sy[0]) self.assertGreaterEqual(start_x, sx[0]) self.assertLessEqual(start_gtu, sg[1]) self.assertLessEqual(start_y, sy[1]) self.assertLessEqual(start_x, sx[1]) shower_max = shower_template.get_new_shower_max() self.assertGreaterEqual(shower_max, m[0]) self.assertLessEqual(shower_max, m[1]) duration = shower_template.get_new_shower_duration() self.assertGreaterEqual(duration, d[0]) self.assertLessEqual(duration, d[1])
def test_equality_check(self): gtu, w, h, ec_w, ec_h = 128, 64, 48, 32, 16 template = templates.PacketTemplate(ec_w, ec_h, w, h, gtu) sx, sy, sg = (3, 5), (1, 10), (2, 4) d, m = (2, 10), (7, 15) shower_template = templates.SimulatedShowerTemplate(template, d, m, start_x=sx, start_y=sy, start_gtu=sg) shower_template2 = templates.SimulatedShowerTemplate(template, d, m, start_x=sx, start_y=sy, start_gtu=sg) self.assertEqual(shower_template, shower_template2) shower_template2.start_x = (sx[0], sx[1] + 1) self.assertNotEqual(shower_template, shower_template2) shower_template2.start_x = sx shower_template2.start_y = (sy[0], sy[1] + 1) self.assertNotEqual(shower_template, shower_template2) shower_template2.start_y = sy shower_template2.start_gtu = (sg[0], sg[1] + 1) self.assertNotEqual(shower_template, shower_template2) shower_template2.start_gtu = sg shower_template2.shower_duration = (d[0], d[1] + 1) self.assertNotEqual(shower_template, shower_template2) shower_template2.shower_duration = d shower_template2.shower_max = (m[0], m[1] + 1) self.assertNotEqual(shower_template, shower_template2) shower_template2.shower_max = m shower_template2.values_generator = gen.FlatValsGenerator(10, 10) self.assertNotEqual(shower_template, shower_template2)
def packet_arg_to_template(self, args): n_gtu, f_h, f_w, ec_h, ec_w = getattr(args, self.long_alias, None)[0:5] return templates.PacketTemplate(ec_w, ec_h, f_w, f_h, n_gtu)
def __init__(self, packet_template=templates.PacketTemplate(16, 16, 48, 48, 128)): self.packet_template = packet_template