b2plot package

Submodules

b2plot.analysis module

Analysis tools

Author:
b2plot.analysis.data_mc_ratio(data, mc, label_data='Data', label_mc='MC', y_label=None, figsize=None, ratio_range=(0, 2), *args, **kwarg)[source]

Perform data mc distributions

Returns:axes
b2plot.analysis.df_var_by2xy()[source]
b2plot.analysis.divhist(h1, h2)[source]
b2plot.analysis.divideEfficiency(n_nom, n_denom, confidence=0.683)[source]

divides two histograms for an efficiency calculation

Parameters:
  • n_nom – y values of nominator histogram (1d or 2d)
  • n_denom – y values of denominator histogram (1d or 2d)
  • confidence – (optional) confidence level, by default 0.683
Returns:

ratio, [lower ratio error, upper ratio error]

b2plot.analysis.exact_CI(k, n, conf=0.683)[source]

calculated clopper pearson confidence intervals

Parameters:
  • k – number of passes (“numerator of proportion”)
  • n – total sample size (“denominator of proportion”)
  • conf – (optional) confidence level, by default 0.683
Returns:

ratio, [upper ratio error, lower ratio error]

b2plot.analysis.flat_bins(x, set=False, nbins=None, fontsize=None, rotation=-90)[source]
b2plot.analysis.get_lower_lim(x, perc=0.1, width=1, maxtries=100)[source]
b2plot.analysis.get_upper_lim(x, perc=0.1, width=1, maxtries=100)[source]
b2plot.analysis.mask_append(xs, xb)[source]

Merge xs and xb into one vector and return it with a boolean mask for each category

b2plot.analysis.minmax(x, perc=0.1, width=1, maxtries=100)[source]
b2plot.analysis.optimal_bin_size(n)[source]

Is this empricially the best?

b2plot.analysis.plot_feature_importance(fi, cols, figsize=None, palette='Blues_d', ax=None, *args, **kwargs)[source]
b2plot.analysis.plot_flatness(sig, tag, bins=None, ax=None, xrange=None, percent_step=5)[source]

Plotting differences of sig distribution in percentiles of tag distribution

Parameters:
  • sig
  • tag
  • bins
  • ax
  • xrange
  • percent_step
b2plot.analysis.pur_eff(x, mask, nbins=None, reverse_too=False)[source]

Plots the distribution x in an equal frequency binning with the purity regarding mask

Parameters:
  • x – Distribution or signal distribution
  • mask – Boolean mask or background distribution
  • nbins
  • do_plot
  • figsize
  • xticks_fontsize

Returns:

b2plot.analysis.pur_eff_cont(x, mask)[source]

Continuus evaluation of the purity vs efficiency

Returns:arrays of len(x)
Return type:efficiency, purity
b2plot.analysis.purity_flatness_proba(x, mask, nbins=10, do_plot=False)[source]

Returns the probability that the purity of x[mask] and x[~mask] is flat.

This can be used as a measure of the information content in this observable regarding the mask.

Parameters:
  • x – Observable or signal distribution
  • mask – Boolean mask, like ‘is signal’, ‘is background’ or background distribution
  • nbins – Number of bins to calculate the purity
  • do_plot (bool) – plot the purity distribution
Returns:

probability of CDF(flat) against CDF(purity), where a value of 1 corresponds to the purity being compatible with a flat distribution.

Return type:

chi2

b2plot.analysis.purity_hist(x, mask, nbins=10, do_plot=True, figsize=None, xticks_fontsize=None, ax=None)[source]

Plots the distribution x in an equal frequency binning with the purity regarding mask

Parameters:
  • x – Distribution or signal distribution
  • mask – Boolean mask or background distribution
  • nbins
  • do_plot
  • figsize
  • xticks_fontsize
  • ax

Returns:

b2plot.analysis.ratio(y1, y2, y1_err=None, y2_err=None)[source]

calculate the ratio between two histograms y1/y2

