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-328: efficient resampling in ellipticity_ryden04 #341

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

Saransh-cpp
Copy link
Member

The code now generates only the required values while resampling making it more efficient. The implementation works with the existing test cases, but given that the function accepts ArrayLike args, the following fails -

In [1]: from glass import ellipticity_ryden04

In [2]: mu, sigma, gamma, sigma_gamma = [-1.85, -2.85], [0.89, 0.89, 0.33], [0.056, 0.02, 0.3, 0.2], [0.222]\

In [3]: ellipticity_ryden04(mu, sigma, gamma, sigma_gamma)
----------------------------------------------------------------------
ValueError                           Traceback (most recent call last)
Cell In[3], line 1
----> 1 ellipticity_ryden04(mu, sigma, gamma, sigma_gamma)

File ~/Code/UCL/glass/glass/shapes.py:143, in ellipticity_ryden04(mu, sigma, gamma, sigma_gamma, size, rng)
    141 # default size if not given
    142 if size is None:
--> 143     size = np.broadcast(mu, sigma, gamma, sigma_gamma).shape
    145 # broadcast all inputs to output shape
    146 # this makes it possible to efficiently resample later
    147 mu = np.broadcast_to(mu, size, subok=True)

ValueError: shape mismatch: objects cannot be broadcast to a single shape.  Mismatch is between arg 0 with shape (2,) and arg 1 with shape (3,).

I am not sure if this is a "valid" case or will scientists never pass in such arguments.

Refs: #328
Changed: resampling in ellipticity_ryden04 is more efficient now

@Saransh-cpp Saransh-cpp added the performance Performance improvements or regressions label Oct 9, 2024
@Saransh-cpp Saransh-cpp self-assigned this Oct 9, 2024
@Saransh-cpp Saransh-cpp marked this pull request as draft October 9, 2024 14:15
@ntessore
Copy link
Collaborator

ntessore commented Oct 9, 2024

From the other PR:

Maybe I am picking up the wrong cases (cases that scientists won't use). For instance, this would not be broadcastable -

mu, sigma, gamma, sigma_gamma = [-1.85, -2.85], [0.89, 0.89, 0.33], [0.056, 0.02, 0.3, 0.2], [0.222]

Maybe I am misunderstanding, but this wouldn't work even now, would it? In any case, all of the inputs must be broadcastable to a single shape (or size, if given).

@Saransh-cpp
Copy link
Member Author

but this wouldn't work even now, would it?

Right, it won't.

Maybe I was just overthinking all the possible edge cases haha, thanks!

@Saransh-cpp Saransh-cpp force-pushed the saransh/optimise-ellipticity_ryden04 branch from 8fd7837 to 01a5587 Compare October 9, 2024 14:54
glass/shapes.py Outdated Show resolved Hide resolved
@Saransh-cpp Saransh-cpp marked this pull request as ready for review October 9, 2024 15:39
@Saransh-cpp Saransh-cpp force-pushed the saransh/optimise-ellipticity_ryden04 branch from 01a5587 to 0c2b53d Compare October 9, 2024 15:39
glass/shapes.py Outdated Show resolved Hide resolved
glass/shapes.py Outdated Show resolved Hide resolved
glass/shapes.py Outdated Show resolved Hide resolved
@Saransh-cpp
Copy link
Member Author

Oops, my bad, thanks for the suggestions!

Saransh-cpp and others added 3 commits October 14, 2024 09:36
gh-328: efficient resampling in ellipticity_ryden04

Co-authored-by: Nicolas Tessore <[email protected]>
Co-authored-by: Nicolas Tessore <[email protected]>
@Saransh-cpp Saransh-cpp force-pushed the saransh/optimise-ellipticity_ryden04 branch from 68367a3 to e7e5e9b Compare October 14, 2024 08:36
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.

Great! 🚀

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.

LGTM

while np.any(bad):
eps[bad] = rng.normal(mu, sigma, size=size)[bad]
bad = eps > 0
while np.any(bad := eps > 0):
Copy link
Member

Choose a reason for hiding this comment

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

Love a good walrus

@Saransh-cpp Saransh-cpp merged commit 8ad8831 into main Oct 14, 2024
16 checks passed
@Saransh-cpp Saransh-cpp deleted the saransh/optimise-ellipticity_ryden04 branch October 14, 2024 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance improvements or regressions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants