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

Source Code for Module mvpa.mappers.samplegroup

  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.dochelpers import enhancedDocString 
 16  from mvpa.mappers.base import Mapper 
 17  from mvpa.misc.transformers import FirstAxisMean 
 18   
 19  if __debug__: 
 20      from mvpa.base import debug 
 21   
 22   
23 -class SampleGroupMapper(Mapper):
24 # name is ugly, please help! 25 """Mapper to apply a mapping function to samples of the same type. 26 27 A customimzable function is applied individually to all samples with the 28 same unique label from the same chunk. This mapper is somewhat 29 unconventional since it doesn't preserve number of samples (ie the size of 30 0-th dimension...) 31 """ 32
33 - def __init__(self, fx=FirstAxisMean):
34 """Initialize the PCAMapper 35 36 Parameters: 37 startpoints: A sequence of index value along the first axis of 38 'data'. 39 boxlength: The number of elements after 'startpoint' along the 40 first axis of 'data' to be considered for averaging. 41 offset: The offset between the starting point and the 42 averaging window (boxcar). 43 collision_resolution : string 44 if a sample belonged to multiple output samples, then on reverse, 45 how to resolve the value (choices: 'mean') 46 """ 47 Mapper.__init__(self) 48 49 self.__fx = fx 50 self.__uniquechunks = None 51 self.__uniquelabels = None 52 self.__chunks = None 53 self.__labels = None 54 self.__datashape = None
55 56 57 __doc__ = enhancedDocString('SampleGroupMapper', locals(), Mapper) 58 59
60 - def train(self, dataset):
61 """ 62 """ 63 # just store the relevant information 64 self.__uniquechunks = dataset.uniquechunks 65 self.__uniquelabels = dataset.uniquelabels 66 self.__chunks = dataset.chunks 67 self.__labels = dataset.labels 68 self.__datashape = (dataset.nfeatures, )
69 70
71 - def forward(self, data):
72 """ 73 """ 74 if self.__datashape is None: 75 raise RuntimeError, \ 76 "SampleGroupMapper needs to be trained before it can be used" 77 78 mdata = [] 79 80 # for each label in each chunk 81 for c in self.__uniquechunks: 82 for l in self.__uniquelabels: 83 mdata.append(self.__fx(data[N.logical_and(self.__labels == l, 84 self.__chunks == c)])) 85 86 return N.array(mdata)
87 88
89 - def reverse(self, data):
90 """This is not implemented.""" 91 raise NotImplementedError
92 93
94 - def getInSize(self):
95 """Returns the number of original samples which were combined. 96 """ 97 return self.__datashape[0]
98 99
100 - def getOutSize(self):
101 """Returns the number of output samples. 102 """ 103 return self.__datashape[0]
104 105
106 - def selectOut(self, outIds):
107 """Just complain for now""" 108 raise NotImplementedError, \ 109 "For feature selection use MaskMapper on output of the %s mapper" \ 110 % self.__class__.__name__
111