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

Hackday mteodoro sky variance readnoise #1556

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

Conversation

mairanteodoro
Copy link
Collaborator

@mairanteodoro mairanteodoro commented Dec 9, 2024

Resolves RCAL-968

This PR adds a sky variance array to the mosaic file created by resample (i.e. model.var_sky). It also enables building a drizzle image weighted by the inverse of the sky variance when running resample with weight_type="ivsky".

Regression test

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 below for change types)
    • 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
news fragment change types...
  • changes/<PR#>.general.rst: infrastructure or miscellaneous change
  • changes/<PR#>.docs.rst
  • changes/<PR#>.stpipe.rst
  • changes/<PR#>.associations.rst
  • changes/<PR#>.scripts.rst
  • changes/<PR#>.mosaic_pipeline.rst
  • changes/<PR#>.patch_match.rst

steps

  • changes/<PR#>.dq_init.rst
  • changes/<PR#>.saturation.rst
  • changes/<PR#>.refpix.rst
  • changes/<PR#>.linearity.rst
  • changes/<PR#>.dark_current.rst
  • changes/<PR#>.jump_detection.rst
  • changes/<PR#>.ramp_fitting.rst
  • changes/<PR#>.assign_wcs.rst
  • changes/<PR#>.flatfield.rst
  • changes/<PR#>.photom.rst
  • changes/<PR#>.flux.rst
  • changes/<PR#>.source_detection.rst
  • changes/<PR#>.tweakreg.rst
  • changes/<PR#>.skymatch.rst
  • changes/<PR#>.outlier_detection.rst
  • changes/<PR#>.resample.rst
  • changes/<PR#>.source_catalog.rst

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 84.61538% with 4 lines in your changes missing coverage. Please review.

Project coverage is 78.17%. Comparing base (8422362) to head (332943c).

Files with missing lines Patch % Lines
romancal/resample/resample_utils.py 81.81% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1556      +/-   ##
==========================================
+ Coverage   78.15%   78.17%   +0.01%     
==========================================
  Files         116      116              
  Lines        7646     7671      +25     
==========================================
+ Hits         5976     5997      +21     
- Misses       1670     1674       +4     

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

@mairanteodoro mairanteodoro added this to the 25Q1_B16 milestone Dec 11, 2024
@mairanteodoro mairanteodoro force-pushed the hackday_mteodoro_sky_variance_readnoise branch from 37cf8b1 to 2448932 Compare December 11, 2024 14:20
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Dec 11, 2024
@mairanteodoro mairanteodoro force-pushed the hackday_mteodoro_sky_variance_readnoise branch from 60b0642 to d8dddf9 Compare December 11, 2024 21:50
@mairanteodoro mairanteodoro marked this pull request as ready for review December 12, 2024 16:03
@mairanteodoro mairanteodoro requested a review from a team as a code owner December 12, 2024 16:03
@braingram
Copy link
Collaborator

I follow the addition of the new weight type but I'm not sure I see the connection between the ticket and resampling each computed var_sky into a var_sky added to the mosaic model.

If the goal is to save var_sky in the mosaic model would you open a rad and roman_datamodels PRs to update the schema to include var_sky?

docs/roman/resample/arguments.rst Outdated Show resolved Hide resolved
romancal/resample/resample_utils.py Show resolved Hide resolved
romancal/resample/resample_utils.py Show resolved Hide resolved
romancal/resample/resample_utils.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@schlafly schlafly left a comment

Choose a reason for hiding this comment

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

I think this looks conceptually right; thanks! Can you put a couple of example files on /grp/roman somewhere, together with an invocation (run strun roman::resample blah.asn --weight_type skyivm ??) so that I can easily poke at some example input and output files?

romancal/resample/resample_utils.py Outdated Show resolved Hide resolved
@mairanteodoro
Copy link
Collaborator Author

If the goal is to save var_sky in the mosaic model would you open a rad and roman_datamodels PRs to update the schema to include var_sky?

Yes, that's the idea.

@mairanteodoro mairanteodoro force-pushed the hackday_mteodoro_sky_variance_readnoise branch from 192c1cb to 65f7acd Compare December 17, 2024 14:35
@mairanteodoro mairanteodoro force-pushed the hackday_mteodoro_sky_variance_readnoise branch from 2064e73 to af738f3 Compare December 30, 2024 20:06
@ddavis-stsci ddavis-stsci modified the milestones: 25Q1_B16, 25Q2_B17 Jan 15, 2025
@mairanteodoro mairanteodoro force-pushed the hackday_mteodoro_sky_variance_readnoise branch from f70fc8a to 48ed0fe Compare January 23, 2025 16:01
@mairanteodoro mairanteodoro force-pushed the hackday_mteodoro_sky_variance_readnoise branch from 48ed0fe to fadc18d Compare January 27, 2025 15:27
@mairanteodoro mairanteodoro requested a review from a team as a code owner January 27, 2025 15:27
romancal/resample/resample_utils.py Outdated Show resolved Hide resolved
romancal/resample/resample_utils.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@braingram braingram left a comment

Choose a reason for hiding this comment

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

Thanks for updating this.

I left a few more comments and questions.

Would you open a RAD PR to add var_sky to the mosaic schema?

def test_resampledata_do_drizzle_default_single_exposure_weight_array(
exposure_1,
weight_type,
):
"""Test that resample methods return non-empty weight arrays."""

# adding a few zero flux pixels
for i, model in enumerate(exposure_1):
model.data[10 + i, 40 - i] = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would you help me understand how setting pixels offset by the index of the exposure in exposure_1 results in the resampled pixel at [10, 40] being equal to 0? Is each exposure offset by 1 pixel in X and 1 in Y?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just added a random pixel with zero flux. I was not interested in the position of the pixel, just that we did have a pixel with zero flux.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm still confused by this. What's the need for a random zero flux pixel per image? If I comment out this code the test doesn't fail.

] / img.data[ok_data] * np.median(img.data)
img["var_sky"][~ok_data] = img.var_rnoise[~ok_data]
except (AttributeError, KeyError, TypeError, ValueError) as e:
raise ValueError("Input model contains invalid data array.") from e
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this raises an exception (if for instance var_rnoise isn't available won't this stop resample from proceeding if ivsky is selected and var_rnoise isn't available? That behavior makes sense to me but is at odds with the documentation (which says thew weight will be 1 for all pixels in this case).

Also I suggest checking for var_rnoise and var_poisson instead of using the try/except (since the try/except will also trigger for other reasons.

Comment on lines +438 to +439
ref_img = input_models.borrow(index=0)
input_models.shelve(model=ref_img, index=0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
ref_img = input_models.borrow(index=0)
input_models.shelve(model=ref_img, index=0)

Is ref_img used?

img["var_sky"] = np.empty_like(img.data)
img["var_sky"][ok_data] = img.var_rnoise[ok_data] + img.var_poisson[
ok_data
] / img.data[ok_data] * np.median(img.data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If data contains a nan won't this cause every var_sky pixel (for each index wither data != 0) to be nan?

for i, img in enumerate(input_models):
try:
ok_data = img.data != 0
img["var_sky"] = np.empty_like(img.data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this could be simplified by starting with something like:

img["var_sky"] = img.var_rnoise.copy()

since all pixels are either:

  • copied from var_sky if ~(img.data != 0)
  • computed below

@@ -86,13 +86,15 @@ def __init__(
)

self.input_models = input_models
# add sky variance array
resample_utils.add_var_sky_array(self.input_models)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will always re-compute var_sky (even if the input contains a var_sky). If that's the goal (always have resample compute var_sky) what about something like:

  • if weight is ivsky compute var_sky for each model
  • when resampling var_sky compute it (if it wasn't computed before)

That way for nonivsky weighting resample won't need to make a separate pass through all the models here (in add_var_sky_array).

def test_resampledata_do_drizzle_default_single_exposure_weight_array(
exposure_1,
weight_type,
):
"""Test that resample methods return non-empty weight arrays."""

# adding a few zero flux pixels
for i, model in enumerate(exposure_1):
model.data[10 + i, 40 - i] = 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm still confused by this. What's the need for a random zero flux pixel per image? If I comment out this code the test doesn't fail.

@@ -712,6 +717,8 @@ def test_resampledata_do_drizzle_default_single_exposure_weight_array(
many_to_one_model = output_models_many_to_one.borrow(0)
assert np.any(many_to_one_model.weight > 0)
assert np.any(many_to_many_model.weight > 0)
assert many_to_one_model.data[10, 40] == 0
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this line testing? The referenced pixel at 10, 40 (seen as the single highlighted pixel in the upper left) falls in the "fill" region of the output image.
Screenshot 2025-02-03 at 1 47 59 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation regression_testing testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants