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

Optimize bits of HSM code #1279

Merged
merged 14 commits into from
Mar 11, 2024
Merged

Optimize bits of HSM code #1279

merged 14 commits into from
Mar 11, 2024

Conversation

arunkannawadi
Copy link
Member

This PR addresses a lot of the concerns raised in #1264. I also separated conversion code from code that checks things as much as possible. I can add some additional unit tests if this all looks sensible.

@property
def observed_shape(self):
return Shear(e1=self._data.observed_e1, e2=self._data.observed_e2)
return Shear(e1=self.observed_e1, e2=self.observed_e2)
Copy link
Member Author

@arunkannawadi arunkannawadi Mar 8, 2024

Choose a reason for hiding this comment

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

@erykoff wanted this to use galsim._Shear here, but that requires conversion from e to g before we invoke _Shear and might not be worthwhile.

Copy link
Member

@rmjarvis rmjarvis Mar 8, 2024

Choose a reason for hiding this comment

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

If you want to optimize this too, you could try:

e = self._data.observed_e1 + 1j*self._data.observed_e2
g = e / (1 + np.sqrt(1.-abs(e)**2))
return _Shear(g)

I suspect that's a little faster, but might just be nibbling at the edges.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the LSST DM code base, the reason to get observed_shape was because there was no way to get observed_e1 directly up until now. Now that we can access them directly, I honestly don't think using _Shear is necessary.

Copy link
Member

@rmjarvis rmjarvis left a comment

Choose a reason for hiding this comment

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

LGTM, module a small coverage drop. And thanks for the typo/doc fixes.

@property
def observed_shape(self):
return Shear(e1=self._data.observed_e1, e2=self._data.observed_e2)
return Shear(e1=self.observed_e1, e2=self.observed_e2)
Copy link
Member

@rmjarvis rmjarvis Mar 8, 2024

Choose a reason for hiding this comment

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

If you want to optimize this too, you could try:

e = self._data.observed_e1 + 1j*self._data.observed_e2
g = e / (1 + np.sqrt(1.-abs(e)**2))
return _Shear(g)

I suspect that's a little faster, but might just be nibbling at the edges.

hsmparams = HSMParams.check(hsmparams)
if check:
_checkWeightAndBadpix(gal_image, weight=weight, badpix=badpix)
Copy link
Member

Choose a reason for hiding this comment

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

You need to add tests of check=False. (For both functions.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Of course! This was not quite what we discussed in #1264 (we talked about going for a _FindAdaptiveMomView there) which is why I first wanted to run this by you before I wrote the tests for the two cases.

Copy link
Member

Choose a reason for hiding this comment

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

Sure. This seems like a reasonable API to me.

@property
def observed_shape(self):
return Shear(e1=self._data.observed_e1, e2=self._data.observed_e2)
return Shear(e1=self.observed_e1, e2=self.observed_e2)
Copy link
Member Author

Choose a reason for hiding this comment

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

In the LSST DM code base, the reason to get observed_shape was because there was no way to get observed_e1 directly up until now. Now that we can access them directly, I honestly don't think using _Shear is necessary.

galsim/hsm.py Outdated
@@ -779,6 +800,8 @@ def FindAdaptiveMom(object_image, weight=None, badpix=None, guess_sig=5.0, preci
``GalSimHSMError`` exception if shear estimation fails. If set to
``False``, then information about failures will be silently stored in
the output ShapeData object. [default: True]
check: Check if the object_image, weight are in the correct format and valid.
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 missed out on badpix here and will include that as well.

@arunkannawadi
Copy link
Member Author

OK, something completely unrelated seems to be failing. test_interpolateImage.py should set up some global variables but that don't seem to be recognized in the test cases. All tests pass locally on my machine.

@@ -1,6 +1,6 @@
.. image:: https://github.com/GalSim-developers/GalSim/workflows/GalSim%20CI/badge.svg
.. image:: https://github.com/GalSim-developers/GalSim/workflows/GalSim%20CI/badge.svg?branch=main
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 also updated this link to show the badge corresponding to the run on the main branch, typically after merging a feature branch. I don't suppose you want to show case that tests on a development branch failed in the repo's README.

@rmjarvis
Copy link
Member

For now, just pin pytest to <8. They changed something with pytest v8 about how they run (or don't!) setup() before the tests functions. I haven't dug in to figure out what to change to make it work with v8 yet.

@rmjarvis
Copy link
Member

Test suite looks good. Thanks.

@rmjarvis rmjarvis merged commit c222904 into main Mar 11, 2024
10 checks passed
@rmjarvis rmjarvis deleted the hsm_optimization branch March 11, 2024 20:03
@rmjarvis rmjarvis added this to the v2.5 milestone Apr 5, 2024
@rmjarvis rmjarvis added docs Related to documentation hsm Related to the galsim.hsm adaptive moments tools optimization/performance Related to the speed and/or memory consumption of some aspect of the code labels Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Related to documentation hsm Related to the galsim.hsm adaptive moments tools optimization/performance Related to the speed and/or memory consumption of some aspect of the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Construct images without sanity checks in _convertMask function in the HSM module
2 participants