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

Source Code for Module mvpa.measures.corrcoef

 1  # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- 
 2  # vi: set ft=python 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   
15  import numpy as N 
16   
17  if externals.exists('scipy', raiseException=True): 
18      # TODO: implement corrcoef optionally without scipy, e.g. N.corrcoef 
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 # init base classes first 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 # Should be safe to assume 0 corr_coef (or 1 pvalue) if value 59 # is actually NaN, although it might not be the case (covar of 60 # 2 constants would be NaN although should be 1) 61 if N.isnan(corrv): 62 if N.var(samples_) == 0.0 and N.var(attrdata) == 0.0 \ 63 and len(samples_): 64 # constant terms 65 corrv = 1.0 - pvalue_index 66 else: 67 corrv = pvalue_index 68 result[ifeature] = corrv 69 70 return result
71