Package mvpa :: Package mappers :: Module pca

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.mappers.pca

     1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
     2  # vi: set ft=python sts=4 ts=4 sw=4 et:
     3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
     4  #
     5  #   See COPYING file distributed along with the PyMVPA package for the
     6  #   copyright and license terms.
     7  #
     8  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
     9  """Data mapper"""
    10
    11  __docformat__ = 'restructuredtext'
    12
    13  import numpy as N
    14
    15  from mvpa.base import warning
    16  from mvpa.base.dochelpers import enhancedDocString
    17  from mvpa.mappers.base import ProjectionMapper
    18
    19  import mvpa.base.externals as externals
    20  if externals.exists('mdp', raiseException=True):
    21      from mdp.nodes import NIPALSNode
    22
    23
    24 -class PCAMapper(ProjectionMapper):
    25      """Mapper to project data onto PCA components estimated from some dataset.
    26
    27      After the mapper has been instantiated, it has to be train first. The PCA
    28      mapper only handles 2D data matrices.
    29      """
    30 -    def __init__(self, transpose=False, **kwargs):
    31          ProjectionMapper.__init__(self, **kwargs)
    32
    33          self._var = None
    34
    35
    36      __doc__ = enhancedDocString('PCAMapper', locals(), ProjectionMapper)
    37
    38
    39 -    def _train(self, dataset):
    40          """Determine the projection matrix onto the components from
    41          a 2D samples x feature data matrix.
    42          """
    43          samples = dataset.samples
    44          dtype = samples.dtype
    45          if str(dtype).startswith('uint') \
    46                 or str(dtype).startswith('int'):
    47                  warning("PCA: input data is in integers. " + \
    48                          "MDP's NIPALSNode operates only on floats, thus "+\
    49                          "coercing to double")
    50                  dtype = N.double
    51                  samples = samples.astype(N.double)
    52
    53          node = NIPALSNode(dtype=dtype)
    54          node.train(samples)
    55          self._proj = N.asmatrix(node.get_projmatrix())
    56          self._recon = N.asmatrix(node.get_recmatrix())
    57
    58          # store variance per PCA component
    59          self._var = node.d
    60
    61
    62      var = property(fget=lambda self: self._var, doc='Variances per component')
    63