Skip to content

Commit

Permalink
typing suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Saransh Chopra <saransh0701@gmail.com>
  • Loading branch information
ntessore and Saransh-cpp authored Feb 14, 2025
1 parent da4efe4 commit a500455
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions glass/core/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def cov_clip(
# put matrix back together
# enforce symmetry
v = xp.sqrt(w[..., None, :]) * v
return xp.matmul(v, xp.matrix_transpose(v)) # type: ignore[no-any-return]
cov_clipped: NDArray[np.float64] = xp.matmul(v, xp.matrix_transpose(v))
return cov_clipped


def nearcorr(
Expand Down Expand Up @@ -250,9 +251,9 @@ def cov_nearest(
raise ValueError(msg)

# store the normalisation of the matrix
norm = xp.sqrt(diag)
norm: NDArray[np.float64] = xp.sqrt(diag)
norm = norm[..., None, :] * norm[..., :, None]

# find nearest correlation matrix
corr = cov / xp.where(norm > 0, norm, 1.0)
return nearcorr(corr, niter=niter, tol=tol) * norm # type: ignore[no-any-return]
return nearcorr(corr, niter=niter, tol=tol) * norm
5 changes: 3 additions & 2 deletions glass/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,16 @@ def check_posdef_spectra(spectra: Cls) -> bool:
"""Test whether angular power spectra are positive semi-definite."""
cov = cov_from_spectra(spectra)
xp = cov.__array_namespace__()
return xp.all(xp.linalg.eigvalsh(cov) >= 0) # type: ignore[no-any-return]
is_positive_semi_definite: bool = xp.all(xp.linalg.eigvalsh(cov) >= 0)
return is_positive_semi_definite


def regularized_spectra(
spectra: Cls,
*,
lmax: int | None = None,
method: Literal["nearest", "clip"] = "nearest",
**method_kwargs: Any, # noqa: ANN401
**method_kwargs: float | None,
) -> Cls:
r"""
Regularise a set of angular power spectra.
Expand Down

0 comments on commit a500455

Please sign in to comment.