fidanka.misc package

Submodules

fidanka.misc.logging module

class fidanka.misc.logging.LoggerManager(*args, **kwargs)

Bases: object

Singleton class to manage a logger that writes log messages to a file and the console.

_instance

The single instance of LoggerManager.

Type:

LoggerManager

get_logger(level, flevel, clevel)

Return the logger object.

config_logger(filename, flevel, clevel)

Configure the logger to write to the provided filename and set the log levels.

classmethod config_logger(filename, level=20, flevel=20, clevel=30)

Configure the logger to write to the provided filename and set the log levels.

Parameters:
  • filename (str) – The name of the file to write log messages to.

  • level (int, optional) – The log level for the logger. Defaults to logging.INFO.

  • flevel (int, optional) – The log level for the file handler. Defaults to logging.INFO.

  • clevel (int, optional) – The log level for the console handler. Defaults to logging.WARNING.

classmethod get_logger(name=None)

Return the logger object.

Returns:

The logger object.

Return type:

logging.Logger

fidanka.misc.parallel module

fidanka.misc.parallel.parallelize(func)

fidanka.misc.utils module

fidanka.misc.utils.closest(array: Sequence[Number], target: Number) Tuple[float | None, float | None]

Find the closest values above and below a given target in an array. If the target is in the array, the function returns the exact target value in both elements of the tuple. If the target is not exactly in the array, the function returns the closest value below the target in the first element of the tuple and the closest value above the target in the second element of the tuple. If the taret is below the minimum value in the array, the first element of the tuple is None. If the target is above the maximum value in the array, the second element of the tuple is None.

Parameters:
  • array (NDArray[float]) – Array to search.

  • target (float) – Target value.

Returns:

  • closest_lower (Union[NDArray[float], None]) – Closest value below the target. If the target is below the minimum value in the array, returns None.

  • closest_upper (Union[NDArray[float], None]) – Closest value above the target. If the target is above the maximum value in the array, returns None.

Examples

Let’s find the closest values above and below 5 in an array.

>>> array = np.array([1,2,3,4,5,6,7,8,9,10])
>>> closest(array, 5)
(5, 6)
fidanka.misc.utils.get_logger(name, fileName='fidanka.log', level=20, flevel=20, clevel=30)
fidanka.misc.utils.get_samples(n: int, f: Callable[[ndarray[Any, dtype[ScalarType]]], ndarray[Any, dtype[ScalarType]]], domain: ndarray[Any, dtype[ScalarType]] = None) ndarray[Any, dtype[ScalarType]]

Sample n values from a given function. The function does not have to be a normalized PDF as the function will be normalized before sampling.

Parameters:
  • n (int) – Number of samples to draw.

  • f (Callable[[FARRAY_1D], FARRAY_1D]) – Function to sample from.

  • domain (FARRAY_1D, default=None) – Domain of the function. If None, defaults to np.linspace(0,1,100000).

Returns:

samples – Array of samples.

Return type:

NDArray[float]

Examples

Let’s sample 10 values from a quadratic function over the domain 0,2.

>>> def quadratic(x):
...     return x**2
>>> get_samples(10, quadratic, domain=np.linspace(0,2,1000))
fidanka.misc.utils.interpolate_arrays(array_lower: ndarray[Any, dtype[ScalarType]], array_upper: ndarray[Any, dtype[ScalarType]], target: float, lower: float, upper: float, joinCol: int | None = None) ndarray[Any, dtype[ScalarType]]

Interpolate between two arrays. The arrays must have the same shape.

Parameters:
  • array_lower (NDArray[float]) – Lower bounding array.

  • array_upper (NDArray[float]) – Upper bounding array.

  • target (float) – Target value to interpolate to.

  • lower (float) – value at lower bounding array

  • upper (float) – value at upper bounding array

  • joinCol (int, default=None) – Column to join on. If None, assumes the arrays are parallel

Returns:

interpolated_array – Interpolated array at target value.

Return type:

NDArray[float]

Examples

Let’s interpolate between two arrays.

>>> array_lower = np.array([1,2,3,4,5,6,7,8,9,10])
>>> array_upper = np.array([11,12,13,14,15,16,17,18,19,20])
>>> interpolate_arrays(array_lower, array_upper, 5.5, 5, 6)
fidanka.misc.utils.interpolate_keyed_arrays(arr1: Sequence, arr2: Sequence, target: float, lower: float, upper: float, key: int = 0) Sequence
fidanka.misc.utils.inverse_cdf_sample(f: Callable[[ndarray[Any, dtype[ScalarType]]], ndarray[Any, dtype[ScalarType]]], x: ndarray[Any, dtype[ScalarType]] = None) Callable[[ndarray[Any, dtype[ScalarType]]], ndarray[Any, dtype[ScalarType]]]

Generate a function that samples from the inverse CDF of a given function.

Parameters:
  • f (Callable[[FARRAY_1D], FARRAY_1D]) – Function to sample from.

  • x (FARRAY_1D, default=None) – Domain of the function. If None, defaults to np.linspace(0,1,100000).

Returns:

inverse_cdf – Function that samples from the inverse CDF of f. To evaluate the function, pass an array of uniform random numbers between 0 and 1.

Return type:

Callable[[FARRAY_1D], FARRAY_1D]

Examples

Let’s sample from the inverse CDF of a Gaussian distribution. First, we define the Gaussian distribution.

>>> def gaussian(x, mu=0, sigma=1):
...     return np.exp(-(x-mu)**2/(2*sigma**2))

Then, we generate the inverse CDF function.

>>> inverse_cdf = inverse_cdf_sample(gaussian)

Finally, we sample from the inverse CDF.

>>> inverse_cdf(np.random.random(10))
fidanka.misc.utils.measusre_perpendicular_distance(f1, f2, domain, pbar=False)

Measure the perpendicular distance between two functions over a given domain.

Parameters:
  • f1 (Callable) – Function of a single parameter f1(x) -> y.

  • f2 (Callable) – Function of a single parameter f2(x) -> y.

  • domain (np.ndarray[float64]) – Domain over which to measure the distance.

  • pbar (bool, default=False) – Show progress bar.

Returns:

minDist – Minimum distance between the two functions over the domain.

Return type:

np.ndarray[float64]

Examples

Let’s measure the perpendicular distance between two functions.

>>> f1 = lambda x: x**2
>>> f2 = lambda x: x**3
>>> domain = np.linspace(0, 1, 100)
>>> minDist = measusre_perpendicular_distance(f1, f2, domain)
fidanka.misc.utils.pfD(r, I)

Return a function which givex the perpendicular distance between a point and a function evaluated at some point x

Parameters:
  • r (np.ndarray[float64]) – 2-vector (x,y), some point

  • I (Callable) – Function of a single parameter I(x) -> y.

Returns:

d – Function of form d(x) which gives the distance between r and I(x)

Return type:

Callable

Module contents