예제 #1
0
def sample_shapes(mu, K, mode='O', no=3, numSamp=10):
    """
    Computes sample shapes from mean and covariance

    :param betamean: numpy ndarray of shape (n, M) describing the mean curve
    :param mu: numpy ndarray of shape (n, M) describing the mean srvf
    :param K: numpy ndarray of shape (M, M) describing the covariance
    :param mode: Open ('O') or closed curve ('C') (default 'O')
    :param no: number of direction (default 3)
    :param numSamp: number of samples (default 10)

    :rtype: tuple of numpy array
    :return samples: sample shapes

    """
    n, T = mu.shape
    modes = ['O', 'C']
    mode = [i for i, x in enumerate(modes) if x == mode]
    if len(mode) == 0:
        mode = 0
    else:
        mode = mode[0]

    U, s, V = svd(K)

    if mode == 0:
        N = 2
    else:
        N = 10

    epsilon = 1./(N-1)

    samples = empty(numSamp, dtype=object)
    for i in range(0, numSamp):
        v = zeros((2, T))
        for m in range(0, no):
            v = v + randn()*sqrt(s[m])*vstack((U[0:T, m], U[T:2*T, m]))

        q1 = mu
        for j in range(0, N-1):
            normv = sqrt(cf.innerprod_q2(v, v))

            if normv < 1e-4:
                q2 = mu
            else:
                q2 = cos(epsilon*normv)*q1+sin(epsilon*normv)*v/normv
                if mode == 1:
                    q2 = cf.project_curve(q2)

            # Parallel translate tangent vector
            basis2 = cf.find_basis_normal(q2)
            v = cf.parallel_translate(v, q1, q2, basis2, mode)

            q1 = q2

        samples[i] = cf.q_to_curve(q2)

    return(samples)
예제 #2
0
def sample_shapes(mu, K, mode='O', no=3, numSamp=10):
    """
    Computes sample shapes from mean and covariance

    :param betamean: numpy ndarray of shape (n, M) describing the mean curve
    :param mu: numpy ndarray of shape (n, M) describing the mean srvf
    :param K: numpy ndarray of shape (M, M) describing the covariance
    :param mode: Open ('O') or closed curve ('C') (default 'O')
    :param no: number of direction (default 3)
    :param numSamp: number of samples (default 10)

    :rtype: tuple of numpy array
    :return samples: sample shapes

    """
    n, T = mu.shape
    modes = ['O', 'C']
    mode = [i for i, x in enumerate(modes) if x == mode]
    if len(mode) == 0:
        mode = 0
    else:
        mode = mode[0]

    U, s, V = svd(K)

    if mode == 0:
        N = 2
    else:
        N = 10

    epsilon = 1./(N-1)

    samples = empty(numSamp, dtype=object)
    for i in range(0, numSamp):
        v = zeros((2, T))
        for m in range(0, no):
            v = v + randn()*sqrt(s[m])*vstack((U[0:T, m], U[T:2*T, m]))

        q1 = mu
        for j in range(0, N-1):
            normv = sqrt(cf.innerprod_q2(v, v))

            if normv < 1e-4:
                q2 = mu
            else:
                q2 = cos(epsilon*normv)*q1+sin(epsilon*normv)*v/normv
                if mode == 1:
                    q2 = cf.project_curve(q2)

            # Parallel translate tangent vector
            basis2 = cf.find_basis_normal(q2)
            v = cf.parallel_translate(v, q1, q2, basis2, mode)

            q1 = q2

        samples[i] = cf.q_to_curve(q2)

    return(samples)
