示例#1
0
def test_construct_rt():
    eye = Rototrans()
    np.testing.assert_equal(eye.time.size, 1)
    np.testing.assert_equal(eye.sel(time=0), np.eye(4))

    eye = Rototrans.from_euler_angles()
    np.testing.assert_equal(eye.time.size, 1)
    np.testing.assert_equal(eye.sel(time=0), np.eye(4))

    # Test the way to create a rt, but not when providing bot angles and sequence
    nb_frames = 10
    random_vector = Angles(np.random.rand(3, 1, nb_frames))

    # with angles
    rt_random_angles = Rototrans.from_euler_angles(angles=random_vector,
                                                   angle_sequence="xyz")
    np.testing.assert_equal(rt_random_angles.time.size, nb_frames)
    np.testing.assert_equal(rt_random_angles[:-1, -1:, :],
                            np.zeros((3, 1, nb_frames)))  # Translation is 0

    # with translation
    rt_random_translation = Rototrans.from_euler_angles(
        translations=random_vector)
    np.testing.assert_equal(rt_random_translation.time.size, nb_frames)
    np.testing.assert_equal(
        rt_random_translation[:3, :3, :],
        np.repeat(np.eye(3)[:, :, np.newaxis], nb_frames, axis=2),
    )  # rotation is eye3
    np.arange(0, rt_random_angles.time.size / 0.5, 1 / 0.5)

    rt_with_time = Rototrans(
        rt_random_angles,
        time=np.arange(0, rt_random_angles.time.size / 100, 1 / 100),
    )
    assert rt_with_time.time[-1] == 0.09

    with pytest.raises(IndexError):
        Rototrans(data=np.zeros(1))

    with pytest.raises(ValueError):
        Rototrans(data=np.zeros((4, 4, 1)))

    with pytest.raises(ValueError):
        Rototrans(data=np.ones((4, 4, 1)))

    with pytest.raises(IndexError):
        Rototrans.from_euler_angles(
            angles=random_vector[..., :5],
            translations=random_vector,
            angle_sequence="x",
        )

    with pytest.raises(IndexError):
        Rototrans.from_euler_angles(angles=random_vector, angle_sequence="x")

    with pytest.raises(ValueError):
        Rototrans.from_euler_angles(angles=random_vector, angle_sequence="nop")
def test_rototrans_creation():
    dims = ("row", "col", "time")
    array = Rototrans()
    np.testing.assert_array_equal(x=array,
                                  y=xr.DataArray(np.eye(4)[..., np.newaxis]))
    assert array.dims == dims

    array = Rototrans(MARKERS_DATA.values, time=MARKERS_DATA.time)
    is_expected_array(array, **EXPECTED_VALUES[67])

    size = 4, 4, 100
    array = Rototrans.from_random_data(size=size)
    assert array.shape == size
    assert array.dims == dims

    with pytest.raises(ValueError):
        Angles(ANALOGS_DATA)
def test_rototrans_creation():
    dims = ("row", "col", "time")
    array = Rototrans()
    np.testing.assert_array_equal(x=array,
                                  y=xr.DataArray(np.eye(4)[..., np.newaxis]))
    assert array.dims == dims

    data = Markers(MARKERS_DATA.values)
    array = Rototrans.from_markers(
        origin=data.isel(channel=[0]),
        axis_1=data.isel(channel=[0, 1]),
        axis_2=data.isel(channel=[0, 2]),
        axes_name="xy",
        axis_to_recalculate="y",
    )
    is_expected_array(array, **EXPECTED_VALUES[67])

    size = 4, 4, 100
    array = Rototrans.from_random_data(size=size)
    assert array.shape == size
    assert array.dims == dims

    with pytest.raises(ValueError):
        Angles(ANALOGS_DATA)
示例#4
0
w = vertcat(transX, transY, transZ, angleX, angleY, angleZ)
prob = {'f': J, 'x': w}
options = {
    'ipopt.hessian_approximation': "exact",
    # 'ipopt.tol':1e-10,'ipopt.dual_inf_tol':1e-15
}
solver = nlpsol('solver', 'ipopt', prob, options)
w0 = np.zeros((6, 1))
# w0[0]= marker_model[0,0,0] - marker_treat[0,0,0]
# w0[1]= marker_model[1,0,0] - marker_treat[1,0,0]
# w0[2]= marker_model[2,0,0] - marker_treat[2,0,0]

solve = solver(x0=w0, lbx=-1000, ubx=10000)
w_opt = solve['x']
print(w_opt)
rt = Rototrans((RT(w_opt[0], w_opt[1], w_opt[2], w_opt[3], w_opt[4],
                   w_opt[5])))
# rt = Rototrans((RT(0.841114, 0.103637, -0.596597, -0.458244, 0.145129, 3.21253)))
marker_rotate = Markers.from_rototrans(marker_treat, rt)
# Data for three-dimensional scattered points
ax = plt.axes(projection='3d')
zdata = marker_rotate[2, :, 0]
xdata = marker_rotate[0, :, 0]
ydata = marker_rotate[1, :, 0]
ax.scatter3D(xdata, ydata, zdata, cmap='Greens')
zdata = marker_model[2, :, 0]
xdata = marker_model[0, :, 0]
ydata = marker_model[1, :, 0]
ax.scatter3D(xdata, ydata, zdata, edgecolors="red")
ax.view_init(60, 35)
plt.show()
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")
        })