Exemple #1
0
def calc_alphadot(alpha, basis, T=100, k=5):
    """
    calculates derivative along the path alpha

    :param alpha: numpy ndarray of shape (2,M) of M samples
    :param basis: list of numpy ndarray of shape (2,M) of M samples
    :param T: Number of samples of curve (Default = 100)
    :param k: number of samples along path (Default = 5)

    :rtype: numpy ndarray
    :return alphadot: derivative of alpha

    """
    alphadot = zeros((2, T, k))

    for tau in range(0, k):
        if tau == 0:
            v = (k - 1) * (alpha[:, :, tau + 1] - alpha[:, :, tau])
        elif tau == (k - 1):
            v = (k - 1) * (alpha[:, :, tau] - alpha[:, :, (tau - 1)])
        else:
            v = ((k - 1) / 2.0) * (alpha[:, :, tau + 1] - alpha[:, :, (tau - 1)])

        alphadot[:, :, tau] = cf.project_tangent(v, alpha[:, :, tau], basis[tau])

    return alphadot
Exemple #2
0
def calc_alphadot(alpha, basis, T=100, k=5):
    """
    calculates derivative along the path alpha

    :param alpha: numpy ndarray of shape (2,M) of M samples
    :param basis: list of numpy ndarray of shape (2,M) of M samples
    :param T: Number of samples of curve (Default = 100)
    :param k: number of samples along path (Default = 5)

    :rtype: numpy ndarray
    :return alphadot: derivative of alpha

    """
    alphadot = zeros((2, T, k))

    for tau in range(0, k):
        if tau == 0:
            v = (k-1)*(alpha[:, :, tau+1] - alpha[:, :, tau])
        elif tau == (k-1):
            v = (k-1)*(alpha[:, :, tau] - alpha[:, :, (tau-1)])
        else:
            v = ((k-1)/2.0)*(alpha[:, :, tau+1] - alpha[:, :, (tau-1)])

        alphadot[:, :, tau] = cf.project_tangent(v, alpha[:, :, tau],
                                                 basis[tau])

    return(alphadot)
Exemple #3
0
def karcher_calc(mu, q, basis, closed, rotation, method):
    # Compute shooting vector from mu to q_i
    qn_t, R, gamI = cf.find_rotation_and_seed_unique(mu, q, closed, rotation,
                                                     method)
    qn_t = qn_t / sqrt(cf.innerprod_q2(qn_t, qn_t))

    q1dotq2 = cf.innerprod_q2(mu, qn_t)

    if (q1dotq2 > 1):
        q1dotq2 = 1

    d = arccos(q1dotq2)

    u = qn_t - q1dotq2 * mu
    normu = sqrt(cf.innerprod_q2(u, u))
    if (normu > 1e-4):
        w = u * arccos(q1dotq2) / normu
    else:
        w = zeros(qn_t.shape)

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

    return (v, gamI, d)
Exemple #4
0
def karcher_calc(beta, q, betamean, mu, basis, mode):
    # Compute shooting vector from mu to q_i
    w, d = cf.inverse_exp_coord(betamean, beta, mode)

    # 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)
Exemple #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)
Exemple #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)
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)