Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/psfmachine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,42 @@ def spline1d(x, knots, degree=3):


def _make_A_cartesian(x, y, n_knots=10, radius=3.0, knot_spacing_type="sqrt", degree=3):
# Must be odd
n_odd_knots = n_knots if n_knots % 2 == 1 else n_knots + 1
if knot_spacing_type == "sqrt":
knots = np.linspace(-np.sqrt(radius), np.sqrt(radius), n_knots)
knots = np.sign(knots) * knots ** 2
x_knots = np.linspace(-np.sqrt(radius), np.sqrt(radius), n_odd_knots)
x_knots = np.sign(x_knots) * x_knots ** 2
y_knots = np.linspace(-np.sqrt(radius), np.sqrt(radius), n_odd_knots)
y_knots = np.sign(y_knots) * y_knots ** 2
else:
knots = np.linspace(-radius, radius, n_knots)
x_spline = spline1d(x, knots=knots, degree=degree)
y_spline = spline1d(y, knots=knots, degree=degree)

x_knots = np.linspace(-radius, radius, n_odd_knots)
y_knots = np.linspace(-radius, radius, n_odd_knots)
x_spline = sparse.csr_matrix(
np.asarray(
dmatrix(
"bs(x, knots=knots, degree=degree, include_intercept=True)",
{
"x": list(np.hstack([x_knots.min(), x, x_knots.max()])),
"degree": degree,
"knots": x_knots,
},
)
)[1:-1]
)
y_spline = sparse.csr_matrix(
np.asarray(
dmatrix(
"bs(x, knots=knots, degree=degree, include_intercept=True)",
{
"x": list(np.hstack([y_knots.min(), y, y_knots.max()])),
"degree": degree,
"knots": y_knots,
},
)
)[1:-1]
)
x_spline = x_spline[:, np.asarray(x_spline.sum(axis=0))[0] != 0]
y_spline = y_spline[:, np.asarray(y_spline.sum(axis=0))[0] != 0]
X = sparse.hstack(
[x_spline.multiply(y_spline[:, idx]) for idx in range(y_spline.shape[1])],
format="csr",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_perturbation.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_perturbation_matrix3d():
p3 = PerturbationMatrix3D(
time=time, dx=dx, dy=dy, nknots=4, radius=5, resolution=5, poly_order=1
)
assert p3.cartesian_matrix.shape == (169, 64)
assert p3.cartesian_matrix.shape == (169, 81)
assert p3.vectors.shape == (10, 2)
assert p3.shape == (
p3.cartesian_matrix.shape[0] * p3.ntime,
Expand Down