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_int32(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)) histo = instance.histo() w_histo = instance.weighted_histo() 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_last_bin_closed(self): instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) last_bin_closed = instance.last_bin_closed self.assertEqual(last_bin_closed, False) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins, last_bin_closed=True) last_bin_closed = instance.last_bin_closed self.assertEqual(last_bin_closed, True) instance = HistogramndLut(self.sample, self.histo_range, self.n_bins, last_bin_closed=False) last_bin_closed = instance.last_bin_closed self.assertEqual(last_bin_closed, False)
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_apply_lut_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 apply_lut twice expected_h *= 2 expected_c *= 2 instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) histo, w_histo = instance.apply_lut(self.weights) histo_2, w_histo_2 = instance.apply_lut(self.weights, histo=histo, weighted_histo=w_histo) self.assertEqual(id(histo), id(histo_2)) self.assertEqual(id(w_histo), id(w_histo_2)) 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.assertEqual(instance.histo(), None) self.assertEqual(instance.weighted_histo(), None)
def testNoneNativeTypes(self): type = self.sample.dtype.newbyteorder("B") sampleB = self.sample.astype(type) type = self.sample.dtype.newbyteorder("L") sampleL = self.sample.astype(type) histo_inst = HistogramndLut(sampleB, self.histo_range, self.n_bins) histo_inst = HistogramndLut(sampleL, self.histo_range, self.n_bins)
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_histo_range(self): instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) histo_range = instance.histo_range self.assertTrue(np.array_equal(histo_range, self.histo_range))
def test_nominal_n_bins_array(self): test_n_bins = np.arange(self.ndims) + 10 instance = HistogramndLut(self.sample, self.histo_range, test_n_bins) n_bins = instance.n_bins self.assertTrue(np.array_equal(test_n_bins, n_bins))
def test_nominal_n_bins_scalar(self): test_n_bins = 10 expected_n_bins = np.array([test_n_bins] * self.ndims) instance = HistogramndLut(self.sample, self.histo_range, test_n_bins) n_bins = instance.n_bins self.assertTrue(np.array_equal(expected_n_bins, n_bins))
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))
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_bin_edges(self): instance = HistogramndLut(self.sample, self.histo_range, self.n_bins) bin_edges = instance.bins_edges expected_edges = _get_bin_edges(self.histo_range, self.n_bins, self.ndims) for i_edges, edges in enumerate(expected_edges): self.assertTrue(np.array_equal(bin_edges[i_edges], expected_edges[i_edges]), msg='Testing bin_edges for dim {0}' ''.format(i_edges + 1))