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

Add modelSize to piff config #23

Merged
merged 5 commits into from
Apr 12, 2024

Conversation

esheldon
Copy link
Contributor

@esheldon esheldon commented Apr 4, 2024

add modelSize to piff config

The modelSize is the internal PIFF model grid size.  This gets
sent to the piff config rather than stampSize for the "size" entry.

modelSize defaults to 25. Update the stampSize to default to sqrt(2) * 25 ~ 35
to accomodate rotations

Add a check that stamp size is big enough

update tests to include rotation

This is an important check when working in sky coords

Oddly at 45 degrees piff is giving a singlular matrix error
and fails to fit.  We should track this down before accepting
this PR.  135 works fine so I wonder if there is something
sepcial about 45 for this test

The modelSize is the internal PIFF model grid size.  This gets
sent to the piff config rather than stampSize for the "size" entry.

modelSize defaults to 25. Update the stampSize to default to sqrt(2) * 25 ~ 35
to accomodate rotations

Add a check that stamp size is big enough
This is an important check when working in sky coords

Oddly at 45 degrees piff is giving a singlular matrix error
and fails to fit.  We should track this down before accepting
this PR.  135 works fine so I wonder if there is something
sepcial about 45 for this test
@arunkannawadi arunkannawadi changed the base branch from main to tickets/DM-43490 April 4, 2024 18:15
@esheldon
Copy link
Contributor Author

esheldon commented Apr 5, 2024

The unit tests pass for me. Do we turn on the CI here or is that to be done later?

We think its an issue with the underlying galsim code
rather than the wrapper
@esheldon
Copy link
Contributor Author

esheldon commented Apr 5, 2024

There is a fix in galsim that allows the unit tests to pass.

Should we add more test cases?

@arunkannawadi
Copy link
Member

If by more test cases you mean more angles, I'm happy to do that after merging this PR. But once you're happy with this PR as is, I'll merge it to the ticket branch and run CI tests, but only after the new GalSim release makes it to rubin-env. You'll have an opportunity to review before merging to main.

tests/test_psf.py Show resolved Hide resolved
f'we require stampSize >= '
f'{min_stamp_size}, got {stamp_size}'
)

Copy link
Member

Choose a reason for hiding this comment

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

Note to self: this is a check only on the config, so should be implemented in a validate method.

@arunkannawadi
Copy link
Member

Thanks again for this PR, Erin. If there are no more changes you're planning, I'll go ahead and merge it.

@esheldon
Copy link
Contributor Author

No more changes

# i) aperture flux with 12 pixel radius can be compared to PSF flux.
# ii) fake sources injected to match the 12 pixel aperture flux get
# measured correctly
self.stampSize = 25
# Default modelSize is 25, and 35 is about sqrt(2) * 25
self.stampSize = 35
Copy link
Member

Choose a reason for hiding this comment

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

Given that in the default processing we don't use sky coordinates, I'm inclined to keep this as 25 in the default here. For your testing purposes, you should be able to override this value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rmjarvis Is there any disadvantage to having modelSize == stampSize?

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.

Yes. You definitely don't want stampSize == modelSize. It should be larger for 3 reasons.

  1. The Lanczos interpolation extends the active modeling space out from the modelSize by interpolant.xrange(). I think your default is Lanczos(11), so it will reach out by 11 pixels on all sides. That alone suggests you want stampSize = modelSize + 22.
  2. Even if your centroids are absolutely perfect, there is a sub-pixel variation of half a pixel in some direction. So you would want to add one more pixel of data around the edges to make sure you are properly constraining all the pixels around the edge. That adds another +2 to the stamp size. Maybe a little more if you don't trust your centroids to be very sub-pixel accurate.
  3. Finally, the rotation. This multiplies that calculation by up to sqrt(2). (1/max(|sin(theta)|,|cos(theta)|) if you want to calculate it explicitly for a given WCS.)

And honestly, there is barely any down side to using a larger than needed stamp size. Piff will just ignore any pixels that are not being used to constrain the model. So I'd recommend the default being much larger in fact. Use 80x80 or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default I put in was 35 which was designed to account for your part 3. in the list, since the default modelSize was set to 25. It seems like 1. and 2. imply we should go even larger

Copy link
Member

Choose a reason for hiding this comment

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

The Lanczos interpolation extends the active modeling space out from the modelSize by interpolant.xrange()

Mike, could you confirm that this is needed even if the modeling is happening in pixel coordinates as opposed to sky coordinates?

Copy link
Contributor

Choose a reason for hiding this comment

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

"Needed"? No. Not technically. But it will produce inaccurate PSF models near the edges if you don't.

Copy link
Member

Choose a reason for hiding this comment

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

Also, I remember the compute scales as cubic power of the size, but does it as modelSize**3 or stampSize**3? I hope the former.

Copy link
Contributor

Choose a reason for hiding this comment

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

modelSize**6 actually.

if stamp_size % 2 == 0:
raise ValueError(f'stampSize should be odd, got {stamp_size}')
if model_size % 2 == 0:
raise ValueError(f'modelSize should be odd, got {model_size}')
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.

Why this? I don't think there is any restriction that either of these must be odd. Odd model size is common, but not really necessary. Most PSF profiles have a lot of flux in the central 1 pixel, so odd helps model that better. But there could certainly be cases where even works better. (I did test both odd and even for DES, and the odd sizes in fact worked better in those tests -- I haven't switched to even sizes since then.)
And I usually use even stampSize actually. The default in Piff is 32, which is much larger than our modelSize in DES, even without any rotator.

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 agree that there is no reason in principle.

In practice it is nice to have odd for some applications. For example, odd means the model can be centered on that center pixel, and then convolutions with the PSF image will not cause a shift.

Copy link
Member

@arunkannawadi arunkannawadi Apr 12, 2024

Choose a reason for hiding this comment

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

Hmm, stampSize is enforced to be odd upstream as well, so even if we drop that requirement here, you can be guaranteed that stampSize will be odd. Dropping that requirement upstream will have to get discussed within the Science Pipelines team but I can go with whatever is the consensus is on modelSize. But based on Mike's comment, may be we should not make it a requirement just yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We want it for shear

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.

In practice it is nice to have odd for some applications. For example, odd means the model can be centered on that center pixel, and then convolutions with the PSF image will not cause a shift.

This consideration is orthogonal to the reasons for having the model use an odd size. You can draw it onto an odd-sized image (and center it) regardless of what the underlying model uses.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK.

I thought that re-gridding the model from a center-near-pixel-boundary to center-at-pixel-center would introduce larger interpolation errors. If that's not the case then I withdraw my assertion

Copy link
Member

Choose a reason for hiding this comment

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

I'll be moving around the code a bit after merging, so I will drop the requirement then.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still think people should tend to use odd modelSize in most cases for other reasons. Mostly that typical PSFs have a hot spot right at the center, which an odd model size is better at describing. But making it a requirement feels a little heavy handed. E.g. modeling very out of focus stars, which don't have that central peak, might work better with an even model size for some reason.

@arunkannawadi
Copy link
Member

Looks like we've converged on the various conversations here. I'll give it a few more hours and merge it by EOD.

@arunkannawadi arunkannawadi merged commit 067caea into lsst:tickets/DM-43490 Apr 12, 2024
2 checks passed
@arunkannawadi
Copy link
Member

Continued in #25 .

@esheldon esheldon deleted the model-size branch April 18, 2024 17:59
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