예제 #3
0
    def sample_shapes(self, no=3, numSamp=10):
        """
        Computes sample shapes from mean and covariance

        :param no: number of direction (default 3)
        :param numSamp: number of samples (default 10)
        """
        n, T = self.q_mean.shape
        modes = ['O', 'C']
        mode = [i for i, x in enumerate(modes) if x == self.mode]
        if len(mode) == 0:
            mode = 0
        else:
            mode = mode[0]

        U, s, V = svd(self.C)

        if mode == 0:
            N = 2
        else:
            N = 10

        epsilon = 1. / (N - 1)

        samples = empty(numSamp, dtype=object)
        for i in range(0, numSamp):
            v = zeros((2, T))
            for m in range(0, no):
                v = v + randn() * sqrt(s[m]) * vstack(
                    (U[0:T, m], U[T:2 * T, m]))

            q1 = self.q_mean
            for j in range(0, N - 1):
                normv = sqrt(cf.innerprod_q2(v, v))

                if normv < 1e-4:
                    q2 = self.q_mean
                else:
                    q2 = cos(epsilon * normv) * q1 + sin(
                        epsilon * normv) * v / normv
                    if mode == 1:
                        q2 = cf.project_curve(q2)

                # Parallel translate tangent vector
                basis2 = cf.find_basis_normal(q2)
                v = cf.parallel_translate(v, q1, q2, basis2, mode)

                q1 = q2

            samples[i] = cf.q_to_curve(q2)

        self.samples = samples
        return
예제 #4
0
def karcher_calc(beta, q, betamean, mu, mode=0):
    if mode == 1:
        basis = cf.find_basis_normal(mu)
    # Compute shooting vector from mu to q_i
    w, d = cf.inverse_exp_coord(betamean, beta)

    # Project to tangent space of manifold to obtain v_i
    if mode == 0:
        v = w
    else:
        v = cf.project_tangent(w, q, basis)

    return(v, d)
예제 #5
0
def karcher_calc(beta, q, betamean, mu, mode=0):
    if mode == 1:
        basis = cf.find_basis_normal(mu)
    # Compute shooting vector from mu to q_i
    w, d = cf.inverse_exp_coord(betamean, beta)

    # Project to tangent space of manifold to obtain v_i
    if mode == 0:
        v = w
    else:
        v = cf.project_tangent(w, q, basis)

    return(v, d)
예제 #6
0
def curve_karcher_cov(betamean, beta, mode='O'):
    """
    This claculates the mean of a set of curves
    :param betamean: numpy ndarray of shape (n, M) describing the mean curve
    :param beta: numpy ndarray of shape (n, M, N) describing N curves
    in R^M
    :param mode: Open ('O') or closed curve ('C') (default 'O')

    :rtype: tuple of numpy array
    :return K: Covariance Matrix

    """
    n, T, N = beta.shape
    modes = ['O', 'C']
    mode = [i for i, x in enumerate(modes) if x == mode]
    if len(mode) == 0:
        mode = 0
    else:
        mode = mode[0]

    # Compute Karcher covariance of uniformly sampled mean
    betamean = cf.resamplecurve(betamean, T)
    mu = cf.curve_to_q(betamean)
    if mode == 1:
        mu = cf.project_curve(mu)
        basis = cf.find_basis_normal(mu)

    v = zeros((n, T, N))
    for i in range(0, N):
        beta1 = beta[:, :, i]

        w, dist = cf.inverse_exp_coord(betamean, beta1)
        # Project to the tangent sapce of manifold to obtain v_i
        if mode == 0:
            v[:, :, i] = w
        else:
            v[:, :, i] = cf.project_tangent(w, mu, basis)

    K = zeros((2*T, 2*T))

    for i in range(0, N):
        w = v[:, :, i]
        wtmp = w.reshape((T*n, 1), order='C')
        K = K + wtmp.dot(wtmp.T)

    K = K/(N-1)

    return(K)
