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

Calculate centers #461

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

Calculate centers #461

wants to merge 14 commits into from

Conversation

esheldon
Copy link
Contributor

This adds code to calculate centers from shot photons

These positions will be accurate in all cases, whereas the current available x, y are from converting ra, dec to x, y using the batoid wcs. That wcs does not include all physics (e.g. DCR) making the WCS inaccurate in some cases.

Currently this is "always on" and uses a fixed number of photons.

I need help to understand how to make this configurable within the galsim config system, as imsim interfaces with it.

@esheldon
Copy link
Contributor Author

The tests such as test_lsst_silicon_builder_passes_correct_photon_ops_to_drawImage don't actually result in any flux in the image, so the code is currently failing. Was no flux drawn the intention?

but this and other tests fail because nothing is actually drawn
The only way is through the stamp sub-config
@esheldon
Copy link
Contributor Author

Anyone know how to make the tests draw a non zero flux?

@esheldon esheldon mentioned this pull request Apr 11, 2024
@rmjarvis
Copy link
Contributor

Rather than change what that test does, probably better to make this centroid calculation optional, which we need to do anyway. We definitely don't want it turned on by default, since it's much too slow for objects that have very few photons. (Which are the vast majority of objects in typical use cases.)

@esheldon
Copy link
Contributor Author

The recent commits make it optional

@esheldon
Copy link
Contributor Author

I can't make any progress on this PR until I can get the tests to actually produce photons. Currently the tests as set up produce zero flux drawn in the stamp but I don't understand why. Does anyone know how to modify the test to produce flux?

# In GalSim 2.5+, this is now a SimpleChromaticTransformation
phot_type = 'galsim.SimpleChromaticTransformation'

with mock.patch(phot_type+'.drawImage', return_value=image) as mock_drawImage:
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't use mock. Have this call the real drawImage function, and then it will produce an actual output image. The above tests that use mock were testing something very specific about how parameters were being passed to drawImage, which mock lets you test. I think you want to actually call the drawImage function and check that the centroid values get computed correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I didn't know what that context did, but I found I had to use it because otherwise I got this error:

imsim/stamp.py:778: in draw
    image = gal.drawImage(bp_for_drawImage,
../../miniforge3/envs/stack/lib/python3.11/site-packages/galsim/chromatic.py:2136: in drawImage
    image = ChromaticObject.drawImage(self, bandpass, image, integrator, **kwargs)
../../miniforge3/envs/stack/lib/python3.11/site-packages/galsim/chromatic.py:485: in drawImage
    image = prof0.drawImage(image=image, **kwargs)
../../miniforge3/envs/stack/lib/python3.11/site-packages/galsim/gsobject.py:1775: in drawImage
    added_photons, photons = prof.drawPhot(imview, gain, add_to_image,
../../miniforge3/envs/stack/lib/python3.11/site-packages/galsim/gsobject.py:2383: in drawPhot
    op.applyTo(photons, local_wcs, rng)
imsim/photon_ops.py:90: in applyTo
    v = self.photon_velocity(photon_array, local_wcs, rng)
imsim/photon_ops.py:189: in photon_velocity
    return self.rubin_diffraction.photon_velocity(
imsim/photon_ops.py:267: in photon_velocity
    v = photon_velocity(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

photon_array = galsim.PhotonArray(100, x=array([-4.95703380547105, -1.7774137125821585, 5.4679574148088355, 1.3853752380310527, -11.0...28, 567.8319745320754, 610.7805936794955, 588.7526624330898, 631.0916929335601, 659.
8606846139876, 662.0577956513312]))
xy_to_v = <imsim.photon_ops.XyToV object at 0x7165d0d3b350>, get_n = <bound method Medium.getN of ConstMedium(1.0)>

    def photon_velocity(photon_array, xy_to_v: "XyToV", get_n) -> np.ndarray:
        """Computes the velocity of a photon array."""
    
>       assert photon_array.hasAllocatedPupil()
E       AssertionError

imsim/photon_ops.py:123: AssertionError

Copy link
Contributor

@rmjarvis rmjarvis Apr 12, 2024

Choose a reason for hiding this comment

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

You need to either use an atmosphere (as PSF or photon_op) or PupilAnnulusSampler. The RubinDiffractionOptics photon_op requires that something else has already populated the pupil plane positions. In normal imsim usage, it's the AtmosphericPSF. But you can also do it manually with PupilAnnulusSampler (or PupilImageSampler works too, but you probably don't want to use that one.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also had to add a TimeSampler

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that sounds right.

There was no time sampler or pupil.
@esheldon
Copy link
Contributor Author

I think this is ready for review.

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.

The code looks fine for what it is, but I don't love this API for turning on the calculation. I think I'd rather have two optional parameters in the stamp: calculate_centroids: bool and centroid_n_photons: int. The latter should have a documented default value (maybe 1000 is good, though you mention 100_000 in the doc above). Then to turn this on, most people would just set calculate_centroids: True. And if they want to adjust the precision, they could also set centroid_n_photons to something.

@esheldon
Copy link
Contributor Author

I have my own issues with this PR, mainly that it is calculating a centroid for every object which is not necessary.

For images with a lot of galaxies this slows down the code significantly.

We only need a subset of the objects, say a random 100 or 200, with which to fit a WCS and validate it.

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.

2 participants