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.mappeddataset import MappedDataset
16 from mvpa.mappers import MaskMapper
17
18 if __debug__:
19 from mvpa.misc 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 """Initialize `MaskedDataset` instance
31
32 :Parameters:
33 - `mask`: an ndarray where the chosen features equal the non-zero
34 mask elements.
35
36 """
37
38 if not samples is None:
39 samples = N.array(samples)
40 if mask is None:
41
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
49
50
51 mask = N.array(mask, ndmin=len(samples.shape[1:]))
52
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
59
60 MappedDataset.__init__(self,
61 samples=samples,
62 mapper=MaskMapper(mask),
63 **(kwargs))
64
65 else:
66 MappedDataset.__init__(self, **(kwargs))
67
68
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
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
90 fmask = self.mapper.forward( comb_mask != 0 )
91
92
93 return self.selectFeatures(fmask.nonzero()[0], plain=plain)
94