points[indices[2], :],  #2
                theta0_13,
                theta1_03,
                theta2_03)
        if counter > 1:
            print('Warning: possibly degenerate configuration.')
        points[i, :] = p_i
    return points


if __name__ == "__main__":
    from angle_set import AngleSet
    from pylocus.algorithms import procrustes

    d = 2
    N = 5
    np.random.seed(51)
    angle_set = AngleSet(N=N, d=d)
    angle_set.set_points(mode='random')

    points = reconstruct_from_angles(angle_set.theta_tensor)
    points_fitted, *_ = procrustes(angle_set.points, points, scale=True)

    plt.figure()
    plt.scatter(*points_fitted.T, label='fitted')
    plt.scatter(*angle_set.points.T, label='original', marker='x')
    plt.axis('equal')
    plt.legend(loc='best')
    plt.show()
    #assert np.allclose(points_fitted, angle_set.points)
Beispiel #2
0
def inner_loop(angle_set, verbose=False, learned=False):
    df = pd.DataFrame(columns=COLUMNS)
    df_counter = 0

    n_rays = angle_set.get_n_rays()
    n_poly = angle_set.get_n_poly()
    n_linear_total = n_rays + n_poly
    n_sine_total = angle_set.get_n_sine()
    necessary = angle_set.M - angle_set.get_DOF()
    assert n_sine_total + n_linear_total == necessary, '{} {} {}'.format(
        necessary, n_linear_total, n_sine_total)
    print('number of sine constraints for N={}: {}'.format(
        angle_set.N, n_sine_total))

    # create noisy angle vector
    theta_noisy = get_noisy(angle_set.theta)

    # create linear constraints
    if not learned:
        Apoly, bpoly = angle_set.get_triangle_constraints()
        Arays, brays = angle_set.get_ray_constraints(verbose=False)
        Afull = np.vstack([Arays, Apoly[:n_poly]])
        bfull = np.hstack([brays, bpoly[:n_poly]])
    else:
        Afull, bfull = generate_linear_constraints(angle_set.points)
        if Afull.shape[0] != n_linear_total:
            raise RuntimeError('Did not learn enough linear constraints.')

    # reconstruct raw for baseline
    theta_noisy_reconstructed, points_noisy = reconstruct_theta(
        theta_noisy, angle_set.corners, angle_set.N)
    error_noisy = mse(points_noisy, angle_set.points)

    # first linear then sine.
    for n_linear in range(n_linear_total + 1):
        if n_linear < n_linear_total:
            n_sine_here = 0
        else:
            n_sine_here = n_sine_total

        for n_sine in range(n_sine_here + 1):
            if verbose and n_sine > 0:
                print('n_sine {}/{}'.format(n_sine, n_sine_total))
                print('n_total', n_total)

            choices_sine = range(n_sine)

            n_total = n_linear + n_sine

            choices_linear = range(n_linear)
            eps = 1e-10
            theta_sine, success = solve_constrained_optimization(
                theta_noisy,
                angle_set.corners,
                N=angle_set.N,
                Afull=Afull,
                bfull=bfull,
                choices_sine=choices_sine,
                choices_linear=choices_linear,
                eps=eps)
            if any(theta_sine == eps):
                raise RuntimeError('Found zero angle.')

            theta_sine_reconstructed, points_sine = reconstruct_theta(
                theta_sine, angle_set.corners, angle_set.N)

            points_sine, *_ = procrustes(angle_set.points,
                                         points_sine,
                                         scale=True)
            error_sine = mse(points_sine, angle_set.points)

            df.loc[df_counter, :] = {
                'theta_sine': theta_sine,
                'theta_sine_reconstructed': theta_sine_reconstructed,
                'theta_noisy': theta_noisy,
                'theta_noisy_reconstructed': theta_noisy_reconstructed,
                'n_sine': n_sine,
                'n_linear': n_linear,
                'n_total': n_total,
                'n_it': None,
                'N': None,
                'success': success,
                'error_sine': error_sine,
                'error_noisy': error_noisy
            }
            df_counter += 1
    return df
    for size in sizes:
        print('size', size, '/', sizes)
        for i in range(max_it):
            print('n_it', i, '/', max_it - 1)
            np.random.seed(i)
            angle_set.set_points('random', size=size)
            Afull, bfull = generate_linear_constraints(angle_set.points)
            n_sine = angle_set.get_n_sine()
            choices_linear = range(Afull.shape[0])
            choices_sine = range(n_sine)

            for j, sigma in enumerate(sigmas_distance):
                print('distance', j, '/', len(sigmas_distance) - 1)
                noisy_edm, SNR_edm = add_edm_noise(angle_set.edm, sigma=sigma)
                x_edm = reconstruct_mds(noisy_edm, all_points=angle_set.points)
                x_edm, *_ = procrustes(angle_set.points, x_edm)
                error_edm = mse(x_edm, angle_set.points)

                # effective noise is only sigma/2
                # because of symmetry of EDM.
                df_results.loc[df_counter, :] = dict(sigma=sigma / 2,
                                                     SNR=SNR_edm,
                                                     error=error_edm,
                                                     type='distance',
                                                     N=N,
                                                     n_it=i,
                                                     size=size)
                df_counter += 1

            for k, sigma in enumerate(sigmas_angle):
                print('angle', k, '/', len(sigmas_angle) - 1)