Package mvpa :: Package measures :: Module corrstability

[hide private]
[frames] | no frames]

# Source Code for Module mvpa.measures.corrstability

     1  #emacs: -*- mode: python; 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 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
    18 -class CorrStability(FeaturewiseDatasetMeasure):
    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          # init base classes first
    38          FeaturewiseDatasetMeasure.__init__(self, **kwargs)
    39
    40          self.__attr = attr
    41
    42
    43 -    def _call(self, dataset):
    44          """Computes featurewise scores."""
    45
    46          # get the attributes (usally the labels) and the samples
    47          attrdata = eval('dataset.' + self.__attr)
    48          samples = dataset.samples
    49
    50          # take mean within chunks
    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                      # no instances, so skip
    59                      continue
    60                  # append the mean, and the label/chunk info
    61                  dat.append(samples[ind,:].mean(0))
    62                  labels.append(l)
    63                  chunks.append(c)
    64
    65          # convert to arrays
    66          dat = N.asarray(dat)
    67          labels = N.asarray(labels)
    68          chunks = N.asarray(chunks)
    69
    70          # get indices for correlation (all pairwise values across
    71          # chunks)
    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                          # the labels match, so add them
    81                          ind1.extend(v1)
    82                          ind2.extend(v2)
    83
    84          # convert the indices to arrays
    85          ind1 = N.asarray(ind1)
    86          ind2 = N.asarray(ind2)
    87
    88          # remove the mean from the datasets
    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          # calculate the correlation from the covariance and std
    93          covar = (dat1*dat2).mean(0) / dat1.std(0) * dat2.std(0)
    94
    95          return covar
    96