Package mvpa :: Package tests :: Module test_waveletmapper
[hide private]
[frames] | no frames]

Source Code for Module mvpa.tests.test_waveletmapper

  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  """Unit tests for PyMVPA Wavelet mappers""" 
 10   
 11  from mvpa.base import externals 
 12  externals.exists('pywt', raiseException=True) 
 13   
 14  import unittest 
 15  from mvpa.support.copy import deepcopy 
 16  import numpy as N 
 17   
 18  from mvpa.mappers.boxcar import BoxcarMapper 
 19  from mvpa.mappers.wavelet import * 
 20  from mvpa.datasets import Dataset 
 21   
 22  from tests_warehouse import datasets 
 23   
24 -class WaveletMappersTests(unittest.TestCase):
25
26 - def testSimpleWDM(self):
27 """ 28 """ 29 ds = datasets['uni2medium'] 30 d2d = ds.samples 31 ws = 15 # size of timeline for wavelet 32 sp = N.arange(ds.nsamples-ws*2) + ws 33 34 # create 3D instance (samples x timepoints x channels) 35 bcm = BoxcarMapper(sp, ws) 36 d3d = bcm(d2d) 37 38 # use wavelet mapper 39 wdm = WaveletTransformationMapper() 40 d3d_wd = wdm(d3d) 41 d3d_swap = d3d.swapaxes(1,2) 42 43 self.failUnlessRaises(ValueError, WaveletTransformationMapper, 44 wavelet='bogus') 45 self.failUnlessRaises(ValueError, WaveletTransformationMapper, 46 mode='bogus') 47 48 # use wavelet mapper 49 for wdm, wdm_swap in ((WaveletTransformationMapper(), 50 WaveletTransformationMapper(dim=2)), 51 (WaveletPacketMapper(), 52 WaveletPacketMapper(dim=2))): 53 for dd, dd_swap in ((d3d, d3d_swap), 54 (d2d, None)): 55 dd_wd = wdm(dd) 56 if dd_swap is not None: 57 dd_wd_swap = wdm_swap(dd_swap) 58 59 self.failUnless((dd_wd == dd_wd_swap.swapaxes(1,2)).all(), 60 msg="We should have got same result with swapped " 61 "dimensions and explicit mentioining of it. " 62 "Got %s and %s" % (dd_wd, dd_wd_swap)) 63 64 # some sanity checks 65 self.failUnless(dd_wd.shape[0] == dd.shape[0]) 66 67 if not isinstance(wdm, WaveletPacketMapper): 68 # we can do reverse only for DWT 69 dd_rev = wdm.reverse(dd_wd) 70 # inverse transform might be not exactly as the 71 # input... but should be very close ;-) 72 self.failUnlessEqual(dd_rev.shape, dd.shape, 73 msg="Shape should be the same after iDWT") 74 75 diff = N.linalg.norm(dd - dd_rev) 76 ornorm = N.linalg.norm(dd) 77 self.failUnless(diff/ornorm < 1e-10)
78 79
80 - def testSimpleWP1Level(self):
81 """ 82 """ 83 84 ds = datasets['uni2large'] 85 d2d = ds.samples 86 ws = 50 # size of timeline for wavelet 87 sp = (N.arange(ds.nsamples - ws*2) + ws)[:4] 88 89 # create 3D instance (samples x timepoints x channels) 90 bcm = BoxcarMapper(sp, ws) 91 d3d = bcm(d2d) 92 93 # use wavelet mapper 94 wdm = WaveletPacketMapper(level=2, wavelet='sym2') 95 d3d_wd = wdm(d3d) 96 97 # Check dimensionality 98 d3d_wds, d3ds = d3d_wd.shape, d3d.shape 99 self.failUnless(len(d3d_wds) == len(d3ds)+1) 100 self.failUnless(d3d_wds[1] * d3d_wds[2] >= d3ds[1]) 101 self.failUnless(d3d_wds[0] == d3ds[0]) 102 self.failUnless(d3d_wds[-1] == d3ds[-1]) 103 #print d2d.shape, d3d.shape, d3d_wd.shape 104 105 if externals.exists('pywt wp reconstruct'): 106 # Test reverse -- should be identical 107 # we can do reverse only for DWT 108 d3d_rev = wdm.reverse(d3d_wd) 109 110 # inverse transform might be not exactly as the 111 # input... but should be very close ;-) 112 self.failUnlessEqual(d3d_rev.shape, d3d.shape, 113 msg="Shape should be the same after iDWT") 114 115 diff = N.linalg.norm(d3d - d3d_rev) 116 ornorm = N.linalg.norm(d3d) 117 118 if externals.exists('pywt wp reconstruct fixed'): 119 self.failUnless(diff/ornorm < 1e-10) 120 else: 121 self.failUnlessRaises(NotImplementedError, wdm.reverse, d3d_wd)
122 123
124 - def _testCompareToOld(self):
125 """Good just to compare if I didn't screw up anything... treat 126 it as a regression test 127 """ 128 import mvpa.mappers.wavelet_ as wavelet_ 129 130 ds = datasets['uni2medium'] 131 d2d = ds.samples 132 ws = 16 # size of timeline for wavelet 133 sp = N.arange(ds.nsamples-ws*2) + ws 134 135 # create 3D instance (samples x timepoints x channels) 136 bcm = BoxcarMapper(sp, ws) 137 d3d = bcm(d2d) 138 139 # use wavelet mapper 140 for wdm, wdm_ in ((WaveletTransformationMapper(), 141 wavelet_.WaveletTransformationMapper()), 142 (WaveletPacketMapper(), 143 wavelet_.WaveletPacketMapper()),): 144 d3d_wd = wdm(d3d) 145 d3d_wd_ = wdm_(d3d) 146 147 self.failUnless((d3d_wd == d3d_wd_).all(), 148 msg="We should have got same result with old and new code. Got %s and %s" % (d3d_wd, d3d_wd_))
149 150
151 -def suite():
152 return unittest.makeSuite(WaveletMappersTests)
153 154 155 if __name__ == '__main__': 156 import runner 157