Package mvpa :: Package datasets :: Module masked

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.datasets.masked

     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  """Dataset with applied mask"""
    10
    11  __docformat__ = 'restructuredtext'
    12
    13  import numpy as N
    14
    15  from mvpa.datasets.mapped import MappedDataset
    16  from mvpa.mappers.array import DenseArrayMapper
    17
    18  if __debug__:
    19      from mvpa.base 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          """
    31          :Parameters:
    32            mask: ndarray
    33              the chosen features equal the non-zero mask elements.
    34          """
    35          # might contain the default mapper
    36          mapper = None
    37
    38          # need if clause here as N.array(None) != None
    39          if not samples is None:
    40              # XXX should be asanyarray? but then smth segfaults on unittests
    41              samples = N.asarray(samples)
    42              mapper = DenseArrayMapper(mask=mask,
    43                                        shape=samples.shape[1:])
    44
    45          if not mapper is None:
    46              if samples is None:
    47                  raise ValueError, \
    48                        "Constructor of MaskedDataset requires both a samples " \
    49                        "array and a mask if one of both is provided."
    50              # init base class -- MappedDataset takes care of all the forward
    51              # mapping stuff
    52              MappedDataset.__init__(
    53                  self,
    54                  samples=samples,
    55                  mapper=mapper,
    56                  **(kwargs))
    57          else:
    58              MappedDataset.__init__(self, **(kwargs))
    59
    60
    61 -    def selectFeaturesByMask(self, mask, plain=False):
    62          """Use a mask array to select features from the current set.
    63
    64          :Parameters:
    65            mask : ndarray
    66              input mask
    67            plain : bool
    68              `True` directs to return a simple `Dataset`,
    69              `False` -- a new `MaskedDataset` object
    70
    71          Returns a new MaskedDataset object with a view of the original pattern
    72          array (no copying is performed).
    73          The final selection mask only contains features that are present in the
    74          current feature mask AND the selection mask passed to this method.
    75          """
    76          # AND new and old mask to get the common features
    77          comb_mask = N.logical_and(mask != 0,
    78                                    self.mapper.getMask(copy=False) != 0)
    79          if __debug__:
    80              debug('DS', "VERY SUBOPTIMAL - do not rely on performance")
    81          # transform mask into feature space
    82          fmask = self.mapper.forward( comb_mask != 0 )
    83          #TODO all this will be gone soon anyway -- need proper selectIn within
    84          # a mapper
    85          return self.selectFeatures(fmask.nonzero()[0], plain=plain)
    86