Exemple #1
0
def mds_withprocrust(a, t, **kwargs):
    # data should already be in the needed scale -- we just care about
    # rotation, shift, reflection
    pm = ProcrusteanMapper(reflection=True,
                           scaling=False,
                           reduction=False,
                           oblique=False)

    a_ = mdsf(a, **kwargs)
    ds = dataset_wizard(a_, targets=t)
    pm.train(ds)
    return pm.forward(a_)
Exemple #2
0
    def test_reflection(self, rep=10):
        for i in range(rep):
            from mvpa2.testing.datasets import get_random_rotation
            d = np.random.random((100, 2))
            T = get_random_rotation(d.shape[1])
            d2 = np.dot(d, T)
            # scale it up a bit
            d2 *= 1.2
            # add a reflection by flipping the first dimension
            d2[:, 0] *= -1
            ds = dataset_wizard(samples=d, targets=d2)

            norm0 = np.linalg.norm(d - d2)

            mapper = ProcrusteanMapper(scaling=False, reflection=False)
            mapper.train(ds)
            norm1 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            eps = 1e-7
            self.assertLess(norm1,
                            norm0 + eps,
                            msg='Procrustes should reduce difference, '
                            'but %f > %f' % (norm1, norm0))

            mapper = ProcrusteanMapper(scaling=True, reflection=False)
            mapper.train(ds)
            norm2 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            self.assertLess(norm2,
                            norm1 + eps,
                            msg='Procrustes with scaling should work better, '
                            'but %f > %f' % (norm2, norm1))

            mapper = ProcrusteanMapper(scaling=False, reflection=True)
            mapper.train(ds)
            norm3 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            self.assertLess(
                norm3,
                norm1 + eps,
                msg='Procrustes with reflection should work better, '
                'but %f > %f' % (norm3, norm1))

            mapper = ProcrusteanMapper(scaling=True, reflection=True)
            mapper.train(ds)
            norm4 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            self.assertLess(norm4,
                            norm3 + eps,
                            msg='Procrustes with scaling should work better, '
                            'but %f > %f' % (norm4, norm3))
            self.assertLess(
                norm4,
                norm2 + eps,
                msg='Procrustes with reflection should work better, '
                'but %f > %f' % (norm4, norm2))
Exemple #3
0
def hypalign(source, target, node, surfsel, projmat,mask):
    slneighbors = surfsel[node]
    if len(slneighbors) == 0:
        return projmat

    sl = []
    sl = [source[:, slneighbors], target[:, slneighbors]]
    
    hmapper = ProcrusteanMapper(space='commonspace')
    refds = sl[1].copy()
    commonspace = refds.samples
    zscore(commonspace,chunks_attr=None)
    ds_new = sl[0].copy()
    ds_new.sa[hmapper.get_space()] = commonspace.astype(float)
    hmapper.train(ds_new)
    conproj = hmapper.proj
    m, n = conproj.shape
    index = np.array(slneighbors)
    projmat[np.ix_(index,index)] += np.asarray(conproj)
    return projmat