Parameters:
  • y1 – y values of first histogram
  • y2 – y values of second histogram
  • y1_err – (optional) error of first
  • y2_err – (optional) error of second
Returns:

ratio, ratio_error

b2plot.analysis.sig_bkg_plot(df, col, by=None, ax=None, bins=None, range=None, labels=None, normed=False)[source]
Parameters:
  • df
  • col
  • by
  • ax
  • bins
  • range
  • labels
  • normed

Returns:

b2plot.analysis.subhist(h1, h2, rethist=False)[source]

b2plot.colors module

Color definitions

b2plot.colors.b2blues(n=5)[source]
b2plot.colors.b2greens(n=3)[source]
b2plot.colors.b2helix(n)[source]
b2plot.colors.cm(n)[source]

b2plot.correlations module

Tools for studying correlations

Author:
b2plot.correlations.corrmatrix(corr, separate_first=0, x_label_rot=45, invert_y=True, label_font_size=None, ax=None, *args, **kwargs)[source]

Recommendation:

with plt.style.context([‘default’,’seaborn-bright’]):
corrmatrix(corrm_s,separate_first=2)
b2plot.correlations.corrplot(data, size_scale=500, marker='s', tfs=12, separate_first=0, *args, **kwargs)[source]

Correlation plot

Based on: https://towardsdatascience.com/better-heatmaps-and-correlation-matrix-plots-in-python-41445d0f2bec

b2plot.correlations.flat_corr_matrix(df, pdf=None, tight=False, labels=None, label_size=None, size=12, n_labels=3, fontsize='auto', draw_cbar=False, tick_label_rotation=45, formatter='%.2e', label_rotation=45, cmap='PiYG')[source]

Draws a flat correlation matrix of df

Parameters:
  • df
  • pdf
  • tight
  • col_numbers
  • labels
  • label_size
  • size
  • n_labels
  • fontsize
  • draw_cbar
  • rotation
  • formatter

Returns:

b2plot.correlations.flat_correlation(x, y, nbins='auto', zoom=1, nlabels=5, ax=None, ax_fmt='%.2e', x_label_rot=45, invert_y=True, draw_labels=True, get_im=False, cmap='jet')[source]

Calculate and plot a 2D correlation in flat binning. This function calculates an equal frequency binning for x and y and fills a 2D histogram with this binning. Thus each slice in x and y contains the same number of entries for continuus distributions. For uncorrelated distributions the expected amount of each bin is N_expected = N_total / N_bins**2 This plot shows the statistical significance of the deviation from N_expected.

Parameters:
  • x – array of values to be binned in x direction
  • y – array of values to be binned in y direction
  • nbins – int or ‘auto’, number of bins in x and y
  • zoom – factor f of the significance [-f*5,f*5]
  • nlabels – number of x,y labels
  • ax – axes, if None, takes current
  • ax_fmt – formatter for tick labls
  • x_label_rot – rotation for x labels
Returns:

chi2 probability for flat distribution

b2plot.correlations.heatmap(x, y, tfs=12, bkg_color='#F1F1F1', separate_first=0, **kwargs)[source]

Calculate a heatmap

Based on: https://towardsdatascience.com/better-heatmaps-and-correlation-matrix-plots-in-python-41445d0f2bec

b2plot.correlations.set_flat_labels(ax, x, n_labels=5, axis=1, labelsize=12, rotation=45, formatter='%.3e')[source]

Helper function to draw the correct x-labels to a flat plot

Parameters:
  • ax
  • x
  • n_labels
  • axis
  • labelsize
  • rotation
  • formatter

Returns:

b2plot.decorations module

b2plot.decorations.decorate(*args, **kwargs)[source]
b2plot.decorations.draw_y_label(label='Entries', unit=None, ha='right', brackets=True, ax=None, *args, **kwargs)[source]

Plotting scientific notation y label

Parameters:
  • label
  • unit
  • ha
  • brackets
  • *args
  • **kwargs

Returns:

