def setup_two_channel_mct(self):
        mct = MultiChannelTemplate()

        for channel_name in self.channels:
            mct.define_channel(name=channel_name,
                               bins=self.bins,
                               range=self.range)

        for process in self.processes:
            mct.define_process(process)

        for iris_channel, name in zip([channel_1, channel_2], self.channels):
            setosa = iris_channel.query("class_id==1")["sepal_length"].values
            versicolor = iris_channel.query(
                "(class_id==2)")["sepal_length"].values
            virginica = iris_channel.query(
                "(class_id==3)")["sepal_length"].values

            hsetosa = Hist1d(self.bins, range=self.range, data=setosa)
            tsetosa = Template1d("setosa", "length", hsetosa)
            hversico = Hist1d(self.bins, range=self.range, data=versicolor)
            tversico = Template1d("versicolor", "length", hversico)
            hvirgini = Hist1d(self.bins, range=self.range, data=virginica)
            tvirgini = Template1d("virginica", "length", hvirgini)

            mct.add_template(name, "setosa", tsetosa)
            mct.add_template(name, "versicolor", tversico)
            mct.add_template(name, "virginica", tvirgini)

        return mct
    def test_add_data_as_dict(self):

        mct = self.setup_two_channel_mct()

        hdata_ch1 = Hist1d(self.bins,
                           self.range,
                           data=channel_1["sepal_length"])
        hdata_ch2 = Hist1d(self.bins,
                           self.range,
                           data=channel_2["sepal_length"])
        mct.add_data(**{"Test1": hdata_ch1, "Test2": hdata_ch2})

        for channel in mct.channels.values():
            self.assertTrue(channel.has_data)
Пример #3
0
 def add_variation(self, data, weights_up, weights_down):
     """Add a new covariance matrix from a given systematic variation
     of the underlying histogram to the template."""
     hup = Hist1d(bins=self._hist.num_bins,
                  range=self._range,
                  data=data,
                  weights=weights_up)
     hdown = Hist1d(bins=self._hist.num_bins,
                    range=self._range,
                    data=data,
                    weights=weights_down)
     cov_mat = get_systematic_cov_mat(self._flat_bin_counts,
                                      hup.bin_counts.flatten(),
                                      hdown.bin_counts.flatten())
     self._add_cov_mat(cov_mat)
Пример #4
0
    def test_hist_from_array_bins(self):
        bins = np.linspace(0, 10, 11)
        iris_bc, iris_be = np.histogram(self.iris_sepal_length, bins=bins)
        iris_hist = Hist1d(data=self.iris_sepal_length, bins=bins)

        np.testing.assert_array_equal(iris_hist.bin_counts, iris_bc)
        np.testing.assert_array_equal(iris_hist.bin_edges, iris_be)
Пример #5
0
    def test_range(self):
        nbins = 10
        range = (0, 10)
        iris_hist = Hist1d(data=self.iris_sepal_length,
                           bins=nbins,
                           range=range)

        self.assertEqual(iris_hist.range, (0, 10))
Пример #6
0
    def test_add_not_comp_data(self):
        hiris = Hist1d(2, hist_range=self.range, data=iris_data)
        with self.assertRaises(RuntimeError) as e:
            self.channel.add_data(hiris)

        self.assertEqual(
            "Given data histogram is not compatible with the Channel.",
            str(e.exception))
Пример #7
0
    def test_empty_template(self):
        hist = Hist1d(bins=self.bins, range=self.range)
        template = Template1d("test", "test_var", hist)

        np.testing.assert_array_equal(template.values, hist.bin_counts)
        np.testing.assert_array_equal(template.errors, np.zeros(self.bins))
        np.testing.assert_array_equal(template.fractions(np.zeros(self.bins)),
                                      np.zeros(self.bins))
Пример #8
0
    def test_nll_contrib_is_scalar(self):
        np.random.seed(5)
        yields = np.random.randn(self.num_templates) + 50
        nui_params = np.random.randn(self.num_templates * self.bins)
        hiris = Hist1d(self.bins, hist_range=self.range, data=iris_data)
        self.channel.add_data(hiris)

        nll_contrib = self.channel.nll_contribution(yields, nui_params)
        self.assertEqual(type(nll_contrib), np.float64)
