示例#1
0
文件: ica.py 项目: pyspace/test
 def _train(self, data, label=None):
     """ Uses *data* to learn a decomposition into independent components."""
     # We simply ignore the class label since we
     # are doing unsupervised learning
     if self.channel_names is None:
         self.channel_names = data.channel_names
     if self.wrapped_node is None:
         self.wrapped_node = FastICANode()
     self.wrapped_node.train(data)
def estimate_components_ica(d):
    """
    Compute the ICA based on the input data d.
    """
    U, s, Vt = pca_components_gf(d, True)
    U = U[:, :NUM_COMPONENTS]
    V = np.transpose(Vt)
    V = V[:, :NUM_COMPONENTS]
    f = FastICANode(whitened = True, max_it = 10000, g = 'tanh', fine_g = 'tanh', max_it_fine = 1000)
    Vr = f.execute(V)
    P = f.get_projmatrix()
    Ur = np.dot(U, P)
    Ur /= np.sum(Ur**2, axis = 0) ** 0.5
    return Ur
示例#3
0
def _demixing_matrix(dataset):
    """Calculate the linear transformation to demix two channels.

    Parameters
    ----------
    dataset : ImagingDataset
        The dataset which is to be demixed. This must have two channels.

    Returns
    -------
    array
        Matrix by which the data can be (left) multiplied to be demixed.
    """
    from mdp.nodes import FastICANode

    # Make matrix of the time averaged channels.
    time_avgs = np.concatenate(
        [im.reshape(-1, 1) for im in dataset.time_averages], axis=1)

    # Perform ICA on the time averaged data.
    node = FastICANode()  # TODO: switch to ICA from scikit-learn ?
    node(time_avgs)
    W = np.dot(node.white.v, node.filters).T

    # Reorder and normalize the rows so that the diagonal coefficients
    # are 1 and the off diagonals have minimal magnitude.
    if abs(old_div(W[0, 0], W[0, 1])) < abs(old_div(W[1, 0], W[1, 1])):
        W = W[::-1]
    W[0] /= W[0, 0]
    W[1] /= W[1, 1]
    assert np.allclose(np.diag(W), 1.)

    return W
def estimate_components_ica(d):
    """
    Compute the ICA based on the input data d.
    """
    U, s, Vt = pca_components_gf(d)
    U = U[:, :NUM_COMPONENTS]
    V = np.transpose(Vt)
    V = V[:, :NUM_COMPONENTS]
    f = FastICANode(whitened=True,
                    max_it=10000,
                    g='tanh',
                    fine_g='tanh',
                    max_it_fine=1000)
    f.execute(V)
    P = f.get_projmatrix()
    Ur = np.dot(U, P)
    Ur /= np.sum(Ur**2, axis=0)**0.5
    return Ur
示例#5
0
 def _train(self, data, label = None):
     """ Uses *data* to learn a decomposition into independent components."""
     # We simply ignore the class label since we 
     # are doing unsupervised learning
     if self.channel_names is None:
         self.channel_names = data.channel_names
     if self.wrapped_node is None:
         self.wrapped_node = FastICANode()
     self.wrapped_node.train(data)
示例#6
0
文件: ica.py 项目: gorlins/PyMVPA
    def _train(self, dataset):
        """Determine the projection matrix onto the components from
        a 2D samples x feature data matrix.
        """
        white_param = {}

        # more features than samples? -> rank deficiancy
        # if not tranposing the data, MDP has to do SVD prior to ICA
        if dataset.samples.shape[1] > dataset.samples.shape[0] \
           and not self._transpose:
            white_param['svd'] = True

        if self._algorithm == 'fastica':
            node = FastICANode(white_parm=white_param,
                               dtype=dataset.samples.dtype)
        elif self._algorithm == 'cubica':
            node = CuBICANode(white_parm=white_param,
                              dtype=dataset.samples.dtype)
        else:
            raise NotImplementedError

#            node.train(dataset.samples.T)
#            self._proj = dataset.samples.T * N.asmatrix(node.get_projmatrix())
#            print self._proj.shape
#        else:
        node.train(dataset.samples)
        self._proj = N.asmatrix(node.get_projmatrix())
        self._recon = N.asmatrix(node.get_recmatrix())
