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