hyperspy.learn package

Submodules

hyperspy.learn.mlpca module

hyperspy.learn.mlpca.mlpca(X, varX, p, convlim=1e-10, maxiter=50000, fast=False)

This function performs MLPCA with missing data.

Parameters:
  • X (numpy array) – is the mxn matrix of observations.
  • stdX (numpy array) – is the mxn matrix of standard deviations associated with X (zeros for missing measurements).
  • p (int) – The model dimensionality.
Returns:

  • U,S,V (numpy array) – are the pseudo-svd parameters.
  • Sobj (numpy array) – is the value of the objective function.
  • ErrFlag ({0, 1}) – indicates exit conditions: 0 = nkmal termination 1 = max iterations exceeded.

hyperspy.learn.mva module

class hyperspy.learn.mva.LearningResults

Bases: object

bss_algorithm = None
bss_factors = None
bss_loadings = None
centre = None
crop_decomposition_dimension(n)

Crop the score matrix up to the given number. It is mainly useful to save memory and reduce the storage size

decomposition_algorithm = None
explained_variance = None
explained_variance_ratio = None
factors = None
load(filename)
Load the results of a previous decomposition and
demixing analysis from a file.
Parameters:filename (string) –
loadings = None
mean = None
navigation_mask = None
original_shape = None
output_dimension = None
poissonian_noise_normalized = None
save(filename, overwrite=None)

Save the result of the decomposition and demixing analysis :param filename: :type filename: string :param overwrite: If True(False) overwrite(don’t overwrite) the file if it exists.

If None (default) ask what to do if file exists.
signal_mask = None
summary()

Prints a summary of the decomposition and demixing parameters to the stdout

unfolded = None
unmixing_matrix = None
class hyperspy.learn.mva.MVA

Bases: object

Multivariate analysis capabilities for the Signal1D class.

blind_source_separation(number_of_components=None, algorithm='sklearn_fastica', diff_order=1, diff_axes=None, factors=None, comp_list=None, mask=None, on_loadings=False, pretreatment=None, **kwargs)

Blind source separation (BSS) on the result on the decomposition.

Available algorithms: FastICA, JADE, CuBICA, and TDSEP

Parameters:
  • number_of_components (int) – number of principal components to pass to the BSS algorithm
  • algorithm ({FastICA, JADE, CuBICA, TDSEP}) –
  • diff_order (int) – Sometimes it is convenient to perform the BSS on the derivative of the signal. If diff_order is 0, the signal is not differentiated.
  • diff_axes (None or list of ints or strings) – If None, when diff_order is greater than 1 and signal_dimension (navigation_dimension) when on_loadings is False (True) is greater than 1, the differences are calculated across all signal (navigation) axes. Otherwise the axes can be specified in a list.
  • factors (Signal or numpy array.) – Factors to decompose. If None, the BSS is performed on the factors of a previous decomposition. If a Signal instance the navigation dimension must be 1 and the size greater than 1. If a numpy array (deprecated) the factors are stored in a 2d array stacked over the last axis.
  • comp_list (boolen numpy array) –
    choose the components to use by the boolean list. It permits
    to choose non contiguous components.
  • mask (bool numpy array or Signal instance.) – If not None, the signal locations marked as True are masked. The mask shape must be equal to the signal shape (navigation shape) when on_loadings is False (True).
  • on_loadings (bool) – If True, perform the BSS on the loadings of a previous decomposition. If False, performs it on the factors.
  • pretreatment (dict) –
  • **kwargs (extra key word arguments) – Any keyword arguments are passed to the BSS algorithm.
decomposition(*args, **kwargs)

Decomposition with a choice of algorithms

The results are stored in self.learning_results

Parameters:
  • normalize_poissonian_noise (bool) – If True, scale the SI to normalize Poissonian noise
  • algorithm ('svd' | 'fast_svd' | 'mlpca' | 'fast_mlpca' | 'nmf' |) – ‘sparse_pca’ | ‘mini_batch_sparse_pca’
  • output_dimension (None or int) – number of components to keep/calculate
  • centre (None | 'variables' | 'trials') – If None no centring is applied. If ‘variable’ the centring will be performed in the variable axis. If ‘trials’, the centring will be performed in the ‘trials’ axis. It only has effect when using the svd or fast_svd algorithms
  • auto_transpose (bool) – If True, automatically transposes the data to boost performance. Only has effect when using the svd of fast_svd algorithms.
  • navigation_mask (boolean numpy array) – The navigation locations marked as True are not used in the decompostion.
  • signal_mask (boolean numpy array) – The signal locations marked as True are not used in the decomposition.
  • var_array (numpy array) – Array of variance for the maximum likelihood PCA algorithm
  • var_func (function or numpy array) – If function, it will apply it to the dataset to obtain the var_array. Alternatively, it can a an array with the coefficients of a polynomial.
  • polyfit
  • reproject (None | signal | navigation | both) – If not None, the results of the decomposition will be projected in the selected masked area.

