1
2
3
4
5
6
7
8
9 """FeaturewiseDatasetMeasure of correlation with the labels."""
10
11 __docformat__ = 'restructuredtext'
12
13 from mvpa.base import externals
14 externals.exists('scipy', raiseException=True)
15
16
17 import numpy as N
18 from scipy.stats import pearsonr
19
20 from mvpa.measures.base import FeaturewiseDatasetMeasure
21
22 -class CorrCoef(FeaturewiseDatasetMeasure):
23 """`FeaturewiseDatasetMeasure` that performs correlation with labels
24
25 XXX: Explain me!
26 """
27
28 - def __init__(self, pvalue=False, attr='labels', **kwargs):
29 """Initialize
30
31 :Parameters:
32 pvalue : bool
33 Either to report p-value of pearsons correlation coefficient
34 instead of pure correlation coefficient
35 attr : basestring
36 What attribut to correlate with
37 """
38
39 FeaturewiseDatasetMeasure.__init__(self, **kwargs)
40
41 self.__pvalue = int(pvalue)
42 self.__attr = attr
43
44
45 - def _call(self, dataset):
46 """Computes featurewise scores."""
47
48 attrdata = eval('dataset.' + self.__attr)
49 samples = dataset.samples
50 pvalue_index = self.__pvalue
51 result = N.empty((dataset.nfeatures,), dtype=float)
52
53 for ifeature in xrange(dataset.nfeatures):
54 samples_ = samples[:, ifeature]
55 corr = pearsonr(samples_, attrdata)
56 corrv = corr[pvalue_index]
57
58
59
60 if N.isnan(corrv):
61 if N.var(samples_) == 0.0 and N.var(attrdata) == 0.0 \
62 and len(samples_):
63
64 corrv = 1.0 - pvalue_index
65 else:
66 corrv = pvalue_index
67 result[ifeature] = corrv
68
69 return result
70