예제 #7
0
def curve_karcher_cov(betamean, beta, mode='O'):
    """
    This claculates the mean of a set of curves
    :param betamean: numpy ndarray of shape (n, M) describing the mean curve
    :param beta: numpy ndarray of shape (n, M, N) describing N curves
    in R^M
    :param mode: Open ('O') or closed curve ('C') (default 'O')

    :rtype: tuple of numpy array
    :return K: Covariance Matrix

    """
    n, T, N = beta.shape
    modes = ['O', 'C']
    mode = [i for i, x in enumerate(modes) if x == mode]
    if len(mode) == 0:
        mode = 0
    else:
        mode = mode[0]

    # Compute Karcher covariance of uniformly sampled mean
    betamean = cf.resamplecurve(betamean, T)
    mu = cf.curve_to_q(betamean)
    if mode == 1:
        mu = cf.project_curve(mu)
        basis = cf.find_basis_normal(mu)

    v = zeros((n, T, N))
    for i in range(0, N):
        beta1 = beta[:, :, i]

        w, dist = cf.inverse_exp_coord(betamean, beta1)
        # Project to the tangent sapce of manifold to obtain v_i
        if mode == 0:
            v[:, :, i] = w
        else:
            v[:, :, i] = cf.project_tangent(w, mu, basis)

    K = zeros((2*T, 2*T))

    for i in range(0, N):
        w = v[:, :, i]
        wtmp = w.reshape((T*n, 1), order='C')
        K = K + wtmp.dot(wtmp.T)

    K = K/(N-1)

    return(K)
예제 #8
0
def find_basis_normal_path(alpha, k=5):
    """
    computes orthonormalized basis vectors to the normal space at each of the
    k points (q-functions) of the path alpha

    :param alpha: numpy ndarray of shape (2,M) of M samples (path)
    :param k: number of samples along path (Default = 5)

    :rtype: numpy ndarray
    :return basis: basis vectors along the path

    """
    basis = empty(k, dtype=object)
    for tau in range(0, k):
        q = alpha[:, :, tau]
        b = cf.find_basis_normal(q)
        basis_tmp = cf.gram_schmidt(b)
        basis[tau] = basis_tmp

    return(basis)
예제 #9
0
def find_basis_normal_path(alpha, k=5):
    """
    computes orthonormalized basis vectors to the normal space at each of the
    k points (q-functions) of the path alpha

    :param alpha: numpy ndarray of shape (2,M) of M samples (path)
    :param k: number of samples along path (Default = 5)

    :rtype: numpy ndarray
    :return basis: basis vectors along the path

    """
    basis = empty(k, dtype=object)
    for tau in range(0, k):
        q = alpha[:, :, tau]
        b = cf.find_basis_normal(q)
        basis_tmp = cf.gram_schmidt(b)
        basis[tau] = basis_tmp

    return basis
예제 #10
0
def curve_principal_directions(betamean, mu, K, mode='O', no=3, N=5):
    """
    Computes principal direction of variation specified by no. N is
    Number of shapes away from mean. Creates 2*N+1 shape sequence

    :param betamean: numpy ndarray of shape (n, M) describing the mean curve
    :param mu: numpy ndarray of shape (n, M) describing the mean srvf
    :param K: numpy ndarray of shape (M, M) describing the covariance
    :param mode: Open ('O') or closed curve ('C') (default 'O')
    :param no: number of direction (default 3)
    :param N: number of shapes (2*N+1) (default 5)

    :rtype: tuple of numpy array
    :return pd: principal directions

    """
    n, T = betamean.shape
    modes = ['O', 'C']
    mode = [i for i, x in enumerate(modes) if x == mode]
    if len(mode) == 0:
        mode = 0
    else:
        mode = mode[0]

    U, s, V = svd(K)

    qarray = empty((no, 2*N+1), dtype=object)
    qarray1 = empty(N, dtype=object)
    qarray2 = empty(N, dtype=object)
    pd = empty((no, 2*N+1), dtype=object)
    pd1 = empty(N, dtype=object)
    pd2 = empty(N, dtype=object)
    for m in range(0, no):
        princDir = vstack((U[0:T, m], U[T:2*T, m]))
        v = sqrt(s[m]) * princDir
        q1 = mu
        epsilon = 2./N

        # Forward direction from mean
        for i in range(0, N):
            normv = sqrt(cf.innerprod_q2(v, v))

            if normv < 1e-4:
                q2 = mu
            else:
                q2 = cos(epsilon*normv)*q1 + sin(epsilon*normv)*v/normv
                if mode == 1:
                    q2 = cf.project_curve(q2)

            qarray1[i] = q2
            p = cf.q_to_curve(q2)
            centroid1 = -1*cf.calculatecentroid(p)
            beta_scaled, scale = cf.scale_curve(p + tile(centroid1, [T, 1]).T)
            pd1[i] = beta_scaled

            # Parallel translate tangent vector
            basis2 = cf.find_basis_normal(q2)
            v = cf.parallel_translate(v, q1, q2, basis2, mode)

            q1 = q2

        # Backward direction from mean
        v = -sqrt(s[m])*princDir
        q1 = mu
        for i in range(0, N):
            normv = sqrt(cf.innerprod_q2(v, v))

            if normv < 1e-4:
                q2 = mu
            else:
                q2 = cos(epsilon*normv)*q1+sin(epsilon*normv)*v/normv
                if mode == 1:
                    q2 = cf.project_curve(q2)

            qarray2[i] = q2
            p = cf.q_to_curve(q2)
            centroid1 = -1*cf.calculatecentroid(p)
            beta_scaled, scale = cf.scale_curve(p + tile(centroid1, [T, 1]).T)
            pd2[i] = beta_scaled

            # Parallel translate tangent vector
            basis2 = cf.find_basis_normal(q2)
            v = cf.parallel_translate(v, q1, q2, basis2, mode)

            q1 = q2

        for i in range(0, N):
            qarray[m, i] = qarray2[(N-1)-i]
            pd[m, i] = pd2[(N-1)-i]

        qarray[m, N] = mu
        centroid1 = -1*cf.calculatecentroid(betamean)
        beta_scaled, scale = cf.scale_curve(betamean +
                                            tile(centroid1, [T, 1]).T)
        pd[m, N] = beta_scaled

        for i in range(N+1, 2*N+1):
            qarray[m, i] = qarray1[i-(N+1)]
            pd[m, i] = pd1[i-(N+1)]

    return(pd)