Пример #9
0
    def test_add_non_comp_template(self):
        with self.assertRaises(RuntimeError) as e:
            hsetosa = Hist1d(5, (3, 4), data=setosa)
            self.channel.add_template('bla',
                                      Template1d('test', 'test', hsetosa))

        self.assertEqual(
            "Trying to add a non compatible template with the Channel.",
            str(e.exception))
    def setUp(self):
        self.bins = 10
        self.range = (3, 8)
        self.variable = "sepal_length"

        hsetosa = Hist1d(self.bins, range=self.range, data=setosa)
        self.tsetosa = Template1d("setosa", "length", hsetosa)
        hversico = Hist1d(self.bins, range=self.range, data=versicolor)
        self.tversico = Template1d("versicolor", "length", hversico)
        hvirgini = Hist1d(self.bins, range=self.range, data=virginica)
        self.tvirgini = Template1d("virginica", "length", hvirgini)

        self.processes = ("setosa", "versicolor", "virginica")

        self.channels = ["Test1", "Test2"]

        self.templates = [self.tsetosa, self.tversico, self.tvirgini]
        self.num_templates = len(self.templates)
        self.efficiencies = 0.8
Пример #11
0
    def test_from_binned_data(self):
        bins = np.linspace(0, 10, 11)
        iris_bc, iris_be = np.histogram(self.iris_sepal_length, bins=bins)

        iris_hist = Hist1d.from_binned_data(bin_counts=iris_bc,
                                            bin_edges=iris_be)

        np.testing.assert_array_equal(iris_hist.bin_counts, iris_bc)
        np.testing.assert_array_equal(iris_hist.bin_errors, np.sqrt(iris_bc))
        np.testing.assert_array_equal(iris_hist.bin_edges, iris_be)
Пример #12
0
    def test_hist_with_range_option(self):
        nbins = 10
        range = (4, 6)
        iris_bc, iris_be = np.histogram(self.iris_sepal_length,
                                        bins=nbins,
                                        range=range)
        iris_hist = Hist1d(data=self.iris_sepal_length,
                           bins=nbins,
                           range=range)

        np.testing.assert_array_equal(iris_hist.bin_counts, iris_bc)
        np.testing.assert_array_equal(iris_hist.bin_edges, iris_be)
Пример #13
0
    def test_weighted_data(self):
        bins = np.linspace(0, 10, 11)
        weights = np.random.normal(1, 0.1, (len(self.iris_sepal_length)))
        iris_bc, iris_be = np.histogram(self.iris_sepal_length,
                                        weights=weights,
                                        bins=bins)
        iris_hist = Hist1d(data=self.iris_sepal_length,
                           weights=weights,
                           bins=bins)

        np.testing.assert_array_almost_equal(iris_hist.bin_counts, iris_bc)
        np.testing.assert_array_equal(iris_hist.bin_edges, iris_be)
Пример #14
0
    def setUp(self):

        self.bins = 10
        self.range = (3, 8)
        self.variable = "length"

        hsetosa = Hist1d(self.bins, hist_range=self.range, data=setosa)
        self.tsetosa = Template1d("setosa", "length", hsetosa)
        hversico = Hist1d(self.bins, hist_range=self.range, data=versicolor)
        self.tversico = Template1d("versicolor", "length", hversico)
        hvirgini = Hist1d(self.bins, hist_range=self.range, data=virginica)
        self.tvirgini = Template1d("virginica", "length", hvirgini)

        self.channel = Channel("test", self.bins, self.range)

        self.processes = ["setosa", "versicolor", "virginica"]
        self.templates = [self.tsetosa, self.tversico, self.tvirgini]
        self.num_templates = len(self.templates)
        self.efficiencies = 0.8
        for process, template in zip(self.processes, self.templates):
            self.channel.add_template(process,
                                      template,
                                      efficiency=self.efficiencies)
