Skip to content

RCAL-1342: Source Injection Nan Pixel Support#2368

Open
PaulHuwe wants to merge 26 commits into
spacetelescope:mainfrom
PaulHuwe:RCAL-1342_SINaN
Open

RCAL-1342: Source Injection Nan Pixel Support#2368
PaulHuwe wants to merge 26 commits into
spacetelescope:mainfrom
PaulHuwe:RCAL-1342_SINaN

Conversation

@PaulHuwe

Copy link
Copy Markdown
Collaborator

Resolves RCAL-1342

Closes #2022

This PR adds support for injecting sources onto NaN pixels. Previously, if an object was to be injected at the location of a NaN pixel, it would be skipped. This PR will inject the source at a location with a single NaN at the grid point, using an interpolated pixel value for injection. If a source is to be injected in a NaN region, it is skipped.

NOTES:

  1. The injection is coded in such a way that it can be readily expanded to a region of NaN pixels that are some fraction of the source size, if desired.
  2. This PR adds some code duplication in the test file, which was needed to avoid changing other tests. I want to fully clean up this test file in a separate PR.

Tasks

  • request a review from someone specific, to avoid making the maintainers review every PR
  • add a build milestone, i.e. 24Q4_B15 (use the latest build if not sure)
  • Does this PR change user-facing code / API? (if not, label with no-changelog-entry-needed)
    • write news fragment(s) in changes/: echo "changed something" > changes/<PR#>.<changetype>.rst (see changelog readme for instructions)
      • if your change breaks existing functionality, also add a changes/<PR#>.breaking.rst news fragment
    • update or add relevant tests
    • update relevant docstrings and / or docs/ page
    • start a regression test and include a link to the running job (click here for instructions)
      • Do truth files need to be updated ("okified")?
        • after the reviewer has approved these changes, run okify_regtests to update the truth files
  • if a JIRA ticket exists, make sure it is resolved properly

@PaulHuwe
PaulHuwe requested a review from schlafly June 27, 2026 22:01
@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.54%. Comparing base (740761f) to head (9ae612e).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2368      +/-   ##
==========================================
+ Coverage   80.87%   81.54%   +0.67%     
==========================================
  Files         159      157       -2     
  Lines        9531     9406     -125     
==========================================
- Hits         7708     7670      -38     
+ Misses       1823     1736      -87     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@schlafly schlafly left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. I identified some of the same code duplication in the testing that you did. I agree that it makes sense to clean this up; I don't think we want this PR to introduce more duplication in preparation for a later PR that removes it?

I left a few other in line comments about the tests.

More importantly, I'm not sure I really get what the purpose of the new block in _multiband_catalog is. If the PR were only the deletion of 302-306 in _injection, what would go wrong?

Comment thread romancal/multiband_catalog/tests/test_multiband_catalog.py Outdated
Comment thread romancal/multiband_catalog/tests/test_multiband_catalog.py Outdated
# Ensure all objects matched
assert len(matched_grid) == len(results["NoNan"].source_injection_catalog)

# Test that all matched objects have similar magnitudes

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this test depend on the fact that the NaNs are far from the sources? Broadly I think it's fine if the magnitudes are wrong if you NaN out the center of a source; I don't think we want this test to depend on how the fake sources are located relative to the NaNs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The NaNs are only at the center of the injected sources.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. What is the typical dispersion here? If I understand right, this is matching the recovered magnitude of the source on a partially NaNed image to the recovered magnitude of the same source, on an image without NaNs. Conceptually I expect the recovered magnitude to depend on whether the the image has NaNs. For stars, I expect it to depend a lot because the stars are small and NaNing their centers makes a big impact on the flux estimate you would get. For galaxies, it depends on how big the galaxy is and things are less clear to me. 2 mmag is a very tight tolerance and I worry that this is a sign that something is not working right. What am I missing?

Comment thread romancal/multiband_catalog/tests/test_multiband_catalog.py Outdated
Comment thread romancal/multiband_catalog/_multiband_catalog.py
Comment thread romancal/multiband_catalog/_multiband_catalog.py Outdated
braingram and others added 16 commits July 9, 2026 13:36
…spacetelescope#2360)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: William Jamieson <william+github@jamieson.phd>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…telescope#2367)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…escope#2324)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Eddie Schlafly <eschlafly@gmail.com>
…pacetelescope#2355)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 9, 2026
@PaulHuwe

Copy link
Copy Markdown
Collaborator Author

@PaulHuwe
PaulHuwe marked this pull request as ready for review July 10, 2026 01:40
@PaulHuwe
PaulHuwe requested a review from schlafly July 10, 2026 01:40
Comment thread romancal/multiband_catalog/tests/test_multiband_catalog.py Outdated

# Temporarily set NaNs to zero for injections
si_model.data[y_pos_idx[nanmask], x_pos_idx[nanmask]] = 0
si_model.var_poisson[y_pos_idx[nanmask], x_pos_idx[nanmask]] = 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Per our discussion Wednesday, the ratio here is used to determine the exposure time, so we should fill with nanmedian or something rather than just zeros (I don't know what exposure time 0/0 would correspond to).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

When data and var_poisson is zero, exposure time is 0/1 - the method changes any 0 var_poisson to 1. I was going for the least bespoke change, but I am happy to switch to nanmedian.
https://github.com/spacetelescope/romanisim/blob/main/romanisim/l3.py#L172C1-L177C27

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's fill with the nanmedians of the whole image so that these sources have sane exposure times.

# Ensure all objects matched
assert len(matched_grid) == len(results["NoNan"].source_injection_catalog)

# Test that all matched objects have similar magnitudes

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. What is the typical dispersion here? If I understand right, this is matching the recovered magnitude of the source on a partially NaNed image to the recovered magnitude of the same source, on an image without NaNs. Conceptually I expect the recovered magnitude to depend on whether the the image has NaNs. For stars, I expect it to depend a lot because the stars are small and NaNing their centers makes a big impact on the flux estimate you would get. For galaxies, it depends on how big the galaxy is and things are less clear to me. 2 mmag is a very tight tolerance and I worry that this is a sign that something is not working right. What am I missing?

)
assert np.allclose(
matched_block["kron_f184_abmag"], matched_si_bl["kron_f184_abmag"], rtol=3e-2
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Similarly to the comment above... this is for the case where large chunks of the image are NaN. So we expect some sources to not be recovered at all. I guess in this section there's no assertion that the lengths are identical, reflecting that. But the requirement that the kron mags match at 0.03 mag means that no sources are allowed to be injected near the edge?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Source are injected near the edge, but are not detected.

Injected sources for NaN Block:
image

Zoom of the center NaN Block corner:
image

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Your suspicions are well founded - during PR revisions, the NANPOINTS drifted. I am rewriting that section (and tests) to be less brittle.

Co-authored-by: Eddie Schlafly <eschlafly@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revise Source Injection NaN Handling

7 participants