utils#
The py_fatigue.utils
module collects all the utility functions
and classes.
- py_fatigue.utils.ensure_array(method: Callable) Callable #
Ensures that the input variable of a class method is an array.
- Parameters:
method (Callable) – Input method
- Returns:
Input method output
- Return type:
Callable
- py_fatigue.utils.check_iterable(function: Callable) Callable #
Decorator checking whether function
*args
are iterable.- Parameters:
function (Callable) – Generic function
- Returns:
Generic function output
- Return type:
Callable
- py_fatigue.utils.check_str(function: Callable) Callable #
Decorator that checks whether a variable is string or NoneType.
- Parameters:
function (Callable) – Input function
- Return type:
Input function output
- py_fatigue.utils.inplacify(method: Callable) Callable #
Make a method inplace.
- Parameters:
method (Callable) – The method to make inplace.
- Returns:
The inplace method.
- Return type:
Callable
- py_fatigue.utils.make_axes(fig: Figure | None = None, ax: Axes | None = None) Tuple[Figure, Axes] #
Check if a figure and axes are provided, and if not, create them.
- Parameters:
fig (Optional[matplotlib.figure.Figure], optional) – The figure instance, by default None
ax (Optional[matplotlib.axes.Axes], optional) – The axes instance, by default None
- Returns:
The figure and axes instances
- Return type:
- Raises:
TypeError – If fig is not a matplotlib.figure.Figure instance
- class py_fatigue.utils.IntInt(mean_bin_nr: int, range_bin_nr: int)#
Represents a pair of integers, namely the number of bins for mean and ranges
- Parameters:
NamedTuple (NamedTuple)
Create new instance of IntInt(mean_bin_nr, range_bin_nr)
- class py_fatigue.utils.ArrayArray(mean_bin_edges: np.ndarray, range_bin_edges: np.ndarray)#
Represents a pair of arrays, namely the mean and range bin edge values
- Parameters:
NamedTuple (NamedTuple)
Create new instance of ArrayArray(mean_bin_edges, range_bin_edges)
- mean_bin_edges: ndarray#
Alias for field number 0
- range_bin_edges: ndarray#
Alias for field number 1
- class py_fatigue.utils.IntArray(mean_bin_nr: int, range_bin_edges: np.ndarray)#
Represents an integer and an array, namely the number of bins for mean and range bin edge values
- Parameters:
NamedTuple (NamedTuple)
Create new instance of IntArray(mean_bin_nr, range_bin_edges)
- range_bin_edges: ndarray#
Alias for field number 1
- class py_fatigue.utils.ArrayInt(mean_bin_edges: np.ndarray, range_bin_edges: int)#
Represents an array and an integer, namely the number of bins for range and mean bin edge values
- Parameters:
NamedTuple (NamedTuple)
Create new instance of ArrayInt(mean_bin_edges, range_bin_edges)
- mean_bin_edges: ndarray#
Alias for field number 0
- class py_fatigue.utils.FatigueStress(_counts: ndarray, _values: ndarray, bin_width: float, _bin_lb: float | None = None, _bin_ub: float | None = None)#
Fatigue stress class. Mean stress, range, maximum, and minimum can inherit from this class that defines the following properties:
bin_edges: the bin edges for the histogram
bin_centers: the bin centers for the histogram
full: the full cycles
full_counts: the counts of the full cycles
half: the half cycles
half_counts: the counts of the half cycles
bins_idx: the index of the full cycles bins
binned_values: the binned full cycles
- property counts: ndarray#
The number of cycles.
- Returns:
counts
- Return type:
np.ndarray
- property values: ndarray#
The stress values.
- Returns:
values
- Return type:
np.ndarray
- property bin_edges: ndarray#
The bin edges.
- Returns:
bin_edges
- Return type:
np.ndarray
- property bin_centers#
The bin centers.
- Returns:
bin_centers
- Return type:
np.ndarray
- property full#
The full cycles stresses.
- Returns:
full
- Return type:
np.ndarray
- property half#
The half cycles stresses.
- Returns:
half
- Return type:
np.ndarray
- property bins_idx#
The index of the full cycles bins.
- Returns:
bins_idx
- Return type:
np.ndarray
- property binned_values#
The binned full cycles.
- Returns:
binned_values
- Return type:
np.ndarray
- py_fatigue.utils.calc_bin_edges(bin_lb: float, bin_ub: float, bin_width: float) ndarray #
Calculate the bin edges for a fatigue histogram using the given bin width, lower bound and upper bound.
- py_fatigue.utils.bin_upper_bound(bin_lower_bound: float, bin_width: float, max_val: float) float #
Returns the upper bound of the bin edges, given the lower bound, bin width and maximum value of the array. If the maximum value is not a multiple of the bin width, the upper bound is rounded up to the next multiple of the bin width. Also, if the maximum value is smaller than the lower bound, the upper bound is set to the lower bound.
- py_fatigue.utils.split_full_cycles_and_residuals(count_cycle: ndarray) tuple #
Returns the stress ranges or mean stresses from only the full cycles.
- py_fatigue.utils.calc_full_cycles(stress: ndarray, count_cycle: ndarray, debug_mode: bool = False) ndarray #
Returns the stress ranges or mean stresses from only the full cycles.
- py_fatigue.utils.calc_half_cycles(stress: ndarray, count_cycle: ndarray, debug_mode: bool = False) ndarray #
Returns the stress ranges or mean stresses from only the half cycles.
- py_fatigue.utils.compare(a: Any, b: Any) bool #
Compare two objects.
- Parameters:
a (Any)
b (Any)
- Returns:
True if the a and b are equal, False otherwise.
- Return type:
- py_fatigue.utils.split(what: int, by: int)#
Split an integer into a list of integers.
>>> split(5, 2) [2, 3] >>> split(20, 7) [3, 3, 3, 3, 3, 3, 2]
- class py_fatigue.utils.TypedArray#
Wrapper class for numpy arrays that stores and validates type information. This can be used in place of a numpy array, but when used in a pydantic BaseModel or with pydantic.validate_arguments, its dtype will be coerced at runtime to the declared type.
- classmethod validate(val, field)#
Validate the value of the field.
- py_fatigue.utils.to_numba_dict(data: dict, key_type: type = <class 'str'>, val_type: type = <class 'float'>) DictType #
Converts a dictionary to a numba typed dict, provided the output key and value types.
- py_fatigue.utils.chunks(lst: list[Any], n: int) Generator[list[Any], None, None] #
Yield successive n-sized chunks from lst.
- py_fatigue.utils.calc_slope_intercept(x: ndarray | List[float], y: ndarray | List[float]) tuple[ndarray, ndarray] #
Given a series of (x, y) knee point coordinates, return the slope and intercept of the line that passes through the knee point and the previous point.
Equation of the line:
\[y = mx + c\]where:
\(m\) is the slope of the line, calculated as \(\frac{y_2 - y_1}{x_2 - x_1}\)
\(c\) is the intercept of the line, calculated as \(\exp(y_2 - m \times x_2)\)
- Parameters:
x (ArrayLike) – The x-coordinates of the knee points.
y (ArrayLike) – The y-coordinates of the knee points.
- Returns:
The slope and intercept of the line that passes through the knee point and the previous point.
- Return type:
tuple[ArrayLike, ArrayLike]
- py_fatigue.utils.compile_specialized_bisect(fun)#
Returns a compiled bisection implementation for f.
- py_fatigue.utils.compile_specialized_newton(fun)#
Returns a compiled Newton–Raphson implementation for f that accepts extra arguments. A finite-difference approximation is used to compute the derivative.
- py_fatigue.utils.py_bisect(fun, a, b, tol, mxiter, *args)#
A pure Python implementation of the bisection algorithm that accepts extra arguments.
- py_fatigue.utils.py_newton(fun, x0, tol, mxiter, *args)#
A pure Python Newton–Raphson implementation that uses finite differences to approximate the derivative.
- py_fatigue.utils.numba_bisect(fun, a, b, tol, mxiter, *args)#
A wrapper that compiles f if it is a regular Python function.
- py_fatigue.utils.numba_newton(fun, x0, tol, mxiter, *args)#
A wrapper that compiles f if it is a regular Python function and calls the Newton–Raphson routine with extra arguments.
- class py_fatigue.utils.CustomFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#
Logging Formatter to add colors and count warning / errors
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()
({}
) formatting orstring.Template
formatting in your format string.Changed in version 3.2: Added the
style
parameter.- format(record)#
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.