qem.utils
Functions
|
|
|
For an unmeshed set of coordinates broadcast to a meshed ND array. |
|
Export the parameters to a file. |
|
2D FFT of a numpy array. |
|
Find the indices of duplicate rows in a NumPy array. |
|
Find indices of elements of array1 in array2. |
|
Find all peaks that lie within the rectangle defined by origin, origin+a, origin+b, and origin+a+b. |
|
Efficiently find the indices of rows of array1 in array2. |
|
2D inverse FFT of a numpy array. |
|
Determine if a point is inside a polygon using the ray casting algorithm. |
|
Plot an image. |
|
Return the appropriately scaled 2D reciprocal space coordinates. |
|
Remove coordinates that are within a specified threshold distance of each other. |
|
|
|
|
|
Safely convert a Keras tensor to numpy array, handling different backends. |
|
Safely convert a numpy array to Keras tensor. |
|
Safely deep copy a parameter dictionary containing tensors. |
|
Natural logarithm function, avoiding division by zero warnings. |
- qem.utils.plot_image(image, x_labels, y_labels, colormap='gray', colorbar=True)[source]
Plot an image. with x and y labels.
- Parameters:
image (
numpy.ndarray
) – The image to plot.x_labels (
list
) – The labels for the x-axis.y_labels (
list
) – The labels for the y-axis.colormap (
str
, optional) – The colormap to use.colorbar (
bool
, optional) – Whether to show a colorbar.
- Returns:
matplotlib.pyplot.figure
– The figure containing the plot.
- qem.utils.fft2d(array)[source]
2D FFT of a numpy array.
- Parameters:
array (
numpy.ndarray
) – The array to transform.- Returns:
numpy.ndarray
– The transformed array.
- qem.utils.ifft2d(array)[source]
2D inverse FFT of a numpy array.
- Parameters:
array (
numpy.ndarray
) – The array to transform.- Returns:
numpy.ndarray
– The transformed array.
- qem.utils.make_mask_circle_centre(arr, radius)
Create a circular mask with same shape as arr
The circle is centered on the center of the array, with the circle having True values.
Similar to _make_circular_mask, but simpler and potentially faster.
Numba jit compatible.
- Parameters:
arr (
NumPy array
) – Must be 2 dimensionsradius (
scalar
) – Radius of the circle
- Returns:
mask (
NumPy array
) – Boolean array
Example
>>> import atomap.atom_finding_refining as afr >>> arr = np.random.randint(100, size=(20, 20)) >>> mask = afr._make_mask_circle_centre(arr, 10)
- qem.utils.find_duplicate_row_indices(array)[source]
Find the indices of duplicate rows in a NumPy array.
Parameters: - array: NumPy array to check for duplicate rows.
Returns: - idx_duplicates: NumPy array of indices of the duplicate rows.
- qem.utils.find_row_indices(array1, array2)[source]
Efficiently find the indices of rows of array1 in array2.
- Parameters:
array1 – A NumPy array of shape (M, N).
array2 – A NumPy array of shape (K, N).
- Returns:
A NumPy array of indices indicating the position of each row of array1 in array2. If a row from array1 is not found in array2, the corresponding index is -1.
- qem.utils.find_element_indices(array1, array2)[source]
Find indices of elements of array1 in array2.
- Parameters:
array1 – A 1D NumPy array of elements to find.
array2 – A 1D NumPy array where to search for the elements.
- Returns:
A list of indices indicating the position of each element of array1 in array2. If an element from array1 is not found in array2, the corresponding index is -1.
- qem.utils.remove_close_coordinates(coordinates, threshold)[source]
Remove coordinates that are within a specified threshold distance of each other.
Parameters: - threshold: The distance below which coordinates are considered too close and should be removed.
- qem.utils.export_params(params, filename)[source]
Export the parameters to a file.
Parameters: - params: Dictionary of parameters to export. - filename: Name of the file to export to.
- qem.utils.is_point_in_polygon(point, polygon)[source]
Determine if a point is inside a polygon using the ray casting algorithm.
Parameters: - point: A 2D point as a tuple or numpy array (x, y). - polygon: A list of tuples or numpy arrays [(x1, y1), (x2, y2), …, (xn, yn)] representing the polygon vertices.
Returns: - Boolean indicating whether the point is inside the polygon.
- qem.utils.find_peaks_in_rectangle(peaks, origin, a, b)[source]
Find all peaks that lie within the rectangle defined by origin, origin+a, origin+b, and origin+a+b.
Parameters: - peaks: A list of peak positions as tuples or numpy arrays (x, y). - origin: The origin point as a tuple or numpy array (x, y). - a: The vector a as a tuple or numpy array (x, y). - b: The vector b as a tuple or numpy array (x, y).
Returns: - A list of peaks within the defined rectangle.
- qem.utils.q_space_array(pixels, gridsize, meshed=True)[source]
Return the appropriately scaled 2D reciprocal space coordinates.
- Parameters:
pixels (
(N,) array_like
) – Pixels in each dimension of a ND arraygridsize (
(N,) array_like
) – Dimensions of the array in real space unitsmeshed (
bool
, optional) – Option to output dense meshed grid (True) or output unbroadcasted arrays (False)pixels – Pixels in each dimension of a 2D array
gridsize – Dimensions of the array in real space units
- qem.utils.broadcast_from_unmeshed(coords)[source]
For an unmeshed set of coordinates broadcast to a meshed ND array.
Examples
>>> broadcast_from_unmeshed([np.arange(5),np.arange(6)]) [array([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4]]), array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]])]
- qem.utils.safe_convert_to_numpy(tensor)[source]
Safely convert a Keras tensor to numpy array, handling different backends.
- Parameters:
tensor – Keras tensor or numpy array
- Returns:
numpy.ndarray – The tensor converted to numpy array