b2plot.decorations.expand(factor=1.2)[source]
b2plot.decorations.labels(xlabel=None, ylabel=None, unit=None, root_style=False, brackets=True, overwrite=None, ax=None, *args, **kwargs)[source]
b2plot.decorations.lumi(l='$5\\; \\mathrm{pb}^{-1}$', px=0.033, py=0.839, *args, **kwargs)[source]
b2plot.decorations.set_style()[source]
b2plot.decorations.text(t, px=0.033, py=0.763, *args, **kwargs)[source]
b2plot.decorations.watermark(t=None, logo='Belle II', px=0.033, py=0.915, fontsize=16, alpha=0.8, alpha_logo=0.95, shift=0.15, bstyle='italic', *args, **kwargs)[source]
Parameters:
  • t
  • logo
  • px
  • py
  • fontsize
  • alpha
  • shift
  • *args
  • **kwargs

Returns:

b2plot.functions module

In this file all the matplolib wrappers are located.

b2plot.functions.save(filename, *args, **kwargs)[source]

Save a file and do the subplot_adjust to fit the page with larger labels

Parameters:
  • filename
  • *args
  • **kwargs

Returns:

b2plot.functions.save_adjust(filename, bottom=0.15, left=0.13, right=0.96, top=0.95, *args, **kwargs)[source]

Save a file and do the subplot_adjust to fit the page with larger labels

Args:
filename: bottom: left: right: top: *args: **kwargs:
bbox_inches=’tight’,
Returns:
b2plot.functions.text(t, x=0.8, y=0.9, fontsize=22, *args, **kwargs)[source]
Parameters:
  • t
  • x
  • y
  • fontsize
  • args
  • kwargs
Returns:

b2plot.functions.xlim(low=None, high=None, ax=None)[source]
Parameters:
  • low
  • high
  • ax

Returns:

b2plot.helpers module

Helper function and classes are defined here.

class b2plot.helpers.Singleton(decorated)[source]

Bases: object

A non-thread-safe helper class to ease implementing singletons. This should be used as a decorator – not a metaclass – to the class that should be a singleton.

The decorated class can define one __init__ function that takes only the self argument. Other than that, there are no restrictions that apply to the decorated class.

To get the singleton instance, use the Instance method. Trying to use __call__ will result in a TypeError being raised.

Limitations: The decorated class cannot be inherited from.

Instance()[source]

Returns the singleton instance. Upon its first call, it creates a new instance of the decorated class and calls its __init__ method. On all subsequent calls, the already created instance is returned.

b2plot.helpers.figure(*args, **kwargs)[source]
b2plot.helpers.get_optimal_bin_size(n)[source]

This function calculates the optimal amount of bins for the number of events n. :param n: number of Events :return: optimal bin size

b2plot.helpers.hist2root(h, name=None, title='')[source]

Convert your histograms to a root histogram.. if you really want to

Parameters:
  • h – array with the histogram h=(y_values, bin_edges, (patches=optional))
  • name – Name for the root histogram, optional
  • title – Title for the root histogram, optional
Returns:

TH1F or TH2F depending on the input array

Examples

>>> data = [1,2,3,4,5,6,7]
>>> h = b2plot.hist(data)
>>> from b2plot.helpers import hist2root
>>> root_hist = hist2root(h)
b2plot.helpers.nf()[source]
b2plot.helpers.replot()[source]
b2plot.helpers.xaxis()[source]

b2plot.histogram module

In this file all the histogram related functions.

b2plot.histogram.bar(y, binedges, ax=None, *args, **kwargs)[source]

Bar plot

Parameters:
  • y
  • binedges
  • ax
  • *args
  • **kwargs
b2plot.histogram.errorbar(bin_centers, y, y_err, x_err=None, box=False, plot_zero=True, fmt='.', color=None, ax=None, label=None, alpha=0.4, hatch=None, *args, **kwargs)[source]

Error graph plotting x-y points with errorbars

