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.close_all()[source]

Close all opened windows.

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.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:

bool

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:

bool

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:

bool

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.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

espm.utils.symbol_list()[source]
espm.utils.symbol_to_number_dict(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.symbol_to_number_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)