1
2
3
4
5
6
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
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
36 mapper = None
37
38
39 if not samples is None:
40
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
51
52 MappedDataset.__init__(
53 self,
54 samples=samples,
55 mapper=mapper,
56 **(kwargs))
57 else:
58 MappedDataset.__init__(self, **(kwargs))
59
60
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
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
82 fmask = self.mapper.forward( comb_mask != 0 )
83
84
85 return self.selectFeatures(fmask.nonzero()[0], plain=plain)
86