コード例 #1
0
def insert_measurements(date, version, con):
    #Insert measurements in DB
    with con:
        measurements_cur = con.cursor()
        measurements_gen = measurements.get_measurements(date, version)
        for measurement in measurements_gen:
            measurements_cur.execute(
                "INSERT INTO measurements VALUES(%s, %s, %s, %s, %s, %s)",
                measurement)
コード例 #2
0
    def set_measurements(self, seed=None):
        #  random trajectory and anchors
        self.traj.set_coeffs(seed=seed)
        if seed is not None:
            np.random.seed(seed)
        self.anchors = create_anchors(self.traj.dim, self.n_anchors)

        # get measurements
        self.basis, self.D_topright = get_measurements(self.traj, self.anchors, seed=seed, n_samples=20)
コード例 #3
0
    def setUp(self):
        self.t1 = Trajectory(dim=2,
                             n_complexity=2,
                             model='polynomial',
                             coeffs=np.array([[0., 0.], [0., 1.]]))
        self.t2 = Trajectory(dim=2,
                             n_complexity=2,
                             model='polynomial',
                             coeffs=np.array([[-3., 3.], [1., 0.]]))
        times1 = np.linspace(0, 1, 10)
        times2 = np.linspace(1, 2, 20)

        np.random.seed(1)
        self.anchors = 4 * np.random.rand(2, 5)

        b1, D1 = get_measurements(self.t1, self.anchors, seed=1, times=times1)
        b2, D2 = get_measurements(self.t2, self.anchors, seed=1, times=times2)
        self.F = np.hstack((b1, b2))
        self.D = np.vstack((D1, D2))
        self.times = np.r_[times1, times2]
コード例 #4
0
    def test_f_multidim(self):
        from trajectory import Trajectory
        from measurements import get_measurements, create_anchors
        anchors = create_anchors(dim=2, n_anchors=5)
        trajectory = Trajectory(n_complexity=4, dim=2)
        basis, D_topright = get_measurements(trajectory, anchors, n_samples=10)

        eps = 1e-10
        self.assertTrue(np.all(abs(f_multidim(anchors, basis, D_topright, trajectory.coeffs)) < eps))
        self.assertTrue(abs(f_onedim(anchors, basis, D_topright, trajectory.coeffs)) < eps)
        coeffs_hat = np.squeeze(compute_exact(D_topright, anchors, basis))
        np.testing.assert_allclose(coeffs_hat, trajectory.coeffs)
コード例 #5
0
    def setUp(self):
        np.random.seed(2)
        n_complexity = 5
        self.traj = Trajectory(model='bandlimited')
        self.traj.set_n_complexity(n_complexity)
        self.traj.set_coeffs(1)

        self.anchors = np.random.rand(self.traj.dim,
                                      10) * 10  # between 0 and 10.

        self.times = self.traj.get_times(n_samples=200)
        self.basis, self.D_gt = get_measurements(self.traj,
                                                 self.anchors[:2, :],
                                                 times=self.times)

        points_gt = self.traj.get_sampling_points(self.times)
        self.indices = range(len(self.times))[::self.traj.dim + 1]
        self.points_sub = points_gt[:, self.indices]
