def create_complexities(traj, D, times, anchors, list_complexities): """ Evaluate our algorithm and srls, srls for different complexities. Used to create Figure 7, first row, in Relax and Recover paper. """ grid = get_grid(anchors, grid_size=0.5) results = pd.DataFrame(columns=['method', 'result', 'n_complexity']) num_seeds = 3 for n_complexity in list_complexities: traj.set_n_complexity(n_complexity) basis = traj.get_basis(times=times) Chat = trajectory_recovery(D, anchors[:2, :], basis, weighted=True) results.loc[len(results), :] = dict(n_complexity=n_complexity, method='ours-weighted', result=Chat) indices = range(D.shape[0])[traj.dim + 2::3] p_rls, __ = pointwise_rls(D, anchors, traj, indices, grid) p_srls, __ = pointwise_srls(D, anchors, traj, indices) results.loc[len(results), :] = dict(n_complexity=n_complexity, method='srls', result=p_srls) results.loc[len(results), :] = dict(n_complexity=n_complexity, method='rls', result=p_rls) return results
def plot_complexities_old(traj, D, times, anchors, full_df, list_complexities, srls=False, rls=True): if rls: grid = get_grid(anchors, grid_size=0.5) fig, axs = plt.subplots(1, len(list_complexities), sharex=True, sharey=True) for ax, n_complexity in zip(axs, list_complexities): traj.set_n_complexity(n_complexity) basis = traj.get_basis(times=times) Chat = trajectory_recovery(D, anchors[:2, :], basis, weighted=True) traj.set_coeffs(coeffs=Chat) traj.plot_pretty(times=times, color="C0", label='ours', ax=ax) ax.plot(full_df.px, full_df.py, color='black', ls=':', linewidth=1., label='GPS') ax.set_title('K = {}'.format(traj.n_complexity)) remove_ticks(ax) i = 1 if srls: indices = range(D.shape[0])[::3] points, used_indices = pointwise_srls(D, anchors, traj, indices) label = 'SRLS' for x in points: ax.scatter(*x, color=f'C{i}', label=label, s=4.0) label = None i += 1 if rls: indices = range(D.shape[0])[traj.dim + 2::3] points, used_indices = pointwise_rls(D, anchors, traj, indices, grid) label = 'RLS' for x in points: ax.scatter(*x, color=f'C{i}', label=label, s=4.0) label = None add_scalebar(axs[0], 20, loc='lower left') return fig, axs
def test_pointwise_rls(self): grid_size = 1.0 grid = get_grid(self.points_sub, grid_size) points, __ = pointwise_rls(self.D_gt, self.anchors, self.traj, self.indices, grid=grid) points = points.T np.testing.assert_allclose(points, self.points_sub, atol=grid_size, rtol=grid_size)
def test_single_rls_srls(self): from other_algorithms import RLS from pylocus.lateration import SRLS anchors = np.random.uniform(0, 10, size=(2, 4)) grid_size = 0.1 grid = get_grid(anchors, grid_size=grid_size) chosen_idx = np.random.choice(range(grid.shape[0])) pos_real = grid[chosen_idx, :] r2_real = np.linalg.norm(anchors - pos_real[:, None], axis=0)**2 np.testing.assert_allclose(pos_real, RLS(anchors.T, r2_real, grid)) r2_real = r2_real.reshape((-1, 1)) weights = np.ones(r2_real.shape) np.testing.assert_allclose(pos_real, SRLS(anchors.T, weights, r2_real))
def create_subsample(traj, D, times, anchors, n_measurements_list): """ Evaluate our algorithm and srls, srls for different numbers of measurements. Used to create Figure 7, second row, in Relax and Recover paper. """ grid = get_grid(anchors, grid_size=0.5) results = pd.DataFrame(columns=['method', 'result', 'n_measurements']) num_seeds = 3 for n_measurements in n_measurements_list: for seed in range(num_seeds): np.random.seed(seed) indices = np.random.choice(D.shape[0], n_measurements, replace=False) D_small = D[indices, :] times_small = np.array(times)[indices] basis_small = traj.get_basis(times=times_small) Chat = trajectory_recovery(D_small, anchors[:2, :], basis_small, weighted=True) results.loc[len(results), :] = dict(n_measurements=n_measurements, method='ours-weighted', result=Chat) p_rls, __ = pointwise_rls(D, anchors, traj, indices, grid) p_srls, __ = pointwise_srls(D, anchors, traj, indices) results.loc[len(results), :] = dict(n_measurements=n_measurements, method='srls', result=p_srls) results.loc[len(results), :] = dict(n_measurements=n_measurements, method='rls', result=p_rls) return results
def plot_subsample_old(traj, D, times, anchors, full_df, n_measurements_list, srls=False, rls=True): import hypothesis as h basis = traj.get_basis(times=times) fig, axs = plt.subplots(1, len(n_measurements_list), sharex=True, sharey=True) if rls: grid = get_grid(anchors, grid_size=0.5) alpha = 1.0 num_seeds = 3 for ax, n_measurements in zip(axs, n_measurements_list): label = 'ours' coeffs = np.empty([traj.dim, traj.n_complexity, 0]) colors = {0: 0, 1: 2, 2: 3} for seed in range(num_seeds): np.random.seed(seed) indices = np.random.choice(D.shape[0], n_measurements, replace=False) D_small = D[indices, :] mask = (D_small > 0).astype(np.float) p = np.sort(np.sum(mask, axis=0))[::-1] if not h.limit_condition(list(p), traj.dim + 1, traj.n_complexity): print("insufficient rank") times_small = np.array(times)[indices] basis_small = traj.get_basis(times=times_small) Chat = trajectory_recovery(D_small, anchors[:2, :], basis_small, weighted=True) coeffs = np.dstack([coeffs, Chat]) traj.set_coeffs(coeffs=Chat) traj.plot_pretty(times=times, color='C{}'.format(colors[seed]), ax=ax, alpha=alpha) Chat_avg = np.mean(coeffs, axis=2) traj.set_coeffs(coeffs=Chat_avg) traj.plot_pretty(times=times, color='C0', label=label, ax=ax) i = 1 if srls: points, used_indices = pointwise_srls(D, anchors, traj, indices) label = 'SRLS' for x in points: ax.scatter(*x, color=f'C{i}', label=label, s=4.0) label = None i += 1 if rls: points, used_indices = pointwise_rls(D, anchors, traj, indices, grid) label = 'RLS' for x in points: ax.scatter(*x, color=f'C{i}', label=label, s=4.0) label = None ax.plot(full_df.px, full_df.py, ls=':', linewidth=1., color='black', label='GPS') remove_ticks(ax) ax.set_title('N = {}'.format(n_measurements), y=-0.22) add_scalebar(axs[0], 20, loc='lower left') return fig, axs