Skip to content

Commit b9479e7

Browse files
committed
fix: normalise fold direction by its length and keep dgz shape when inverting
get_deformed_orientation divided the fold direction by the sum of its components, not by its length. With invert_norm, it returned only a subset of the rows of dgz. Now the fold direction is unit length and invert_norm flips all of dgz.
1 parent 1544b4e commit b9479e7

2 files changed

Lines changed: 53 additions & 14 deletions

File tree

‎LoopStructural/modelling/features/fold/_fold.py‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,11 @@ def get_deformed_orientation(self, points):
104104
dgz[mask, :] /= np.linalg.norm(dgz[mask, :], axis=1)[:, None]
105105
R2 = self.rot_mat(fold_axis, self.fold_limb_rotation(gx))
106106
fold_direction = np.einsum("ijk,ki->kj", R2, dgx)
107-
fold_direction /= np.sum(fold_direction, axis=1)[:, None]
108-
# calculate dot product between fold_direction and axis
109-
# if its less than 0 then inverse dgz
110-
d = np.einsum("ij,ik->i", fold_direction, fold_axis)
107+
fold_direction /= np.linalg.norm(fold_direction, axis=1)[:, None]
111108

112109
if self.invert_norm:
113-
new_dgz = -dgz[mask][d[mask] < 0]
114-
return fold_direction, fold_axis, new_dgz
115-
116-
elif not self.invert_norm:
117-
return fold_direction, fold_axis, dgz
118-
else:
119-
logger.warning("invert fold frame param not valid. Defaulting to false.")
120-
return fold_direction, fold_axis, dgz
121-
122-
# return fold_direction, fold_axis, dgz
110+
dgz = -dgz
111+
return fold_direction, fold_axis, dgz
123112

124113
# def get_regularisation_direction(self, points):
125114
# self.foldframe.features[2].evaluate_gradient(points)

‎tests/unit/modelling/test_fold_event.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,53 @@ def test_constant_fold_axis():
99

1010
def test_rotation_fold_axis():
1111
pass
12+
13+
14+
from types import SimpleNamespace
15+
16+
import numpy as np
17+
18+
from LoopStructural.modelling.features.fold import FoldEvent
19+
20+
21+
class _PlaneFeature:
22+
"""Stub fold frame coordinate with a constant gradient"""
23+
24+
def __init__(self, gradient):
25+
self.gradient = np.array(gradient, dtype=float)
26+
27+
def evaluate_value(self, points):
28+
return points @ self.gradient
29+
30+
def evaluate_gradient(self, points):
31+
return np.tile(self.gradient, (points.shape[0], 1))
32+
33+
34+
def _fold_event(invert_norm=False):
35+
frame = SimpleNamespace(
36+
features=[_PlaneFeature([2, 0, 0]), _PlaneFeature([0, 3, 0]), _PlaneFeature([0, 0, 1])]
37+
)
38+
return FoldEvent(
39+
frame,
40+
fold_limb_rotation=lambda gx: np.full_like(gx, 30.0),
41+
fold_axis=np.array([0, 1.0, 0]),
42+
invert_norm=invert_norm,
43+
)
44+
45+
46+
def test_deformed_orientation_is_unit_length():
47+
points = np.random.default_rng(0).random((20, 3))
48+
fold_direction, fold_axis, dgz = _fold_event().get_deformed_orientation(points)
49+
assert fold_direction.shape == points.shape
50+
assert dgz.shape == points.shape
51+
assert np.allclose(np.linalg.norm(fold_direction, axis=1), 1.0)
52+
assert np.allclose(np.linalg.norm(dgz, axis=1), 1.0)
53+
assert np.allclose(np.einsum("ij,ij->i", fold_direction, fold_axis), 0.0)
54+
55+
56+
def test_invert_norm_flips_dgz():
57+
points = np.random.default_rng(0).random((20, 3))
58+
_, _, dgz = _fold_event().get_deformed_orientation(points)
59+
_, _, inverted = _fold_event(invert_norm=True).get_deformed_orientation(points)
60+
assert inverted.shape == points.shape
61+
assert np.allclose(inverted, -dgz)

0 commit comments

Comments
 (0)