def test_unmanaged_dtypes(self): """ """ for err_unmanaged_dtype in self.err_unmanaged_dtypes: test_msg = ('Testing unmanaged dtypes : {0}' ''.format(err_unmanaged_dtype)) sample = self.sample.astype(err_unmanaged_dtype[0]) weights = self.weights.astype(err_unmanaged_dtype[1]) expected_txt = ('Case not supported - sample:{0} ' 'and weights:{1}.' ''.format(sample.dtype, weights.dtype)) ex_str = None try: histogramnd(sample, self.histo_range, self.n_bins, weights=weights) except TypeError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_reuse_histo(self): """ """ expected_h_tpl = np.array([2, 3, 2, 2, 2]) expected_c_tpl = np.array([0.0, -7007, -5.0, 0.1, 3003.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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights)[0:2] sample_2 = self.sample[:] if len(sample_2.shape) == 1: idx = [slice(0, None)] else: idx = [slice(0, None), self.tested_dim] sample_2[idx] += 2 histo_2, cumul = histogramnd(sample_2, # <==== !! self.histo_range, self.n_bins, weights=10 * self.weights, # <==== !! histo=histo)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c)) self.assertEqual(id(histo), id(histo_2))
def test_reuse_histo(self): """ """ expected_h_tpl = np.array([2, 3, 2, 2, 2]) expected_c_tpl = np.array([0.0, -7007, -5.0, 0.1, 3003.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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights)[0:2] sample_2 = self.sample[:] if len(sample_2.shape) == 1: idx = (slice(0, None),) else: idx = slice(0, None), self.tested_dim sample_2[idx] += 2 histo_2, cumul = histogramnd(sample_2, # <==== !! self.histo_range, self.n_bins, weights=10 * self.weights, # <==== !! histo=histo)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c)) self.assertEqual(id(histo), id(histo_2))
def test_histo_range_shape(self): """ """ n_dims = 1 if len(self.s_shape) == 1 else self.s_shape[1] expected_txt_tpl = ('<histo_range> error : expected {n_dims} sets ' 'of lower and upper bin edges, ' 'got the following instead : {histo_range}. ' '(provided <sample> contains ' '{n_dims}D values)') for err_histo_range in self.err_histo_range_shapes: test_msg = ('Testing invalid histo_range shape : {0}' ''.format(err_histo_range)) expected_txt = expected_txt_tpl.format(histo_range=err_histo_range, n_dims=n_dims) ex_str = None try: histo, cumul = histogramnd(self.sample, err_histo_range, self.n_bins, weights=self.weights)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_cumul_dtype(self): """ """ # using the same values as histo for err_h_dtype in self.err_histo_dtypes: test_msg = ('Testing invalid weighted_histo dtype : {0}' ''.format(err_h_dtype)) cumul = np.zeros(shape=self.h_shape, dtype=err_h_dtype) expected_txt = ('Provided <weighted_histo> array doesn\'t have ' 'the expected type ' ': should be {0} or {1} instead of {2}.' ''.format(np.float64, np.float32, cumul.dtype)) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, weighted_histo=cumul)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_histo_dtype(self): """ """ for err_h_dtype in self.err_histo_dtypes: test_msg = ('Testing invalid histo dtype : {0}' ''.format(err_h_dtype)) histo = np.zeros(shape=self.h_shape, dtype=err_h_dtype) expected_txt = ('Provided <histo> array doesn\'t have ' 'the expected type ' ': should be {0} instead of {1}.' ''.format(np.uint32, histo.dtype)) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, histo=histo)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_nominal_uncontiguous_weights(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) shape = list(self.weights.shape) shape[0] *= 2 weights = np.zeros(shape, dtype=self.weights.dtype) uncontig_weights = weights[::2, ...] uncontig_weights[:] = self.weights self.assertFalse(uncontig_weights.flags['C_CONTIGUOUS'], msg='Making sure the array is not contiguous.') histo, cumul, bin_edges = histogramnd(self.sample, self.histo_range, self.n_bins, weights=uncontig_weights) self.assertEqual(cumul.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c))
def test_weighted_histo_shape(self): """ """ # using the same values as histo for err_h_shape in self.err_histo_shapes: # windows & python 2.7 : numpy shapes are long values if platform.system() == 'Windows': version = (sys.version_info.major, sys.version_info.minor) if version <= (2, 7): err_h_shape = tuple([long(val) for val in err_h_shape]) test_msg = ('Testing invalid weighted_histo shape : {0}' ''.format(err_h_shape)) expected_txt = ('Provided <weighted_histo> array doesn\'t have ' 'a shape compatible with <n_bins> ' ': should be {0} instead of {1}.' ''.format(self.h_shape, err_h_shape)) cumul = np.zeros(shape=err_h_shape, dtype=np.double) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, weighted_histo=cumul)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_nominal(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) histo, cumul, bin_edges = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights) expected_edges = _get_bin_edges(self.histo_range, self.n_bins, self.ndims) self.assertEqual(cumul.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c)) 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))
def test_weights_shape(self): """ """ for err_w_shape in self.err_weights_shapes: test_msg = ('Testing invalid weights shape : {0}' ''.format(err_w_shape)) err_weights = np.random.randint(0, high=10, size=err_w_shape) err_weights = err_weights.astype(np.double) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=err_weights)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, '<weights> must be an array whose length ' 'is equal to the number of samples.')
def test_nominal_uncontiguous_weights(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) shape = list(self.weights.shape) shape[0] *= 2 weights = np.zeros(shape, dtype=self.weights.dtype) uncontig_weights = weights[::2, ...] uncontig_weights[:] = self.weights self.assertFalse(uncontig_weights.flags['C_CONTIGUOUS'], msg='Making sure the array is not contiguous.') histo, cumul, bin_edges = histogramnd(self.sample, self.histo_range, self.n_bins, weights=uncontig_weights) self.assertEqual(cumul.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c))
def test_nominal(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) histo, cumul, bin_edges = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights) expected_edges = _get_bin_edges(self.histo_range, self.n_bins, self.ndims) self.assertEqual(cumul.dtype, np.float64) self.assertEqual(histo.dtype, np.uint32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c)) 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))
def test_reuse_histo(self): """ """ result_c_1 = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True) result_np_1 = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range) np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range, weights=self.weights) sample_2, weights_2 = self.generate_data() result_c_2 = histogramnd(sample_2, self.histo_range, self.n_bins, weights=weights_2, last_bin_closed=True, histo=result_c_1[0]) result_np_2 = np.histogramdd(sample_2, bins=self.n_bins, range=self.histo_range) result_np_w_2 = np.histogramdd(sample_2, bins=self.n_bins, range=self.histo_range, weights=weights_2) # comparing "hits" hits_cmp = np.array_equal(result_c_2[0], result_np_1[0] + result_np_2[0]) # comparing weights weights_cmp = np.array_equal(result_c_2[1], result_np_w_2[0]) self.assertTrue(hits_cmp, msg=self.state_msg) self.assertTrue(weights_cmp, msg=self.state_msg)
def test_filter_minmax(self): """ """ result_c = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True, weight_min=self.filter_min, weight_max=self.filter_max) # !!!!!!!!!!!!!!!!!!!!!!!!!!!! filter_min = self.dtype_weights(self.filter_min) filter_max = self.dtype_weights(self.filter_max) weight_idx = _get_in_range_indices( self.weights, filter_min, # <------ !!! filter_max, # <------ !!! minop=operator.ge, maxop=operator.le) result_np = np.histogramdd(self.sample[weight_idx], bins=self.n_bins, range=self.histo_range) result_np_w = np.histogramdd(self.sample[weight_idx], bins=self.n_bins, range=self.histo_range, weights=self.weights[weight_idx]) # comparing "hits" hits_cmp = np.array_equal(result_c[0], result_np[0]) # comparing weights weights_cmp = np.array_equal(result_c[1], result_np_w[0]) self.assertTrue(hits_cmp) self.assertTrue(weights_cmp) bins_min = [rng[0] for rng in self.histo_range] bins_max = [rng[1] for rng in self.histo_range] inrange_idx = _get_in_range_indices(self.sample[weight_idx], bins_min, bins_max, minop=operator.ge, maxop=operator.le) inrange_idx = weight_idx[inrange_idx] self.assertEqual(result_c[0].sum(), len(inrange_idx), msg=self.state_msg) # we have to sum the weights using the same precision as the # histogramnd function weights_sum = self.weights[inrange_idx].astype(result_c[1].dtype).sum() self.assertTrue(self.array_compare(result_c[1].sum(), weights_sum), msg=self.state_msg)
def test_filter_minmax(self): """ """ result_c = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True, weight_min=self.filter_min, weight_max=self.filter_max) # !!!!!!!!!!!!!!!!!!!!!!!!!!!! filter_min = self.dtype_weights(self.filter_min) filter_max = self.dtype_weights(self.filter_max) weight_idx = _get_in_range_indices(self.weights, filter_min, # <------ !!! filter_max, # <------ !!! minop=operator.ge, maxop=operator.le) result_np = np.histogramdd(self.sample[weight_idx], bins=self.n_bins, range=self.histo_range) result_np_w = np.histogramdd(self.sample[weight_idx], bins=self.n_bins, range=self.histo_range, weights=self.weights[weight_idx]) # comparing "hits" hits_cmp = np.array_equal(result_c[0], result_np[0]) # comparing weights weights_cmp = np.array_equal(result_c[1], result_np_w[0]) self.assertTrue(hits_cmp) self.assertTrue(weights_cmp) bins_min = [rng[0] for rng in self.histo_range] bins_max = [rng[1] for rng in self.histo_range] inrange_idx = _get_in_range_indices(self.sample[weight_idx], bins_min, bins_max, minop=operator.ge, maxop=operator.le) inrange_idx = weight_idx[inrange_idx] self.assertEqual(result_c[0].sum(), len(inrange_idx), msg=self.state_msg) # we have to sum the weights using the same precision as the # histogramnd function weights_sum = self.weights[inrange_idx].astype(result_c[1].dtype).sum() self.assertTrue(self.array_compare(result_c[1].sum(), weights_sum), msg=self.state_msg)
def test_reuse_cumul_float(self): """ """ expected_h_tpl = np.array([0, 2, 1, 1, 1]) expected_c_tpl = np.array([-700.7, -7007.5, -4.99, 300.4, 3503.5], dtype=np.float32) 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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights)[0:2] # converting the cumul array to float cumul = cumul.astype(np.float32) sample_2 = self.sample[:] if len(sample_2.shape) == 1: idx = (slice(0, None), ) else: idx = slice(0, None), self.tested_dim sample_2[idx] += 2 histo, cumul_2 = histogramnd( sample_2, # <==== !! self.histo_range, self.n_bins, weights=10 * self.weights, # <==== !! weighted_histo=cumul)[0:2] self.assertEqual(cumul.dtype, np.float32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertEqual(id(cumul), id(cumul_2)) self.assertTrue(np.allclose(cumul, expected_c, rtol=10e-15))
def test_reuse_cumul_float(self): """ """ expected_h_tpl = np.array([0, 2, 1, 1, 1]) expected_c_tpl = np.array([-700.7, -7007.5, -4.99, 300.4, 3503.5], dtype=np.float32) 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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights)[0:2] # converting the cumul array to float cumul = cumul.astype(np.float32) sample_2 = self.sample[:] if len(sample_2.shape) == 1: idx = [slice(0, None)] else: idx = [slice(0, None), self.tested_dim] sample_2[idx] += 2 histo, cumul_2 = histogramnd(sample_2, # <==== !! self.histo_range, self.n_bins, weights=10 * self.weights, # <==== !! weighted_histo=cumul)[0:2] self.assertEqual(cumul.dtype, np.float32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertEqual(id(cumul), id(cumul_2)) self.assertTrue(np.allclose(cumul, expected_c, rtol=10e-15))
def test_reuse_cumul_float(self): """ """ n_bins = np.array(self.n_bins, ndmin=1) if len(self.sample.shape) == 2: if len(n_bins) == self.sample.shape[1]: shp = tuple([x for x in n_bins]) else: shp = (self.n_bins, ) * self.sample.shape[1] cumul = np.zeros(shp, dtype=np.float32) else: shp = (self.n_bins, ) cumul = np.zeros(shp, dtype=np.float32) result_c_1 = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True, weighted_histo=cumul) result_np_1 = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range) result_np_w_1 = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range, weights=self.weights) # comparing "hits" hits_cmp = np.array_equal(result_c_1[0], result_np_1[0]) self.assertTrue(hits_cmp, msg=self.state_msg) self.assertEqual(result_c_1[1].dtype, np.float32, msg=self.state_msg) bins_min = [rng[0] for rng in self.histo_range] bins_max = [rng[1] for rng in self.histo_range] inrange_idx = _get_in_range_indices(self.sample, bins_min, bins_max, minop=operator.ge, maxop=operator.le) weights_sum = \ self.weights[inrange_idx].astype(np.float32).sum(dtype=np.float64) self.assertTrue(np.allclose(result_c_1[1].sum(dtype=np.float64), weights_sum), msg=self.state_msg) self.assertTrue(np.allclose(result_c_1[1].sum(dtype=np.float64), result_np_w_1[0].sum(dtype=np.float64)), msg=self.state_msg)
def test_reuse_cumul_float(self): """ """ n_bins = np.array(self.n_bins, ndmin=1) if len(self.sample.shape) == 2: if len(n_bins) == self.sample.shape[1]: shp = tuple([x for x in n_bins]) else: shp = (self.n_bins,) * self.sample.shape[1] cumul = np.zeros(shp, dtype=np.float32) else: shp = (self.n_bins,) cumul = np.zeros(shp, dtype=np.float32) result_c_1 = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True, weighted_histo=cumul) result_np_1 = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range) result_np_w_1 = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range, weights=self.weights) # comparing "hits" hits_cmp = np.array_equal(result_c_1[0], result_np_1[0]) self.assertTrue(hits_cmp, msg=self.state_msg) self.assertEqual(result_c_1[1].dtype, np.float32, msg=self.state_msg) bins_min = [rng[0] for rng in self.histo_range] bins_max = [rng[1] for rng in self.histo_range] inrange_idx = _get_in_range_indices(self.sample, bins_min, bins_max, minop=operator.ge, maxop=operator.le) weights_sum = \ self.weights[inrange_idx].astype(np.float32).sum(dtype=np.float64) self.assertTrue(np.allclose(result_c_1[1].sum(dtype=np.float64), weights_sum), msg=self.state_msg) self.assertTrue(np.allclose(result_c_1[1].sum(dtype=np.float64), result_np_w_1[0].sum(dtype=np.float64)), msg=self.state_msg)
def test_uncontiguous_weighted_histo(self): """ """ # non contiguous array shape = np.array(self.n_bins, ndmin=1) shape[0] *= 2 cumul_tmp = np.zeros(shape) cumul = cumul_tmp[::2, ...] expected_txt = ('<weighted_histo> must be a C_CONTIGUOUS numpy array.') ex_str = None try: histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, weighted_histo=cumul) except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str) self.assertEqual(ex_str, expected_txt)
def test_nominal_wo_weights(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=None)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(cumul is None)
def test_nominal_wo_weights(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=None)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(cumul is None)
def test_last_bin_open(self): """ """ result_c = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=False) bins_max = [rng[1] for rng in self.histo_range] filtered_idx = _get_values_index(self.sample, bins_max) result_np = np.histogramdd(self.sample[filtered_idx], bins=self.n_bins, range=self.histo_range) result_np_w = np.histogramdd(self.sample[filtered_idx], bins=self.n_bins, range=self.histo_range, weights=self.weights[filtered_idx]) # comparing "hits" hits_cmp = np.array_equal(result_c[0], result_np[0]) # comparing weights weights_cmp = np.array_equal(result_c[1], result_np_w[0]) self.assertTrue(hits_cmp, msg=self.state_msg) self.assertTrue(weights_cmp, msg=self.state_msg) bins_min = [rng[0] for rng in self.histo_range] bins_max = [rng[1] for rng in self.histo_range] inrange_idx = _get_in_range_indices(self.sample, bins_min, bins_max, minop=operator.ge, maxop=operator.lt) self.assertEqual(result_c[0].sum(), len(inrange_idx), msg=self.state_msg) # we have to sum the weights using the same precision as the # histogramnd function weights_sum = self.weights[inrange_idx].astype(result_c[1].dtype).sum() self.assertTrue(self.array_compare(result_c[1].sum(), weights_sum), msg=self.state_msg)
def test_nbins_values(self): """ """ expected_txt = ('<n_bins> : only positive values allowed.') for err_n_bins in self.err_n_bins_values: test_msg = ('Testing invalid n_bins value : {0}' ''.format(err_n_bins)) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, err_n_bins, weights=self.weights)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_nominal_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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c))
def test_nominal_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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c))
def test_bin_ranges(self): """ """ result_c = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True) result_np = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range) for i_edges, edges in enumerate(result_c[2]): # allclose for now until I can try with the latest version (TBD) # of numpy self.assertTrue(np.allclose(edges, result_np[1][i_edges]), msg='{0}. Testing bin_edges for dim {1}.' ''.format(self.state_msg, i_edges + 1))
def test_bin_ranges(self): """ """ result_c = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, last_bin_closed=True) result_np = np.histogramdd(self.sample, bins=self.n_bins, range=self.histo_range) for i_edges, edges in enumerate(result_c[2]): # allclose for now until I can try with the latest version (TBD) # of numpy self.assertTrue(np.allclose(edges, result_np[1][i_edges]), msg='{0}. Testing bin_edges for dim {1}.' ''.format(self.state_msg, i_edges+1))
def test_nominal_wh_dtype(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.float32) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) self.fill_histo(expected_c, expected_c_tpl, self.ndims - 1) histo, cumul, bin_edges = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, wh_dtype=np.float32) self.assertEqual(cumul.dtype, np.float32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.allclose(cumul, expected_c))
def test_nominal_wh_dtype(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.float32) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) self.fill_histo(expected_c, expected_c_tpl, self.ndims-1) histo, cumul, bin_edges = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, wh_dtype=np.float32) self.assertEqual(cumul.dtype, np.float32) self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.allclose(cumul, expected_c))
def test_nominal_wo_weights_w_histo(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) # creating an array of ones just to make sure that # it is not cleared by histogramnd histo_in = np.ones(self.n_bins, dtype=np.uint32) self.fill_histo(expected_h, expected_h_tpl, self.ndims-1) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=None, histo=histo_in)[0:2] self.assertTrue(np.array_equal(histo, expected_h + 1)) self.assertTrue(cumul is None) self.assertEqual(id(histo), id(histo_in))
def test_nominal_wo_weights_w_histo(self): """ """ expected_h_tpl = np.array([2, 1, 1, 1, 1]) expected_h = np.zeros(shape=self.n_bins, dtype=np.double) # creating an array of ones just to make sure that # it is not cleared by histogramnd histo_in = np.ones(self.n_bins, dtype=np.uint32) self.fill_histo(expected_h, expected_h_tpl, self.ndims - 1) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=None, histo=histo_in)[0:2] self.assertTrue(np.array_equal(histo, expected_h + 1)) self.assertTrue(cumul is None) self.assertEqual(id(histo), id(histo_in))
def test_wh_histo_dtype(self): """ """ # using the same values as histo for err_h_dtype in self.err_histo_dtypes: test_msg = ('Testing invalid wh_dtype dtype : {0}' ''.format(err_h_dtype)) expected_txt = ('<wh_dtype> type not supported : {0}.' ''.format(err_h_dtype)) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights, wh_dtype=err_h_dtype)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_int32_weights_double_weights_range(self): """ """ weight_min = -299.9 # ===> will be cast to -299 weight_max = 499.9 # ===> will be cast to 499 expected_h_tpl = np.array([0, 1, 1, 1, 0]) expected_c_tpl = np.array([0., 0., 0., 300., 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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights.astype(np.int32), weight_min=weight_min, weight_max=weight_max)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c))
def test_weights_shape(self): """ """ for err_w_shape in self.err_weights_shapes: test_msg = ('Testing invalid weights shape : {0}' ''.format(err_w_shape)) err_weights = np.random.randint(0, high=10, size=err_w_shape) err_weights = err_weights.astype(np.double) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=err_weights)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual( ex_str, '<weights> must be an array whose length ' 'is equal to the number of samples.')
def test_nbins_shape(self): """ """ expected_txt = ('n_bins must be either a scalar (same number ' 'of bins for all dimensions) or ' 'an array (number of bins for each ' 'dimension).') for err_n_bins in self.err_n_bins_shapes: test_msg = ('Testing invalid n_bins shape : {0}' ''.format(err_n_bins)) ex_str = None try: histo, cumul = histogramnd(self.sample, self.histo_range, err_n_bins, weights=self.weights)[0:2] except ValueError as ex: ex_str = str(ex) self.assertIsNotNone(ex_str, msg=test_msg) self.assertEqual(ex_str, expected_txt, msg=test_msg)
def test_int32_weights_double_weights_range(self): """ """ weight_min = -299.9 # ===> will be cast to -299 weight_max = 499.9 # ===> will be cast to 499 expected_h_tpl = np.array([0, 1, 1, 1, 0]) expected_c_tpl = np.array([0., 0., 0., 300., 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) histo, cumul = histogramnd(self.sample, self.histo_range, self.n_bins, weights=self.weights.astype(np.int32), weight_min=weight_min, weight_max=weight_max)[0:2] self.assertTrue(np.array_equal(histo, expected_h)) self.assertTrue(np.array_equal(cumul, expected_c))