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

Refactor image/stamp workflow #424

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

g-braeunlich
Copy link
Contributor

No description provided.

@g-braeunlich g-braeunlich self-assigned this Sep 28, 2023
@g-braeunlich g-braeunlich force-pushed the u/g-braeunlich/refactor-image branch 2 times, most recently from e58012d to c145f0c Compare October 10, 2023 12:34
@g-braeunlich g-braeunlich force-pushed the u/g-braeunlich/refactor-image branch 2 times, most recently from acbac83 to 9f49df7 Compare November 30, 2023 12:45
@welucas2
Copy link
Collaborator

The latest commit aligns photon objects in the photon pooling pipeline with their positions in the original pipeline.

@welucas2 welucas2 self-assigned this Apr 29, 2024
@welucas2
Copy link
Collaborator

Almost there, but blocked by GalSim-developers/GalSim#1284.

@welucas2 welucas2 force-pushed the u/g-braeunlich/refactor-image branch from bdaadae to b786cb0 Compare June 5, 2024 13:10
@welucas2 welucas2 marked this pull request as ready for review June 5, 2024 13:32
@welucas2
Copy link
Collaborator

welucas2 commented Jun 5, 2024

This is finally open for review -- all comments welcome.

Copy link
Contributor

@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.

Thanks William. I have a bunch of comments, but this is definitely a good start. If it would be helpful to talk through any of this on a zoom call, let me know.

# If using photon pooling, FFT and photon objects are batched separately.
# There will probably be few enough FFT objects to do in a single batch.
# Bright photon objects are treated in all photon batches, but at 1/nbatch_photon of
# their flux. Faint photonobjects are placed at their full flux in random batches.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# their flux. Faint photonobjects are placed at their full flux in random batches.
# their flux. Faint photon objects are placed at their full flux in random batches.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for spotting this. Fixing in the next commit.

# These parameters only affect LSST_PhotonPoolingImage images.
# The batch numbers can be changed if desired.
nbatch_fft: 1
nbatch_photon: 10
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be the same parameter as the simple nbatch. We can just document that with photon pooling, nbatch refers only to the photon-shooting objects. Then nbatch_fft can be an optional parameter that defaults to 1. (Maybe not even worth mentioning here if most users wouldn't change it?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with this. Nice to keep the number of parameters down. nbatch_photon will be no more, and the photon pooling will be controlled directly with nbatch.


class LSST_ImageBuilder(LSST_ImageBuilderBase):
"""This is mostly the same as the GalSim "Scattered" image type.
So far the only change is in the sky background image."""
Copy link
Contributor

Choose a reason for hiding this comment

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

This doc is probably obsolete. There are quite a few differences now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Happy to change this. Can you suggest something more suitable?

'x' : { 'type' : 'Random' , 'min' : xmin , 'max' : xmax },
'y' : { 'type' : 'Random' , 'min' : ymin , 'max' : ymax }
}
set_config_image_pos(config, base)
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than making this a free function in a different file, this should probably be a private method of the base class, which both subclasses can call.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed, doing this.

base["stamp"]["photon_ops"] = []
photon_ops = base["stamp"]["photon_ops"]
shift_op = {'type': 'Shift'}
shift_index = next((index for (index, d) in enumerate(photon_ops) if d["type"] == "Shift"), None)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't love this implementation. I get why something like this is now required. But I think I'd rather have RubinOptics and RubinDiffractionOptics just have an optional parameter to apply this shift or not. Then when running this class, base[image_pos] is defined and they can apply the shift based on that.

In the photon pooling class, once we've pooled all the photons, we should set the object-specific values (image_pos, sky_pos, maybe others) that apply to a particular object to None. Then the RubinOptics and RubinDiffractionOptics photon ops would see that the image_pos isn't defined, so not apply any shift.

