1
2
3
4
5
6
7
8
9 """FeaturewiseDatasetMeasure of stability of labels across chunks based
10 on correlation."""
11
12 __docformat__ = 'restructuredtext'
13
14 import numpy as N
15
16 from mvpa.measures.base import FeaturewiseDatasetMeasure
17
19 """`FeaturewiseDatasetMeasure` that assesses feature stability
20 across runs for each unique label by correlating label activity
21 for pairwise combinations of the chunks.
22
23 If there are multiple samples with the same label in a single
24 chunk (as is typically the case) this algorithm will take the
25 featurewise average of the sample activations to get a single
26 value per label, per chunk.
27
28 """
29
30 - def __init__(self, attr='labels', **kwargs):
31 """Initialize
32
33 :Parameters:
34 attr : basestring
35 Attribute to correlate across chunks.
36 """
37
38 FeaturewiseDatasetMeasure.__init__(self, **kwargs)
39
40 self.__attr = attr
41
42
43 - def _call(self, dataset):
44 """Computes featurewise scores."""
45
46
47 attrdata = eval('dataset.' + self.__attr)
48 samples = dataset.samples
49
50
51 dat = []
52 labels = []
53 chunks = []
54 for c in dataset.uniquechunks:
55 for l in N.unique(attrdata):
56 ind = (dataset.chunks==c)&(attrdata==l)
57 if ind.sum() == 0:
58
59 continue
60
61 dat.append(samples[ind,:].mean(0))
62 labels.append(l)
63 chunks.append(c)
64
65
66 dat = N.asarray(dat)
67 labels = N.asarray(labels)
68 chunks = N.asarray(chunks)
69
70
71
72 ind1 = []
73 ind2 = []
74 for i,c1 in enumerate(N.unique(chunks)[:-1]):
75 for c2 in N.unique(chunks)[i+1:]:
76 for l in N.unique(labels):
77 v1 = N.where((chunks==c1)&(labels==l))[0]
78 v2 = N.where((chunks==c2)&(labels==l))[0]
79 if labels[v1] == labels[v2]:
80
81 ind1.extend(v1)
82 ind2.extend(v2)
83
84
85 ind1 = N.asarray(ind1)
86 ind2 = N.asarray(ind2)
87
88
89 dat1 = dat[ind1,:] - dat[ind1,:].mean(0)[N.newaxis,:].repeat(dat[ind1,:].shape[0],0)
90 dat2 = dat[ind2,:] - dat[ind2,:].mean(0)[N.newaxis,:].repeat(dat[ind2,:].shape[0],0)
91
92
93 covar = (dat1*dat2).mean(0) / dat1.std(0) * dat2.std(0)
94
95 return covar
96