def test_proc_matmul(): restart_seed() random_markers_1 = Markers.from_random_data() random_markers_2 = Markers.from_random_data() markers_matmul = random_markers_1.meca.matmul(random_markers_2) ref_markers_matmul = random_markers_1 @ random_markers_2 np.testing.assert_almost_equal(markers_matmul, -33729.52497131, decimal=6) np.testing.assert_almost_equal(markers_matmul, ref_markers_matmul, decimal=6)
def test_markers_creation(): dims = ("axis", "channel", "time") array = Markers() np.testing.assert_array_equal(x=array, y=xr.DataArray()) assert array.dims == dims array = Markers(MARKERS_DATA.values) is_expected_array(array, **EXPECTED_VALUES[57]) size = 3, 10, 100 array = Markers.from_random_data(size=size) assert array.shape == (4, size[1], size[2]) assert array.dims == dims with pytest.raises(ValueError): Markers(ANALOGS_DATA)
def test_rotate(): n_frames = 100 n_markers = 10 angles = Angles.from_random_data(size=(3, 1, n_frames)) rt = Rototrans.from_euler_angles(angles, "xyz") markers = Markers.from_random_data(size=(3, n_markers, n_frames)) rotated_markers = Markers.from_rototrans(markers, rt) expected_rotated_marker = np.ndarray((4, n_markers, n_frames)) for marker in range(n_markers): for frame in range(n_frames): expected_rotated_marker[:, marker, frame] = np.dot( rt.isel(time=frame), markers.isel(channel=marker, time=frame), ) np.testing.assert_array_almost_equal(rotated_markers, expected_rotated_marker, decimal=10) rotated_markers = Markers.from_rototrans(markers.isel(time=0), rt.isel(time=0)) expected_rotated_marker = np.ndarray(rotated_markers.shape) for marker in range(n_markers): expected_rotated_marker[:, marker] = np.dot( rt.isel(time=0), markers.isel(channel=marker, time=0)) np.testing.assert_array_almost_equal(rotated_markers, expected_rotated_marker, decimal=10) rotated_markers = Markers.from_rototrans(markers, rt.isel(time=0)) expected_rotated_marker = np.ndarray(rotated_markers.shape) for marker in range(n_markers): expected_rotated_marker[:, marker] = np.dot(rt.isel(time=0), markers.isel(channel=marker)) np.testing.assert_array_almost_equal(rotated_markers, expected_rotated_marker, decimal=10) with pytest.raises(ValueError): Markers.from_rototrans(markers.isel(time=0), rt)
def test_proc_norm_marker(): n_frames = 100 n_markers = 10 random_marker = Markers.from_random_data(size=(3, n_markers, n_frames)) norm = random_marker.meca.norm(dim="axis") norm_without_ones = random_marker.drop_sel(axis="ones").meca.norm( dim="axis") np.testing.assert_array_equal(norm, norm_without_ones) expected_norm = np.ndarray((n_markers, n_frames)) for marker in range(n_markers): for frame in range(n_frames): expected_norm[marker, frame] = np.sqrt( random_marker[0:3, marker, frame].dot(random_marker[0:3, marker, frame])) np.testing.assert_array_equal(norm, expected_norm)
def test_rt_from_markers(): all_m = Markers.from_random_data() rt_xy = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 1]), axis_2=all_m.isel(channel=[0, 2]), axes_name="xy", axis_to_recalculate="y", ) rt_yx = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 2]), axis_2=all_m.isel(channel=[0, 1]), axes_name="yx", axis_to_recalculate="y", ) rt_xy_x_recalc = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 1]), axis_2=all_m.isel(channel=[0, 2]), axes_name="yx", axis_to_recalculate="x", ) rt_xy_x_recalc = rt_xy_x_recalc.isel(col=[1, 0, 2, 3]) rt_xy_x_recalc[:, 2, :] = -rt_xy_x_recalc[:, 2, :] rt_yz = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 1]), axis_2=all_m.isel(channel=[0, 2]), axes_name="yz", axis_to_recalculate="z", ) rt_zy = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 2]), axis_2=all_m.isel(channel=[0, 1]), axes_name="zy", axis_to_recalculate="z", ) rt_xy_from_yz = rt_yz.isel(col=[1, 2, 0, 3]) rt_xz = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 1]), axis_2=all_m.isel(channel=[0, 2]), axes_name="xz", axis_to_recalculate="z", ) rt_zx = Rototrans.from_markers( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 2]), axis_2=all_m.isel(channel=[0, 1]), axes_name="zx", axis_to_recalculate="z", ) rt_xy_from_zx = rt_xz.isel(col=[0, 2, 1, 3]) rt_xy_from_zx[:, 2, :] = -rt_xy_from_zx[:, 2, :] np.testing.assert_array_equal(rt_xy, rt_xy_x_recalc) np.testing.assert_array_equal(rt_xy, rt_yx) np.testing.assert_array_equal(rt_yz, rt_zy) np.testing.assert_array_equal(rt_xz, rt_zx) np.testing.assert_array_equal(rt_xy, rt_xy_from_yz) np.testing.assert_array_equal(rt_xy, rt_xy_from_zx) # Produce one that we know the solution ref_m = Markers( np.array(((1, 2, 3), (4, 5, 6), (6, 5, 4))).T[:, :, np.newaxis]) rt_xy_from_known_m = Rototrans.from_markers( origin=ref_m.isel(channel=[0]), axis_1=ref_m.isel(channel=[0, 1]), axis_2=ref_m.isel(channel=[0, 2]), axes_name="xy", axis_to_recalculate="y", ) rt_xy_expected = Rototrans( np.array([ [0.5773502691896257, 0.7071067811865475, -0.408248290463863, 1.0], [0.5773502691896257, 0.0, 0.816496580927726, 2.0], [0.5773502691896257, -0.7071067811865475, -0.408248290463863, 3.0], [0, 0, 0, 1.0], ])) np.testing.assert_array_equal(rt_xy_from_known_m, rt_xy_expected) exception_default_params = dict( origin=all_m.isel(channel=[0]), axis_1=all_m.isel(channel=[0, 1]), axis_2=all_m.isel(channel=[0, 2]), axes_name="xy", axis_to_recalculate="y", ) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(origin=all_m.isel(channel=[0, 1])) }) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(axis_1=all_m.isel(channel=[0])) }) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(axis_2=all_m.isel(channel=[0])) }) with pytest.raises(ValueError): Rototrans.from_markers( **{ **exception_default_params, **dict(axis_1=all_m.isel(channel=[0, 1], time=slice(None, 50))), }) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(axes_name="yyz") }) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(axes_name="xxz") }) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(axes_name="zzz") }) with pytest.raises(ValueError): Rototrans.from_markers(**{ **exception_default_params, **dict(axis_to_recalculate="h") })