def test_invalid_histo_range(self):
        data = np.random.random((60, 60))
        nbins = 10

        with self.assertRaises(ValueError):
            histo_range = data.min(), np.inf

            Histogramnd(sample=data.ravel(),
                        histo_range=histo_range,
                        n_bins=nbins)

            histo_range = data.min(), np.nan

            Histogramnd(sample=data.ravel(),
                        histo_range=histo_range,
                        n_bins=nbins)
    def test_accumulate_no_weights(self):
        """
        """

        expected_h_tpl = np.array([2, 3, 2, 2, 2])
        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_inst = Histogramnd(self.sample,
                                 self.histo_range,
                                 self.n_bins,
                                 weights=self.weights)

        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_inst.accumulate(sample_2)  # <==== !!

        histo = histo_inst.histo
        cumul = histo_inst.weighted_histo
        bin_edges = histo_inst.edges

        self.assertEqual(cumul.dtype, np.float64)
        self.assertTrue(np.array_equal(histo, expected_h))
        self.assertTrue(np.allclose(cumul, expected_c, rtol=10e-15))
    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 = Histogramnd(sampleB,
                                 self.histo_range,
                                 self.n_bins,
                                 weights=self.weights)

        histo_inst = Histogramnd(sampleL,
                                 self.histo_range,
                                 self.n_bins,
                                 weights=self.weights)
    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_empty_init_accumulate(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_inst = Histogramnd(None, self.histo_range, self.n_bins)

        histo_inst.accumulate(self.sample, weights=self.weights)

        histo = histo_inst.histo
        cumul = histo_inst.weighted_histo
        bin_edges = histo_inst.edges

        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_nominal_no_sample(self):
        """
        """

        histo_inst = Histogramnd(None, self.histo_range, self.n_bins)

        histo, weighted_histo, edges = histo_inst

        self.assertIsNone(histo)
        self.assertIsNone(weighted_histo)
        self.assertIsNone(edges)
        self.assertIsNone(histo_inst.histo)
        self.assertIsNone(histo_inst.weighted_histo)
        self.assertIsNone(histo_inst.edges)
    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_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_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_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))