def test_nominal_accumulate_int32_double(self): """ int32 weights """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_c_tpl = np.array([-700, 0, 0, 300, 500]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.int32) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) self.fill_histo(expected_c, expected_c_tpl, self.ndims - 1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights.astype(np.int32)) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() expected_h *= 2 expected_c *= 2 self.assertEqual(w_histo.dtype, np.int32) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c))
def test_nominal_accumulate_last_bin_closed(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 2]) expected_c_tpl = np.array([-700.7, -0.5, 0.01, 300.3, 1101.1]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) self.fill_histo(expected_c, expected_c_tpl, self.ndims - 1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins, last_bin_closed=True) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() self.assertEqual(w_histo.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c))
def test_nominal_accumulate_weight_min_max(self): """ """ weight_min = -299.9 weight_max = 499.9 expected_h_tpl = np.array([0, 1, 1, 1, 0]) expected_c_tpl = np.array([0., -0.5, 0.01, 300.3, 0.]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) self.fill_histo(expected_c, expected_c_tpl, self.ndims - 1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights, weight_min=weight_min, weight_max=weight_max) histo = instance.histo() w_histo = instance.weighted_histo() self.assertEqual(w_histo.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c))
def test_nominal_accumulate_int32_double(self): """ int32 weights """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_c_tpl = np.array([-700, 0, 0, 300, 500]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.int32) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) self.fill_histo(expected_c, expected_c_tpl, self.ndims-1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights.astype(np.int32)) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() expected_h *= 2 expected_c *= 2 self.assertEqual(w_histo.dtype, np.int32) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c))
def test_nominal_accumulate_twice(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_c_tpl = np.array([-700.7, -0.5, 0.01, 300.3, 500.5]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) self.fill_histo(expected_c, expected_c_tpl, self.ndims - 1) # calling accumulate twice expected_h *= 2 expected_c *= 2 instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() self.assertEqual(w_histo.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c)) self.assertTrue(np.array_equal(instance.histo(), expected_h)) self.assertTrue(np.array_equal(instance.weighted_histo(), expected_c))
def test_nominal_accumulate_weight_min_max(self): """ """ weight_min = -299.9 weight_max = 499.9 expected_h_tpl = np.array([0, 1, 1, 1, 0]) expected_c_tpl = np.array([0., -0.5, 0.01, 300.3, 0.]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) self.fill_histo(expected_c, expected_c_tpl, self.ndims-1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights, weight_min=weight_min, weight_max=weight_max) histo = instance.histo() w_histo = instance.weighted_histo() self.assertEqual(w_histo.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c))
def test_nominal_accumulate_last_bin_closed(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 2]) expected_c_tpl = np.array([-700.7, -0.5, 0.01, 300.3, 1101.1]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) self.fill_histo(expected_c, expected_c_tpl, self.ndims-1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins, last_bin_closed=True) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() self.assertEqual(w_histo.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c))
def test_nominal_accumulate_twice(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_c_tpl = np.array([-700.7, -0.5, 0.01, 300.3, 500.5]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) self.fill_histo(expected_c, expected_c_tpl, self.ndims-1) # calling accumulate twice expected_h *= 2 expected_c *= 2 instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() self.assertEqual(w_histo.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c)) self.assertTrue(np.array_equal(instance.histo(), expected_h)) self.assertTrue(np.array_equal(instance.weighted_histo(), expected_c))
def test_nominal_histo_ref(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_c_tpl = np.array([-700.7, -0.5, 0.01, 300.3, 500.5]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) expected_c = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) self.fill_histo(expected_c, expected_c_tpl, self.ndims-1) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) instance.accumulate(self.weights) histo = instance.histo() w_histo = instance.weighted_histo() histo_ref = instance.histo(copy=False) w_histo_ref = instance.weighted_histo(copy=False) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c)) self.assertTrue(np.array_equal(histo_ref, expected_h)) self.assertTrue(np.array_equal(w_histo_ref, expected_c)) histo_ref[0, ...] = histo_ref[0, ...] + 10 w_histo_ref[0, ...] = w_histo_ref[0, ...] + 20 self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(w_histo, expected_c)) self.assertFalse(np.array_equal(histo_ref, expected_h)) self.assertFalse(np.array_equal(w_histo_ref, expected_c)) histo_2 = instance.histo() w_histo_2 = instance.weighted_histo() self.assertFalse(np.array_equal(histo_2, expected_h)) self.assertFalse(np.array_equal(w_histo_2, expected_c)) self.assertTrue(np.array_equal(histo_2, histo_ref)) self.assertTrue(np.array_equal(w_histo_2, w_histo_ref))