b2plot package¶
Submodules¶
b2plot.analysis module¶
Analysis tools
- Author:
- Simon Wehle (swehle@desy.de)
- Henrikas Svidras (henrikas.svidras@desy.de)
-
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.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.mask_append(xs, xb)[source]¶ Merge xs and xb into one vector and return it with a boolean mask for each category
-
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.colors module¶
Color definitions
b2plot.correlations module¶
Tools for studying correlations
- Author:
- Simon Wehle (swehle@desy.de)
-
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.decorations module¶
-
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.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
- bbox_inches=’tight’,
- Returns:
b2plot.helpers module¶
Helper function and classes are defined here.
-
class
b2plot.helpers.Singleton(decorated)[source]¶ Bases:
objectA 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.
-
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.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.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.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.mva module¶
Tools to analyse data for mva trainings
b2plot.transform module¶
Transfromation of Data
-
class
b2plot.transform.CDF(*args)[source]¶ Bases:
b2plot.transform.TransformCalculates the cummulative distribution (CDF)
-
class
b2plot.transform.MapTo(fromlow, fromhigh, tolow, tohigh, limit=False, *args)[source]¶ Bases:
b2plot.transform.TransformLinear map to some values
-
class
b2plot.transform.To11(*args)[source]¶ Bases:
b2plot.transform.Transform
-
class
b2plot.transform.ToFlat(x=None, *args)[source]¶ Bases:
b2plot.transform.TransformThis transformation uses the CDF to transform input data to a flat transformation.
-
class
b2plot.transform.ToGauss(*args)[source]¶ Bases:
b2plot.transform.Transform
-
class
b2plot.transform.ToNorm[source]¶ Bases:
b2plot.transform.Transform
-
class
b2plot.transform.ToRawPurity(n_bins=None)[source]¶ Bases:
b2plot.transform.Transform