예제 #11
0
def curve_principal_directions(betamean, mu, K, mode='O', no=3, N=5):
    """
    Computes principal direction of variation specified by no. N is
    Number of shapes away from mean. Creates 2*N+1 shape sequence

    :param betamean: numpy ndarray of shape (n, M) describing the mean curve
    :param mu: numpy ndarray of shape (n, M) describing the mean srvf
    :param K: numpy ndarray of shape (M, M) describing the covariance
    :param mode: Open ('O') or closed curve ('C') (default 'O')
    :param no: number of direction (default 3)
    :param N: number of shapes (2*N+1) (default 5)

    :rtype: tuple of numpy array
    :return pd: principal directions

    """
    n, T = betamean.shape
    modes = ['O', 'C']
    mode = [i for i, x in enumerate(modes) if x == mode]
    if len(mode) == 0:
        mode = 0
    else:
        mode = mode[0]

    U, s, V = svd(K)

    qarray = empty((no, 2*N+1), dtype=object)
    qarray1 = empty(N, dtype=object)
    qarray2 = empty(N, dtype=object)
    pd = empty((no, 2*N+1), dtype=object)
    pd1 = empty(N, dtype=object)
    pd2 = empty(N, dtype=object)
    for m in range(0, no):
        princDir = vstack((U[0:T, m], U[T:2*T, m]))
        v = sqrt(s[m]) * princDir
        q1 = mu
        epsilon = 2./N

        # Forward direction from mean
        for i in range(0, N):
            normv = sqrt(cf.innerprod_q2(v, v))

            if normv < 1e-4:
                q2 = mu
            else:
                q2 = cos(epsilon*normv)*q1 + sin(epsilon*normv)*v/normv
                if mode == 1:
                    q2 = cf.project_curve(q2)

            qarray1[i] = q2
            p = cf.q_to_curve(q2)
            centroid1 = -1*cf.calculatecentroid(p)
            beta_scaled, scale = cf.scale_curve(p + tile(centroid1, [T, 1]).T)
            pd1[i] = beta_scaled

            # Parallel translate tangent vector
            basis2 = cf.find_basis_normal(q2)
            v = cf.parallel_translate(v, q1, q2, basis2, mode)

            q1 = q2

        # Backward direction from mean
        v = -sqrt(s[m])*princDir
        q1 = mu
        for i in range(0, N):
            normv = sqrt(cf.innerprod_q2(v, v))

            if normv < 1e-4:
                q2 = mu
            else:
                q2 = cos(epsilon*normv)*q1+sin(epsilon*normv)*v/normv
                if mode == 1:
                    q2 = cf.project_curve(q2)

            qarray2[i] = q2
            p = cf.q_to_curve(q2)
            centroid1 = -1*cf.calculatecentroid(p)
            beta_scaled, scale = cf.scale_curve(p + tile(centroid1, [T, 1]).T)
            pd2[i] = beta_scaled

            # Parallel translate tangent vector
            basis2 = cf.find_basis_normal(q2)
            v = cf.parallel_translate(v, q1, q2, basis2, mode)

            q1 = q2

        for i in range(0, N):
            qarray[m, i] = qarray2[(N-1)-i]
            pd[m, i] = pd2[(N-1)-i]

        qarray[m, N] = mu
        centroid1 = -1*cf.calculatecentroid(betamean)
        beta_scaled, scale = cf.scale_curve(betamean +
                                            tile(centroid1, [T, 1]).T)
        pd[m, N] = beta_scaled

        for i in range(N+1, 2*N+1):
            qarray[m, i] = qarray1[i-(N+1)]
            pd[m, i] = pd1[i-(N+1)]

    return(pd)
