def evaluate(self): """Run FastICA on the given time series data.""" # problem dimensions data = self.time_series.data n_time, n_svar, n_node, n_mode = data.shape self.n_components = n_comp = self.n_components or n_node if n_time < n_comp: msg = ("ICA requires more time points (received %d) than number of components (received %d)." " Please run a longer simulation, use a higher sampling frequency or specify a lower" " number of components to extract.") msg %= n_time, n_comp raise ValueError(msg) # ICA operates on matrices, here we perform for all state variables and modes W = numpy.zeros((n_comp, n_comp, n_svar, n_mode)) # unmixing K = numpy.zeros((n_comp, n_node, n_svar, n_mode)) # whitening matrix src = numpy.zeros((n_time, n_comp, n_svar, n_mode)) # component time series for mode in range(n_mode): for var in range(n_svar): sl = Ellipsis, var, mode K[sl], W[sl], src[sl] = fastica(data[:, var, :, mode], self.n_components) return mode_decompositions.IndependentComponents(source=self.time_series, component_time_series=src, prewhitening_matrix=K, unmixing_matrix=W, n_components=n_comp)
def compute_ica_decomposition(time_series, n_components): """ # type: (TimeSeries, int) -> IndependentComponents Run FastICA on the given time series data. Parameters __________ time_series : TimeSeries The timeseries to which the ICA is to be applied. n_components : int Number of principal components to unmix. """ # problem dimensions data = time_series.data n_time, n_svar, n_node, n_mode = data.shape n_components = n_comp = n_components or n_node if n_time < n_comp: msg = ( "ICA requires more time points (received %d) than number of components (received %d)." " Please run a longer simulation, use a higher sampling frequency or specify a lower" " number of components to extract.") msg %= n_time, n_comp raise ValueError(msg) # ICA operates on matrices, here we perform for all state variables and modes W = numpy.zeros((n_comp, n_comp, n_svar, n_mode)) # unmixing K = numpy.zeros((n_comp, n_node, n_svar, n_mode)) # whitening matrix src = numpy.zeros( (n_time, n_comp, n_svar, n_mode)) # component time series for mode in range(n_mode): for var in range(n_svar): sl = Ellipsis, var, mode K[sl], W[sl], src[sl] = fastica(data[:, var, :, mode], n_components) return mode_decompositions.IndependentComponents(source=time_series, component_time_series=src, prewhitening_matrix=K, unmixing_matrix=W, n_components=n_comp)
def test_independentcomponents(self): data = numpy.random.random((10, 10, 10, 10)) ts = time_series.TimeSeries(data=data) n_comp = 5 dt = mode_decompositions.IndependentComponents( source = ts, component_time_series = numpy.random.random((10, n_comp, 10, 10)), prewhitening_matrix = numpy.random.random((n_comp, 10, 10, 10)), unmixing_matrix = numpy.random.random((n_comp, n_comp, 10, 10)), n_components = n_comp) dt.compute_norm_source() dt.compute_component_time_series() dt.compute_normalised_component_time_series() summary = dt.summary_info assert summary['Mode decomposition type'] == 'IndependentComponents' assert dt.source is not None assert dt.mixing_matrix.shape == (0,) assert dt.unmixing_matrix.shape == (n_comp, n_comp, 10, 10) assert dt.prewhitening_matrix.shape == (n_comp, 10, 10, 10) assert dt.norm_source.shape == (10, 10, 10, 10) assert dt.component_time_series.shape == (10, 10, n_comp, 10)
def evaluate(self): """ Compute the independent sources """ cls_attr_name = self.__class__.__name__+".time_series" self.time_series.trait["data"].log_debug(owner = cls_attr_name) ts_shape = self.time_series.data.shape #Need more observations than variables if ts_shape[0] < ts_shape[2]: msg = "ICA requires a longer timeseries (tpts > number of nodes)." LOG.error(msg) raise Exception, msg #Need more variables than components if self.n_components > ts_shape[2]: msg = "ICA requires more variables than components to extract (number of nodes > number of components)." LOG.error(msg) raise Exception, msg if self.n_components is None: self.n_components = ts_shape[2] #(n_components, n_components, state-variables, modes) -- unmixing matrix unmixing_matrix_shape = (self.n_components, self.n_components, ts_shape[1], ts_shape[3]) LOG.info("unmixing matrix shape will be: %s" % str(unmixing_matrix_shape)) # (n_components, nodes, state_variables, modes) -- prewhitening matrix prewhitening_matrix_shape = (self.n_components, ts_shape[2], ts_shape[1], ts_shape[3]) LOG.info("prewhitening matrix shape will be: %s" % str(prewhitening_matrix_shape)) unmixing_matrix = numpy.zeros(unmixing_matrix_shape) prewhitening_matrix = numpy.zeros(prewhitening_matrix_shape) #(tpts, n_components, state_variables, modes) -- unmixed sources time series data_ica = numpy.zeros((ts_shape[0], self.n_components, ts_shape[1], ts_shape[3])) #One un/mixing matrix for each state-var & mode. for mode in range(ts_shape[3]): for var in range(ts_shape[1]): # Assumes data must be whitened ica = fastica(self.time_series.data[:, var, :, mode], n_components = self.n_components, whiten = True) # unmixed sources - component_time_series data_ica[:, :, var, mode] = ica[2] # prewhitening matrix prewhitening_matrix[:, :, var, mode] = ica[0] # unmixing matrix unmixing_matrix[:, :, var, mode] = ica[1] util.log_debug_array(LOG, prewhitening_matrix, "whitening_matrix") util.log_debug_array(LOG, unmixing_matrix, "unmixing_matrix") ica_result = mode_decompositions.IndependentComponents(source = self.time_series, component_time_series = data_ica, #mixing_matrix = mixing_matrix, prewhitening_matrix = prewhitening_matrix, unmixing_matrix = unmixing_matrix, n_components = self.n_components, use_storage = False) return ica_result