コード例 #6
0
def run_simulation(parameters, outfolder=None, solver=None, verbose=False):
    """ Run simulation. 

    :param parameters: Can be either the name of the folder where parameters.json is stored, or a new dict of parameters.

    """
    if type(parameters) == str:
        fname = parameters + 'parameters.json'
        parameters = read_params(fname)
        print('read parameters from file {}.'.format(fname))

    elif type(parameters) == dict:
        parameters = parameters

        # if we are trying new parameters and saving in a directory that already exists,
        # we need to make sure that the saved parameters are actually the same.
        if outfolder is not None:
            try:
                parameters_old = read_params(outfolder + 'parameters.json')
                parameters['time'] = parameters_old['time']
                assert parameters == parameters_old, 'found conflicting parameters file: {}'.format(outfolder +
                                                                                                    'parameters.json')
            except FileNotFoundError:
                print('no conflicting parameters file found.')
            except AssertionError as error:
                raise (error)
    else:
        raise TypeError('parameters needs to be folder name or dictionary.')

    if 'noise_to_square' not in parameters:
        parameters['noise_to_square'] = False

    if 'measure_distances' not in parameters:
        parameters['measure_distances'] = False

    if 'sampling_strategy' not in parameters:
        parameters['sampling_strategy'] = 'uniform'

    complexities = parameters['complexities']
    anchors = parameters['anchors']
    positions = parameters['positions']
    n_its = parameters['n_its']
    noise_sigmas = parameters['noise_sigmas']
    success_thresholds = parameters['success_thresholds']
    assert len(success_thresholds) == len(noise_sigmas)

    if parameters['sampling_strategy'] == 'single_time':
        max_measurements = max(positions)
    else:
        max_measurements = max(positions) * max(anchors)

    successes = np.full((len(complexities), len(anchors), len(positions), len(noise_sigmas), max_measurements), np.nan)
    errors = np.full(successes.shape, np.nan)
    relative_errors = np.full(successes.shape, np.nan)
    absolute_errors = np.full(successes.shape, np.nan)
    num_not_solved = np.full(successes.shape, np.nan)
    num_not_accurate = np.full(successes.shape, np.nan)
    squared_distances = []

    for c_idx, n_complexity in enumerate(complexities):
        print('n_complexity', n_complexity)

        for a_idx, n_anchors in enumerate(anchors):
            print('n_anchors', n_anchors)

            for p_idx, n_positions in enumerate(positions):
                print('n_positions', n_positions)

                if parameters['sampling_strategy'] == 'single_time':
                    n_measurements = n_positions
                else:
                    n_measurements = n_positions * n_anchors
                for m_idx, n_missing in enumerate(range(n_measurements)):
                    if verbose:
                        print('measurements idx', m_idx)

                    for noise_idx, noise_sigma in enumerate(noise_sigmas):
                        indexes = np.s_[c_idx, a_idx, p_idx, noise_idx, m_idx]
                        if verbose:
                            print("noise", noise_sigma)

                        # set all values to 0 since we have visited them.
                        if np.isnan(successes[indexes]):
                            successes[indexes] = 0.0
                        if np.isnan(num_not_solved[indexes]):
                            num_not_solved[indexes] = 0.0
                        if np.isnan(num_not_accurate[indexes]):
                            num_not_accurate[indexes] = 0.0

                        for _ in range(n_its):

                            trajectory = Trajectory(n_complexity, dim=DIM)
                            anchors_coord = create_anchors(DIM, n_anchors)
                            trajectory.set_coeffs(seed=None)

                            basis, D_topright = get_measurements(trajectory, anchors_coord, n_samples=n_positions)
                            distances = np.sqrt(D_topright)
                            D_topright = add_noise(D_topright, noise_sigma, parameters["noise_to_square"])
                            mask = create_mask(n_positions,
                                               n_anchors,
                                               strategy=parameters['sampling_strategy'],
                                               n_missing=n_missing)
                            if parameters['measure_distances']:
                                squared_distances.extend(D_topright.flatten().tolist())
                            D_topright = np.multiply(D_topright, mask)

                            try:
                                assert p.full_rank_condition(
                                    np.sort(np.sum(mask, axis=0))[::-1], DIM + 1, n_complexity), "insufficient rank"
                                if (solver is None) or (solver == "semidef_relaxation_noiseless"):
                                    X = semidef_relaxation_noiseless(D_topright,
                                                                     anchors_coord,
                                                                     basis,
                                                                     chosen_solver=cvxpy.CVXOPT)
                                    P_hat = X[:DIM, DIM:]
                                elif solver == 'trajectory_recovery':
                                    P_hat = trajectory_recovery(D_topright, anchors_coord, basis)
                                elif solver == 'weighted_trajectory_recovery':
                                    P_hat = trajectory_recovery(D_topright, anchors_coord, basis, weighted=True)
                                else:
                                    raise ValueError(solver)

                                # calculate reconstruction error with respect to distances
                                trajectory_estimated = Trajectory(coeffs=P_hat)
                                _, D_estimated = get_measurements(trajectory_estimated,
                                                                  anchors_coord,
                                                                  n_samples=n_positions)
                                estimated_distances = np.sqrt(D_estimated)

                                robust_add(errors, indexes, np.linalg.norm(P_hat - trajectory.coeffs))
                                robust_add(relative_errors, indexes,
                                           np.linalg.norm((distances - estimated_distances) / (distances + 1e-10)))
                                robust_add(absolute_errors, indexes, np.linalg.norm(distances - estimated_distances))

                                assert not np.linalg.norm(P_hat - trajectory.coeffs) > success_thresholds[noise_idx]

                                robust_increment(successes, indexes)

                            except cvxpy.SolverError:
                                logging.info("could not solve n_positions={}, n_missing={}".format(
                                    n_positions, n_missing))
                                robust_increment(num_not_solved, indexes)

                            except ZeroDivisionError:
                                logging.info("could not solve n_positions={}, n_missing={}".format(
                                    n_positions, n_missing))
                                robust_increment(num_not_solved, indexes)

                            except np.linalg.LinAlgError:
                                robust_increment(num_not_solved, indexes)

                            except AssertionError as e:
                                if str(e) == "insufficient rank":
                                    robust_increment(num_not_solved, indexes)
                                else:
                                    logging.info("result not accurate n_positions={}, n_missing={}".format(
                                        n_positions, n_missing))
                                    robust_increment(num_not_accurate, indexes)

                        errors[indexes] = errors[indexes] / (n_its - num_not_solved[indexes])
                        relative_errors[indexes] = relative_errors[indexes] / (n_its - num_not_solved[indexes])

    results = {
        'successes': successes,
        'num-not-solved': num_not_solved,
        'num-not-accurate': num_not_accurate,
        'errors': errors,
        'relative-errors': relative_errors,
        'absolute-errors': absolute_errors,
        'distances': squared_distances
    }

    if outfolder is not None:
        print('Done with simulation. Saving results...')

        parameters['time'] = time.time()

        if not os.path.exists(outfolder):
            os.makedirs(outfolder)

        save_params(outfolder + 'parameters.json', **parameters)
        save_results(outfolder + 'result_{}_{}', results)
    else:
        return results