See also

plot_decomposition_factors(), plot_decomposition_loadings(), plot_lev()

get_bss_model(components=None)

Return the spectrum generated with the selected number of independent components

Parameters:components (None, int, or list of ints) – if None, rebuilds SI from all components if int, rebuilds SI from components in range 0-given int if list of ints, rebuilds SI from only components in given list
Returns:
Return type:Signal instance
get_decomposition_model(components=None)

Return the spectrum generated with the selected number of principal components

Parameters:components (None, int, or list of ints) – if None, rebuilds SI from all components if int, rebuilds SI from components in range 0-given int if list of ints, rebuilds SI from only components in given list
Returns:
Return type:Signal instance
get_explained_variance_ratio()

Return the explained variation ratio of the PCA components as a Signal1D.

Returns:
  • s (Signal1D) – Explained variation ratio.
  • See Also
  • ———
  • plot_explained_variance_ration, decomposition,
  • get_decomposition_loadings,
  • get_decomposition_factors.
normalize_bss_components(target='factors', function=<function sum>)

Normalize BSS components.

Parameters:
  • target ({"factors", "loadings"}) –
  • function (numpy universal function, optional, default np.sum) – Each target component is divided by the output of function(target). function must return a scalar when operating on numpy arrays and must have an axis.
normalize_decomposition_components(target='factors', function=<function sum>)

Normalize decomposition components.

Parameters:
  • target ({"factors", "loadings"}) –
  • function (numpy universal function, optional, default np.sum) – Each target component is divided by the output of function(target). function must return a scalar when operating on numpy arrays and must have an axis.
normalize_factors(which='bss', by='area', sort=True)

Normalises the factors and modifies the loadings accordingly.

Parameters:
  • which ('bss' | 'decomposition') –
  • by ('max' | 'area') –
  • sort (bool) –
normalize_poissonian_noise(navigation_mask=None, signal_mask=None)

Scales the SI following Surf. Interface Anal. 2004; 36: 203–212 to “normalize” the poissonian data for decomposition analysis

Parameters:
  • navigation_mask (boolen numpy array) –
  • signal_mask (boolen numpy array) –
plot_cumulative_explained_variance_ratio(n=50)

Plot the principal components explained variance up to the given number

Parameters:n (int) –
plot_explained_variance_ratio(n=50, log=True)

Plot the decomposition explained variance ratio vs index number.

Parameters:
  • n (int) – Number of components.
  • log (bool) – If True, the y axis uses a log scale.
Returns:

  • ax (matplotlib.axes)
  • See Also
  • ———
  • get_explained_variance_ration, decomposition,
  • get_decomposition_loadings,
  • get_decomposition_factors.

reverse_bss_component(component_number)

Reverse the independent component

Parameters:component_number (list or int) – component index/es

Examples

>>> s = hs.load('some_file')
>>> s.decomposition(True) # perform PCA
>>> s.blind_source_separation(3)  # perform ICA on 3 PCs
>>> s.reverse_bss_component(1) # reverse IC 1
>>> s.reverse_bss_component((0, 2)) # reverse ICs 0 and 2
reverse_decomposition_component(component_number)

Reverse the decomposition component

Parameters:component_number (list or int) – component index/es

Examples

>>> s = hs.load('some_file')
>>> s.decomposition(True) # perform PCA
>>> s.reverse_decomposition_component(1) # reverse IC 1
>>> s.reverse_decomposition_component((0, 2)) # reverse ICs 0 and 2
undo_treatments()

Undo normalize_poissonian_noise

hyperspy.learn.mva.centering_and_whitening(X)
hyperspy.learn.mva.get_derivative(signal, diff_axes, diff_order)

hyperspy.learn.svd_pca module

hyperspy.learn.svd_pca.svd_pca(data, fast=False, output_dimension=None, centre=None, auto_transpose=True)

Perform PCA using SVD.

Parameters:
  • data (numpy array) – MxN array of input data (M variables, N trials)
  • fast (bool) – Wheter to use randomized svd estimation to estimate a limited number of componentes given by output_dimension
  • output_dimension (int) – Number of components to estimate when fast is True
  • centre (None | 'variables' | 'trials') – If None no centring is applied. If ‘variable’ the centring will be performed in the variable axis. If ‘trials’, the centring will be performed in the ‘trials’ axis.
  • auto_transpose (bool) – If True, automatically transposes the data to boost performance
Returns:

  • factors (numpy array)
  • loadings (numpy array)
  • explained_variance (numpy array)
  • mean (numpy array or None (if center is None))

Module contents