Package mvpa :: Package measures :: Module corrcoef
[hide private]
[frames] | no frames]

Source Code for Module mvpa.measures.corrcoef

 1  #emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  #ex: set sts=4 ts=4 sw=4 et: 
 3  ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## 
 4  # 
 5  #   See COPYING file distributed along with the PyMVPA package for the 
 6  #   copyright and license terms. 
 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  # TODO: implement corrcoef optionally without scipy, e.g. N.corrcoef 
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 # init base classes first 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 # Should be safe to assume 0 corr_coef (or 1 pvalue) if value 58 # is actually NaN, although it might not be the case (covar of 59 # 2 constants would be NaN although should be 1) 60 if N.isnan(corrv): 61 if N.var(samples_) == 0.0 and N.var(attrdata) == 0.0 \ 62 and len(samples_): 63 # constant terms 64 corrv = 1.0 - pvalue_index 65 else: 66 corrv = pvalue_index 67 result[ifeature] = corrv 68 69 return result
70