def independent_components_analysis( self, number_of_components=None, algorithm='CuBICA', diff_order=1, factors=None, comp_list = None, mask = None, on_peaks=False, on_scores=False, pretreatment = None, **kwargs): """Independent components analysis. Available algorithms: FastICA, JADE, CuBICA, and TDSEP Parameters ---------- number_of_components : int number of principal components to pass to the ICA algorithm algorithm : {FastICA, JADE, CuBICA, TDSEP} diff_order : int factors : numpy array externally provided components comp_list : boolen numpy array choose the components to use by the boolean list. It permits to choose non contiguous components. mask : numpy boolean array with the same dimension as the PC If not None, only the selected channels will be used by the algorithm. pretreatment: dict Any extra parameter is passed to the ICA algorithm """ target=self._get_target(on_peaks) if not hasattr(target, 'factors') or target.factors==None: self.decomposition(on_peaks=on_peaks) else: if factors is None: if on_scores: factors = target.scores else: factors = target.factors bool_index = np.zeros((factors.shape[0]), dtype = 'bool') if number_of_components is not None: bool_index[:number_of_components] = True else: if self.output_dimension is not None: number_of_components = self.output_dimension bool_index[:number_of_components] = True if comp_list is not None: for ifactors in comp_list: bool_index[ifactors] = True number_of_components = len(comp_list) factors = factors[:,bool_index] if diff_order > 0 and pretreatment is None: factors = np.diff(factors, diff_order, axis = 0) if pretreatment is not None: from hyperspy.signals.spectrum import Spectrum sfactors = Spectrum({'data' : factors.T}) if pretreatment['algorithm'] == 'savitzky_golay': sfactors.smooth_savitzky_golay( number_of_points = pretreatment['number_of_points'], polynomial_order = pretreatment['polynomial_order'], differential_order = diff_order) if pretreatment['algorithm'] == 'tv': sfactors.smooth_tv( pretreatment_parameter= pretreatment[ 'pretreatment_parameter'], differential_order = diff_order) factors = sfactors.data.T if pretreatment['algorithm'] == 'butter': b, a = sp.signal.butter(pretreatment['order'], pretreatment['cutoff'], pretreatment['type']) for i in range(factors.shape[1]): factors[:,i] = sp.signal.filtfilt(b, a, factors[:,i]) if mask is not None: factors = factors[mask.ravel(), :] # first centers and scales data factors,invsqcovmat = centering_and_whitening(factors) if algorithm == 'orthomax': _, unmixing_matrix = utils.orthomax(factors, **kwargs) unmixing_matrix = unmixing_matrix.T elif algorithm == 'sklearn_fastica': target.ica_node = sklearn.decomposition.FastICA(**kwargs) target.ica_node.whiten = False target.ica_node.fit(factors) unmixing_matrix = target.ica_node.unmixing_matrix_ else: to_exec = 'target.ica_node=mdp.nodes.%sNode(' % algorithm for key, value in kwargs.iteritems(): to_exec += '%s=%s,' % (key, value) to_exec += ')' exec(to_exec) target.ica_node.train(factors) unmixing_matrix = target.ica_node.get_recmatrix() target.unmixing_matrix = np.dot(unmixing_matrix, invsqcovmat) self._unmix_factors(target) self._unmix_scores(target) self._auto_reverse_ic(target) target.ica_algorithm = algorithm
def blind_source_separation( self, number_of_components=None, algorithm="sklearn_fastica", diff_order=1, 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. factors : numpy.array Factors to decompose. If None, the BSS is performed on the result of a previous decomposition. comp_list : boolen numpy array choose the components to use by the boolean list. It permits to choose non contiguous components. mask : numpy boolean array with the same dimension as the signal If not None, the signal locations marked as True (masked) will not be passed to the BSS algorithm. 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. """ target = self.learning_results if not hasattr(target, "factors") or target.factors == None: raise AttributeError( "A decomposition must be performed before blind " "source seperation or factors must be provided." ) else: if factors is None: if on_loadings: factors = target.loadings else: factors = target.factors bool_index = np.zeros((factors.shape[0]), dtype="bool") if number_of_components is not None: bool_index[:number_of_components] = True else: if target.output_dimension is not None: number_of_components = target.output_dimension bool_index[:number_of_components] = True if comp_list is not None: for ifactors in comp_list: bool_index[ifactors] = True number_of_components = len(comp_list) factors = factors[:, bool_index] if pretreatment is not None: from hyperspy.signals.spectrum import Spectrum sfactors = Spectrum({"data": factors.T}) if pretreatment["algorithm"] == "savitzky_golay": sfactors.smooth_savitzky_golay( number_of_points=pretreatment["number_of_points"], polynomial_order=pretreatment["polynomial_order"], differential_order=diff_order, ) if pretreatment["algorithm"] == "tv": sfactors.smooth_tv( smoothing_parameter=pretreatment["smoothing_parameter"], differential_order=diff_order ) factors = sfactors.data.T if pretreatment["algorithm"] == "butter": b, a = sp.signal.butter(pretreatment["order"], pretreatment["cutoff"], pretreatment["type"]) for i in range(factors.shape[1]): factors[:, i] = sp.signal.filtfilt(b, a, factors[:, i]) elif diff_order > 0: factors = np.diff(factors, diff_order, axis=0) if mask is not None: factors = factors[~mask] # first center and scale the data factors, invsqcovmat = centering_and_whitening(factors) if algorithm == "orthomax": _, unmixing_matrix = utils.orthomax(factors, **kwargs) unmixing_matrix = unmixing_matrix.T elif algorithm == "sklearn_fastica": # if sklearn_installed is False: # raise ImportError( #'sklearn is not installed. Nothing done') if "tol" not in kwargs: kwargs["tol"] = 1e-10 target.bss_node = FastICA(**kwargs) target.bss_node.whiten = False target.bss_node.fit(factors) unmixing_matrix = target.bss_node.unmixing_matrix_ else: if mdp_installed is False: raise ImportError("MDP is not installed. Nothing done") to_exec = "target.bss_node=mdp.nodes.%sNode(" % algorithm for key, value in kwargs.iteritems(): to_exec += "%s=%s," % (key, value) to_exec += ")" exec (to_exec) target.bss_node.train(factors) unmixing_matrix = target.bss_node.get_recmatrix() target.unmixing_matrix = np.dot(unmixing_matrix, invsqcovmat) self._unmix_factors(target) self._unmix_loadings(target) self._auto_reverse_bss_component(target) target.bss_algorithm = algorithm