Package mvpa :: Package mappers :: Module zscore
[hide private]
[frames] | no frames]

Source Code for Module mvpa.mappers.zscore

 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  """Simple mapper to perform zscoring""" 
10   
11  __docformat__ = 'restructuredtext' 
12   
13  from mvpa.base import warning, externals 
14   
15  import numpy as N 
16  from mvpa.base.dochelpers import enhancedDocString 
17  from mvpa.mappers.base import ProjectionMapper 
18   
19  if externals.exists('scipy', raiseException=True): 
20      import scipy.sparse 
21      if externals.versions['scipy'] >= (0, 7, 0): 
22          _identity = scipy.sparse.identity 
23      else: 
24          _identity = scipy.sparse.spidentity 
25   
26   
27  __all__ = [ 'ZScoreMapper' ]            # just to don't leak all the evil ;) 
28   
29 -class ZScoreMapper(ProjectionMapper):
30 """Mapper to project data into standardized values (z-scores). 31 32 After the mapper has been instantiated, it has to be train first. 33 34 Since it tries to reuse ProjectionMapper, invariant features will 35 simply be assigned a std == 1, which would be equivalent to not 36 standardizing them at all. This is similar to not touching them 37 at all, so similar to what zscore function currently does 38 """
39 - def __init__(self, 40 baselinelabels=None, 41 **kwargs):
42 """Initialize ZScoreMapper 43 44 :Parameters: 45 baselinelabels : None or list of int or string 46 Used to compute mean and variance 47 TODO: not in effect now and need to be adherent to all 48 `ProjectionMapper`s 49 """ 50 51 ProjectionMapper.__init__(self, **kwargs) 52 if baselinelabels is not None: 53 raise NotImplementedError, "Support for baseline labels " \ 54 "is not yet implemented in ZScoreMapper" 55 self.baselinelabels = baselinelabels
56 #self._var = None 57 58 59 __doc__ = enhancedDocString('ZScoreMapper', locals(), ProjectionMapper) 60 61
62 - def _train(self, dataset):
63 """Determine the diagonal matrix with coefficients for standartization 64 """ 65 samples = dataset.samples 66 X = self._demeanData(samples) 67 std = X.std(axis=0) 68 69 # ??? equivalent to not touching values at all, but we don't 70 # have such ability in ProjectionMapper atm afaik 71 std[std == 0] = 1.0 72 n = len(std) 73 74 # scipy or numpy manages to screw up: 75 # or YOH is too tired?: 76 # (Pydb) zsm._proj 77 # <1x1 sparse matrix of type '<type 'numpy.float64'>' 78 # with 1 stored elements (space for 1) 79 # in Compressed Sparse Column format> 80 # *(Pydb) (N.asmatrix(ds1.samples) - zsm._mean).shape 81 # (120, 1) 82 # *(Pydb) (N.asmatrix(ds1.samples) - zsm._mean) * zsm._proj 83 # matrix([[-0.13047326]]) 84 # 85 # so we will handle case with n = 1 with regular non-sparse 86 # matrices 87 if n > 1: 88 proj = _identity(n) 89 proj.setdiag(1.0/std) 90 recon = _identity(n) 91 recon.setdiag(std) 92 self._proj = proj 93 self._recon = recon 94 else: 95 self._proj = N.matrix([[1.0/std[0]]]) 96 self._recon = N.matrix([std])
97