예제 #12
0
    def karcher_mean(self, parallel=False, cores=-1):
        """
        This calculates the mean of a set of curves
        :param parallel: run in parallel (default = F)
        :param cores: number of cores for parallel (default = -1 (all))
        """
        n, T, N = self.beta.shape

        modes = ['O', 'C']
        mode = [i for i, x in enumerate(modes) if x == self.mode]
        if len(mode) == 0:
            mode = 0
        else:
            mode = mode[0]

        # Initialize mu as one of the shapes
        mu = self.q[:, :, 0]
        betamean = self.beta[:, :, 0]
        itr = 0

        gamma = zeros((T, N))
        maxit = 20

        sumd = zeros(maxit + 1)
        v = zeros((n, T, N))
        normvbar = zeros(maxit + 1)

        delta = 0.5
        tolv = 1e-4
        told = 5 * 1e-3

        print("Computing Karcher Mean of %d curves in SRVF space.." % N)
        while itr < maxit:
            print("updating step: %d" % (itr + 1))

            if iter == maxit:
                print("maximal number of iterations reached")

            mu = mu / sqrt(cf.innerprod_q2(mu, mu))
            if mode == 1:
                self.basis = cf.find_basis_normal(mu)
            else:
                self.basis = []

            sumv = zeros((n, T))
            sumd[0] = inf
            sumd[itr + 1] = 0
            out = Parallel(n_jobs=cores)(delayed(karcher_calc)(
                self.beta[:, :, n], self.q[:, :,
                                           n], betamean, mu, self.basis, mode)
                                         for n in range(N))
            v = zeros((n, T, N))
            for i in range(0, N):
                v[:, :, i] = out[i][0]
                sumd[itr + 1] = sumd[itr + 1] + out[i][1]**2

            sumv = v.sum(axis=2)

            # Compute average direction of tangent vectors v_i
            vbar = sumv / float(N)

            normvbar[itr] = sqrt(cf.innerprod_q2(vbar, vbar))
            normv = normvbar[itr]

            if normv > tolv and fabs(sumd[itr + 1] - sumd[itr]) > told:
                # Update mu in direction of vbar
                mu = cos(delta * normvbar[itr]) * mu + sin(
                    delta * normvbar[itr]) * vbar / normvbar[itr]

                if mode == 1:
                    mu = cf.project_curve(mu)

                x = cf.q_to_curve(mu)
                a = -1 * cf.calculatecentroid(x)
                betamean = x + tile(a, [T, 1]).T
            else:
                break

            itr += 1

        self.q_mean = mu
        self.beta_mean = betamean
        self.v = v
        self.qun = sumd[0:(itr + 1)]
        self.E = normvbar[0:(itr + 1)]

        return