Exemple #4
0
    def test_reflection(self, rep=10):
        for i in range(rep):
            from mvpa2.testing.datasets import get_random_rotation
            d = np.random.random((100, 2))
            T = get_random_rotation(d.shape[1])
            d2 = np.dot(d, T)
            # scale it up a bit
            d2 *= 1.2
            # add a reflection by flipping the first dimension
            d2[:, 0] *= -1
            ds = dataset_wizard(samples=d, targets=d2)

            norm0 = np.linalg.norm(d - d2)

            mapper = ProcrusteanMapper(scaling=False, reflection=False)
            mapper.train(ds)
            norm1 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            eps = 1e-7
            self.assertLess(norm1, norm0 + eps,
                            msg='Procrustes should reduce difference, '
                            'but %f > %f' % (norm1, norm0))

            mapper = ProcrusteanMapper(scaling=True, reflection=False)
            mapper.train(ds)
            norm2 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            self.assertLess(norm2, norm1 + eps,
                            msg='Procrustes with scaling should work better, '
                            'but %f > %f' % (norm2, norm1))

            mapper = ProcrusteanMapper(scaling=False, reflection=True)
            mapper.train(ds)
            norm3 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            self.assertLess(norm3, norm1 + eps,
                            msg='Procrustes with reflection should work better, '
                            'but %f > %f' % (norm3, norm1))

            mapper = ProcrusteanMapper(scaling=True, reflection=True)
            mapper.train(ds)
            norm4 = np.linalg.norm(d2 - mapper.forward(ds).samples)
            self.assertLess(norm4, norm3 + eps,
                            msg='Procrustes with scaling should work better, '
                            'but %f > %f' % (norm4, norm3))
            self.assertLess(norm4, norm2 + eps,
                            msg='Procrustes with reflection should work better, '
                            'but %f > %f' % (norm4, norm2))
Exemple #5
0
    def test_simple(self, oblique):
        d_orig = datasets['uni2large'].samples
        d_orig2 = datasets['uni4large'].samples
        for sdim, nf_s, nf_t, full_test \
                in (('Same 2D',  2,  2,  True),
                    ('Same 10D', 10, 10, True),
                    ('2D -> 3D', 2,  3,  True),
                    ('3D -> 2D', 3,  2,  False)):
            # figure out some "random" rotation
            d = max(nf_s, nf_t)
            R = get_random_rotation(nf_s, nf_t, d_orig)
            if nf_s == nf_t:
                adR = np.abs(1.0 - np.linalg.det(R))
                self.failUnless(adR < 1e-10,
                                "Determinant of rotation matrix should "
                                "be 1. Got it 1+%g" % adR)
                self.failUnless(norm(np.dot(R, R.T)
                                     - np.eye(R.shape[0])) < 1e-10)

            for s, scaling in ((0.3, True), (1.0, False)):
                pm = ProcrusteanMapper(scaling=scaling, oblique=oblique)
                pm2 = ProcrusteanMapper(scaling=scaling, oblique=oblique)

                t1, t2 = d_orig[23, 1], d_orig[22, 1]

                # Create source/target data
                d = d_orig[:, :nf_s]
                d_s = d + t1
                d_t = np.dot(s * d, R) + t2

                # train bloody mapper(s)
                ds = dataset_wizard(samples=d_s, targets=d_t)
                pm.train(ds)
                ## not possible with new interface
                #pm2.train(d_s, d_t)

                ## verify that both created the same transformation
                #npm2proj = norm(pm.proj - pm2.proj)
                #self.failUnless(npm2proj <= 1e-10,
                #                msg="Got transformation different by norm %g."
                #                " Had to be less than 1e-10" % npm2proj)
                #self.failUnless(norm(pm._offset_in - pm2._offset_in) <= 1e-10)
                #self.failUnless(norm(pm._offset_out - pm2._offset_out) <= 1e-10)

                # do forward transformation on the same source data
                d_s_f = pm.forward(d_s)

                self.failUnlessEqual(d_s_f.shape, d_t.shape,
                    msg="Mapped shape should be identical to the d_t")

                dsf = d_s_f - d_t
                ndsf = norm(dsf)/norm(d_t)
                if full_test:
                    dsR = norm(s*R - pm.proj)

                    if not oblique:
                        self.failUnless(dsR <= 1e-12,
                            msg="We should have got reconstructed rotation+scaling "
                                "perfectly. Now got d scale*R=%g" % dsR)

                        self.failUnless(np.abs(s - pm._scale) < 1e-12,
                            msg="We should have got reconstructed scale "
                                "perfectly. Now got %g for %g" % (pm._scale, s))

                    self.failUnless(ndsf <= 1e-12,
                      msg="%s: Failed to get to the target space correctly."
                        " normed error=%g" % (sdim, ndsf))

                # Test if we get back
                d_s_f_r = pm.reverse(d_s_f)

                dsfr = d_s_f_r - d_s
                ndsfr = norm(dsfr)/norm(d_s)
                if full_test:
                    self.failUnless(ndsfr <= 1e-12,
                      msg="%s: Failed to reconstruct into source space correctly."
                        " normed error=%g" % (sdim, ndsfr))