Пример #15
0
    def test_hist_from_int_bin(self):
        nbins = 10
        range = (0, 10)
        iris_bc, iris_be = np.histogram(self.iris_sepal_length,
                                        bins=nbins,
                                        range=range)
        iris_hist = Hist1d(data=self.iris_sepal_length,
                           bins=nbins,
                           range=range)

        np.testing.assert_array_equal(iris_hist.bin_counts, iris_bc)
        np.testing.assert_array_equal(iris_hist.bin_edges, iris_be)

        self.assertFalse(iris_hist.is_empty)
Пример #16
0
    def test_hist_bin_mids_widths(self):
        nbins = 10
        range = (0, 10)
        iris_bc, iris_be = np.histogram(self.iris_sepal_length,
                                        bins=nbins,
                                        range=range)
        iris_hist = Hist1d(data=self.iris_sepal_length,
                           bins=nbins,
                           range=range)

        mids = (iris_be[:-1] + iris_be[1:]) / 2
        widths = iris_be[1:] - iris_be[:-1]

        np.testing.assert_array_equal(iris_hist.bin_mids, mids)
        np.testing.assert_array_equal(iris_hist.bin_widths, widths)
Пример #17
0
    def setUp(self):
        self.data = np.array([
            5.1, 4.9, 4.7, 4.6, 5., 5.4, 4.6, 5., 4.4, 4.9, 5.4, 4.8, 4.8, 4.3,
            5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5., 5., 5.2,
            5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5., 5.5, 4.9, 4.4, 5.1, 5., 4.5,
            4.4, 5., 5.1, 4.8, 5.1, 4.6, 5.3, 5., 7., 6.4, 6.9, 5.5, 6.5, 5.7,
            6.3, 4.9, 6.6, 5.2, 5., 5.9, 6., 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6,
            5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6., 5.7, 5.5, 5.5, 5.8, 6.,
            5.4, 6., 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5., 5.6, 5.7, 5.7, 6.2,
            5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5,
            6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7, 6., 6.9, 5.6, 7.7, 6.3,
            6.7, 7.2, 6.2, 6.1, 6.4, 7.2, 7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3,
            6.4, 6., 6.9, 6.7, 6.9, 5.8, 6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9
        ])

        self.bins = 10
        self.range = (2, 7)
        self.hist = Hist1d(bins=self.bins, range=self.range, data=self.data)
        self.template = Template1d("test", "test_var", self.hist)
Пример #18
0
 def test_add_data(self):
     hiris = Hist1d(self.bins, hist_range=self.range, data=iris_data)
     self.channel.add_data(hiris)
Пример #19
0
 def test_toy_data_set(self):
     htoy = self.channel.generate_toy_dataset()
     hiris = Hist1d(self.bins, hist_range=self.range, data=iris_data)
     self.assertEqual(htoy.bin_counts.shape, hiris.bin_counts.shape)
     self.assertEqual(htoy.bin_edges.shape, hiris.bin_edges.shape)
     self.assertEqual(htoy.bin_errors.shape, hiris.bin_errors.shape)
Пример #20
0
 def test_asimov_data_set(self):
     hasimov = self.channel.generate_asimov_dataset()
     hiris = Hist1d(self.bins, hist_range=self.range, data=iris_data)
     np.testing.assert_array_equal(hasimov.bin_counts, hiris.bin_counts)
     np.testing.assert_array_equal(hasimov.bin_edges, hiris.bin_edges)
     np.testing.assert_array_equal(hasimov.bin_errors, hiris.bin_errors)
Пример #21
0
 def y_projection(self):
     return Hist1d.from_binned_data(
         np.sum(self.bin_counts, axis=0),
         self.y_edges,
         np.sqrt(np.sum(self._bin_errors_sq, axis=0))
     )
Пример #22
0
 def test_add_data_wo_templates(self):
     empty_channel = Channel("test_empty", self.bins, self.range)
     hiris = Hist1d(self.bins, hist_range=self.range, data=iris_data)
     empty_channel.add_data(hiris)
Пример #23
0
    def test_empty_template(self):
        hist = Hist1d(bins=self.bins, hist_range=self.range)
        template = Template1d("test", "test_var", hist)

        self.assertEqual(template.yield_param, 0)