xrf_explorer.server.dim_reduction package

Submodules

xrf_explorer.server.dim_reduction.embedding module

apply_umap(data: ndarray, n_neighbors: int, min_dist: float, n_components: int, metric: str) ndarray | None[source]

Reduces the dimensionality of the given data using uniform manifold approximation and projection (UMAP). The original data is not modified. For more information on UMAP, see: https://umap-learn.readthedocs.io/en/latest/.

Parameters:
  • data – np.ndarray, shape (n_samples, n_features). The data on which UMAP is used to reduce the dimension of features to n_components

  • n_neighbors – The size of local neighborhood. See UMAP documentation for more information

  • min_dist – The minimum distance between points in the embedding. See UMAP documentation for more information

  • n_components – The dimension of the embedded space. See UMAP documentation for more information

  • metric – The metric to use for distance computation. See UMAP documentation for more information

Returns:

np.ndarray, shape (n_samples, n_components) containing the result of UMAP applied to given data with the given parameters. If UMAP fails, None is returned

filter_elemental_cube(elemental_cube: ndarray, element: int, threshold: int, max_indices: int) tuple[ndarray, ndarray][source]

Get indices for which the value of the given element in the normalized elemental data cube is above the threshold.

Parameters:
  • elemental_cube – shape (3, m, n) elemental data cube

  • element – The element to filter on

  • threshold – The threshold to filter by

  • max_indices – The maximum number of indices to return

Returns:

Indices for which the value of the given element in the normalized elemental data cube is above the threshold; the reduced list of indices

generate_embedding(data_source: str, element: int, threshold: int, new_umap_parameters=None) str[source]

Generate the embedding (lower dimensional representation of the data) of the elemental data cube using the dimensionality reduction method “UMAP”. The embedding with the list of indices (which pixels from the elemental data cube are in the embedding) are stored in the folder specified in the backend config file. The order the indices occur in the indices list is the same order as the positions of the mapped pixels in the embedding.

Parameters:
  • data_source – The name of the data source to generate the embedding for

  • element – The element to generate the embedding for

  • threshold – The threshold to filter the data cube by

  • new_umap_parameters – The parameters passed on to the UMAP algorithm

Returns:

string code indicating the status of the embedding generation. “error” when error occurred, “success” when embedding was generated successfully, “downsampled” when successful and the number of data points was downsampled

xrf_explorer.server.dim_reduction.general module

create_image_of_indices_to_embedding(data_source: str) bool[source]

Creates the image for polygon selection that decodes to which points in the embedding the pixels of the elemental data cube are mapped. Uses the current embedding and indices to create the image.

Parameters:

data_source – Name of the data source

Returns:

True if the image was created successfully, otherwise False

get_image_of_indices_to_embedding(data_source: str) str[source]

Returns the path to the image that maps the indices of the elemental data cube to the embedding.

Parameters:

data_source – Name of the data source

Returns:

Path to the image. If the image is not found, an empty string is returned

get_path_to_dr_folder(data_source: str) str[source]

Get the path to the dimensionality reduction folder for a given datasource. If it does not exist the folder is created.

Parameters:

data_source – The name of the datasource

Returns:

The path to the dimensionality reduction folder for the given datasource

imwrite(filename, img[, params]) retval

