Skip to content

Commit

Permalink
update docstring, argument, error, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp committed Feb 20, 2025
1 parent 6dcdf00 commit e46321b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
33 changes: 11 additions & 22 deletions glass/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
Cls = Sequence[NDArray[Any]]


def nfields_from_nspectra(triangle_number: int) -> int:
def nfields_from_nspectra(nspectra: int) -> int:
r"""
The :math:`n`-th triangle number is :math:`T_n = n \, (n+1)/2`. If
the argument is :math:`T_n`, then :math:`n` is returned. Otherwise,
a :class:`ValueError` is raised.
Returns the number of fields for a number of spectra.
Given the number of spectra nspectra, returns the number of
fields n such that n * (n + 1) // 2 == nspectra.
"""
n = math.floor(math.sqrt(2 * triangle_number))
if n * (n + 1) // 2 != triangle_number:
msg = f"not a triangle number: {triangle_number}"
n = math.floor(math.sqrt(2 * nspectra))
if n * (n + 1) // 2 != nspectra:
msg = f"invalid numer of spectra: {nspectra}"
raise ValueError(msg)
return n

Expand Down Expand Up @@ -274,11 +275,7 @@ def discretized_cls(
"""
if ncorr is not None:
try:
n = nfields_from_nspectra(len(cls))
except ValueError:
msg = "length of cls array is not a triangle number"
raise ValueError(msg) from None
n = nfields_from_nspectra(len(cls))
cls = [
cls[i * (i + 1) // 2 + j] if j <= ncorr else np.asarray([])
for i in range(n)
Expand Down Expand Up @@ -591,11 +588,7 @@ def effective_cls(
"""
# this is the number of fields
try:
n = nfields_from_nspectra(len(cls))
except ValueError:
msg = "length of cls is not a triangle number"
raise ValueError(msg) from None
n = nfields_from_nspectra(len(cls))

# find lmax if not given
if lmax is None:
Expand Down Expand Up @@ -914,11 +907,7 @@ def cov_from_spectra(spectra: Cls, *, lmax: int | None = None) -> NDArray[Any]:
"""
# recover the number of fields from the number of spectra
try:
n = nfields_from_nspectra(len(spectra))
except ValueError:
msg = "invalid number of spectra"
raise ValueError(msg) from None
n = nfields_from_nspectra(len(spectra))

if lmax is None: # noqa: SIM108
# maximum length in input spectra
Expand Down
8 changes: 3 additions & 5 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ def test_discretized_cls() -> None:

# check ValueError for triangle number

with pytest.raises(
ValueError, match="length of cls array is not a triangle number"
):
with pytest.raises(ValueError, match="invalid numer of spectra:"):
glass.discretized_cls([np.arange(10), np.arange(10)], ncorr=1)

# ncorr not None
Expand Down Expand Up @@ -307,7 +305,7 @@ def test_effective_cls() -> None:

# check ValueError for triangle number

with pytest.raises(ValueError, match="length of cls is not a triangle number"):
with pytest.raises(ValueError, match="invalid numer of spectra:"):
glass.effective_cls([np.arange(10), np.arange(10)], np.ones((2, 1)))

# check ValueError for triangle number
Expand Down Expand Up @@ -425,7 +423,7 @@ def test_nfields_from_nspectra():
not_triangle_numbers = [2, 4, 5, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19, 20]

for t in not_triangle_numbers:
with pytest.raises(ValueError, match="not a triangle number"):
with pytest.raises(ValueError, match=f"invalid numer of spectra: {t}"):
glass.nfields_from_nspectra(t)


Expand Down

0 comments on commit e46321b

Please sign in to comment.