示例#7
0
class ICAWrapperNode(SpatialFilteringNode): #, FastICANodeWrapper):
    """ Wrapper around the Independent Component Analysis filtering of mdp
    
    This Node implements the unsupervised independent component
    analysis algorithm for spatial filtering.

    **Parameters**
        :retained_channels: Determines how many of the ICA pseudo channels
            are retained. Default is None which means "all channels".
            
            (*optional, default: None*)

        :load_path: An absolute path from which the ICA filter 
            is loaded.
            If not specified, this matrix is learned from the training data.

            (*optional, default: None*)

    **Exemplary Call**
    
    .. code-block:: yaml
    
        -
            node : ICA
            parameters:
                retained_channels : 42
    """
    def __init__(self, retained_channels=None, load_path = None, **kwargs):
        if import_error:
            raise ImportError(import_error)
        # Must be set before constructor of superclass is set
        self.trainable = (load_path == None) 
        if "output_dim" in kwargs:
            kwargs.pop("output_dim")
        super(ICAWrapperNode, self).__init__(**kwargs)
        # Load filters from file if requested
        wrapped_node=None
        if load_path != None:
            filters_file = open(load_path, 'r')
            filters, white, whitened = cPickle.load(filters_file)
            wrapped_node = FastICANodeWrapper(trainable=False)
            wrapped_node.filters=filters
            wrapped_node.white = white
            wrapped_node.whitened = whitened
            wrapped_node._training = False
            wrapped_node._train_phase = -1
            wrapped_node._train_phase_started = False
            self.set_permanent_attributes(filters=filters, white=white,
                                          whitened=whitened)

        self.set_permanent_attributes(# The number of channels that will be retained
                                      retained_channels=retained_channels,
                                      # Determine whether this node is trainable
                                      trainable=(load_path == None),
                                      output_dim=retained_channels,
                                      new_channel_names = None,
                                      channel_names = None,
                                      wrapped_node=wrapped_node)

    def is_trainable(self):
        """ Returns whether this node is trainable. """
        return self.trainable
        
    def is_supervised(self):
        """ Returns whether this node requires supervised training. """
        return False
    
    def train(self, data, label=None):
        super(ICAWrapperNode, self).train(data)
    
    def _train(self, data, label = None):
        """ Uses *data* to learn a decomposition into independent components."""
        # We simply ignore the class label since we 
        # are doing unsupervised learning
        if self.channel_names is None:
            self.channel_names = data.channel_names
        if self.wrapped_node is None:
            self.wrapped_node = FastICANode()
        self.wrapped_node.train(data)
       

    def _execute(self, data):
        """ Execute learned transformation on *data*.
        
        Changes the base of the space in which the data is located so
        that the dimensions correspond to independent components
        """
        
        # If this is the first data sample we obtain
        if self.retained_channels == None:
            # Count the number of channels
            self.set_permanent_attributes(retained_channels = data.shape[1])
        if self.channel_names is None:
            self.channel_names = data.channel_names
        if len(self.channel_names)<self.retained_channels:
            self.retained_channels = len(self.channel_names)
            self._log("To many channels chosen for the retained channels! Replaced by maximum number.",level=logging.CRITICAL)
        if not(self.output_dim==self.retained_channels):
            # overwrite internal output_dim variable, since it is set wrong
            self._output_dim = self.retained_channels

        projected_data = self.wrapped_node.execute(data.view(numpy.ndarray)) #super(ICAWrapperNode, self)._execute(data)
        
        # Select the channels that should be retained
        # Note: We have to create a new array since otherwise the removed  
        #       channels remains in memory
        projected_data = numpy.array(projected_data[:, :self.retained_channels])
        
        if self.new_channel_names is None:
            self.new_channel_names = ["ica%03d" % i 
                                for i in range(projected_data.shape[1])]
        return TimeSeries(projected_data, self.new_channel_names,
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)
    
    def store_state(self, result_dir, index=None):
        """ Stores this node in the given directory *result_dir*. """
        if self.store:
            node_dir = os.path.join(result_dir, self.__class__.__name__)
            create_directory(node_dir)
            # This node only stores the learned eigenvector and eigenvalues
            name = "%s_sp%s.pickle" % ("filters", self.current_split)
            result_file = open(os.path.join(node_dir, name), "wb")
            result_file.write(cPickle.dumps((self.wrapped_node.filters, self.wrapped_node.white, self.wrapped_node.whitened),
                                             protocol=2))
            result_file.close()


    def get_filter(self):
        return self.get_projmatrix()