This feels to me more elegant and possibly more efficient.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should now be done -- see RubinOptics and RubinDIffractionOptics. If an optional shift_photons = True is passed, the shift is performed.

},
"psf": {"type": "Convolve", "items": [{"type": "Gaussian", "fwhm": 0.3}]},
"stamp": {
"type": "LSST_Silicon",
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be the new stamp type when image_type is LSST_PhotonPoolingImage

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, photon pooling now needs to use the new stamp type rather messing with base. The test sets the correct stamp type.


def test_lsst_image_photon_pooling_pipeline():
"""Check that LSST_PhotonPoolingImage batches objects as expected and renders objects at the correct positions."""
run_lsst_image("LSST_PhotonPoolingImage")
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like we probably want a few additional tests in this pipeline. E.g. that the FFT and faint objects only got drawn once. And the others got drawn nbatch times.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've written a set of tests for the partitioning and batching process. As mentioned above, it also confirms that total flux is conserved for photon objects,

help="Similar to -k of pytest / unittest: restrict tests to tests starting with the specified prefix.",
default="test_",
)
args = parser.parse_args()
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, when working on stuff, I usually just put the name of the test function I'm working on at the start of this name == '__main__' block, followed by quit(). Then remove it before committing. Seems easier than rolling all of this argparse stuff, but 🤷.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is also done in test_diffraction_fft.py, but I'm happy to change it if you'd like me to.

expected_x_pic_center = 564.5
expected_y_pic_center = -1431.4
expected_x_pic_center = -989.5971378245167
expected_y_pic_center = -3840.3512012842157
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are these (and subsequent tests) changing so much? This isn't close.

Regression tests are supposed to make sure we don't change the functionality of existing code. I know the implementation of these photon ops changed, but I think we want the tests to remain the same or at least very very close. What happened here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is still a big issue. I'm 99% convinced the error is in the original test itself. My current best guess is that with the old code it may not have been internally self-consistent around the WCS used, while the new XyToV should enforce self-consistency. But it still needs to be looked at.

@@ -52,7 +53,8 @@ def create_test_config():
}],
},
"bandpass": galsim.Bandpass('LSST_r.dat', wave_type='nm'),
"wcs": galsim.PixelScale(0.2),
"wcs": wcs,
"current_image": galsim.Image(1024, 1024, wcs=wcs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this change required?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The change to the wcs field isn't actually necessary, it looks. The addition of current_image is needed for deserialize_rubin_optics, deserialize_rubin_diffraction_optics and deserialize_rubin_diffraction which uses it to get img_wcs, required by the new version of XyToV.

@welucas2
Copy link
Collaborator

welucas2 commented Nov 7, 2024

The latest set of commits should address most of the comments above. I'll respond to those individually. A couple do still need to be looked at.

@rmjarvis
Copy link
Contributor

Are you waiting on me for anything here? I thought you still had some more comments to address, so I was waiting for that to be done. But if it would be helpful for me to review this again at this point, let me know.

@welucas2
Copy link
Collaborator

Not just yet -- I have a couple more commits on the way with fixes and tests for the batching. I'll push these later today or tomorrow, and then I think that should be everything ready for review again.

@welucas2 welucas2 force-pushed the u/g-braeunlich/refactor-image branch from 46461fb to eeb67dc Compare November 20, 2024 16:54
@welucas2
Copy link
Collaborator

I think this is ready for re-review now, though CI is failing with an error in test_stamp_bandpass_airmass from numpy coming through GalSim. I saw the same error a couple of weeks ago on an ARM system when using GalSim 2.6 which introduces its own utilities.least_squares(). I wasn't able to investigate further before the system went down and only came back up yesterday.

@welucas2
Copy link
Collaborator

I think there is actually one more commit incoming. I've realised that in edge cases where nominally bright photon objects' phot_flux falls below nbatch, we can end up with batches containing objects with 0 flux. I think I have a fix, by having these objects be treated as faint objects for batching purposes. I'll test this and if all is well push it tomorrow.

@welucas2
Copy link
Collaborator

OK, that should be it -- code open for review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants