From 08b3ced9acf4ff9e0f30d4d532767b25a8db3040 Mon Sep 17 00:00:00 2001 From: Joey Howlett Date: Fri, 7 May 2021 08:52:10 -0400 Subject: [PATCH] move nD binning to parent class --- GOFevaluation/evaluators_nd.py | 18 +++--------------- GOFevaluation/test_statistics.py | 6 +++++- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/GOFevaluation/evaluators_nd.py b/GOFevaluation/evaluators_nd.py index bd0226c..21f10a6 100644 --- a/GOFevaluation/evaluators_nd.py +++ b/GOFevaluation/evaluators_nd.py @@ -5,19 +5,7 @@ from GOFevaluation import test_statistics -class nd_test_statistics(test_statistics): - """ - Override binning to work in arbitrary dimensions - """ - def bin_data(self, bin_edges): - # function to bin nD data: - if len(self.data.shape)==1: - self.binned_data, _ = np.histogram(self.data, bins=bin_edges) - else: - self.binned_data, _ = np.histogramdd(self.data, bins=bin_edges) - - -class binned_poisson_chi2_gof(nd_test_statistics): +class binned_poisson_chi2_gof(test_statistics): """ computes the binned poisson modified Chi2 from Baker+Cousins In the limit of large bin counts (10+) this is Chi2 distributed. @@ -41,7 +29,7 @@ def from_binned(cls, data, expectations): In this case the bin-edges don't matter, so we bypass the usual init """ self = cls(None, None, None, None) - nd_test_statistics.__init__(self=self, + test_statistics.__init__(self=self, data=data, pdf=expectations / np.sum(expectations), nevents_expected=np.sum(expectations)) @@ -56,7 +44,7 @@ def __init__(self, data, pdf, bin_edges, nevents_expected): # bypass init, using binned data return # initialise with the common call signature - nd_test_statistics.__init__(self=self, + test_statistics.__init__(self=self, data=data, pdf=pdf, nevents_expected=nevents_expected) diff --git a/GOFevaluation/test_statistics.py b/GOFevaluation/test_statistics.py index 7233e3a..bcf966f 100644 --- a/GOFevaluation/test_statistics.py +++ b/GOFevaluation/test_statistics.py @@ -13,7 +13,11 @@ def calculate_gof(self): raise NotImplementedError("Your goodnes of fit computation goes here!") def bin_data(self, bin_edges): - self.binned_data, _ = np.histogram(self.data, bins=bin_edges) + # function to bin nD data: + if len(self.data.shape)==1: + self.binned_data, _ = np.histogram(self.data, bins=bin_edges) + else: + self.binned_data, _ = np.histogramdd(self.data, bins=bin_edges) def get_result_as_dict(self): assert self._name is not None, str(self.__class__.__name__) + ": You need to define self._name for your goodnes of fit measure!"