Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-575: bring back inv_triangle_number #577

Merged
merged 7 commits into from
Mar 11, 2025

Conversation

Saransh-cpp
Copy link
Member

@Saransh-cpp Saransh-cpp commented Mar 7, 2025

Description

Add _is_inv_triangle_number to check for triangle numbers and use it internally in nfields_from_nspectra. The refactor might not look very useful right now, but it paves the way for adding an nfields_from_nspectra-equivalent function for alm in #533 without repeating the core logic.

Closes: #575

Checks

  • Is your code passing linting?
  • Is your code passing tests?
  • Have you added additional tests (if required)?
  • Have you modified/extended the documentation (if required)?
  • Have you added a one-liner changelog entry above (if required)?

@Saransh-cpp Saransh-cpp self-assigned this Mar 7, 2025
@Saransh-cpp Saransh-cpp added maintenance Maintenance: refactoring, typos, etc. api An (incompatible) API change labels Mar 7, 2025
@Saransh-cpp Saransh-cpp marked this pull request as ready for review March 10, 2025 13:38
glass/fields.py Outdated
@@ -51,15 +51,25 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
return decorator


def _is_inv_triangle_number(triangle_number: int) -> tuple[int, bool]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of this return signature. The signature minimises repetitions but I am open to any better implementations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just have _inv_triangle_number() throw an exception if it's not -- both downstream functions can catch and rethrow it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have updated the implementation.

glass/fields.py Outdated
def nfields_from_nspectra(nspectra: int) -> int:
r"""
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 * nspectra))
if n * (n + 1) // 2 != nspectra:
n, is_inv_triangle = _is_inv_triangle_number(nspectra)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_is_inv_triangle_number will similarly be used in a new function (to be added in #533) which will be specific to alm.

@Saransh-cpp Saransh-cpp marked this pull request as draft March 10, 2025 14:16
@Saransh-cpp Saransh-cpp marked this pull request as ready for review March 10, 2025 15:08
@Saransh-cpp Saransh-cpp requested a review from paddyroddy March 10, 2025 15:09
@Saransh-cpp
Copy link
Member Author

The docstrings are fixed now.

Co-authored-by: Patrick J. Roddy <[email protected]>
@paddyroddy paddyroddy self-requested a review March 10, 2025 16:22
Copy link
Member

@paddyroddy paddyroddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@Saransh-cpp Saransh-cpp requested a review from paddyroddy March 11, 2025 10:18
Copy link
Member

@paddyroddy paddyroddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're getting test failures now.

I have recently got numeric errors here https://github.com/astro-informatics/sleplet/actions/runs/13656527580 so I wonder if there has been a recent NumPy change or something

@Saransh-cpp Saransh-cpp requested a review from paddyroddy March 11, 2025 16:49
Copy link
Collaborator

@ntessore ntessore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@Saransh-cpp Saransh-cpp merged commit cfd448a into main Mar 11, 2025
16 checks passed
@Saransh-cpp Saransh-cpp deleted the saransh/add-inv_triangle_number-back branch March 11, 2025 17:09
Saransh-cpp added a commit that referenced this pull request Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api An (incompatible) API change maintenance Maintenance: refactoring, typos, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add inv_triangle_number back along with a helper function
3 participants