mathutils#

cryocat.utils.mathutils.compute_frequency_array(shape, pixel_size)#

Compute the frequency array for a given shape and pixel size.

Parameters:
shapetuple of int

The shape of the array for which the frequency array is computed.

pixel_sizefloat

The size of each pixel in the spatial domain.

Returns:
numpy.ndarray

An array representing the frequency magnitudes corresponding to the input shape.

Notes

The function uses the Fast Fourier Transform (FFT) to compute the frequency bins and then calculates the magnitude of the frequency vector for each point in the frequency domain.

cryocat.utils.mathutils.compute_rmse(array1, array2)#

Compute the Root Mean Square Error (RMSE) between two arrays along each column.

Parameters:
array1ndarray

First input array.

array2ndarray

Second input array, must have the same shape as array1.

Returns:
rmsendarray

An array containing the RMSE computed for each column of the input arrays.

Raises:
ValueError

If input arrays are not nparray type or if they don’t contain numbers.

Notes

This function computes the RMSE by first calculating the squared differences between corresponding elements of the input arrays, then taking the mean of these squared differences along each column, and finally taking the square root of these means.

Examples

>>> array1 = np.array([[1, 2], [3, 4]])
>>> array2 = np.array([[1, 1], [1, 1]])
>>> compute_rmse(array1, array2)
array([1.41421356, 2.12132034])
cryocat.utils.mathutils.get_all_pairs(input_numbers)#

Generate all possible unique pairs from a list of numbers.

Parameters:
input_numberslist

List of integers or floats from which pairs are to be generated.

Returns:
list

A list of tuples, each containing a pair of numbers from the input list.

Raises:
ValueError

If input isn’t a list or the list doesn’t contain only integers and floats

Examples

>>> get_all_pairs([1, 2, 3])
[(1, 2), (1, 3), (2, 3)]
cryocat.utils.mathutils.get_number_of_digits(input_number)#

Return the number of digits in the given input number.

Parameters:
input_numberint or float

The number for which the number of digits needs to be calculated.

Returns:
int

The number of digits in the input number.

Examples

>>> get_number_of_digits(12345)
5
>>> get_number_of_digits(3.14)
3
cryocat.utils.mathutils.get_similar_size_factors(number, order='ascending')#

Return two factors of a number that are closest in size - either in ascending or descending order.

Parameters:
number: int

The number for which to find factors.

order: str, default=”ascending”

Order in which the numbers should be returned.

Returns:
tuple:

A tuple containing two factors of the number that are closest in size sorted by specified order. If no factors are found, returns the number itself and 1 (also sorted based on the specified order).

Raises:
ValueError

If input number is not an integer.

cryocat.utils.mathutils.otsu_threshold(input_values)#

Calculate the Otsu threshold for binarization based on the histogram of input values.

Parameters:
input_valuesndarray

An array of input values for which the histogram and threshold need to be computed.

Returns:
float

The computed threshold value according to Otsu’s method.

Notes

Otsu’s method is used to automatically perform histogram shape-based image thresholding. The algorithm assumes that the data contains two classes of values following a bimodal histogram, it then calculates the optimum threshold separating the two classes so that their combined spread (intra-class variance) is minimal.

References

Taken from: https://www.kdnuggets.com/2018/10/basic-image-analysis-python-p4.html

Examples

>>> import numpy as np
>>> input_values = np.random.randint(0, 256, 1000)
>>> threshold = otsu_threshold(input_values)
>>> print("Otsu's threshold:", threshold)
cryocat.utils.mathutils.randomize_phases(vol, fourier_cutoff)#

Return a real-space map with phases randomized beyond fourier_cutoff Fourier pixels.

Phases of the input volume’s Fourier transform are replaced by a random permutation of the original phases at all shells with radial pixel distance >= fourier_cutoff. Amplitudes are preserved exactly.

Parameters:
volndarray

3-D real-space volume.

fourier_cutoffint

Radial threshold in Fourier pixels. Phases at shells with distance >= this value are randomly permuted.

Returns:
ndarray

Real-space volume (float64) with randomized high-frequency phases.