Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 14, 2025
1 parent 586a6f5 commit dba338d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions hera_filters/dspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2933,11 +2933,11 @@ def sparse_linear_fit_2D(
If True, the solver will apply a preconditioner to the basis matrices before
solving the least-squares problem. This option is useful when the input weights
are frequency or time dependent and are either very large or very small, or when
the basis matrices are ill-conditioned due to large stretches of zeros.
The preconditioner is computed using the the inverse of the regularized Gramian
matrix of the basis matrices. Prior computing the inverse, the eigenvalues of the
Gramian matrix are regularized by adding a small value proportional to the smallest
eigenvalue. This helps to stabilize the computation of the inverse. The regularization
the basis matrices are ill-conditioned due to large stretches of zeros.
The preconditioner is computed using the the inverse of the regularized Gramian
matrix of the basis matrices. Prior computing the inverse, the eigenvalues of the
Gramian matrix are regularized by adding a small value proportional to the smallest
eigenvalue. This helps to stabilize the computation of the inverse. The regularization
factor is computed as the minimum eigenvalue of the Gramian matrix multiplied by the
`eig_scaling_factor` parameter.
eig_scaling_factor : float, optional, default 1e-1
Expand Down Expand Up @@ -2994,7 +2994,7 @@ def sparse_linear_fit_2D(
axis_1_pcond = np.linalg.pinv(
XTX_axis_1 + np.eye(XTX_axis_1.shape[0]) * axis_1_lambda
)

# Compute the preconditioner for the second axis
XTX_axis_2 = np.dot(axis_2_basis.T.conj() * axis_2_wgts, axis_2_basis)
eigenval, _ = np.linalg.eig(XTX_axis_2)
Expand Down
4 changes: 2 additions & 2 deletions hera_filters/tests/test_dspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ def test_precondition_sparse_solver():
axis_1_weights = (~time_flags[:, 0]).astype(float) * rng.integers(1, 10, size=(ntimes,))
axis_2_weights = (~freq_flags[0]).astype(float)
wgts = np.outer(axis_1_weights, axis_2_weights)

# Add frequency dependence to the weights to make the problem more ill-conditioned
wgts *= (freqs / 150e6) ** -3.5

Expand Down Expand Up @@ -1589,4 +1589,4 @@ def test_precondition_sparse_solver():
# Check that the fit closely matches to the separable fit
np.testing.assert_allclose(sol, sol_sparse, atol=1e-8, rtol=1e-6)
np.testing.assert_allclose(sol, sol_sparse_precond, atol=1e-8, rtol=1e-6)
np.testing.assert_array_less(meta_precond['iter_num'], meta['iter_num'])
np.testing.assert_array_less(meta_precond['iter_num'], meta['iter_num'])

0 comments on commit dba338d

Please sign in to comment.