calibr8.utils

This module implements helper functions for a variety of tasks, including imports, timestamp parsing and plotting.

exception calibr8.utils.BuildMismatchException

Bases: CompatibilityException

exception calibr8.utils.CompatibilityException

Bases: Exception

class calibr8.utils.ImportWarner(module_name)

Bases: object

Mock for an uninstalled package, raises ImportError when used.

exception calibr8.utils.MajorMismatchException

Bases: CompatibilityException

exception calibr8.utils.MinorMismatchException

Bases: CompatibilityException

exception calibr8.utils.PatchMismatchException

Bases: CompatibilityException

calibr8.utils.assert_version_match(vA: str, vB: str)

Compares two version numbers and raises exceptions that indicate where they missmatch.

Parameters:
vAstr

first version number

vBstr

second version number

Raises:
MajorMismatchException

difference on the first level

MinorMismatchException

difference on the second level

PatchMismatchException

difference on the third level

BuildMismatchException

difference on the fourth level

calibr8.utils.check_scale_degree(scale_degree: int) int

Evaluates user input for the scale degree and raises warning/error if the value is unexpected. Returns the user input.

Parameters:
scale_degreeint

Degree of scale as set by the user.

Returns:
scale_degreeint

Degree of scale as set by the user.

Raises:
ValueError

scale_degree is None or negative

UserWarning

scale_degree is unexpectedly high

calibr8.utils.format_datetime(dt: datetime | None) str | None

Formats a datetime like 2020-08-05T13:37:00Z.

Returns:
resultoptional, str

may be None when the input was None

calibr8.utils.istensor(input: object) bool
“Convenience function to test whether an input is a TensorVariable

or if an input array or list contains TensorVariables.

Parameters:
inputobject

an object shat shall be analyzed

calibr8.utils.parse_datetime(s: str | None) datetime | None

Parses a timezone-aware datetime formatted like 2020-08-05T13:37:00Z.

Returns:
resultoptional, datetime

may be None when the input was None

calibr8.utils.plot_continuous_band(ax, independent, model, residual_type: str | None = None)

Helper function for plotting the 68, 90 and 95 % likelihood-bands of a univariate distribution.

Parameters:
axmatplotlib.Axes

subplot object to plot into

independentarray-like

x-values for the plot

modelCalibrationModel

A fitted calibration model with data. The predict_dependent method should return a tuple where the mean is the first entry.

residual_typestr, optional

One of { None, “absolute”, “relative” }. Specifies if bands are for no, absolute or relative residuals.

Returns:
artistslist of matplotlib.Artist

the created artists (1x Line2D, 6x PolyCollection (alternating plot & legend))

calibr8.utils.plot_model(model, *, fig: Figure | None = None, axs: Sequence[Axes] | None = None, residual_type='absolute', band_xlim: Tuple[float | None, float | None] = (None, None))

Makes a plot of the model with its data.

Parameters:
modelCalibrationModel

A fitted calibration model with data. The predict_dependent method should return a tuple where the mean is the first entry.

figoptional, matplotlib.figure.Figure

An existing figure (to be used in combination with [axs] argument).

axsoptional, [matplotlib.axes.Axes]

matplotlib subplots to use instead of creating new ones.

residual_typeoptional, str

Specifies if residuals are plotted absolutely or relatively.

band_xlimtuple

Optional overrides for the minimum/maximum x coordinates of the likelihood bands in the left and center plots. Either entry can be None or a float. Defaults to the min/max of the calibration data.

Returns:
figmatplotlib.figure.Figure

the (created) figure

axs[matplotlib.axes.Axes]

subplots of x-linear, x-log and residuals (xlog)

calibr8.utils.plot_norm_band(ax, independent, mu, scale)

Helper function for plotting the 68, 90 and 95 % likelihood-bands of a Normal distribution.

Parameters:
axmatplotlib.Axes

subplot object to plot into

independentarray-like

x-values for the plot

muarray-like

mu parameter of the Normal distribution

scalearray-like

scale parameter of the Normal distribution

Returns:
artistslist of matplotlib.Artist

the created artists (1x Line2D, 6x PolyCollection (alternating plot & legend))

calibr8.utils.plot_t_band(ax, independent, mu, scale, df, *, residual_type: str | None = None)

Helper function for plotting the 68, 90 and 95 % likelihood-bands of a t-distribution.

Parameters:
axmatplotlib.Axes

subplot object to plot into

independentarray-like

x-values for the plot

muarray-like

mu parameter of the t-distribution

scalearray-like

scale parameter of the t-distribution

dfarray-like

density parameter of the t-distribution

residual_typestr, optional

One of { None, “absolute”, “relative” }. Specifies if bands are for no, absolute or relative residuals.

Returns:
artistslist of matplotlib.Artist

the created artists (1x Line2D, 6x PolyCollection (alternating plot & legend))