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

Source Code for Module mvpa.datasets.maskeddataset

 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  """Dataset with applied mask""" 
10   
11  __docformat__ = 'restructuredtext' 
12   
13  import numpy as N 
14   
15  from mvpa.datasets.mappeddataset import MappedDataset 
16  from mvpa.mappers import MaskMapper 
17   
18  if __debug__: 
19      from mvpa.misc import debug 
20   
21 -class MaskedDataset(MappedDataset):
22 """Helper class which is `MappedDataset` with using `MaskMapper`. 23 24 TODO: since what it does is simply some checkes/data_mangling in the 25 constructor, it might be absorbed inside generic `MappedDataset` 26 27 """ 28
29 - def __init__(self, samples=None, mask=None, **kwargs):
30 """Initialize `MaskedDataset` instance 31 32 :Parameters: 33 - `mask`: an ndarray where the chosen features equal the non-zero 34 mask elements. 35 36 """ 37 # need if clause here as N.array(None) != None 38 if not samples is None: 39 samples = N.array(samples) 40 if mask is None: 41 # make full dataspace mask if nothing else is provided 42 mask = N.ones(samples.shape[1:], dtype='bool') 43 if not mask is None: 44 if samples is None: 45 raise ValueError, \ 46 "Constructor of MaskedDataset requires both a samples " \ 47 "array and a mask if one of both is provided." 48 # expand mask to span all dimensions but first one 49 # necessary e.g. if only one slice from timeseries of volumes is 50 # requested. 51 mask = N.array(mask, ndmin=len(samples.shape[1:])) 52 # check for compatibility 53 if not samples.shape[1:] == mask.shape: 54 raise ValueError, "The mask dataspace shape [%s] is not " \ 55 "compatible with the shape of the provided " \ 56 "data samples [%s]." % (`mask.shape`, 57 `samples.shape[1:]`) 58 # init base class -- MappedDataset takes care of all the forward 59 # mapping stuff 60 MappedDataset.__init__(self, 61 samples=samples, 62 mapper=MaskMapper(mask), 63 **(kwargs)) 64 65 else: 66 MappedDataset.__init__(self, **(kwargs))
67 68
69 - def selectFeaturesByMask(self, mask, plain=False):
70 """Use a mask array to select features from the current set. 71 72 :Parameters: 73 mask : ndarray 74 input mask 75 plain : bool 76 `True` directs to return a simple `Dataset`, 77 `False` -- a new `MaskedDataset` object 78 79 Returns a new MaskedDataset object with a view of the original pattern 80 array (no copying is performed). 81 The final selection mask only contains features that are present in the 82 current feature mask AND the selection mask passed to this method. 83 """ 84 # AND new and old mask to get the common features 85 comb_mask = N.logical_and(mask != 0, 86 self.mapper.getMask(copy=False) != 0) 87 if __debug__: 88 debug('DS', "VERY SUBOPTIMAL - do not rely on performance") 89 # transform mask into feature space 90 fmask = self.mapper.forward( comb_mask != 0 ) 91 #TODO all this will be gone soon anyway -- need proper selectIn within 92 # a mapper 93 return self.selectFeatures(fmask.nonzero()[0], plain=plain)
94