Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

NF: FxyMapper

For now only a small doctest, real unittest will come with first
application.
  • Loading branch information...
commit 528456c848a6a7c20ba261412dcaa410e642f076 1 parent e408474
Michael Hanke hanke authored

Showing 3 changed files with 93 additions and 0 deletions. Show diff stats Hide diff stats

  1. +2 0  Changelog
  2. +90 0 mvpa2/mappers/fxy.py
  3. +1 0  mvpa2/suite.py
2  Changelog
@@ -41,6 +41,8 @@ Releases
41 41 confusion matrices with a error function interface (e.g. for
42 42 CrossValidation(errorfx=...). This class existed for a long time, but
43 43 was hidden in the unit tests.
  44 + - Add :class:`~mvpa2.mappers.fxy.FxyMapper` to perform arbitrary
  45 + computations involving two datasets.
44 46
45 47 * 2.0.0 (Mon, Dec 19 2011)
46 48
90 mvpa2/mappers/fxy.py
... ... @@ -0,0 +1,90 @@
  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 +""""""
  10 +
  11 +__docformat__ = 'restructuredtext'
  12 +
  13 +from mvpa2.base.dochelpers import _str, _repr_attrs
  14 +from mvpa2.datasets import Dataset
  15 +from mvpa2.mappers.base import Mapper
  16 +from mvpa2.base.dochelpers import borrowdoc
  17 +
  18 +class FxyMapper(Mapper):
  19 + """Mapper to execute a callable with two datasets as arguments.
  20 +
  21 + The first dataset is passed to the mapper during training, the second
  22 + dataset is passed to forward/call(). This mapper is useful to, for example,
  23 + compare two datasets regarding particular aspects, merge them, or perform
  24 + other operations that require the presence of two datasets.
  25 + """
  26 +
  27 + def __init__(self, fx, train_as_1st=True, **kwargs):
  28 + """
  29 + Parameters
  30 + ----------
  31 + fx : callable
  32 + Functor that is called with the two datasets upon forward-mapping.
  33 + train_as_1st : bool
  34 + If True, the training dataset is passed to the target callable as
  35 + the first argument and the other dataset as the second argument.
  36 + If False, it is vice versa.
  37 +
  38 + Examples
  39 + --------
  40 + >>> from mvpa2.mappers.fxy import FxyMapper
  41 + >>> from mvpa2.datasets import Dataset
  42 + >>> callable = lambda x,y: len(x) > len(y)
  43 + >>> ds1 = Dataset(range(5))
  44 + >>> ds2 = Dataset(range(3))
  45 + >>> fxy = FxyMapper(callable)
  46 + >>> fxy.train(ds1)
  47 + >>> fxy(ds2).item()
  48 + True
  49 + >>> fxy = FxyMapper(callable, train_as_1st=False)
  50 + >>> fxy.train(ds1)
  51 + >>> fxy(ds2).item()
  52 + False
  53 + """
  54 + Mapper.__init__(self, **kwargs)
  55 + self._fx = fx
  56 + self._train_as_1st = train_as_1st
  57 + self._ds_train = None
  58 +
  59 + @borrowdoc(Mapper)
  60 + def __repr__(self, prefixes=[]):
  61 + return super(FxyMapper, self).__repr__(
  62 + prefixes=prefixes + _repr_attrs(self, ['fx']))
  63 +
  64 + def __str__(self):
  65 + return _str(self, fx=self._fx.__name__)
  66 +
  67 + def _train(self, ds):
  68 + self._ds_train = ds
  69 +
  70 + def _untrain(self):
  71 + self._ds_train = None
  72 +
  73 + @borrowdoc(Mapper)
  74 + def _forward_dataset(self, ds):
  75 + # apply function
  76 + if self._train_as_1st:
  77 + out = self._fx(self._ds_train, ds)
  78 + else:
  79 + out = self._fx(ds, self._ds_train)
  80 + # wrap output in a dataset if necessary
  81 + if not isinstance(out, Dataset):
  82 + try:
  83 + out = Dataset(out)
  84 + except ValueError:
  85 + # not a sequence?
  86 + out = Dataset([out])
  87 + return out
  88 +
  89 + fx = property(fget=lambda self:self.__fx)
  90 +
1  mvpa2/suite.py
@@ -143,6 +143,7 @@
143 143 from mvpa2.mappers.procrustean import *
144 144 from mvpa2.mappers.boxcar import *
145 145 from mvpa2.mappers.fx import *
  146 +from mvpa2.mappers.fxy import *
146 147 from mvpa2.mappers.som import *
147 148 from mvpa2.mappers.zscore import *
148 149 if externals.exists('scipy'):

0 comments on commit 528456c

Please sign in to comment.
Something went wrong with that request. Please try again.