1
2
3
4
5
6
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
25
27 """
28 """
29 ds = datasets['uni2medium']
30 d2d = ds.samples
31 ws = 15
32 sp = N.arange(ds.nsamples-ws*2) + ws
33
34
35 bcm = BoxcarMapper(sp, ws)
36 d3d = bcm(d2d)
37
38
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
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
65 self.failUnless(dd_wd.shape[0] == dd.shape[0])
66
67 if not isinstance(wdm, WaveletPacketMapper):
68
69 dd_rev = wdm.reverse(dd_wd)
70
71
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
81 """
82 """
83
84 ds = datasets['uni2large']
85 d2d = ds.samples
86 ws = 50
87 sp = (N.arange(ds.nsamples - ws*2) + ws)[:4]
88
89
90 bcm = BoxcarMapper(sp, ws)
91 d3d = bcm(d2d)
92
93
94 wdm = WaveletPacketMapper(level=2, wavelet='sym2')
95 d3d_wd = wdm(d3d)
96
97
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
104
105 if externals.exists('pywt wp reconstruct'):
106
107
108 d3d_rev = wdm.reverse(d3d_wd)
109
110
111
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
149
150
153
154
155 if __name__ == '__main__':
156 import runner
157