Package mvpa :: Package datasets :: Module mappeddataset
[hide private]
[frames] | no frames]

Source Code for Module mvpa.datasets.mappeddataset

  1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
  2  #ex: set 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  """Mapped dataset""" 
 10   
 11  __docformat__ = 'restructuredtext' 
 12   
 13  import copy 
 14   
 15  from mvpa.datasets import Dataset 
 16   
 17   
18 -class MappedDataset(Dataset):
19 """A `Dataset` which is created by applying a `Mapper` to the data. 20 21 It uses a mapper to transform samples from their original 22 dataspace into the feature space. Various mappers can be used. The 23 "easiest" one is `MaskMapper` which allows to select the features 24 (voxels) to be used in the analysis: see `MaskedDataset` 25 """ 26
27 - def __init__(self, samples=None, mapper=None, dsattr=None, **kwargs):
28 """Initialize `MaskedDataset` 29 30 :Parameters: 31 - `mapper`: Instance of `Mapper` used to map input data 32 33 """ 34 # there are basically two mode for the constructor: 35 # 1. internal mode - only data and dsattr dict 36 # 2. user mode - samples != None # and mapper != None 37 38 # see if dsattr is none, if so, set to empty dict 39 if dsattr is None: 40 dsattr = {} 41 42 # if a mapper was passed, store it in dsattr dict that gets passed 43 # to base Dataset 44 if not mapper is None: 45 # TODO: check mapper for compliance with dimensionality within _data 46 # may be only within __debug__ 47 dsattr['mapper'] = mapper 48 49 # if the samples are passed to the special arg, use the mapper to 50 # transform them. 51 if not samples is None: 52 if dsattr['mapper'] is None: 53 raise ValueError, \ 54 "Constructor of MappedDataset requires a mapper " \ 55 "if unmapped samples are provided." 56 Dataset.__init__(self, 57 samples=mapper.forward(samples), 58 dsattr=dsattr, 59 **(kwargs)) 60 else: 61 Dataset.__init__(self, dsattr=dsattr, **(kwargs))
62 63 64
65 - def mapForward(self, data):
66 """Map data from the original dataspace into featurespace. 67 """ 68 return self.mapper.forward(data)
69 70
71 - def mapReverse(self, data):
72 """Reverse map data from featurespace into the original dataspace. 73 """ 74 return self.mapper.reverse(data)
75 76
77 - def selectFeatures(self, ids, plain=False, sort=False):
78 """Select features given their ids. 79 80 :Parameters: 81 - `ids`: iterable container to select ids 82 - `plain`: `bool`, if to return MappedDataset (or just Dataset) 83 - `sort`: `bool`, if to sort Ids. Order matters and selectFeatures 84 assumes incremental order. If not such, in non-optimized 85 code selectFeatures would verify the order and sort 86 """ 87 88 # call base method to get selected feature subset 89 if plain: 90 sdata = Dataset(self._data, self._dsattr, check_data=False, 91 copy_samples=False, copy_data=False, 92 copy_dsattr=False) 93 return sdata.selectFeatures(ids, sort) 94 else: 95 sdata = Dataset.selectFeatures(self, ids) 96 # since we have new DataSet we better have a new mapper 97 sdata._dsattr['mapper'] = copy.deepcopy(sdata._dsattr['mapper']) 98 sdata._dsattr['mapper'].selectOut(ids, sort) 99 return sdata
100 101 102 # read-only class properties 103 mapper = property(fget=lambda self: self._dsattr['mapper'])
104