Utils
Utils for the ESPM package
- espm.utils.approx_density(atomic_fraction=False, *, elements_dict={})[source]
Wrapper to the density_of_mixture function of hyperspy. Takes a dict of chemical composition expressed in atomic weight fractions. Returns an approximated density.
- espm.utils.arg_helper(params, d_params, replace=True)[source]
Check if all parameter of d_params are in params. If not, they are added to params with the default value.
- Parameters:
- paramsdict
Dictionary of parameters to be checked.
- d_paramsdict
Dictionary of default parameters.
- Returns:
- paramsdict
Dictionary of parameters with the default parameters added if not present.
- espm.utils.atomic_to_weight_dict(*, elements_dict={})[source]
Wrapper to the atomic_to_weight function of hyperspy. Takes a dict of chemical composition expressed in atomic fractions. Returns a dict of chemical composition expressed in atomic weight fratiom.
- espm.utils.bin_spim(data, n, m)[source]
Take a 3D array of size (x,y,k) [px, py, e] Returns a 3D array of size (n,m,k) [new_px, new_py, e]
- espm.utils.check_keys(params, d_params, upperkeys='', toprint=True, replace=True)[source]
Check if all parameter of d_params are in params. If not, they are added to params with the default value.
- Parameters:
- paramsdict
Dictionary of parameters to be checked.
- d_paramsdict
Dictionary of default parameters.
- upperkeysstr
String of the upper keys.
- toprintbool
If True, print the warning.
- Returns:
- paramsdict
Dictionary of parameters with the default parameters added if not present.
Examples
>>> params = {'a':1,'b':2} >>> d_params = {'a':1,'b':2,'c':3} >>> check_keys(params,d_params) >>> params {'a': 1, 'b': 2, 'c': 3}
- espm.utils.cluster_analysis_concentration_report(s, cluster_source=None, print_std=False)[source]
Performs quantification in atomic % each cluster. s must have been quantified using .elemental_mapping() method.
- Parameters:
- shs.signals.Signal1D
Quantified Spectrum Image. Must have valid s.quantification_signal_1d.
- cluster_source: None, hs.signals.Signal1D or Numpy Array
Object that contains the labeled coordinates where to average the quantification. If None, s.get_cluster_labels() is called.
- print_stdBool
If True, the method also prints the standard deviation of quantifications for each element, for each cluster.
- Returns:
- quantificationnp.ndarray
An array containing the atomic % of each element averaged for each cluster.
- stdnp.ndarray
An array containing the std of the atomic % for each element in each cluster.
- espm.utils.create_laplacian_matrix(nx, ny=None)[source]
Helper method to create the laplacian matrix for the laplacian regularization
- Parameters:
- :param nx: height of the original image
- :param ny: width of the original image
- Returns:
- rtype:
scipy.sparse.csr_matrix ..
- :return:the n x n laplacian matrix, where n = nx*ny
- espm.utils.erode_masks(masks, erosion_radius=1, footprint=array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]), extra_safe=False)[source]
Erodes masks so as to prevent phase overlap regions
- Parameters:
- masks: np.array
Tipically labels from cluster analysis
- erosion_radius: int
The higher the more the masks are eroded
- footprint: See skimage.morphology.binary erosion
- extra_safe: bool
Perfroms erosion before opening/closing. In practice it creats a harsher erosion.
- espm.utils.fancy_cluster_plot(s)[source]
Performs Cluster label plot with fancy colors.
- Parameters:
- shs.signals.Signal1D
s should be a signal for which you have performed cluster analysis.
- espm.utils.get_explained_intensity_W(G, W, H)[source]
Compute the explained intensity of each element of W.
- Parameters:
G (np.array 2D) – G matrix of the ESpM-NMF decomposition
W (np.array 2D) – W matrix of the ESpM-NMF decomposition
H (np.array 2D) – H matrix of the ESpM-NMF decomposition
- Returns:
np.array 2D
- espm.utils.is_number(i)[source]
Return True if i is a number
- Parameters:
i (any) – variable to check
- Returns:
True if i is a number
- Return type:
- espm.utils.is_symbol(i)[source]
Return True if i is a chemical symbol
- Parameters:
i (any) – variable to check
- Returns:
True if i is a chemical symbol
- Return type:
- espm.utils.isdict(p)[source]
Return True if the variable a dictionary.
- Parameters:
p (any) – variable to check
- Returns:
True if p is a dictionary
- Return type:
- espm.utils.num_to_symbol(num)[source]
Converts number to atomic symbol.
- Parameters:
- numstr
Number to be converted to atomic symbol. E. g. “1” return “H”
- Returns:
- elementstr
Corresponding atomic symbol.
- espm.utils.number_to_symbol_dict(func)[source]
Decorator Takes a dict of elements (a.k.a chemical composition) with atomic numbers as keys (e.g. 26 for Fe) returns a dict of elements with symbols as keys (e.g. Fe for iron)
- espm.utils.number_to_symbol_list(func)[source]
Decorator Takes a dict of elements (a.k.a chemical composition) with symbols as keys (e.g. Fe for iron) returns a dict of elements with atomic numbers as keys (e.g. 26 for iron)
- espm.utils.process_losses(losses)[source]
Process the losses to be plotted
- Parameters:
- losses: np.ndarray
Array of losses (output of espm.estimators.NMFEstimator.get_losses method)
- Returns:
- values: np.ndarray
Array of values
- names: list
List of names
- espm.utils.quant_profile_linear_fit(pf, **linfit_kwargs)[source]
Performs linear fit on profile data.
- Parameters:
- pflist of hs.signals.Signal1D
Output of s.quantification_profile
- linfit_kwargs: are passed to sklearn.linear_model.LinearRegression
- espm.utils.quant_spectrum(s1, skip_elements=[])[source]
Performs quantification in atomic % for a single spectrum. Elements from the metadata of another spectrum can be passed. The quantification is done using SmoothNMF with one component so as to only do the fitting.
- Parameters:
- s1hs.signals.Signal1D
Spectrum to be quantified. Elements to be quantified should be defined in the metadata.
- Returns:
- quantificationdict
A dictionary containing the atomic % of each element.
- s1hs.signals.EDS_espm
The spectrum after doing espm quantification. The object contains all espm related information.
- espm.utils.rescaled_DH(D, H)[source]
Rescale the matrices D and H such that the columns of H sums approximately to one.
- Parameters:
D (np.array 2D) – n x k matrix
H (np.array 2D) – k x m matrix
- Returns:
D_rescale, H_rescale
- Return type:
np.array 2D, np.array 2D