def _test_compare_to_old(self): """Good just to compare if I didn't screw up anything... treat it as a regression test """ import mvpa.mappers.wavelet_ as wavelet_ ds = datasets['uni2medium'] d2d = ds.samples ws = 16 # size of timeline for wavelet sp = np.arange(ds.nsamples-ws*2) + ws # create 3D instance (samples x timepoints x channels) bcm = BoxcarMapper(sp, ws) d3d = bcm.forward(d2d) # use wavelet mapper for wdm, wdm_ in ((WaveletTransformationMapper(), wavelet_.WaveletTransformationMapper()), (WaveletPacketMapper(), wavelet_.WaveletPacketMapper()),): d3d_wd = wdm(d3d) d3d_wd_ = wdm_(d3d) self.failUnless((d3d_wd == d3d_wd_).all(), msg="We should have got same result with old and new code. " "Got %s and %s" % (d3d_wd, d3d_wd_))
def test_simple_wdm(self): """ """ ds = datasets['uni2medium'] d2d = ds.samples ws = 15 # size of timeline for wavelet sp = np.arange(ds.nsamples-ws*2) + ws # create 3D instance (samples x timepoints x channels) bcm = BoxcarMapper(sp, ws) d3d = bcm.forward(d2d) # use wavelet mapper wdm = WaveletTransformationMapper() d3d_wd = wdm.forward(d3d) d3d_swap = d3d.swapaxes(1,2) self.failUnlessRaises(ValueError, WaveletTransformationMapper, wavelet='bogus') self.failUnlessRaises(ValueError, WaveletTransformationMapper, mode='bogus') # use wavelet mapper for wdm, wdm_swap in ((WaveletTransformationMapper(), WaveletTransformationMapper(dim=2)), (WaveletPacketMapper(), WaveletPacketMapper(dim=2))): for dd, dd_swap in ((d3d, d3d_swap), (d2d, None)): dd_wd = wdm.forward(dd) if dd_swap is not None: dd_wd_swap = wdm_swap.forward(dd_swap) self.failUnless((dd_wd == dd_wd_swap.swapaxes(1,2)).all(), msg="We should have got same result with swapped " "dimensions and explicit mentioining of it. " "Got %s and %s" % (dd_wd, dd_wd_swap)) # some sanity checks self.failUnless(dd_wd.shape[0] == dd.shape[0]) if not isinstance(wdm, WaveletPacketMapper): # we can do reverse only for DWT dd_rev = wdm.reverse(dd_wd) # inverse transform might be not exactly as the # input... but should be very close ;-) self.failUnlessEqual(dd_rev.shape, dd.shape, msg="Shape should be the same after iDWT") diff = np.linalg.norm(dd - dd_rev) ornorm = np.linalg.norm(dd) self.failUnless(diff/ornorm < 1e-10)
def test_datasetmapping(): # 6 samples, 4 features data = np.arange(24).reshape(6,4) ds = Dataset(data, sa={'timepoints': np.arange(6), 'multidim': data.copy()}, fa={'fid': np.arange(4)}) # with overlapping and non-overlapping boxcars startpoints = [0, 1, 4] boxlength = 2 bm = BoxcarMapper(startpoints, boxlength, inspace='boxy') # train is critical bm.train(ds) mds = bm.forward(ds) assert_equal(len(mds), len(startpoints)) assert_equal(mds.nfeatures, boxlength) # all samples attributes remain, but the can rotated/compressed into # multidimensional attributes assert_equal(sorted(mds.sa.keys()), ['boxy_onsetidx'] + sorted(ds.sa.keys())) assert_equal(mds.sa.multidim.shape, (len(startpoints), boxlength, ds.nfeatures)) assert_equal(mds.sa.timepoints.shape, (len(startpoints), boxlength)) assert_array_equal(mds.sa.timepoints.flatten(), np.array([(s, s+1) for s in startpoints]).flatten()) assert_array_equal(mds.sa.boxy_onsetidx, startpoints) # feature attributes also get rotated and broadcasted assert_array_equal(mds.fa.fid, [ds.fa.fid, ds.fa.fid]) # and finally there is a new one assert_array_equal(mds.fa.boxy_offsetidx, np.repeat(np.arange(boxlength), 4).reshape(2,-1)) # now see how it works on reverse() rds = bm.reverse(mds) # we got at least something of all original attributes back assert_equal(sorted(rds.sa.keys()), sorted(ds.sa.keys())) assert_equal(sorted(rds.fa.keys()), sorted(ds.fa.keys())) # it is not possible to reconstruct the full samples array # some samples even might show up multiple times (when there are overlapping # boxcars assert_array_equal(rds.samples, np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [16, 17, 18, 19], [20, 21, 22, 23]])) assert_array_equal(rds.sa.timepoints, [0, 1, 1, 2, 4, 5]) assert_array_equal(rds.sa.multidim, ds.sa.multidim[rds.sa.timepoints]) # but feature attributes should be fully recovered assert_array_equal(rds.fa.fid, ds.fa.fid)
def test_datasetmapping(): # 6 samples, 4 features data = np.arange(24).reshape(6, 4) ds = Dataset(data, sa={ 'timepoints': np.arange(6), 'multidim': data.copy() }, fa={'fid': np.arange(4)}) # with overlapping and non-overlapping boxcars startpoints = [0, 1, 4] boxlength = 2 bm = BoxcarMapper(startpoints, boxlength, inspace='boxy') # train is critical bm.train(ds) mds = bm.forward(ds) assert_equal(len(mds), len(startpoints)) assert_equal(mds.nfeatures, boxlength) # all samples attributes remain, but the can rotated/compressed into # multidimensional attributes assert_equal(sorted(mds.sa.keys()), ['boxy_onsetidx'] + sorted(ds.sa.keys())) assert_equal(mds.sa.multidim.shape, (len(startpoints), boxlength, ds.nfeatures)) assert_equal(mds.sa.timepoints.shape, (len(startpoints), boxlength)) assert_array_equal(mds.sa.timepoints.flatten(), np.array([(s, s + 1) for s in startpoints]).flatten()) assert_array_equal(mds.sa.boxy_onsetidx, startpoints) # feature attributes also get rotated and broadcasted assert_array_equal(mds.fa.fid, [ds.fa.fid, ds.fa.fid]) # and finally there is a new one assert_array_equal(mds.fa.boxy_offsetidx, np.repeat(np.arange(boxlength), 4).reshape(2, -1)) # now see how it works on reverse() rds = bm.reverse(mds) # we got at least something of all original attributes back assert_equal(sorted(rds.sa.keys()), sorted(ds.sa.keys())) assert_equal(sorted(rds.fa.keys()), sorted(ds.fa.keys())) # it is not possible to reconstruct the full samples array # some samples even might show up multiple times (when there are overlapping # boxcars assert_array_equal( rds.samples, np.array([[0, 1, 2, 3], [4, 5, 6, 7], [4, 5, 6, 7], [8, 9, 10, 11], [16, 17, 18, 19], [20, 21, 22, 23]])) assert_array_equal(rds.sa.timepoints, [0, 1, 1, 2, 4, 5]) assert_array_equal(rds.sa.multidim, ds.sa.multidim[rds.sa.timepoints]) # but feature attributes should be fully recovered assert_array_equal(rds.fa.fid, ds.fa.fid)
def test_simple_wp1_level(self): """ """ ds = datasets['uni2large'] d2d = ds.samples ws = 50 # size of timeline for wavelet sp = (np.arange(ds.nsamples - ws*2) + ws)[:4] # create 3D instance (samples x timepoints x channels) bcm = BoxcarMapper(sp, ws) d3d = bcm.forward(d2d) # use wavelet mapper wdm = WaveletPacketMapper(level=2, wavelet='sym2') d3d_wd = wdm.forward(d3d) # Check dimensionality d3d_wds, d3ds = d3d_wd.shape, d3d.shape self.failUnless(len(d3d_wds) == len(d3ds)+1) self.failUnless(d3d_wds[1] * d3d_wds[2] >= d3ds[1]) self.failUnless(d3d_wds[0] == d3ds[0]) self.failUnless(d3d_wds[-1] == d3ds[-1]) #print d2d.shape, d3d.shape, d3d_wd.shape if externals.exists('pywt wp reconstruct'): # Test reverse -- should be identical # we can do reverse only for DWT d3d_rev = wdm.reverse(d3d_wd) # inverse transform might be not exactly as the # input... but should be very close ;-) self.failUnlessEqual(d3d_rev.shape, d3d.shape, msg="Shape should be the same after iDWT") diff = np.linalg.norm(d3d - d3d_rev) ornorm = np.linalg.norm(d3d) skip_if_no_external('pywt wp reconstruct fixed') self.failUnless(diff/ornorm < 1e-10) else: self.failUnlessRaises(NotImplementedError, wdm.reverse, d3d_wd)
def test_simpleboxcar(): data = np.atleast_2d(np.arange(10)).T sp = np.arange(10) # check if stupid thing don't work assert_raises(ValueError, BoxcarMapper, sp, 0) # now do an identity transformation bcm = BoxcarMapper(sp, 1) trans = bcm.forward(data) # ,0 is a feature below, so we get explicit 2D out of 1D assert_array_equal(trans[:,0], data) # now check for illegal boxes if __debug__: # condition is checked only in __debug__ assert_raises(ValueError, BoxcarMapper(sp, 2).train, data) # now something that should work nbox = 9 boxlength = 2 sp = np.arange(nbox) bcm = BoxcarMapper(sp, boxlength) trans = bcm(data) # check that is properly upcasts the dimensionality assert_equal(trans.shape, (nbox, boxlength) + data.shape[1:]) # check actual values, squeezing the last dim for simplicity assert_array_equal(trans.squeeze(), np.vstack((np.arange(9), np.arange(9)+1)).T) # now test for proper data shape data = np.ones((10,3,4,2)) sp = [ 2, 4, 3, 5 ] trans = BoxcarMapper(sp, 4)(data) assert_equal(trans.shape, (4,4,3,4,2)) # test reverse data = np.arange(240).reshape(10, 3, 4, 2) sp = [ 2, 4, 3, 5 ] boxlength = 2 m = BoxcarMapper(sp, boxlength) m.train(data) mp = m.forward(data) assert_equal(mp.shape, (4, 2, 3, 4, 2)) # try full reconstruct mr = m.reverse(mp) # shape has to match assert_equal(mr.shape, (len(sp) * boxlength,) + data.shape[1:]) # only known samples are part of the results assert_true((mr >= 24).all()) assert_true((mr < 168).all()) # check proper reconstruction of non-conflicting sample assert_array_equal(mr[0].ravel(), np.arange(48, 72)) # check proper reconstruction of samples being part of multiple # mapped samples assert_array_equal(mr[1].ravel(), np.arange(72, 96)) # test reverse of a single sample singlesample = np.arange(48).reshape(2, 3, 4, 2) assert_array_equal(singlesample, m.reverse1(singlesample)) # should not work for shape mismatch, but it does work and is useful when # reverse mapping sample attributes #assert_raises(ValueError, m.reverse, singlesample[0]) # check broadcasting of 'raw' samples into proper boxcars on forward() bc = m.forward1(np.arange(24).reshape(3, 4, 2)) assert_array_equal(bc, np.array(2 * [np.arange(24).reshape(3, 4, 2)]))
def test_simpleboxcar(): data = np.atleast_2d(np.arange(10)).T sp = np.arange(10) # check if stupid thing don't work assert_raises(ValueError, BoxcarMapper, sp, 0) # now do an identity transformation bcm = BoxcarMapper(sp, 1) trans = bcm.forward(data) # ,0 is a feature below, so we get explicit 2D out of 1D assert_array_equal(trans[:, 0], data) # now check for illegal boxes if __debug__: # condition is checked only in __debug__ assert_raises(ValueError, BoxcarMapper(sp, 2).train, data) # now something that should work nbox = 9 boxlength = 2 sp = np.arange(nbox) bcm = BoxcarMapper(sp, boxlength) trans = bcm(data) # check that is properly upcasts the dimensionality assert_equal(trans.shape, (nbox, boxlength) + data.shape[1:]) # check actual values, squeezing the last dim for simplicity assert_array_equal(trans.squeeze(), np.vstack((np.arange(9), np.arange(9) + 1)).T) # now test for proper data shape data = np.ones((10, 3, 4, 2)) sp = [2, 4, 3, 5] trans = BoxcarMapper(sp, 4)(data) assert_equal(trans.shape, (4, 4, 3, 4, 2)) # test reverse data = np.arange(240).reshape(10, 3, 4, 2) sp = [2, 4, 3, 5] boxlength = 2 m = BoxcarMapper(sp, boxlength) m.train(data) mp = m.forward(data) assert_equal(mp.shape, (4, 2, 3, 4, 2)) # try full reconstruct mr = m.reverse(mp) # shape has to match assert_equal(mr.shape, (len(sp) * boxlength, ) + data.shape[1:]) # only known samples are part of the results assert_true((mr >= 24).all()) assert_true((mr < 168).all()) # check proper reconstruction of non-conflicting sample assert_array_equal(mr[0].ravel(), np.arange(48, 72)) # check proper reconstruction of samples being part of multiple # mapped samples assert_array_equal(mr[1].ravel(), np.arange(72, 96)) # test reverse of a single sample singlesample = np.arange(48).reshape(2, 3, 4, 2) assert_array_equal(singlesample, m.reverse1(singlesample)) # should not work for shape mismatch, but it does work and is useful when # reverse mapping sample attributes #assert_raises(ValueError, m.reverse, singlesample[0]) # check broadcasting of 'raw' samples into proper boxcars on forward() bc = m.forward1(np.arange(24).reshape(3, 4, 2)) assert_array_equal(bc, np.array(2 * [np.arange(24).reshape(3, 4, 2)]))
def testSimple(self): """Just the same tests as for transformWithBoxcar. Mention that BoxcarMapper doesn't apply function with each boxcar """ data = N.arange(10) sp = N.arange(10) # check if stupid thing don't work self.failUnlessRaises(ValueError, BoxcarMapper, sp, 0 ) # now do an identity transformation bcm = BoxcarMapper(sp, 1) trans = bcm(data) # ,0 is a feature below, so we get explicit 2D out of 1D self.failUnless( (trans[:,0] == data).all() ) # now check for illegal boxes self.failUnlessRaises(ValueError, BoxcarMapper(sp, 2), data) # now something that should work sp = N.arange(9) bcm = BoxcarMapper(sp,2) trans = bcm(data) self.failUnless( (trans == N.vstack((N.arange(9), N.arange(9)+1)).T ).all() ) # now test for proper data shape data = N.ones((10,3,4,2)) sp = [ 2, 4, 3, 5 ] trans = BoxcarMapper(sp, 4)(data) self.failUnless( trans.shape == (4,4,3,4,2) ) # test reverse data = N.arange(240).reshape(10, 3, 4, 2) sp = [ 2, 4, 3, 5 ] m = BoxcarMapper(sp, 2) mp = m.forward(data) self.failUnless(mp.shape == (4, 2, 3, 4, 2)) # try full reconstruct mr = m.reverse(mp) # shape has to match self.failUnless(mr.shape == data.shape) # first two samples where not in any of the boxcars and cannot be # reconstructed self.failUnless(N.sum(mr[:2]) == 0) # same for the last ones self.failUnless(N.sum(mr[7:]) == 0) # check proper reconstruction of non-conflicting sample self.failUnless((mr[2].ravel() == N.arange(48, 72)).all()) # check proper reconstruction of samples being part of multiple # mapped samples self.failUnless((mr[3].ravel() == N.arange(72, 96)).all()) # test reverse of a single sample singlesample = N.arange(48).reshape(2, 3, 4, 2) self.failUnless((singlesample == m.reverse(singlesample)).all()) # should not work for shape mismatch self.failUnlessRaises(ValueError, m.reverse, singlesample[0]) # check broadcasting of 'raw' samples into proper boxcars on forward() bc = m.forward(N.arange(24).reshape(3, 4, 2)) self.failUnless((bc == N.array(2 * [N.arange(24).reshape(3, 4, 2)])).all())