Пример #1
0
    def plot_spectra(self,string,fig,nrow,nplt):  

        if len(self.pt)>10:
            bins_pt=bayesian_blocks(self.pt)
        else:
            bins_pt=10

        if len(self.eta)>10:
            bins_eta=bayesian_blocks(self.eta)
        else:
            bins_eta=10

        ax=fig.add_subplot(nplt,2,2*nrow-1)
        self.pt_plot=plt.hist(self.pt,bins=bins_pt,facecolor='r',alpha=0.5,density=False)
        plt.title(string)
        plt.xlabel(r'$p_t$ [GeV]')
        plt.ylabel('MC Events')
        ax.grid(linestyle='--',linewidth=1)

        ax=fig.add_subplot(nplt,2,nrow*2)
        self.eta_plot=plt.hist(self.eta,bins=bins_eta,facecolor='r',alpha=0.5,density=False)
        plt.title(string)
        plt.xlabel(r'$\eta$')
        plt.ylabel('MC Events')
        ax.grid(linestyle='--',linewidth=1)
Пример #2
0
def test_bayesian_blocks(cmdopt, data_gen):

    be1 = bayesian_blocks(data_gen[0], p0=0.05)
    be2 = bayesian_blocks(data_gen[0], gamma=0.1)
    be3 = bayesian_blocks(data_gen[0], weights=data_gen[2])

    if cmdopt == "generate":
        with open(answer_dir + "/answers_bayesian_blocks.npz", "wb") as f:
            np.savez(f, be1=be1, be2=be2, be3=be3)
    elif cmdopt == "test":
        answers = np.load(answer_dir + "/answers_bayesian_blocks.npz")
        assert np.all(be1 == answers["be1"])
        assert np.all(be2 == answers["be2"])
        assert np.all(be3 == answers["be3"])
Пример #3
0
 def plot(self,
          fig,
          bins=None,
          xlabel=None,
          loglog=False,
          loglin=False,
          linlog=False,
          density=None,
          weights=None,
          histtype='step',
          rwidth=None,
          color=None,
          label=None,
          stacked=False,
          lw=1.0):
     if bins == 'bayesian_blocks':
         bins = bayesian_blocks(self.spectrum)
     self.ax = fig.add_subplot(1, 1, 1)
     plot = plt.hist(self.spectrum,
                     bins=bins,
                     density=density,
                     weights=weights,
                     lw=lw,
                     histtype=histtype,
                     color=color,
                     label=label,
                     stacked=stacked)
     if loglog:
         plt.yscale('log')
         plt.xscale('log')
     if linlog:
         plt.yscale('log')
     if loglin:
         plt.xscale('log')
     plt.xlabel(xlabel)
     if density:
         plt.ylabel('')
     else:
         plt.ylabel('Events')
     self.ax.grid(linestyle='--', linewidth=0.75)
     plt.legend()
     plt.tight_layout()
Пример #4
0
    def _df_binning_init(self, data, weights):
        '''Do an initial binning to get bin edges, total hist range, and break each set of data and
        weights into a dataframe (easier to handle errorbar calculation moving forward)'''

        # If a range is given, mask data using that range to allow smart binning algos to
        # bin data properly.  This will impact a predetermined bin-edge input.

        if self.bin_range is not None:
            for d in range(self.n_data_sets):
                range_mask = (data[d] >= self.bin_range[0]) & (
                    data[d] <= self.bin_range[-1])
                data[d] = data[d][range_mask]
            if weights is not None:
                weights[d] = weights[d][range_mask]

        # If bin edges are already determined, than skip initial histogramming
        self.bin_edges = None
        if isinstance(self.bins, Iterable) and not isinstance(self.bins, str):
            self.bin_edges = self.bins
            if self.bin_range is None:
                self.bin_range = (self.bin_edges[0], self.bin_edges[-1])
            else:
                # If range is given, it gets priority over self-defined bins
                range_mask = (self.bin_edges >= self.bin_range[0]) & (
                    self.bin_edges <= self.bin_range[-1])
                self.bin_edges = self.bin_edges[range_mask]

        # If bin edges need to be determined, there's a few different cases to consider
        else:
            if self.stacked:
                _n_data_sets = 1
                b_data = [np.concatenate(data)]
                if self.has_weights:
                    b_weights = [np.concatenate(weights)]
                else:
                    b_weights = None
            else:
                _n_data_sets = self.n_data_sets
                b_data = data
                b_weights = weights

            if self.bin_range is None:
                xmin = np.inf
                xmax = -np.inf
                for i in range(_n_data_sets):
                    if len(data[i]) > 0:
                        xmin = min(xmin, min(b_data[i]))
                        xmax = max(xmax, max(b_data[i]))
                self.bin_range = (xmin, xmax)

            # Special case for Bayesian Blocks
            if self.bins in ['block', 'blocks']:

                # Single data-set or stacked
                if _n_data_sets == 1:

                    if self.has_weights:
                        b_weights = b_weights[0]
                    else:
                        b_weights = None
                    self.bin_edges = bayesian_blocks(data=b_data[0],
                                                     weights=b_weights,
                                                     **self.bin_dict)
                else:
                    raise ValueError(
                        'Cannot use Bayesian Blocks with multiple, unstacked datasets'
                    )

            else:
                _, self.bin_edges = np.histogram(b_data,
                                                 bins=self.bins,
                                                 weights=b_weights,
                                                 range=self.bin_range)

        self.widths = np.diff(self.bin_edges)
        self.bin_centers = self.bin_edges[:-1] + self.widths * 0.5

        # Now put the data into dataframes with the weights and bins
        self.df_list = []
        for i in range(self.n_data_sets):
            if weights is None:
                df = pd.DataFrame({'data': data[i]})
            else:
                df = pd.DataFrame({'data': data[i], 'weights': weights[i]})
            df_bins = pd.cut(df.data, self.bin_edges, include_lowest=True)
            df['bins'] = df_bins
            self.df_list.append(df)

        # Make the initial histograms
        if self.histtype == 'marker':
            self.bin_content, _ = np.histogram(data,
                                               self.bin_edges,
                                               weights=weights,
                                               range=self.bin_range)
        else:
            self.bin_content, _, self.vis_object = self.ax.hist(
                data,
                self.bin_edges,
                weights=weights,
                range=self.bin_range,
                stacked=self.stacked,
                **self.hist_dict)

            # if self.stacked and self.errorbars and self.histtype == 'stepfilled':
            #     plt.setp(self.vis_object[-1][0], edgecolor='k')
            #     plt.setp(self.vis_object[-1][0], linewidth=2)

        self.bin_content_orig = self.bin_content[:]

        if self.errorbars == 'calc' and not (self.normed or self.scale):
            self.calc_bin_error(hist_mod='default')