https://raw.githubusercontent.com/MatthewReid854/reliability/master/docs/images/logo.png

Utils

Utils (utilities)

This is a collection of utilities that are used throughout the python reliability library. Functions have been placed here as to declutter the dropdown lists of your IDE and to provide a common resource across multiple modules. It is not expected that users will be using any utils directly.

Included functions are: round_to_decimals - applies different rounding rules to numbers above and below 1 so that small numbers do not get rounded to 0. transform_spaced - Creates linearly spaced array (in transform space) based on a specified transform. This is like np.logspace but it can make an array that is weibull spaced, normal spaced, etc. axes_transforms - Custom scale functions used in Probability_plotting get_axes_limits - gets the current axes limits restore_axes_limits - restores the axes limits based on values from get_axes_limits() generate_X_array - generates the X values for all distributions zeroise_below_gamma - sets all y values to zero when x < gamma. Used when the HF and CHF equations are specified xy_transform - provides conversions between spatial (-inf,inf) and axes coordinates (0,1). probability_plot_xylims - sets the x and y limits on probability plots probability_plot_xyticks - sets the x and y ticks on probability plots anderson_darling - calculates the Anderson-Darling goodness of fit statistic colorprint - prints to the console in color, bold, italic, and underline fitters_input_checking - error checking and default values for all the fitters fill_no_autoscale - creates a shaded region without adding it to the global list of objects to consider when autoscale is calculated line_no_autoscale - creates a line without adding it to the global list of objects to consider when autoscale is calculated distribution_confidence_intervals - calculated and plots the confidence intervals for the distributions linear_regression - given x and y data it will return slope and intercept of line of best fit. Includes options to specify slope or intercept. least_squares - provides parameter estimates for distributions using the method of least squares. Used extensively by Fitters.

class reliability.Utils.ALT_MLE_optimisation(model, dist, LL_func, initial_guess, optimizer, failures, failure_stress_1, failure_stress_2=None, right_censored=None, right_censored_stress_1=None, right_censored_stress_2=None)

This performs the MLE method to find the parameters. If the optimizer is None then multiple optimisers will be tried and the best result (lowest LL) will be returned. If the optimiser is specified then it will be used. If it fails then nelder-mead will be used. If nelder-mead fails then the initial guess and a warning will be returned.

class reliability.Utils.ALT_fitters_input_checking(dist, life_stress_model, failures, failure_stress_1, failure_stress_2=None, right_censored=None, right_censored_stress_1=None, right_censored_stress_2=None, CI=0.95, use_level_stress=None, optimizer=None)

performs error checking and some basic default operations for all the inputs given to each of the ALT_fitters

reliability.Utils.ALT_least_squares(model, failures, stress_1_array, stress_2_array=None)

Uses least squares regression (with linear algebra) to fit the parameters of the ALT stress-life distribution to the time to failure data. The output of this method may be used as the initial guess for the MLE method.

return the model’s parameters in a list
Exponential - [a,b] Eyring - [a,c] Power - [a,n] Dual_Exponential - [a,b,c] Power_Exponential - [a,c,n] Dual_Power - [c,m,n]
reliability.Utils.ALT_prob_plot(dist, model, stresses_for_groups, failure_groups, right_censored_groups, life_func, shape, scale_for_change_df, shape_for_change_df, use_level_stress)

Generates an ALT probability plot using the inputs provided.

class reliability.Utils.LS_optimisation(func_name, LL_func, failures, right_censored, method='LS', force_shape=None, LL_func_force=None)

Performs optimisation using least squares regression. There is no actual “optimisation” done here, with the exception of checking which method (RRX or RRY) gave the better solution. This function is used be each of the Fitters.

class reliability.Utils.MLE_optimisation(func_name, LL_func, initial_guess, failures, right_censored, optimizer, force_shape=None, LL_func_force=None)

This function performs the heavy lifting of finding the optimal parameters using the method of maximum likelihood expectation (MLE). This functions is used be each of the fitters.

reliability.Utils.anderson_darling(fitted_cdf, empirical_cdf)

Calculates the Anderson-Darling goodness of fit statistic These formulas are based on the method used in MINITAB which gives an adjusted form of the original AD statistic described on Wikipedia

class reliability.Utils.axes_transforms

Custom scale functions used in Probability_plotting

