clawdia.lib#

Collection of auxiliary functions.

This module provides a temporary collection of various utility functions, including mathematical operations, signal normalization, optimization routines, and signal patching utilities.

Notes#

As the CLAWDIA pipeline grows, these functions will be organized into more semantically appropriate modules.

clawdia.lib.abs_normalize(array, axis=-1)[source]#

TODO Normalitza inplace un array ignorant els errors de divissió entre 0 i canviant els nan a 0.

clawdia.lib.extract_patches(signals, *, patch_size, n_patches=None, random_state=None, step=1, limits=None, patch_min=1, l2_normed=False, return_norm_coefs=False, allow_allzeros=True) tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]] | ndarray[Any, dtype[_ScalarType_co]][source]#

Extract patches from ‘signals’.

TODO

Note that if randomly selected, it is not prevented to repeat patches.

Parameters:
signals: ndarray

If 2d-array, must be in C-contiguous order with shape (n_signals, l_signal).

patch_size: int

Length of the patches to extract.

limits: ndarray, optional

Limit(s) in ‘signals’ such that:

` i0, i1 = limits[i_signal] signal = signals[i_signal, i0:i1] `

patch_min: int, optional

When ‘limits’ are given, patch_min is the minimum samples inside the limits to be included in the extracted patches. Defaults to 1.

n_patches: int, optional

Total number of patches to extract. If None (default) extract the maximum amount.

random_state: {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional

Given to ‘numpy.random.PCG64(random_state)’.

step: int, optional

Minimum windowing step (separation between patches).

l2_normed: bool, optional

If True will norm each patch to its L2 norm. False by default.

return_norm_coefs: bool, optional

If True, return also the coefficients used to normalize the signals (useful for when ‘signals’ come from a windowed signal that will be reassembled afterwards). False by default.

allow_allzeros: bool, optional

When extracting random patches, if False and l2_normed == True, generate another random window position until the l2 norm is != 0.

clawdia.lib.l2_normalize(array, axis=-1)[source]#

TODO Normalitza inplace un array amb la norma L2 ignorant els errors de divissió entre 0 i canviant els nan a 0.

clawdia.lib.reconstruct_from_patches_1d(patches, step)[source]#
clawdia.lib.semibool_bisect(f, a, b, args=(), xtol=2e-12, rtol=8.881784197001252e-16, maxiter=100, verbose=False)[source]#

Semi-boolean bisection method for solving f(x).

Find the boundary point x0 in the interval [a, b] using a modified bisection method such that:

  • f(x) > 0 for x <= x0

  • f(x) == 0 for x > x0

or vice versa. One of the two endpoints of the interval [a, b] must satisfy f(x) = 0. The method iteratively narrows down the interval [a, b] until the solution is found to within the specified tolerances xtol and rtol, or the maximum number of iterations is reached.

This algorithm is based on the scipy.optimize.bisect method, but includes modifications for this specific boundary behavior.

Parameters:
fcallable

A continuous function of a single variable, f(x). The function must accept a single positional argument and any additional arguments via args.

afloat

Lower bound of the interval [a, b].

bfloat

Upper bound of the interval [a, b].

argstuple, optional

Additional arguments to pass to the function f.

xtolfloat, optional

Absolute tolerance for the solution. Default is 2e-12.

rtolfloat, optional

Relative tolerance for the solution. Default is _rtol.

maxiterint, optional

Maximum number of iterations to perform. Default is 100.

verbosebool, optional

If True, prints the progress of each iteration, including the evaluation of f(x) at the midpoint. Default is False.

Returns:
solver_statsdict

A dictionary containing information about the solution:

  • xfloat

    The last point where f(x) != 0. Approximates the boundary point x0.

  • ffloat

    The value of f(x) at the solution.

  • convergedbool

    Indicates whether the algorithm converged to a solution.

  • nitersint

    The number of iterations performed.

  • funcallsint

    The total number of function evaluations.

Raises:
BoundaryError

If the initial interval [a, b] does not satisfy the condition that one endpoint evaluates to f(x) = 0.

ValueError

If the function is not continuous or if a and b are not proper bounds.

Notes

  • The xtol parameter controls the absolute precision of the solution, while the rtol parameter ensures relative precision with respect to the value of x.

  • This method assumes that f(x) contains exactly one boundary point within [a, b].

Examples

>>> def f(x):
...     return 0 if x < 2 else x
>>> result = semibool_bisect(f, 0, 4)
>>> print(result['x'])
2.0
>>> print(result['converged'])
True

Custom Exceptions#

class clawdia.lib.BoundaryError[source]#

Bases: ValueError

Exception raised when a boundary condition is violated.

This exception is a subclass of ValueError and is used to signal issues with input values exceeding or falling below defined boundaries.