Parameters:
  • bin_centers
  • y
  • y_err
  • x_err
  • box
  • plot_zero
  • fmt
  • color
  • ax
  • label
  • alpha
  • hatch
  • *args
  • **kwargs – w
b2plot.histogram.errorhist(data, bins=None, color=None, normed=False, density=False, fmt='.', range=None, scale=None, x_err=False, box=False, ax=None, weights=None, plot_zero=True, label=None, *args, **kwargs)[source]

Histogram as error bar

Parameters:
  • data
  • bins
  • color
  • normed
  • density
  • fmt
  • range
  • scale
  • x_err
  • box
  • ax
  • weights
  • plot_zero
  • label
  • *args
  • **kwargs

Returns:

b2plot.histogram.flat_x(x, nbins=25)[source]
b2plot.histogram.get_xaxis()[source]
b2plot.histogram.hist(data, bins=None, fill=False, range=None, lw=1.0, ax=None, style=None, color=None, scale=None, weights=None, label=None, edgecolor=None, fillalpha=0.5, *args, **kwargs)[source]
Parameters:
  • data
  • bins
  • fill
  • range
  • lw
  • ax
  • style
  • color
  • scale
  • weights
  • *args
  • **kwargs

Returns:

b2plot.histogram.profile(x, y, bins=None, range=None, fmt='.', *args, **kwargs)[source]

Profile plot of x vs y; the mean and std of y in bins of x as errorbar

Parameters:
  • x
  • y
  • bins
  • range
  • fmt
  • *args
  • **kwargs

Returns:

b2plot.histogram.set_xaxis(bins, flat=False)[source]
b2plot.histogram.stacked(df, col=None, by=None, bins=None, color=None, range=None, lw=0.5, ax=None, edgecolor='black', weights=None, scale=None, label=None, transform=None, *args, **kwargs)[source]

Create stacked histogram

Parameters:
  • df (DataFrame or list of arrays) –
  • col
  • by
  • bins
  • color
  • lw
  • *args
  • **kwargs

Returns:

b2plot.histogram.to_stack(df, col, by, transform=None, get_cats=False)[source]

Convert columns of a dataframe to a list of lists by ‘by’

Parameters:
  • df
  • col
  • by
  • transform

Returns:

b2plot.mva module

Tools to analyse data for mva trainings

b2plot.mva.mva_input_plot(x_sig, x_bkg, density=True, log=False, *args, **kwargs)[source]
b2plot.mva.plot_features(df, variables, condition, *args, **kwargs)[source]

b2plot.transform module

Transfromation of Data

class b2plot.transform.CDF(*args)[source]

Bases: b2plot.transform.Transform

Calculates the cummulative distribution (CDF)

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.MapTo(fromlow, fromhigh, tolow, tohigh, limit=False, *args)[source]

Bases: b2plot.transform.Transform

Linear map to some values

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.MySpline(x, y)[source]

Bases: object

can be pickled

class b2plot.transform.To11(*args)[source]

Bases: b2plot.transform.Transform

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.ToFlat(x=None, *args)[source]

Bases: b2plot.transform.Transform

This transformation uses the CDF to transform input data to a flat transformation.

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.ToGauss(*args)[source]

Bases: b2plot.transform.Transform

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.ToNorm[source]

Bases: b2plot.transform.Transform

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.ToRawPurity(n_bins=None)[source]

Bases: b2plot.transform.Transform

transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

class b2plot.transform.Transform(name='Original', n_bins=None)[source]

Bases: object

Base Class for the transformations. The function _fit() is overwritten by the sub classes.

fit(x, y=None)[source]
initialise(x)[source]
set_limits(x)[source]
set_n_bins(n)[source]
transform(x)[source]

This is defined in the children and overwritten. In the base class it does nothing and returns the original distribution.

b2plot.transform.get_average_in_bins(n)[source]
b2plot.transform.get_optimal_bin_size(n)[source]

This function calculates the optimal amount of bins for the number of events n. :param n: number of Events :return: optimal bin size

Module contents