static beta_forward(F, alpha, beta)
static beta_inverse(R, alpha, beta)
static exponential_forward(F)
static exponential_inverse(R)
static gamma_forward(F, beta)
static gamma_inverse(R, beta)
static gumbel_forward(F)
static gumbel_inverse(R)
static loglogistic_forward(F)
static loglogistic_inverse(R)
static normal_forward(F)
static normal_inverse(R)
static weibull_forward(F)
static weibull_inverse(R)
reliability.Utils.colorprint(string, text_color=None, background_color=None, bold=False, underline=False, italic=False)

colorprint - Provides easy access to color printing in the console Parameter names are self explanatory. Color options are grey, red, green, yellow, blue, pink, turquoise. Some flexibility in color names is allowed. eg. red and r will both give red.

class reliability.Utils.distribution_confidence_intervals

Contains functions that provide all the confidence intervals for CDF, SF, CHF for each distribution for which it is implemented

static CI_kwarg_handler(self, kwargs)

Processes specific arguments from kwargs and self to ensure the CI_type and plot_CI are extracted appropriately and passed to the confidence interval methods, without being passed to the plot method. This function is used within each CDF, SF, CHF before the plt.plot method is used.

static beta_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Beta CDF, SF, and CHF functions.

static exponential_CI(self, func, plot_CI=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Exponential CDF, SF, and CHF functions.

static gamma_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Gamma CDF, SF, and CHF functions.

static gumbel_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Gumbel CDF, SF, and CHF functions.

static loglogistic_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Loglogistic CDF, SF, and CHF functions.

static lognormal_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Lognormal CDF, SF, and CHF functions.

static normal_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Normal CDF, SF, and CHF functions.

static weibull_CI(self, func, plot_CI=None, CI_type=None, CI=None, text_title='', color=None, q=None)

Generates the confidence intervals for CDF, SF, and CHF This is a utility function intended only for use by the Weibull CDF, SF, and CHF functions.

reliability.Utils.fill_no_autoscale(xlower, xupper, ylower, yupper, plot_type='CDF', **kwargs)

creates a filled region (polygon) without adding it to the global list of autoscale objects. Use this when you want to plot something but not have it considered when autoscale sets the range

class reliability.Utils.fitters_input_checking(dist, failures, right_censored=None, method=None, optimizer=None, CI=0.95, percentiles=False, force_beta=None, force_sigma=None, CI_type=None)

performs error checking and some basic default operations for all the inputs given to each of the fitters

reliability.Utils.generate_X_array(dist, xvals=None, xmin=None, xmax=None)

generates the array of X values for each of the PDf, CDF, SF, HF, CHF functions within reliability.Distributions This is done with a variety of cases in order to ensure that for regions of high gradient (particularly asymptotes to inf) the points are more concentrated. This ensures that the line always looks as smooth as possible using only 200 data points

reliability.Utils.get_axes_limits()

This function works in a pair with restore_axes_limits This function gets the previous xlim and ylim and also checks whether there was a previous plot (based on whether the default 0,1 axes had been changed. It returns a list of items that are used by restore_axes_limits after the plot has been performed

reliability.Utils.least_squares(dist, failures, right_censored, method='RRX', force_shape=None)

Uses least squares or non-linear least squares estimation to fit the parameters of the distribution to the plotting positions. Plotting positions are based on failures and right_censored so while least squares estimation does not consider the right_censored data in the same way as MLE, the plotting positions do. The output of this method may be used as the initial guess for the MLE method. method may be RRX or RRY. Default is RRX.

return the model’s parameters in a list.
E.g. for “Weibull_2P” it will return [alpha,beta]
for “Weibull_3P” it will return [alpha,beta,gamma]
reliability.Utils.life_stress_plot(model, dist, life_func, failure_groups, stresses_for_groups, use_level_stress)

Generates a life stress plot using the inputs provided. The life stress plot is an output from each of the ALT_fitters.

reliability.Utils.line_no_autoscale(x, y, **kwargs)

creates a line without adding it to the global list of autoscale objects. Use this when you want to plot something but not have it considered when autoscale sets the range

reliability.Utils.linear_regression(x, y, slope=None, x_intercept=None, y_intercept=None, RRX_or_RRY='RRX', show_plot=False, **kwargs)

linear algebra solution to find line of best fix passing through points (x,y) specify slope or intercept to force these parameters. Rank regression can be on X (RRX) or Y (RRY). Default is RRX. note that slope depends on RRX_or_RRY. If you use RRY then slope is dy/dx but if you use RRX then slope is dx/dy. :returns slope,intercept in terms of Y = slope * X + intercept

class reliability.Utils.make_fitted_dist_params_for_ALT_probplots(dist, params)

creates a class structure for the ALT probability plots to give to Probability_plotting

reliability.Utils.probability_plot_xylims(x, y, dist, spacing=0.1, gamma_beta=None, beta_alpha=None, beta_beta=None)

finds the x and y limits of probability plots. This function is called by probability_plotting

reliability.Utils.probability_plot_xyticks(yticks=None)

Sets the x and y ticks for probability plots X ticks are selected using either MaxNLocator or LogLocator. X ticks are formatted using a custom formatter. Y ticks are specified with FixedLocator due to their irregular spacing. Minor y ticks use MaxNLocator Y ticks are formatted using a custom Percent Formatter that handles decimals better This function is called by probability_plotting

reliability.Utils.removeNaNs(X)

removes NaNs from a list or array. This is better than simply using “x = x[numpy.logical_not(numpy.isnan(x))]” as numpy crashes for str and bool. returns a list or array of the same type as the input

reliability.Utils.restore_axes_limits(limits, dist, func, X, Y, xvals=None, xmin=None, xmax=None)

This function works in a pair with get_axes_limits Inputs: limits - a list of xlim, ylim, use_prev_lims created by get_axes_limits dist - the distribution object to which it is applied X - the xvalues of the plot Y - the yvalues of the plot xvals - the xvals specified. May be None if not specified

No scaling will be done if the axes are not linear due to errors that result from log and function scaled axes when a limit of 0 is used.

reliability.Utils.round_to_decimals(number, decimals=5, integer_floats_to_ints=True)

This function is used to round a number to a specified number of decimals. It is used heavily in the formatting of the parameter titles within reliability.Distributions It is not the same as rounding to a number of significant figures as it keeps preceeding zeros for numbers less than 1.

Inputs: number - the number to be rounded decimals - the number of decimals (not including preceeding zeros) that are to be in the output integer_floats_to_ints - True/False. Default is True. Removes trailing zeros if there are no significant decimals (eg. 12.0 becomes 12).

examples (with decimals = 5): 1234567.1234567 ==> 1234567.12345 0.0001234567 ==> 0.00012345 1234567 ==> 1234567 0.00 ==> 0

reliability.Utils.transform_spaced(transform, y_lower=1e-08, y_upper=0.99999999, num=1000, alpha=None, beta=None)

Creates linearly spaced array based on a specified transform This is similar to np.linspace or np.logspace but is designed for weibull space, exponential space, normal space, gamma space, loglogistic space, and beta space. It is useful if the points generated are going to be plotted on axes that are scaled using the same transform and need to look equally spaced in the transform space Note that lognormal is the same as normal, since the x-axis is what is transformed, not the y-axis.

Parameters:
  • (str) (transform) – the transform name. Must be either weibull, exponential, normal, gamma, or beta.
  • (float) (y_lower) – the lower bound (must be within the bounds 0 to 1). Default is 1e-8
  • (float) – the upper bound (must be within the bounds 0 to 1). Default is 1-1e-8
  • (int) (num) – the number of values in the array. Default is 1000.
  • (int, float) (beta) – the alpha value of the beta distribution. Only used if the transform is beta
  • (int, float) – the beta value of the beta or gamma distribution. Only used if the transform is beta or gamma
Returns:

linearly spaced array (appears linearly spaced when plotted in transform space)

reliability.Utils.write_df_to_xlsx(df, path, **kwargs)

Writes a dataframe to an xlsx file For use exclusively by the Convert_data module

reliability.Utils.xy_transform(value, direction='forward', axis='x')

Converts between data values and axes coordinates (based on xlim() or ylim()). If direction is forward the returned value will always be between 0 and 1 provided value is on the plot. If direction is reverse the input should be between 0 and 1 and the returned value will be the data value based on the current plot lims axis must be x or y

reliability.Utils.zeroise_below_gamma(X, Y, gamma)

This will make all Y values 0 for the corresponding X values being below gamma. Used by HF and CHF which need to be zeroized if the gamma shifted form of the equation is used.