Exemple #6
0
    def test_simple(self, svd, oblique):
        d_orig = datasets['uni2large'].samples
        d_orig2 = datasets['uni4large'].samples
        for sdim, nf_s, nf_t, full_test \
                in (('Same 2D',  2,  2,  True),
                    ('Same 10D', 10, 10, True),
                    ('2D -> 3D', 2,  3,  True),
                    ('3D -> 2D', 3,  2,  False)):
            # figure out some "random" rotation
            d = max(nf_s, nf_t)
            R = get_random_rotation(nf_s, nf_t, d_orig)
            if nf_s == nf_t:
                adR = np.abs(1.0 - np.linalg.det(R))
                self.assertTrue(
                    adR < 1e-10, "Determinant of rotation matrix should "
                    "be 1. Got it 1+%g" % adR)
                self.assertTrue(
                    norm(np.dot(R, R.T) - np.eye(R.shape[0])) < 1e-10)

            for s, scaling in ((0.3, True), (1.0, False)):
                pm = ProcrusteanMapper(scaling=scaling,
                                       oblique=oblique,
                                       svd=svd)
                # pm2 = ProcrusteanMapper(scaling=scaling, oblique=oblique)

                t1, t2 = d_orig[23, 1], d_orig[22, 1]

                # Create source/target data
                d = d_orig[:, :nf_s]
                d_s = d + t1
                d_t = np.dot(s * d, R) + t2

                # train bloody mapper(s)
                ds = dataset_wizard(samples=d_s, targets=d_t)
                pm.train(ds)
                ## not possible with new interface
                #pm2.train(d_s, d_t)

                ## verify that both created the same transformation
                #npm2proj = norm(pm.proj - pm2.proj)
                #self.assertTrue(npm2proj <= 1e-10,
                #                msg="Got transformation different by norm %g."
                #                " Had to be less than 1e-10" % npm2proj)
                #self.assertTrue(norm(pm._offset_in - pm2._offset_in) <= 1e-10)
                #self.assertTrue(norm(pm._offset_out - pm2._offset_out) <= 1e-10)

                # do forward transformation on the same source data
                d_s_f = pm.forward(d_s)

                self.assertEqual(
                    d_s_f.shape,
                    d_t.shape,
                    msg="Mapped shape should be identical to the d_t")

                dsf = d_s_f - d_t
                ndsf = norm(dsf) / norm(d_t)
                if full_test:
                    dsR = norm(s * R - pm.proj)

                    if not oblique:
                        self.assertTrue(
                            dsR <= 1e-12,
                            msg=
                            "We should have got reconstructed rotation+scaling "
                            "perfectly. Now got d scale*R=%g" % dsR)

                        self.assertTrue(
                            np.abs(s - pm._scale) < 1e-12,
                            msg="We should have got reconstructed scale "
                            "perfectly. Now got %g for %g" % (pm._scale, s))

                    self.assertTrue(
                        ndsf <= 1e-12,
                        msg="%s: Failed to get to the target space correctly."
                        " normed error=%g" % (sdim, ndsf))

                # Test if we get back
                d_s_f_r = pm.reverse(d_s_f)

                dsfr = d_s_f_r - d_s
                ndsfr = norm(dsfr) / norm(d_s)
                if full_test:
                    self.assertTrue(
                        ndsfr <= 1e-12,
                        msg=
                        "%s: Failed to reconstruct into source space correctly."
                        " normed error=%g" % (sdim, ndsfr))