示例#8
0
文件: ica.py 项目: jhuebotter/pyspace
class ICAWrapperNode(SpatialFilteringNode):
    """ Wrapper around the Independent Component Analysis filtering of mdp
    
    This Node implements the unsupervised independent component
    analysis algorithm for spatial filtering.

    **Parameters**
        :retained_channels: Determines how many of the ICA pseudo channels
            are retained. Default is None which means "all channels".
            For ICA channels, there is no sorting,
            hence data has to be reduced in the internal whitening beforehand.
            
            (*optional, default: None*)

        :load_path: An absolute path from which the ICA filter 
            is loaded.
            If not specified, this matrix is learned from the training data.

            (*optional, default: None*)

    **Exemplary Call**
    
    .. code-block:: yaml
    
        -
            node : ICA
            parameters:
                retained_channels : 42
    """
    def __init__(self, retained_channels=None, load_path=None, **kwargs):
        if import_error:
            raise ImportError(import_error)
        # Must be set before constructor of superclass is set
        self.trainable = (load_path is None)
        if "output_dim" in kwargs:
            kwargs.pop("output_dim")
        super(ICAWrapperNode, self).__init__(**kwargs)
        # Load filters from file if requested
        wrapped_node = None
        if load_path is not None:
            filters_file = open(load_path, 'r')
            filters, white, whitened = cPickle.load(filters_file)
            wrapped_node = FastICANodeWrapper(trainable=False,
                                              white_comp=retained_channels)
            wrapped_node.filters = filters
            wrapped_node.white = white
            wrapped_node.whitened = whitened
            wrapped_node._training = False
            wrapped_node._train_phase = -1
            wrapped_node._train_phase_started = False
            self.set_permanent_attributes(filters=filters,
                                          white=white,
                                          whitened=whitened)

        self.set_permanent_attributes(
            # The number of channels that will be retained
            retained_channels=retained_channels,
            # Determine whether this node is trainable
            trainable=(load_path is None),
            output_dim=retained_channels,
            new_channel_names=None,
            channel_names=None,
            wrapped_node=wrapped_node)

    def is_trainable(self):
        """ Returns whether this node is trainable. """
        return self.trainable

    def is_supervised(self):
        """ Returns whether this node requires supervised training. """
        return False

    def _train(self, data, label=None):
        """ Uses *data* to learn a decomposition into independent components."""
        # We simply ignore the class label since we
        # are doing unsupervised learning
        if self.channel_names is None:
            self.channel_names = data.channel_names
        if self.wrapped_node is None:
            self.wrapped_node = FastICANode()
        data *= 1.0
        self.wrapped_node.train(data)

    def _execute(self, data):
        """ Execute learned transformation on *data*.
        
        Changes the base of the space in which the data is located so
        that the dimensions correspond to independent components
        """

        # If this is the first data sample we obtain
        if self.retained_channels is None:
            # Count the number of channels
            self.set_permanent_attributes(retained_channels=data.shape[1])
        if self.channel_names is None:
            self.channel_names = data.channel_names
        if len(self.channel_names) < self.retained_channels:
            self.retained_channels = len(self.channel_names)
            self._log(
                "To many channels chosen for the retained channels!"
                " Replaced by maximum number.",
                level=logging.CRITICAL)
        if not (self.output_dim == self.retained_channels):
            # overwrite internal output_dim variable, since it is set wrong
            self._output_dim = self.retained_channels

        projected_data = self.wrapped_node.execute(data.view(numpy.ndarray))

        # Select the channels that should be retained
        # Note: We have to create a new array since otherwise the removed
        #       channels remains in memory
        projected_data = numpy.array(
            projected_data[:, :self.retained_channels])

        if self.new_channel_names is None:
            self.new_channel_names = [
                "ica%03d" % i for i in range(projected_data.shape[1])
            ]
        return TimeSeries(projected_data, self.new_channel_names,
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)

    def store_state(self, result_dir, index=None):
        """ Stores this node in the given directory *result_dir*. """
        if self.store:
            node_dir = os.path.join(result_dir, self.__class__.__name__)
            create_directory(node_dir)
            # This node only stores the learned eigenvector and eigenvalues
            name = "%s_sp%s.pickle" % ("filters", self.current_split)
            result_file = open(os.path.join(node_dir, name), "wb")
            result_file.write(
                cPickle.dumps(
                    (self.wrapped_node.filters, self.wrapped_node.white,
                     self.wrapped_node.whitened),
                    protocol=2))
            result_file.close()

    def get_filter(self):
        return self.wrapped_node.get_projmatrix()