. @brief Saves an image to a specified file. . . The function imwrite saves the image to the specified file. The image format is chosen based on the . filename extension (see cv::imread for the list of extensions). In general, only 8-bit unsigned (CV_8U) . single-channel or 3-channel (with ‘BGR’ channel order) images . can be saved using this function, with these exceptions: . . - With OpenEXR encoder, only 32-bit float (CV_32F) images can be saved. . - 8-bit unsigned (CV_8U) images are not supported. . - With Radiance HDR encoder, non 64-bit float (CV_64F) images can be saved. . - All images will be converted to 32-bit float (CV_32F). . - With JPEG 2000 encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - With PAM encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - With PNG encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - PNG images with an alpha channel can be saved using this function. To do this, create . 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels . should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below). . - With PGM/PPM encoder, 8-bit unsigned (CV_8U) and 16-bit unsigned (CV_16U) images can be saved. . - With TIFF encoder, 8-bit unsigned (CV_8U), 16-bit unsigned (CV_16U), . 32-bit float (CV_32F) and 64-bit float (CV_64F) images can be saved. . - Multiple images (vector of Mat) can be saved in TIFF format (see the code sample below). . - 32-bit float 3-channel (CV_32FC3) TIFF images will be saved . using the LogLuv high dynamic range encoding (4 bytes per pixel) . . If the image format is not supported, the image will be converted to 8-bit unsigned (CV_8U) and saved that way. . . If the format, depth or channel order is different, use . Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O . functions to save the image to XML or YAML format. . . The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file. . It also demonstrates how to save multiple images in a TIFF file: . @include snippets/imgcodecs_imwrite.cpp . @param filename Name of the file. . @param img (Mat or vector of Mat) Image or Images to be saved. . @param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::ImwriteFlags

valid_element(element: int, data_cube: ndarray) bool[source]

Verifies whether the given element is valid for the given data cube.

Parameters:
  • element – The element to verify

  • data_cube – The data cube to verify the element for

Returns:

True if the element is valid, otherwise False

xrf_explorer.server.dim_reduction.overlay module

create_element_overlay(element: int, indices: ndarray, data_cube: ndarray, embedding: ndarray) tuple[ndarray, ndarray][source]

Creates an intensity overlay of the given element.

Parameters:
  • element – The element to create the overlay for

  • indices – The indices of the data to create the overlay for

  • data_cube – The data to create the overlay from

  • embedding – The embedding data

Returns:

The reordered embedding and overlay of the given element

create_embedding_image(data_source: str, overlay_type: str) str[source]

Creates the embedding image from the embedding.

Parameters:
  • data_source – Name of the data source to create the embedding image for

  • overlay_type – The type of overlay to create. Can be the name of image prefixed by contextual_ or an element number prefixed by elemental_

Returns:

Path to created embedding image is successful, otherwise empty string

create_image_overlay(registered_image: ndarray, indices: ndarray) ndarray[source]

Creates the overlay based on the given image type. This is done by getting the pixels out of the image at the given indices.

Parameters:
  • registered_image – The pixels of the registered image to create the overlay from

  • indices – The indices to get the pixels from

Returns:

The normalized pixels at the given indices of the image

cvtColor(src, code[, dst[, dstCn]]) dst

. @brief Converts an image from one color space to another. . . The function converts an input image from one color space to another. In case of a transformation . to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note . that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the . bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue . component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and . sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on. . . The conventional ranges for R, G, and B channel values are: . - 0 to 255 for CV_8U images . - 0 to 65535 for CV_16U images . - 0 to 1 for CV_32F images . . In case of linear transformations, the range does not matter. But in case of a non-linear . transformation, an input RGB image should be normalized to the proper value range to get the correct . results, for example, for RGB f$rightarrowf$ L*u*v* transformation. For example, if you have a . 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will . have the 0..255 value range instead of 0..1 assumed by the function. So, before calling #cvtColor , . you need first to scale the image down: . @code . img *= 1./255; . cvtColor(img, img, COLOR_BGR2Luv); . @endcode . If you use #cvtColor with 8-bit images, the conversion will have some information lost. For many . applications, this will not be noticeable but it is recommended to use 32-bit images in applications . that need the full range of colors or that convert an image before an operation and then convert . back. . . If conversion adds the alpha channel, its value will set to the maximum of corresponding channel . range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F. . . @param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision . floating-point. . @param dst output image of the same size and depth as src. . @param code color space conversion code (see #ColorConversionCodes). . @param dstCn number of channels in the destination image; if the parameter is 0, the number of the . channels is derived automatically from src and code. . . @see @ref imgproc_color_conversions

plot_embedding_with_overlay(embedding: ndarray, overlay: ndarray, path: str) str[source]

Makes the image of the given embedding with the given overlay and saves it to the given path.

Parameters:
  • embedding – The embedding data

  • overlay – The overlay data

  • path – The path to save the image

Returns:

Path to the created image

Module contents

This module handles everything related to dimensionality reduction.