def test_back_proj_pca(): trajs = Trajectories(data.with_gaps_df()) rotated, pca = transformation.do_pca(trajs, suffix='', append=False, return_pca=True) back_proj = transformation.back_proj_pca(rotated, pca, coords=['x', 'y', 'z']) assert_almost_equal(back_proj.iloc[0].x, trajs.iloc[0].x)
def test_brownian_gap_close(): trajs = Trajectories(data.with_gaps_df()) max_speed = 10. cost_func = BrownianGapCloseCostFunction(parameters={'max_speed': max_speed}) in_idxs = [(3, 0), (3, 0), (5, 1), (13, 2), (13, 2), (16, 3)] out_idxs = [(5, 3), (7, 4), (7, 4), (15, 5), (18, 6), (18, 6)] cost_func.context['trajs'] = trajs cost_func.context['idxs_in'] = in_idxs cost_func.context['idxs_out'] = out_idxs cost_func.get_block() m = cost_func.mat m_true = [[np.nan, np.nan, np.nan, 2.0553543815820543, 45.74823093541038, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, 1.1530875267623917, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, 0.8254178191605767, 23.84513283483594], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 4.916599888362159], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]] assert_array_almost_equal(m, m_true)
def test_brownian_gap_close(): trajs = Trajectories(data.with_gaps_df()) max_speed = 10. cost_func = BrownianGapCloseCostFunction( parameters={'max_speed': max_speed}) in_idxs = [(3, 0), (3, 0), (5, 1), (13, 2), (13, 2), (16, 3)] out_idxs = [(5, 3), (7, 4), (7, 4), (15, 5), (18, 6), (18, 6)] cost_func.context['trajs'] = trajs cost_func.context['idxs_in'] = in_idxs cost_func.context['idxs_out'] = out_idxs cost_func.get_block() m = cost_func.mat m_true = [[ np.nan, np.nan, np.nan, 2.0553543815820543, 45.74823093541038, np.nan, np.nan ], [np.nan, np.nan, np.nan, np.nan, 1.1530875267623917, np.nan, np.nan], [ np.nan, np.nan, np.nan, np.nan, np.nan, 0.8254178191605767, 23.84513283483594 ], [ np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 4.916599888362159 ], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]] assert_array_almost_equal(m, m_true)
def test_radial_speed(): trajs = Trajectories(data.with_gaps_df()) trajs.reset_index(level='label', drop=True, inplace=True) trajs.set_index('true_label', drop=True, append=True, inplace=True) trajs.index.set_names(['t_stamp', 'label'], inplace=True) rd_speeds = rotation.radial_speed(trajs, in_coords=['x', 'y'], from_polar=False)
def test_by_frame_solver_with_missing_data(): true_trajs = data.with_gaps_df() solver = ByFrameSolver.for_brownian_motion(true_trajs, max_speed=5, penalty=2.) trajs = solver.track() min_chi_square, conserved_trajectories_number, scores = get_scores_on_trajectories(trajs) assert min_chi_square == 0 and conserved_trajectories_number == 3 / 7.
def test_back_proj_interp(): trajs = Trajectories(data.with_gaps_df()) interpolated = transformation.time_interpolate(trajs, sampling=3, s=0) back_proj = transformation.back_proj_interp(interpolated, trajs, 3) assert_almost_equal(back_proj.iloc[0].x, trajs.iloc[0].x) interpolated = transformation.time_interpolate(trajs, sampling=3, s=2) back_proj = transformation.back_proj_interp(interpolated, trajs, 3) assert_almost_equal(back_proj.iloc[0].x, trajs.iloc[0].x)
def test_gap_close(): trajs = data.with_gaps_df() max_speed = 10. maximum_gap = 5 gc_solver = GapCloseSolver.for_brownian_motion(trajs, max_speed=max_speed, maximum_gap=maximum_gap, use_t_stamp=True) gc_solver.track() seg_shapes = [seg[1].shape for seg in gc_solver.trajs.iter_segments] assert seg_shapes == [(18, 5), (19, 5), (19, 5)]
def test_gap_close_get_candidates(): trajs = data.with_gaps_df() max_speed = 10. maximum_gap = 5 gc_solver = GapCloseSolver.for_brownian_motion(trajs, max_speed=max_speed, maximum_gap=maximum_gap, use_t_stamp=True) in_idxs, out_idxs = gc_solver._get_candidates() assert in_idxs == [(3, 0), (3, 0), (5, 1), (13, 2), (13, 2), (16, 3)] assert out_idxs == [(5, 3), (7, 4), (7, 4), (15, 5), (18, 6), (18, 6)]
def test_by_frame_solver_with_missing_data(): true_trajs = data.with_gaps_df() solver = ByFrameSolver.for_brownian_motion(true_trajs, max_speed=5, penalty=2.) trajs = solver.track() min_chi_square, conserved_trajectories_number, scores = get_scores_on_trajectories( trajs) assert min_chi_square == 0 and conserved_trajectories_number == 3 / 7.
def test_interpolate(): trajs = Trajectories(data.with_gaps_df()) trajs.set_index('true_label', inplace=True, append=True) trajs.reset_index(level='label', drop=True, inplace=True) trajs.index.set_names(['t_stamp', 'label'], inplace=True) interpolated = trajs.time_interpolate(sampling=3, time_step=0.1, s=1) # t_stamps_in = interpolated.index.get_level_values('t_stamp') # indexer = t_stamps_in % 2 == 0 # interpolated.loc[indexer].shape, trajs.shape # indexer = interpolated.t_stamps % 3 == 0 # assert interpolated.loc[indexer].shape[0] == trajs.shape[0] dts = interpolated.get_segments()[0].t.diff().dropna() # All time points should be equaly spaced assert_almost_equal(dts.min(), dts.max())