upxo.pxtalops.detect_grains_from_mcstates module
This module provides functions for identifying and labeling grains in a 2D microstructure.
The module includes the following functions: - identify_grains_upxo_2d: Deprecated function for identifying grains in a 2D microstructure. - mcgs2d: Identifies and labels grains in a 2D microstructure. - quick_plot_s: Plots the state matrix for a given temporal slice of a microstructural system.
The module also includes a deprecated function identify_grains_upxo_2d and a helper function quick_plot_s for visualizing the state matrix.
Note: The functions in this module require the installation and configuration of necessary image processing libraries such as OpenCV or scikit-image.
Examples
Import and initialize the function:
from upxo.pxtalops import detect_grains_from_mcstates as get_grains gs_dict = {…} # Pre-loaded microstructure data msteps = 0 # Temporal slice index to analyze
Call the function using scikit-image for grain detection:
- gs_dict, state_ng = get_grains.mcgs2d(library=’scikit-image’,
gs_dict = PXGS.gs, msteps = 10, isograin_pxl_neigh_order=2, store_state_ng=True )
- gs_dict, state_ng = get_grains.mcgs2d(library=’scikit-image’,
gs_dict = PXGS.gs, msteps = PXGS.m, isograin_pxl_neigh_order=2, store_state_ng=True )
- upxo.pxtalops.detect_grains_from_mcstates.mcgs2d(library=None, gs_dict=None, msteps=None, kernel_order=2, store_state_ng=True, connectivity=8, process_individual_states=False, delta=0, lfiDtype=numpy.int32, verbose=False)[source]
Identifies and labels grains in a 2D microstructure.
This function labels grains in a given microstructure temporal slice using specified image processing libraries. It supports both OpenCV and scikit-image for grain detection and labeling, based on the user’s choice. The function updates the input microstructure dictionary (gs_dict) with grain identification information and optionally stores the count of grains per state.
- Parameters:
library ({'opencv', 'scikit-image'}, optional) – The library to use for grain identification. If not specified, the function raises a NotImplementedError for ‘upxo’.
gs_dict (dict) – A dictionary containing the microstructural system’s data. It must include the state matrix for the specified temporal slice (m).
msteps (list) – The indices of the temporal slices to analyze within gs_dict.
kernel_order ({1, 2}, optional) – The pixel connectivity criterion for labeling grains. Use 1 for 4-connectivity and 2 for 8-connectivity. Defaults to 2.
store_state_ng (bool, optional) – If True, stores the number of grains for each distinct state in the microstructure. Defaults to True.
- Returns:
gs_dict (dict) – The updated microstructure dictionary with added grain identification information for the specified temporal slice.
state_ng (numpy.ndarray) – An array containing the number of grains for each state, if store_state_ng is True.
- Raises:
NotImplementedError – If the specified library is not supported or ‘upxo’ is provided as the library parameter.
Notes
The function modifies the input gs_dict in place, adding grain identification information for the specified temporal slice.
Zero is reserved and not used as a valid grain label. Grains are labeled starting from 1.
The function assumes the presence of necessary libraries (opencv or scikit-image). Users must ensure these dependencies are installed.
Examples
Import
from upxo.pxtalops import detect_grains_from_mcstates as get_grains
Call the function using OpenCV for grain detection:
- gs_dict, state_ng = get_grains.mcgs2d(library=’scikit-image’,
gs_dict=PXGS.gs, msteps=10, kernel_order=2, store_state_ng=True )
- gs_dict, state_ng = get_grains.mcgs2d(library=’scikit-image’,
gs_dict=PXGS.gs, msteps=PXGS.tslices, kernel_order=2, store_state_ng=True )
- upxo.pxtalops.detect_grains_from_mcstates.quick_plot_s(gs_dict, mcstep)[source]
Plot the state matrix for a given temporal slice of a microstructural system.
This function visualizes the state matrix (s) of a microstructural system at a specified temporal slice (m). It is designed for quick inspection of the state configuration in the system, typically used in the analysis or debugging of simulations involving grain structure evolution.
Parameters: - gs_dict (dict): A dictionary containing the microstructural system’s
data. Each key represents a temporal slice index, and its value is an object containing the state matrix (s) among other properties.
- mcstep (int/list): The index of the temporal slice to be visualized.
This corresponds to a key in gs_dict. Type: list is preferred. m of type int will ge put inside a list and used as [mcstep].
Returns: None. Directly displays the plot of the state matrix using matplotlib.
from upxo.pxtalops import detect_grains_from_mcstates as gdetops
gdetops.quick_plot_s(gs_dict, 10) ``` This plots the state matrix for the 10th temporal slice of the microstructural system described in gs_dict.
Note: This function requires matplotlib for plotting. Ensure matplotlib is installed and configured in your environment.