Skip to content

Commit

Permalink
Merge 6d6dcfa into 625c88f
Browse files Browse the repository at this point in the history
  • Loading branch information
pariterre committed Jun 3, 2020
2 parents 625c88f + 6d6dcfa commit 27b1375
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
8 changes: 6 additions & 2 deletions pyomeca/processing/rototrans.py
Expand Up @@ -154,7 +154,9 @@ def rototrans_from_markers(
else:
raise ValueError("`axis_to_recalculate must be `x`, `y` or `z`")

rt = caller(np.zeros((4, 4, origin.time.size)))
rt = np.zeros((4, 4, origin.time.size))
rt[3, 3, :] = 1
rt = caller(rt)
rt[:3, 0, :] = x / np.linalg.norm(x, axis=0)
rt[:3, 1, :] = y / np.linalg.norm(y, axis=0)
rt[:3, 2, :] = z / np.linalg.norm(z, axis=0)
Expand All @@ -165,7 +167,9 @@ def rototrans_from_markers(
def rototrans_from_transposed_rototrans(
caller: Callable, rt: xr.DataArray
) -> xr.DataArray:
rt_t = caller(np.zeros((4, 4, rt.time.size)))
rt_t = np.zeros((4, 4, rt.time.size))
rt_t[3, 3, :] = 1
rt_t = caller(rt_t)

# the rotation part is just the transposed of the rotation
rt_t.meca.rotation = rt.meca.rotation.transpose("col", "row", "time")
Expand Down
14 changes: 11 additions & 3 deletions pyomeca/rototrans.py
Expand Up @@ -71,8 +71,16 @@ def __new__(
coords["time"] = time

# Make sure last line reads [0, 0, 0, 1]
data[3, :3, :] = 0
data[3, 3, :] = 1
zeros = data[3, :3, :]
ones = data[3, 3, :]
if not np.alltrue(zeros == 0) or not np.alltrue(ones == 1):
some_zeros = np.random.choice(zeros.ravel(), 5)
some_ones = np.random.choice(ones.ravel(), 5)
raise ValueError(
"Last line does not read [0, 0, 0, 1].\n"
f"Here are some values that should be 0: {some_zeros}\n"
f"And others that should 1: {some_ones}"
)

return xr.DataArray(
data=data,
Expand All @@ -84,7 +92,7 @@ def __new__(

@classmethod
def from_random_data(
cls, distribution: str = "normal", size: tuple = (4, 4, 100), **kwargs
cls, distribution: str = "normal", size: tuple = (3, 1, 100), **kwargs
) -> xr.DataArray:
"""
Create random data from a specified distribution (normal by default) using random walk.
Expand Down
2 changes: 1 addition & 1 deletion tests/data/is_expected_array_val.csv
Expand Up @@ -65,4 +65,4 @@ idx,shape_val,first_last_val,mean_val,median_val,sum_val,nans_val
64,"(4, 51, 93)","(733.7041, 1.0)",319.803440918195,303.8114,6067310.8811,0
65,"(4, 51, 580)","(44.16278839111328, 1.0)",362.29798490932,337.751922607422,42535594.9182787,915
66,"(1, 38, 11600)","(-0.022051572799682617, 0.0)",-0.001679033595597,0,-740.118008939112,0
67,"(4, 4, 580)","(744.5535888671875, 1.0)",385.4765160798091,278.7399444580078,3535590.605484009,108
67,"(4, 4, 580)","(0.306872, 1.0)",107.58354,0.0,975137.207354,216
9 changes: 8 additions & 1 deletion tests/test_object_creation.py
Expand Up @@ -68,7 +68,14 @@ def test_rototrans_creation():
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)
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
Expand Down
6 changes: 6 additions & 0 deletions tests/test_processing_rt.py
Expand Up @@ -66,6 +66,12 @@ def test_construct_rt():
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],
Expand Down

0 comments on commit 27b1375

Please sign in to comment.